13 #ifndef MLPACK_CORE_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP 14 #define MLPACK_CORE_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP 19 namespace distribution {
39 static const constexpr
double log2pi = 1.83787706640934533908193770912475883;
52 mean(arma::zeros
(dimension)), 53 covariance(arma::eye
(dimension, dimension)), 54 covLower(arma::eye
(dimension, dimension)), 55 invCov(arma::eye
(dimension, dimension)), 91 void Probability(
const arma::mat& x, arma::vec& probabilities)
const 93 probabilities.set_size(x.n_cols);
94 for (
size_t i = 0; i < x.n_cols; ++i)
112 diffs.each_col() -= mean;
117 const arma::mat rhs = -0.5 * invCov * diffs;
118 arma::vec logExponents(diffs.n_cols);
119 for (
size_t i = 0; i < diffs.n_cols; ++i)
120 logExponents(i) = accu(diffs.unsafe_col(i) % rhs.unsafe_col(i));
122 logProbabilities = -0.5 * x.n_rows * log2pi - 0.5 * logDetCov +
139 void Train(
const arma::mat& observations);
146 void Train(
const arma::mat& observations,
147 const arma::vec& probabilities);
152 const arma::vec&
Mean()
const {
return mean; }
157 arma::vec&
Mean() {
return mean; }
172 const arma::mat&
InvCov()
const {
return invCov; }
180 template<
typename Archive>
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);
197 void FactorCovariance();
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.
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.