diagonal_gaussian_distribution.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_DISTRIBUTIONS_DIAGONAL_GAUSSIAN_DISTRIBUTION_HPP
13 #define MLPACK_CORE_DISTRIBUTIONS_DIAGONAL_GAUSSIAN_DISTRIBUTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace distribution {
19 
22 {
23  private:
25  arma::vec mean;
27  arma::vec covariance;
29  arma::vec invCov;
31  double logDetCov;
32 
34  static const constexpr double log2pi = 1.83787706640934533908193770912475883;
35 
36  public:
38  DiagonalGaussianDistribution() : logDetCov(0.0) { /* nothing to do. */ }
39 
46  DiagonalGaussianDistribution(const size_t dimension) :
47  mean(arma::zeros<arma::vec>(dimension)),
48  covariance(arma::ones<arma::vec>(dimension)),
49  invCov(arma::ones<arma::vec>(dimension)),
50  logDetCov(0)
51  { /* Nothing to do. */ }
52 
60  DiagonalGaussianDistribution(const arma::vec& mean,
61  const arma::vec& covariance);
62 
64  size_t Dimensionality() const { return mean.n_elem; }
65 
67  double Probability(const arma::vec& observation) const
68  {
69  return exp(LogProbability(observation));
70  }
71 
73  double LogProbability(const arma::vec& observation) const;
74 
82  void Probability(const arma::mat& x, arma::vec& probabilities) const
83  {
84  arma::vec logProbabilities;
85  LogProbability(x, logProbabilities);
86  probabilities = arma::exp(logProbabilities);
87  }
88 
96  void LogProbability(const arma::mat& observations,
97  arma::vec& logProbabilities) const;
98 
105  arma::vec Random() const;
106 
112  void Train(const arma::mat& observations);
113 
123  void Train(const arma::mat& observations,
124  const arma::vec& probabilities);
125 
127  const arma::vec& Mean() const { return mean; }
128 
130  arma::vec& Mean() { return mean; }
131 
133  const arma::vec& Covariance() const { return covariance; }
134 
136  void Covariance(const arma::vec& covariance);
137 
139  void Covariance(arma::vec&& covariance);
140 
142  template<typename Archive>
143  void serialize(Archive& ar, const uint32_t /* version */)
144  {
145  // We just need to serialize each of the members.
146  ar(CEREAL_NVP(mean));
147  ar(CEREAL_NVP(covariance));
148  ar(CEREAL_NVP(invCov));
149  ar(CEREAL_NVP(logDetCov));
150  }
151 };
152 
153 } // namespace distribution
154 } // namespace mlpack
155 
156 #endif
arma::vec & Mean()
Return a modifiable copy of the mean.
Linear algebra utility functions, generally performed on matrices or vectors.
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
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...
void Probability(const arma::mat &x, arma::vec &probabilities) const
Calculate the multivariate Gaussian probability density function for each data point (column) in the ...
DiagonalGaussianDistribution()
Default constructor, which creates a Gaussian with zero dimension.
size_t Dimensionality() const
Return the dimensionality of this distribution.
void serialize(Archive &ar, const uint32_t)
Serialize the distribution.
DiagonalGaussianDistribution(const size_t dimension)
Create a Gaussian Distribution with zero mean and diagonal covariance with the given dimensionality...
const arma::vec & Covariance() const
Return the covariance matrix.
A single multivariate Gaussian distribution with diagonal covariance.