mlpack  1.0.12
gmm.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_METHODS_MOG_MOG_EM_HPP
16 #define __MLPACK_METHODS_MOG_MOG_EM_HPP
17 
18 #include <mlpack/core.hpp>
19 
20 // This is the default fitting method class.
21 #include "em_fit.hpp"
22 
23 namespace mlpack {
24 namespace gmm {
25 
80 template<typename FittingType = EMFit<> >
81 class GMM
82 {
83  private:
85  size_t gaussians;
89  std::vector<arma::vec> means;
91  std::vector<arma::mat> covariances;
93  arma::vec weights;
94 
95  public:
99  GMM() :
100  gaussians(0),
101  dimensionality(0),
102  localFitter(FittingType()),
104  {
105  // Warn the user. They probably don't want to do this. If this constructor
106  // is being used (because it is required by some template classes), the user
107  // should know that it is potentially dangerous.
108  Log::Debug << "GMM::GMM(): no parameters given; Estimate() may fail "
109  << "unless parameters are set." << std::endl;
110  }
111 
119  GMM(const size_t gaussians, const size_t dimensionality);
120 
131  GMM(const size_t gaussians,
132  const size_t dimensionality,
133  FittingType& fitter);
134 
142  GMM(const std::vector<arma::vec>& means,
143  const std::vector<arma::mat>& covariances,
144  const arma::vec& weights) :
145  gaussians(means.size()),
146  dimensionality((!means.empty()) ? means[0].n_elem : 0),
147  means(means),
148  covariances(covariances),
149  weights(weights),
150  localFitter(FittingType()),
151  fitter(localFitter) { /* Nothing to do. */ }
152 
162  GMM(const std::vector<arma::vec>& means,
163  const std::vector<arma::mat>& covariances,
164  const arma::vec& weights,
165  FittingType& fitter) :
166  gaussians(means.size()),
167  dimensionality((!means.empty()) ? means[0].n_elem : 0),
168  means(means),
169  covariances(covariances),
170  weights(weights),
171  fitter(fitter) { /* Nothing to do. */ }
172 
176  template<typename OtherFittingType>
177  GMM(const GMM<OtherFittingType>& other);
178 
183  GMM(const GMM& other);
184 
188  template<typename OtherFittingType>
189  GMM& operator=(const GMM<OtherFittingType>& other);
190 
195  GMM& operator=(const GMM& other);
196 
203  void Load(const std::string& filename);
204 
210  void Save(const std::string& filename) const;
211 
213  size_t Gaussians() const { return gaussians; }
216  size_t& Gaussians() { return gaussians; }
217 
219  size_t Dimensionality() const { return dimensionality; }
222  size_t& Dimensionality() { return dimensionality; }
223 
225  const std::vector<arma::vec>& Means() const { return means; }
227  std::vector<arma::vec>& Means() { return means; }
228 
230  const std::vector<arma::mat>& Covariances() const { return covariances; }
232  std::vector<arma::mat>& Covariances() { return covariances; }
233 
235  const arma::vec& Weights() const { return weights; }
237  arma::vec& Weights() { return weights; }
238 
240  const FittingType& Fitter() const { return fitter; }
242  FittingType& Fitter() { return fitter; }
243 
250  double Probability(const arma::vec& observation) const;
251 
259  double Probability(const arma::vec& observation,
260  const size_t component) const;
261 
268  arma::vec Random() const;
269 
292  double Estimate(const arma::mat& observations,
293  const size_t trials = 1,
294  const bool useExistingModel = false);
295 
320  double Estimate(const arma::mat& observations,
321  const arma::vec& probabilities,
322  const size_t trials = 1,
323  const bool useExistingModel = false);
324 
341  void Classify(const arma::mat& observations,
342  arma::Col<size_t>& labels) const;
343 
347  std::string ToString() const;
348 
349  private:
359  double LogLikelihood(const arma::mat& dataPoints,
360  const std::vector<arma::vec>& means,
361  const std::vector<arma::mat>& covars,
362  const arma::vec& weights) const;
363 
365  FittingType localFitter;
366 
368  FittingType& fitter;
369 };
370 
371 }; // namespace gmm
372 }; // namespace mlpack
373 
374 // Include implementation.
375 #include "gmm_impl.hpp"
376 
377 #endif
378 
FittingType & fitter
Reference to the fitting object we should use.
Definition: gmm.hpp:368
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:235
std::vector< arma::vec > & Means()
Return a reference to the vector of means (mu).
Definition: gmm.hpp:227
GMM()
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition: gmm.hpp:99
FittingType localFitter
Locally-stored fitting object; in case the user did not pass one.
Definition: gmm.hpp:365
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
std::vector< arma::mat > & Covariances()
Return a reference to the vector of covariance matrices (sigma).
Definition: gmm.hpp:232
size_t & Gaussians()
Modify the number of gaussians in the model.
Definition: gmm.hpp:216
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:91
arma::vec weights
Vector of a priori weights for each Gaussian.
Definition: gmm.hpp:93
FittingType & Fitter()
Return a reference to the fitting type.
Definition: gmm.hpp:242
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:162
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:237
const std::vector< arma::mat > & Covariances() const
Return a const reference to the vector of covariance matrices (sigma).
Definition: gmm.hpp:230
size_t & Dimensionality()
Modify the dimensionality of the model.
Definition: gmm.hpp:222
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:142
const std::vector< arma::vec > & Means() const
Return a const reference to the vector of means (mu).
Definition: gmm.hpp:225
A Gaussian Mixture Model (GMM).
Definition: gmm.hpp:81
std::vector< arma::vec > means
Vector of means; one for each Gaussian.
Definition: gmm.hpp:89
static util::NullOutStream Debug
Dumps debug output into the bit nether regions.
Definition: log.hpp:76
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:240
size_t dimensionality
The dimensionality of the model.
Definition: gmm.hpp:87
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:219
size_t Gaussians() const
Return the number of gaussians in the model.
Definition: gmm.hpp:213
size_t gaussians
The number of Gaussians in the model.
Definition: gmm.hpp:85