MLPACK  1.0.10
gmm.hpp
Go to the documentation of this file.
1 
23 #ifndef __MLPACK_METHODS_MOG_MOG_EM_HPP
24 #define __MLPACK_METHODS_MOG_MOG_EM_HPP
25 
26 #include <mlpack/core.hpp>
27 
28 // This is the default fitting method class.
29 #include "em_fit.hpp"
30 
31 namespace mlpack {
32 namespace gmm {
33 
88 template<typename FittingType = EMFit<> >
89 class GMM
90 {
91  private:
93  size_t gaussians;
97  std::vector<arma::vec> means;
99  std::vector<arma::mat> covariances;
101  arma::vec weights;
102 
103  public:
107  GMM() :
108  gaussians(0),
109  dimensionality(0),
110  localFitter(FittingType()),
112  {
113  // Warn the user. They probably don't want to do this. If this constructor
114  // is being used (because it is required by some template classes), the user
115  // should know that it is potentially dangerous.
116  Log::Debug << "GMM::GMM(): no parameters given; Estimate() may fail "
117  << "unless parameters are set." << std::endl;
118  }
119 
127  GMM(const size_t gaussians, const size_t dimensionality);
128 
139  GMM(const size_t gaussians,
140  const size_t dimensionality,
141  FittingType& fitter);
142 
150  GMM(const std::vector<arma::vec>& means,
151  const std::vector<arma::mat>& covariances,
152  const arma::vec& weights) :
153  gaussians(means.size()),
154  dimensionality((!means.empty()) ? means[0].n_elem : 0),
155  means(means),
156  covariances(covariances),
157  weights(weights),
158  localFitter(FittingType()),
159  fitter(localFitter) { /* Nothing to do. */ }
160 
170  GMM(const std::vector<arma::vec>& means,
171  const std::vector<arma::mat>& covariances,
172  const arma::vec& weights,
173  FittingType& fitter) :
174  gaussians(means.size()),
175  dimensionality((!means.empty()) ? means[0].n_elem : 0),
176  means(means),
177  covariances(covariances),
178  weights(weights),
179  fitter(fitter) { /* Nothing to do. */ }
180 
184  template<typename OtherFittingType>
185  GMM(const GMM<OtherFittingType>& other);
186 
191  GMM(const GMM& other);
192 
196  template<typename OtherFittingType>
197  GMM& operator=(const GMM<OtherFittingType>& other);
198 
203  GMM& operator=(const GMM& other);
204 
211  void Load(const std::string& filename);
212 
218  void Save(const std::string& filename) const;
219 
221  size_t Gaussians() const { return gaussians; }
224  size_t& Gaussians() { return gaussians; }
225 
227  size_t Dimensionality() const { return dimensionality; }
230  size_t& Dimensionality() { return dimensionality; }
231 
233  const std::vector<arma::vec>& Means() const { return means; }
235  std::vector<arma::vec>& Means() { return means; }
236 
238  const std::vector<arma::mat>& Covariances() const { return covariances; }
240  std::vector<arma::mat>& Covariances() { return covariances; }
241 
243  const arma::vec& Weights() const { return weights; }
245  arma::vec& Weights() { return weights; }
246 
248  const FittingType& Fitter() const { return fitter; }
250  FittingType& Fitter() { return fitter; }
251 
258  double Probability(const arma::vec& observation) const;
259 
267  double Probability(const arma::vec& observation,
268  const size_t component) const;
269 
276  arma::vec Random() const;
277 
300  double Estimate(const arma::mat& observations,
301  const size_t trials = 1,
302  const bool useExistingModel = false);
303 
328  double Estimate(const arma::mat& observations,
329  const arma::vec& probabilities,
330  const size_t trials = 1,
331  const bool useExistingModel = false);
332 
349  void Classify(const arma::mat& observations,
350  arma::Col<size_t>& labels) const;
351 
355  std::string ToString() const;
356 
357  private:
367  double LogLikelihood(const arma::mat& dataPoints,
368  const std::vector<arma::vec>& means,
369  const std::vector<arma::mat>& covars,
370  const arma::vec& weights) const;
371 
373  FittingType localFitter;
374 
376  FittingType& fitter;
377 };
378 
379 }; // namespace gmm
380 }; // namespace mlpack
381 
382 // Include implementation.
383 #include "gmm_impl.hpp"
384 
385 #endif
386 
FittingType & fitter
Reference to the fitting object we should use.
Definition: gmm.hpp:376
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:243
std::vector< arma::vec > & Means()
Return a reference to the vector of means (mu).
Definition: gmm.hpp:235
GMM()
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition: gmm.hpp:107
FittingType localFitter
Locally-stored fitting object; in case the user did not pass one.
Definition: gmm.hpp:373
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
std::vector< arma::mat > & Covariances()
Return a reference to the vector of covariance matrices (sigma).
Definition: gmm.hpp:240
size_t & Gaussians()
Modify the number of gaussians in the model.
Definition: gmm.hpp:224
double Estimate(const arma::mat &observations, const size_t trials=1, const bool useExistingModel=false)
Estimate the probability distribution directly from the given observations, using the given algorithm...
double LogLikelihood(const arma::mat &dataPoints, const std::vector< arma::vec > &means, const std::vector< arma::mat > &covars, const arma::vec &weights) const
This function computes the loglikelihood of the given model.
std::vector< arma::mat > covariances
Vector of covariances; one for each Gaussian.
Definition: gmm.hpp:99
arma::vec weights
Vector of a priori weights for each Gaussian.
Definition: gmm.hpp:101
FittingType & Fitter()
Return a reference to the fitting type.
Definition: gmm.hpp:250
void Classify(const arma::mat &observations, arma::Col< size_t > &labels) const
Classify the given observations as being from an individual component in this GMM.
double Probability(const arma::vec &observation) const
Return the probability that the given observation came from this distribution.
GMM(const std::vector< arma::vec > &means, const std::vector< arma::mat > &covariances, const arma::vec &weights, FittingType &fitter)
Create a GMM with the given means, covariances, and weights, and use the given initialized FittingTyp...
Definition: gmm.hpp:170
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:245
const std::vector< arma::mat > & Covariances() const
Return a const reference to the vector of covariance matrices (sigma).
Definition: gmm.hpp:238
size_t & Dimensionality()
Modify the dimensionality of the model.
Definition: gmm.hpp:230
GMM(const std::vector< arma::vec > &means, const std::vector< arma::mat > &covariances, const arma::vec &weights)
Create a GMM with the given means, covariances, and weights.
Definition: gmm.hpp:150
const std::vector< arma::vec > & Means() const
Return a const reference to the vector of means (mu).
Definition: gmm.hpp:233
A Gaussian Mixture Model (GMM).
Definition: gmm.hpp:89
std::vector< arma::vec > means
Vector of means; one for each Gaussian.
Definition: gmm.hpp:97
static util::NullOutStream Debug
Dumps debug output into the bit nether regions.
Definition: log.hpp:84
void Save(const std::string &filename) const
Save a GMM to an XML file.
const FittingType & Fitter() const
Return a const reference to the fitting type.
Definition: gmm.hpp:248
size_t dimensionality
The dimensionality of the model.
Definition: gmm.hpp:95
GMM & operator=(const GMM< OtherFittingType > &other)
Copy operator for GMMs which use different fitting types.
std::string ToString() const
Returns a string representation of this object.
void Load(const std::string &filename)
Load a GMM from an XML file.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
size_t Dimensionality() const
Return the dimensionality of the model.
Definition: gmm.hpp:227
size_t Gaussians() const
Return the number of gaussians in the model.
Definition: gmm.hpp:221
size_t gaussians
The number of Gaussians in the model.
Definition: gmm.hpp:93