gaussian_distribution.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP
14 #define MLPACK_CORE_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace distribution {
20 
25 {
26  private:
28  arma::vec mean;
30  arma::mat covariance;
32  arma::mat covLower;
34  arma::mat invCov;
36  double logDetCov;
37 
39  static const constexpr double log2pi = 1.83787706640934533908193770912475883;
40 
41  public:
45  GaussianDistribution() : logDetCov(0.0) { /* nothing to do */ }
46 
51  GaussianDistribution(const size_t dimension) :
52  mean(arma::zeros<arma::vec>(dimension)),
53  covariance(arma::eye<arma::mat>(dimension, dimension)),
54  covLower(arma::eye<arma::mat>(dimension, dimension)),
55  invCov(arma::eye<arma::mat>(dimension, dimension)),
56  logDetCov(0)
57  { /* Nothing to do. */ }
58 
64  GaussianDistribution(const arma::vec& mean, const arma::mat& covariance);
65 
66  // TODO(stephentu): do we want a (arma::vec&&, arma::mat&&) ctor?
67 
69  size_t Dimensionality() const { return mean.n_elem; }
70 
74  double Probability(const arma::vec& observation) const
75  {
76  return exp(LogProbability(observation));
77  }
78 
82  double LogProbability(const arma::vec& observation) const;
83 
91  void Probability(const arma::mat& x, arma::vec& probabilities) const
92  {
93  // Use LogProbability(), then transform the log-probabilities out of
94  // logspace.
95  arma::vec logProbs;
96  LogProbability(x, logProbs);
97  probabilities = arma::exp(logProbs);
98  }
99 
108  void LogProbability(const arma::mat& x, arma::vec& logProbabilities) const
109  {
110  // Column i of 'diffs' is the difference between x.col(i) and the mean.
111  arma::mat diffs = x;
112  diffs.each_col() -= mean;
113 
114  // Now, we only want to calculate the diagonal elements of (diffs' * cov^-1
115  // * diffs). We just don't need any of the other elements.
116  logProbabilities = -0.5 * x.n_rows * log2pi - 0.5 * logDetCov +
117  sum(diffs % (-0.5 * invCov * diffs), 0).t();
118  }
119 
126  arma::vec Random() const;
127 
133  void Train(const arma::mat& observations);
134 
140  void Train(const arma::mat& observations,
141  const arma::vec& probabilities);
142 
146  const arma::vec& Mean() const { return mean; }
147 
151  arma::vec& Mean() { return mean; }
152 
156  const arma::mat& Covariance() const { return covariance; }
157 
161  void Covariance(const arma::mat& covariance);
162 
163  void Covariance(arma::mat&& covariance);
164 
166  const arma::mat& InvCov() const { return invCov; }
167 
169  double LogDetCov() const { return logDetCov; }
170 
174  template<typename Archive>
175  void serialize(Archive& ar, const uint32_t /* version */)
176  {
177  // We just need to serialize each of the members.
178  ar(CEREAL_NVP(mean));
179  ar(CEREAL_NVP(covariance));
180  ar(CEREAL_NVP(covLower));
181  ar(CEREAL_NVP(invCov));
182  ar(CEREAL_NVP(logDetCov));
183  }
184 
185  private:
191  void FactorCovariance();
192 };
193 
194 } // namespace distribution
195 } // namespace mlpack
196 
197 // Include implementation.
198 #include "gaussian_distribution_impl.hpp"
199 
200 #endif
void LogProbability(const arma::mat &x, arma::vec &logProbabilities) const
Returns the Log probability of the given matrix.
A single multivariate Gaussian distribution.
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
const arma::mat & InvCov() const
Return the invCov.
Linear algebra utility functions, generally performed on matrices or vectors.
GaussianDistribution(const size_t dimension)
Create a Gaussian distribution with zero mean and identity covariance with the given dimensionality...
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
GaussianDistribution()
Default constructor, which creates a Gaussian with zero dimension.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
void serialize(Archive &ar, const uint32_t)
Serialize the distribution.
double LogDetCov() const
Return the logDetCov.
size_t Dimensionality() const
Return the dimensionality of this distribution.
void Probability(const arma::mat &x, arma::vec &probabilities) const
Calculates the multivariate Gaussian probability density function for each data point (column) in the...
void Train(const arma::mat &observations)
Estimate the Gaussian distribution directly from the given observations.
const arma::mat & Covariance() const
Return the covariance matrix.
arma::vec & Mean()
Return a modifiable copy of the mean.
const arma::vec & Mean() const
Return the mean.