diagonal_gmm.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_GMM_DIAGONAL_GMM_HPP
15 #define MLPACK_METHODS_GMM_DIAGONAL_GMM_HPP
16 
17 #include <mlpack/prereqs.hpp>
19 
20 // This is the default fitting method class.
21 #include "em_fit.hpp"
22 
23 // This is the default covariance matrix constraint.
24 #include "diagonal_constraint.hpp"
25 
26 namespace mlpack {
27 namespace gmm {
28 
75 {
76  private:
78  size_t gaussians;
80  size_t dimensionality;
81 
83  std::vector<distribution::DiagonalGaussianDistribution> dists;
84 
86  arma::vec weights;
87 
88  public:
93  gaussians(0),
94  dimensionality(0)
95  {
96  // Warn the user. They probably don't want to do this. If this
97  // constructor is being used (because it is required by some template
98  // classes), the user should know that it is potentially dangerous.
99  Log::Debug << "DiagonalGMM::DiagonalGMM(): no parameters given;"
100  "Estimate() may fail " << "unless parameters are set." << std::endl;
101  }
102 
110  DiagonalGMM(const size_t gaussians, const size_t dimensionality);
111 
118  DiagonalGMM(const std::vector<distribution::DiagonalGaussianDistribution>&
119  dists, const arma::vec& weights) :
120  gaussians(dists.size()),
121  dimensionality((!dists.empty()) ? dists[0].Mean().n_elem : 0),
122  dists(dists),
123  weights(weights) { /* Nothing to do. */ }
124 
126  DiagonalGMM(const DiagonalGMM& other);
127 
129  DiagonalGMM& operator=(const DiagonalGMM& other);
130 
132  size_t Gaussians() const { return gaussians; }
134  size_t Dimensionality() const { return dimensionality; }
135 
142  {
143  return dists[i];
144  }
145 
152  {
153  return dists[i];
154  }
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 
242  template<typename FittingType = EMFit<kmeans::KMeans<>, DiagonalConstraint,
243  distribution::DiagonalGaussianDistribution>>
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<kmeans::KMeans<>, DiagonalConstraint,
275  distribution::DiagonalGaussianDistribution>>
276  double Train(const arma::mat& observations,
277  const arma::vec& probabilities,
278  const size_t trials = 1,
279  const bool useExistingModel = false,
280  FittingType fitter = FittingType());
281 
299  void Classify(const arma::mat& observations,
300  arma::Row<size_t>& labels) const;
301 
305  template<typename Archive>
306  void serialize(Archive& ar, const uint32_t /* version */);
307 
308  private:
318  double LogLikelihood(
319  const arma::mat& observations,
320  const std::vector<distribution::DiagonalGaussianDistribution>& dists,
321  const arma::vec& weights) const;
322 };
323 
324 } // namespace gmm
325 } // namespace mlpack
326 
327 // Include implementation.
328 #include "diagonal_gmm_impl.hpp"
329 
330 #endif // MLPACK_METHODS_GMM_DIAGONAL_GMM_HPP
Linear algebra utility functions, generally performed on matrices or vectors.
arma::vec & Weights()
Return a reference to the a priori weights of each Gaussian.
double Probability(const arma::vec &observation) const
Return the probability that the given observation came from this distribution.
const arma::vec & Weights() const
Return a const reference to the a priori weights of each Gaussian.
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
A Diagonal Gaussian Mixture Model.
The core includes that mlpack expects; standard C++ includes and Armadillo.
const distribution::DiagonalGaussianDistribution & Component(size_t i) const
Return a const reference to a component distribution.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
DiagonalGMM & operator=(const DiagonalGMM &other)
Copy operator for DiagonalGMMs.
DiagonalGMM(const std::vector< distribution::DiagonalGaussianDistribution > &dists, const arma::vec &weights)
Create a DiagonalGMM with the given dists and weights.
distribution::DiagonalGaussianDistribution & Component(size_t i)
Return a reference to a component 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 DiagonalGMM.
DiagonalGMM()
Create an empty Diagonal Gaussian Mixture Model, with zero gaussians.
double LogProbability(const arma::vec &observation) const
Return the log probability that the given observation came from this distribution.
size_t Dimensionality() const
Return the dimensionality of the model.
void serialize(Archive &ar, const uint32_t)
Serialize the DiagonalGMM.
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 Gaussians() const
Return the number of Gaussians in the model.
A single multivariate Gaussian distribution with diagonal covariance.