ensmallen
mlpack
fast, flexible C++ machine learning library
gmm.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_MOG_MOG_EM_HPP
14 #define MLPACK_METHODS_MOG_MOG_EM_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 // This is the default fitting method class.
19 #include "em_fit.hpp"
20 
21 namespace mlpack {
22 namespace gmm {
23 
78 class GMM
79 {
80  private:
82  size_t gaussians;
84  size_t dimensionality;
85 
87  std::vector<distribution::GaussianDistribution> dists;
88 
90  arma::vec weights;
91 
92  public:
96  GMM() :
97  gaussians(0),
98  dimensionality(0)
99  {
100  // Warn the user. They probably don't want to do this. If this constructor
101  // is being used (because it is required by some template classes), the user
102  // should know that it is potentially dangerous.
103  Log::Debug << "GMM::GMM(): no parameters given; Estimate() may fail "
104  << "unless parameters are set." << std::endl;
105  }
106 
114  GMM(const size_t gaussians, const size_t dimensionality);
115 
122  GMM(const std::vector<distribution::GaussianDistribution> & dists,
123  const arma::vec& weights) :
124  gaussians(dists.size()),
125  dimensionality((!dists.empty()) ? dists[0].Mean().n_elem : 0),
126  dists(dists),
127  weights(weights) { /* Nothing to do. */ }
128 
130  GMM(const GMM& other);
131 
133  GMM& operator=(const GMM& other);
134 
136  size_t Gaussians() const { return gaussians; }
138  size_t Dimensionality() const { return dimensionality; }
139 
146  return dists[i]; }
152  distribution::GaussianDistribution& Component(size_t i) { return dists[i]; }
153 
155  const arma::vec& Weights() const { return weights; }
157  arma::vec& Weights() { return weights; }
158 
165  double Probability(const arma::vec& observation) const;
166 
173  double LogProbability(const arma::vec& observation) const;
174 
182  double Probability(const arma::vec& observation,
183  const size_t component) const;
184 
192  double LogProbability(const arma::vec& observation,
193  const size_t component) const;
200  arma::vec Random() const;
201 
224  template<typename FittingType = EMFit<>>
225  double Train(const arma::mat& observations,
226  const size_t trials = 1,
227  const bool useExistingModel = false,
228  FittingType fitter = FittingType());
229 
254  template<typename FittingType = EMFit<>>
255  double Train(const arma::mat& observations,
256  const arma::vec& probabilities,
257  const size_t trials = 1,
258  const bool useExistingModel = false,
259  FittingType fitter = FittingType());
260 
277  void Classify(const arma::mat& observations,
278  arma::Row<size_t>& labels) const;
279 
283  template<typename Archive>
284  void serialize(Archive& ar, const unsigned int /* version */);
285 
286  private:
296  double LogLikelihood(
297  const arma::mat& dataPoints,
298  const std::vector<distribution::GaussianDistribution>& distsL,
299  const arma::vec& weights) const;
300 };
301 
302 } // namespace gmm
303 } // namespace mlpack
304 
305 // Include implementation.
306 #include "gmm_impl.hpp"
307 
308 #endif
309 
GMM(const std::vector< distribution::GaussianDistribution > &dists, const arma::vec &weights)
Create a GMM with the given dists and weights.
Definition: gmm.hpp:122
A single multivariate Gaussian distribution.
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:157
.hpp
Definition: add_to_po.hpp:21
static MLPACK_EXPORT util::NullOutStream Debug
MLPACK_EXPORT is required for global variables, so that they are properly exported by the Windows com...
Definition: log.hpp:79
GMM()
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition: gmm.hpp:96
The core includes that mlpack expects; standard C++ includes and Armadillo.
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:155
GMM & operator=(const GMM &other)
Copy operator for GMMs.
void Classify(const arma::mat &observations, arma::Row< size_t > &labels) const
Classify the given observations as being from an individual component in this GMM.
void serialize(Archive &ar, const unsigned int)
Serialize the GMM.
double Probability(const arma::vec &observation) const
Return the probability that the given observation came from this distribution.
const distribution::GaussianDistribution & Component(size_t i) const
Return a const reference to a component distribution.
Definition: gmm.hpp:145
A Gaussian Mixture Model (GMM).
Definition: gmm.hpp:78
distribution::GaussianDistribution & Component(size_t i)
Return a reference to a component distribution.
Definition: gmm.hpp:152
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
double LogProbability(const arma::vec &observation) const
Return the log probability that the given observation came from this distribution.
double Train(const arma::mat &observations, const size_t trials=1, const bool useExistingModel=false, FittingType fitter=FittingType())
Estimate the probability distribution directly from the given observations, using the given algorithm...
size_t Dimensionality() const
Return the dimensionality of the model.
Definition: gmm.hpp:138
size_t Gaussians() const
Return the number of gaussians in the model.
Definition: gmm.hpp:136