ensmallen
mlpack
fast, flexible C++ machine learning library
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  probabilities.set_size(x.n_cols);
94  for (size_t i = 0; i < x.n_cols; i++)
95  {
96  probabilities(i) = Probability(x.unsafe_col(i));
97  }
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  // Now, we only want to calculate the diagonal elements of (diffs' * cov^-1
114  // * diffs). We just don't need any of the other elements. We can
115  // calculate the right hand part of the equation (instead of the left side)
116  // so that later we are referencing columns, not rows -- that is faster.
117  const arma::mat rhs = -0.5 * invCov * diffs;
118  arma::vec logExponents(diffs.n_cols); // We will now fill this.
119  for (size_t i = 0; i < diffs.n_cols; i++)
120  logExponents(i) = accu(diffs.unsafe_col(i) % rhs.unsafe_col(i));
121 
122  logProbabilities = -0.5 * x.n_rows * log2pi - 0.5 * logDetCov +
123  logExponents;
124  }
125 
132  arma::vec Random() const;
133 
139  void Train(const arma::mat& observations);
140 
146  void Train(const arma::mat& observations,
147  const arma::vec& probabilities);
148 
152  const arma::vec& Mean() const { return mean; }
153 
157  arma::vec& Mean() { return mean; }
158 
162  const arma::mat& Covariance() const { return covariance; }
163 
167  void Covariance(const arma::mat& covariance);
168 
169  void Covariance(arma::mat&& covariance);
170 
172  const arma::mat& InvCov() const { return invCov; }
173 
175  double LogDetCov() const { return logDetCov; }
176 
180  template<typename Archive>
181  void serialize(Archive& ar, const unsigned int /* version */)
182  {
183  // We just need to serialize each of the members.
184  ar & BOOST_SERIALIZATION_NVP(mean);
185  ar & BOOST_SERIALIZATION_NVP(covariance);
186  ar & BOOST_SERIALIZATION_NVP(covLower);
187  ar & BOOST_SERIALIZATION_NVP(invCov);
188  ar & BOOST_SERIALIZATION_NVP(logDetCov);
189  }
190 
191  private:
197  void FactorCovariance();
198 };
199 
200 } // namespace distribution
201 } // namespace mlpack
202 
203 #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.
.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...
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.