mlpack 3.4.2
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
18namespace mlpack {
19namespace 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
A single multivariate Gaussian distribution.
const arma::mat & Covariance() const
Return the covariance matrix.
GaussianDistribution(const size_t dimension)
Create a Gaussian distribution with zero mean and identity covariance with the given dimensionality.
void Probability(const arma::mat &x, arma::vec &probabilities) const
Calculates the multivariate Gaussian probability density function for each data point (column) in the...
const arma::mat & InvCov() const
Return the invCov.
void Train(const arma::mat &observations)
Estimate the Gaussian distribution directly from the given observations.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
const arma::vec & Mean() const
Return the mean.
void Covariance(const arma::mat &covariance)
Set the covariance.
GaussianDistribution()
Default constructor, which creates a Gaussian with zero dimension.
void Covariance(arma::mat &&covariance)
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
size_t Dimensionality() const
Return the dimensionality of this distribution.
arma::vec & Mean()
Return a modifiable copy of the mean.
void LogProbability(const arma::mat &x, arma::vec &logProbabilities) const
Returns the Log probability of the given matrix.
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
double LogDetCov() const
Return the logDetCov.
void Train(const arma::mat &observations, const arma::vec &probabilities)
Estimate the Gaussian distribution from the given observations, taking into account the probability o...
void serialize(Archive &ar, const unsigned int)
Serialize the distribution.
GaussianDistribution(const arma::vec &mean, const arma::mat &covariance)
Create a Gaussian distribution with the given mean and covariance.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.