mlpack  master
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() { /* 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  using data::CreateNVP;
152 
153  // We just need to serialize each of the members.
154  ar & CreateNVP(mean, "mean");
155  ar & CreateNVP(covariance, "covariance");
156  ar & CreateNVP(covLower, "covLower");
157  ar & CreateNVP(invCov, "invCov");
158  ar & CreateNVP(logDetCov, "logDetCov");
159  }
160 
161  private:
167  void FactorCovariance();
168 };
169 
177 inline void GaussianDistribution::LogProbability(const arma::mat& x,
178  arma::vec& logProbabilities) const
179 {
180  // Column i of 'diffs' is the difference between x.col(i) and the mean.
181  arma::mat diffs = x - (mean * arma::ones<arma::rowvec>(x.n_cols));
182 
183  // Now, we only want to calculate the diagonal elements of (diffs' * cov^-1 *
184  // diffs). We just don't need any of the other elements. We can calculate
185  // the right hand part of the equation (instead of the left side) so that
186  // later we are referencing columns, not rows -- that is faster.
187  const arma::mat rhs = -0.5 * invCov * diffs;
188  arma::vec logExponents(diffs.n_cols); // We will now fill this.
189  for (size_t i = 0; i < diffs.n_cols; i++)
190  logExponents(i) = accu(diffs.unsafe_col(i) % rhs.unsafe_col(i));
191 
192  const size_t k = x.n_rows;
193 
194  logProbabilities = -0.5 * k * log2pi - 0.5 * logDetCov + logExponents;
195 }
196 
197 
198 } // namespace distribution
199 } // namespace mlpack
200 
201 #endif
A single multivariate Gaussian distribution.
arma::vec mean
Mean of the distribution.
const arma::vec & Mean() const
Return the mean.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
The core includes that mlpack expects; standard C++ includes and Armadillo.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename std::enable_if_t< HasSerialize< T >::value > *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
void Train(const arma::mat &observations)
Estimate the Gaussian distribution directly from the given observations.
static const constexpr double log2pi
log(2pi)
void FactorCovariance()
This factors the covariance using arma::chol().
arma::mat covariance
Positive definite covariance of the distribution.
void Serialize(Archive &ar, const unsigned int)
Serialize the distribution.
arma::mat covLower
Lower triangular factor of cov (e.g. cov = LL^T).
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
size_t Dimensionality() const
Return the dimensionality of this distribution.
GaussianDistribution(const size_t dimension)
Create a Gaussian distribution with zero mean and identity covariance with the given dimensionality...
const arma::mat & Covariance() const
Return the covariance matrix.
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
arma::mat invCov
Cached inverse of covariance.
GaussianDistribution()
Default constructor, which creates a Gaussian with zero dimension.
void Probability(const arma::mat &x, arma::vec &probabilities) const
Calculates the multivariate Gaussian probability density function for each data point (column) in the...
arma::vec & Mean()
Return a modifiable copy of the mean.