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  void Probability(const arma::mat& observation, arma::vec& probs) const;
174 
181  double LogProbability(const arma::vec& observation) const;
182 
189  void LogProbability(const arma::mat& observation, arma::vec& logProbs) const;
190 
198  double Probability(const arma::vec& observation,
199  const size_t component) const;
200 
208  double LogProbability(const arma::vec& observation,
209  const size_t component) const;
216  arma::vec Random() const;
217 
241  template<typename FittingType = EMFit<>>
242  double Train(const arma::mat& observations,
243  const size_t trials = 1,
244  const bool useExistingModel = false,
245  FittingType fitter = FittingType());
246 
272  template<typename FittingType = EMFit<>>
273  double Train(const arma::mat& observations,
274  const arma::vec& probabilities,
275  const size_t trials = 1,
276  const bool useExistingModel = false,
277  FittingType fitter = FittingType());
278 
295  void Classify(const arma::mat& observations,
296  arma::Row<size_t>& labels) const;
297 
301  template<typename Archive>
302  void serialize(Archive& ar, const uint32_t /* version */);
303 
304  private:
314  double LogLikelihood(
315  const arma::mat& dataPoints,
316  const std::vector<distribution::GaussianDistribution>& distsL,
317  const arma::vec& weights) const;
318 };
319 
320 } // namespace gmm
321 } // namespace mlpack
322 
323 // Include implementation.
324 #include "gmm_impl.hpp"
325 
326 #endif
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.
void serialize(Archive &ar, const uint32_t)
Serialize the GMM.
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:157
Linear algebra utility functions, generally performed on matrices or vectors.
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.
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