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<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)),
91 void Probability(
const arma::mat& x, arma::vec& probabilities)
const 93 arma::vec logProbabilities;
95 probabilities = arma::exp(logProbabilities);
98 void LogProbability(
const arma::mat& x, arma::vec& logProbabilities)
const;
113 void Train(
const arma::mat& observations);
120 void Train(
const arma::mat& observations,
121 const arma::vec& probabilities);
148 template<
typename Archive>
155 ar &
CreateNVP(covariance,
"covariance");
178 arma::vec& logProbabilities)
const 181 arma::mat diffs = x - (
mean * arma::ones<arma::rowvec>(x.n_cols));
187 const arma::mat rhs = -0.5 *
invCov * diffs;
188 arma::vec logExponents(diffs.n_cols);
189 for (
size_t i = 0; i < diffs.n_cols; i++)
190 logExponents(i) = accu(diffs.unsafe_col(i) % rhs.unsafe_col(i));
192 const size_t k = x.n_rows;
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.
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().
double logDetCov
Cached logdet(cov).
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.