gmm.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_GMM_GMM_HPP
14 #define MLPACK_METHODS_GMM_GMM_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 // This is the default fitting method class.
19 #include "em_fit.hpp"
20 
22 
23 namespace mlpack {
24 namespace gmm {
25 
80 class GMM
81 {
82  private:
84  size_t gaussians;
86  size_t dimensionality;
87 
89  std::vector<distribution::GaussianDistribution> dists;
90 
92  arma::vec weights;
93 
94  public:
98  GMM() :
99  gaussians(0),
100  dimensionality(0)
101  {
102  // Warn the user. They probably don't want to do this. If this constructor
103  // is being used (because it is required by some template classes), the user
104  // should know that it is potentially dangerous.
105  Log::Debug << "GMM::GMM(): no parameters given; Estimate() may fail "
106  << "unless parameters are set." << std::endl;
107  }
108 
116  GMM(const size_t gaussians, const size_t dimensionality);
117 
124  GMM(const std::vector<distribution::GaussianDistribution> & dists,
125  const arma::vec& weights) :
126  gaussians(dists.size()),
127  dimensionality((!dists.empty()) ? dists[0].Mean().n_elem : 0),
128  dists(dists),
129  weights(weights) { /* Nothing to do. */ }
130 
132  GMM(const GMM& other);
133 
135  GMM& operator=(const GMM& other);
136 
138  size_t Gaussians() const { return gaussians; }
140  size_t Dimensionality() const { return dimensionality; }
141 
148  return dists[i]; }
154  distribution::GaussianDistribution& Component(size_t i) { return dists[i]; }
155 
157  const arma::vec& Weights() const { return weights; }
159  arma::vec& Weights() { return weights; }
160 
167  double Probability(const arma::vec& observation) const;
168 
175  void Probability(const arma::mat& observation, arma::vec& probs) const;
176 
183  double LogProbability(const arma::vec& observation) const;
184 
191  void LogProbability(const arma::mat& observation, arma::vec& logProbs) const;
192 
200  double Probability(const arma::vec& observation,
201  const size_t component) const;
202 
210  double LogProbability(const arma::vec& observation,
211  const size_t component) const;
218  arma::vec Random() const;
219 
243  template<typename FittingType = EMFit<>>
244  double Train(const arma::mat& observations,
245  const size_t trials = 1,
246  const bool useExistingModel = false,
247  FittingType fitter = FittingType());
248 
274  template<typename FittingType = EMFit<>>
275  double Train(const arma::mat& observations,
276  const arma::vec& probabilities,
277  const size_t trials = 1,
278  const bool useExistingModel = false,
279  FittingType fitter = FittingType());
280 
297  void Classify(const arma::mat& observations,
298  arma::Row<size_t>& labels) const;
299 
303  template<typename Archive>
304  void serialize(Archive& ar, const uint32_t /* version */);
305 
306  private:
316  double LogLikelihood(
317  const arma::mat& dataPoints,
318  const std::vector<distribution::GaussianDistribution>& distsL,
319  const arma::vec& weights) const;
320 };
321 
322 } // namespace gmm
323 } // namespace mlpack
324 
325 // Include implementation.
326 #include "gmm_impl.hpp"
327 
328 #endif
GMM(const std::vector< distribution::GaussianDistribution > &dists, const arma::vec &weights)
Create a GMM with the given dists and weights.
Definition: gmm.hpp:124
constexpr bool empty(Container const &container) noexcept
Definition: iterator.hpp:37
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:159
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
Linear algebra utility functions, generally performed on matrices or vectors.
GMM()
Create an empty Gaussian Mixture Model, with zero gaussians.
Definition: gmm.hpp:98
The core includes that mlpack expects; standard C++ includes, Armadillo, cereal, and a few basic mlpa...
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
Definition: gmm.hpp:157
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:147
A Gaussian Mixture Model (GMM).
Definition: gmm.hpp:80
distribution::GaussianDistribution & Component(size_t i)
Return a reference to a component distribution.
Definition: gmm.hpp:154
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.
static util::NullOutStream Debug
Definition: log.hpp:90
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:140
size_t Gaussians() const
Return the number of gaussians in the model.
Definition: gmm.hpp:138