mlpack  2.0.1
gmm.hpp
Go to the documentation of this file.
1 
16 #ifndef __MLPACK_METHODS_MOG_MOG_EM_HPP
17 #define __MLPACK_METHODS_MOG_MOG_EM_HPP
18 
19 #include <mlpack/core.hpp>
20 
21 // This is the default fitting method class.
22 #include "em_fit.hpp"
23 
24 namespace mlpack {
25 namespace gmm {
26 
81 class GMM
82 {
83  private:
85  size_t gaussians;
88 
90  std::vector<distribution::GaussianDistribution> dists;
91 
93  arma::vec weights;
94 
95  public:
99  GMM() :
100  gaussians(0),
101  dimensionality(0)
102  {
103  // Warn the user. They probably don't want to do this. If this constructor
104  // is being used (because it is required by some template classes), the user
105  // should know that it is potentially dangerous.
106  Log::Debug << "GMM::GMM(): no parameters given; Estimate() may fail "
107  << "unless parameters are set." << std::endl;
108  }
109 
117  GMM(const size_t gaussians, const size_t dimensionality);
118 
125  GMM(const std::vector<distribution::GaussianDistribution> & dists,
126  const arma::vec& weights) :
127  gaussians(dists.size()),
128  dimensionality((!dists.empty()) ? dists[0].Mean().n_elem : 0),
129  dists(dists),
130  weights(weights) { /* Nothing to do. */ }
131 
133  GMM(const GMM& other);
134 
136  GMM& operator=(const GMM& other);
137 
139  size_t Gaussians() const { return gaussians; }
141  size_t Dimensionality() const { return dimensionality; }
142 
149  return dists[i]; }
155  distribution::GaussianDistribution& Component(size_t i) { return dists[i]; }
156 
158  const arma::vec& Weights() const { return weights; }
160  arma::vec& Weights() { return weights; }
161 
168  double Probability(const arma::vec& observation) const;
169 
177  double Probability(const arma::vec& observation,
178  const size_t component) const;
179 
186  arma::vec Random() const;
187 
210  template<typename FittingType = EMFit<>>
211  double Train(const arma::mat& observations,
212  const size_t trials = 1,
213  const bool useExistingModel = false,
214  FittingType fitter = FittingType());
215 
240  template<typename FittingType = EMFit<>>
241  double Train(const arma::mat& observations,
242  const arma::vec& probabilities,
243  const size_t trials = 1,
244  const bool useExistingModel = false,
245  FittingType fitter = FittingType());
246 
263  void Classify(const arma::mat& observations,
264  arma::Row<size_t>& labels) const;
265 
269  template<typename Archive>
270  void Serialize(Archive& ar, const unsigned int /* version */);
271 
272  private:
282  double LogLikelihood(
283  const arma::mat& dataPoints,
284  const std::vector<distribution::GaussianDistribution>& distsL,
285  const arma::vec& weights) const;
286 };
287 
288 } // namespace gmm
289 } // namespace mlpack
290 
291 // Include implementation.
292 #include "gmm_impl.hpp"
293 
294 #endif
295 
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
A single multivariate Gaussian distribution.
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.
GMM(const std::vector< distribution::GaussianDistribution > &dists, const arma::vec &weights)
Create a GMM with the given dists and weights.
Definition: gmm.hpp:125
double LogLikelihood(const arma::mat &dataPoints, const std::vector< distribution::GaussianDistribution > &distsL, const arma::vec &weights) const
This function computes the loglikelihood of the given model.
Linear algebra utility functions, generally performed on matrices or vectors.
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:158
const distribution::GaussianDistribution & Component(size_t i) const
Return a const reference to a component distribution.
Definition: gmm.hpp:148
std::vector< distribution::GaussianDistribution > dists
Vector of Gaussians.
Definition: gmm.hpp:90
size_t gaussians
The number of Gaussians in the model.
Definition: gmm.hpp:85
size_t Dimensionality() const
Return the dimensionality of the model.
Definition: gmm.hpp:141
GMM & operator=(const GMM &other)
Copy operator for GMMs.
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
A Gaussian Mixture Model (GMM).
Definition: gmm.hpp:81
static util::NullOutStream Debug
Dumps debug output into the bit nether regions.
Definition: log.hpp:76
arma::vec weights
Vector of a priori weights for each Gaussian.
Definition: gmm.hpp:93
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...
double Probability(const arma::vec &observation) const
Return the probability that the given observation came from this distribution.
void Serialize(Archive &ar, const unsigned int)
Serialize the GMM.
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:160
size_t dimensionality
The dimensionality of the model.
Definition: gmm.hpp:87
distribution::GaussianDistribution & Component(size_t i)
Return a reference to a component distribution.
Definition: gmm.hpp:155
size_t Gaussians() const
Return the number of gaussians in the model.
Definition: gmm.hpp:139
GMM()
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition: gmm.hpp:99