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  arma::vec logProbabilities;
94  LogProbability(x, logProbabilities);
95  probabilities = arma::exp(logProbabilities);
96  }
97 
98  void LogProbability(const arma::mat& x, arma::vec& logProbabilities) const;
99 
106  arma::vec Random() const;
107 
113  void Train(const arma::mat& observations);
114 
120  void Train(const arma::mat& observations,
121  const arma::vec& probabilities);
122 
126  const arma::vec& Mean() const { return mean; }
127 
131  arma::vec& Mean() { return mean; }
132 
136  const arma::mat& Covariance() const { return covariance; }
137 
141  void Covariance(const arma::mat& covariance);
142 
143  void Covariance(arma::mat&& covariance);
144 
148  template<typename Archive>
149  void serialize(Archive& ar, const unsigned int /* version */)
150  {
151  // We just need to serialize each of the members.
152  ar & BOOST_SERIALIZATION_NVP(mean);
153  ar & BOOST_SERIALIZATION_NVP(covariance);
154  ar & BOOST_SERIALIZATION_NVP(covLower);
155  ar & BOOST_SERIALIZATION_NVP(invCov);
156  ar & BOOST_SERIALIZATION_NVP(logDetCov);
157  }
158 
159  private:
165  void FactorCovariance();
166 };
167 
176  const arma::mat& x,
177  arma::vec& logProbabilities) const
178 {
179  // Column i of 'diffs' is the difference between x.col(i) and the mean.
180  arma::mat diffs = x - (mean * arma::ones<arma::rowvec>(x.n_cols));
181 
182  // Now, we only want to calculate the diagonal elements of (diffs' * cov^-1 *
183  // diffs). We just don't need any of the other elements. We can calculate
184  // the right hand part of the equation (instead of the left side) so that
185  // later we are referencing columns, not rows -- that is faster.
186  const arma::mat rhs = -0.5 * invCov * diffs;
187  arma::vec logExponents(diffs.n_cols); // We will now fill this.
188  for (size_t i = 0; i < diffs.n_cols; i++)
189  logExponents(i) = accu(diffs.unsafe_col(i) % rhs.unsafe_col(i));
190 
191  const size_t k = x.n_rows;
192 
193  logProbabilities = -0.5 * k * log2pi - 0.5 * logDetCov + logExponents;
194 }
195 
196 
197 } // namespace distribution
198 } // namespace mlpack
199 
200 #endif
A single multivariate Gaussian distribution.
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
.hpp
Definition: add_to_po.hpp:21
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.
void serialize(Archive &ar, const unsigned int)
Serialize the distribution.
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...
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.