mlpack  master
Public Member Functions | Private Member Functions | Private Attributes | List of all members
mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy > Class Template Reference

This class contains methods which can fit a GMM to observations using the EM algorithm. More...

Public Member Functions

 EMFit (const size_t maxIterations=300, const double tolerance=1e-10, InitialClusteringType clusterer=InitialClusteringType(), CovarianceConstraintPolicy constraint=CovarianceConstraintPolicy())
 Construct the EMFit object, optionally passing an InitialClusteringType object (just in case it needs to store state). More...
 
const InitialClusteringType & Clusterer () const
 Get the clusterer. More...
 
InitialClusteringType & Clusterer ()
 Modify the clusterer. More...
 
const CovarianceConstraintPolicy & Constraint () const
 Get the covariance constraint policy class. More...
 
CovarianceConstraintPolicy & Constraint ()
 Modify the covariance constraint policy class. More...
 
void Estimate (const arma::mat &observations, std::vector< distribution::GaussianDistribution > &dists, arma::vec &weights, const bool useInitialModel=false)
 Fit the observations to a Gaussian mixture model (GMM) using the EM algorithm. More...
 
void Estimate (const arma::mat &observations, const arma::vec &probabilities, std::vector< distribution::GaussianDistribution > &dists, arma::vec &weights, const bool useInitialModel=false)
 Fit the observations to a Gaussian mixture model (GMM) using the EM algorithm, taking into account the probabilities of each point being from this mixture. More...
 
size_t MaxIterations () const
 Get the maximum number of iterations of the EM algorithm. More...
 
size_t & MaxIterations ()
 Modify the maximum number of iterations of the EM algorithm. More...
 
template<typename Archive >
void Serialize (Archive &ar, const unsigned int version)
 Serialize the fitter. More...
 
double Tolerance () const
 Get the tolerance for the convergence of the EM algorithm. More...
 
double & Tolerance ()
 Modify the tolerance for the convergence of the EM algorithm. More...
 

Private Member Functions

void InitialClustering (const arma::mat &observations, std::vector< distribution::GaussianDistribution > &dists, arma::vec &weights)
 Run the clusterer, and then turn the cluster assignments into Gaussians. More...
 
double LogLikelihood (const arma::mat &data, const std::vector< distribution::GaussianDistribution > &dists, const arma::vec &weights) const
 Calculate the log-likelihood of a model. More...
 

Private Attributes

InitialClusteringType clusterer
 Object which will perform the clustering. More...
 
CovarianceConstraintPolicy constraint
 Object which applies constraints to the covariance matrix. More...
 
size_t maxIterations
 Maximum iterations of EM algorithm. More...
 
double tolerance
 Tolerance for convergence of EM. More...
 

Detailed Description

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
class mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >

This class contains methods which can fit a GMM to observations using the EM algorithm.

It requires an initial clustering mechanism, which is by default the KMeans algorithm. The clustering mechanism must implement the following method:

This method should create 'clusters' clusters, and return the assignment of each point to a cluster.

Definition at line 43 of file em_fit.hpp.

Constructor & Destructor Documentation

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::EMFit ( const size_t  maxIterations = 300,
const double  tolerance = 1e-10,
InitialClusteringType  clusterer = InitialClusteringType(),
CovarianceConstraintPolicy  constraint = CovarianceConstraintPolicy() 
)

Construct the EMFit object, optionally passing an InitialClusteringType object (just in case it needs to store state).

Setting the maximum number of iterations to 0 means that the EM algorithm will iterate until convergence (with the given tolerance).

The parameter forcePositive controls whether or not the covariance matrices are checked for positive definiteness at each iteration. This could be a time-consuming task, so, if you know your data is well-behaved, you can set it to false and save some runtime.

Parameters
maxIterationsMaximum number of iterations for EM.
toleranceLog-likelihood tolerance required for convergence.
forcePositiveCheck for positive-definiteness of each covariance matrix at each iteration.
clustererObject which will perform the initial clustering.

Member Function Documentation

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
const InitialClusteringType& mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Clusterer ( ) const
inline
template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
InitialClusteringType& mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Clusterer ( )
inline

Modify the clusterer.

Definition at line 114 of file em_fit.hpp.

References mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::clusterer.

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
const CovarianceConstraintPolicy& mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Constraint ( ) const
inline

Get the covariance constraint policy class.

Definition at line 117 of file em_fit.hpp.

References mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::constraint.

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
CovarianceConstraintPolicy& mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Constraint ( )
inline

Modify the covariance constraint policy class.

Definition at line 119 of file em_fit.hpp.

References mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::constraint.

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
void mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Estimate ( const arma::mat &  observations,
std::vector< distribution::GaussianDistribution > &  dists,
arma::vec &  weights,
const bool  useInitialModel = false 
)

Fit the observations to a Gaussian mixture model (GMM) using the EM algorithm.

The size of the vectors (indicating the number of components) must already be set. Optionally, if useInitialModel is set to true, then the model given in the means, covariances, and weights parameters is used as the initial model, instead of using the InitialClusteringType::Cluster() option.

Parameters
observationsList of observations to train on.
meansVector to store trained means in.
covariancesVector to store trained covariances in.
weightsVector to store a priori weights in.
useInitialModelIf true, the given model is used for the initial clustering.
template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
void mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Estimate ( const arma::mat &  observations,
const arma::vec &  probabilities,
std::vector< distribution::GaussianDistribution > &  dists,
arma::vec &  weights,
const bool  useInitialModel = false 
)

Fit the observations to a Gaussian mixture model (GMM) using the EM algorithm, taking into account the probabilities of each point being from this mixture.

The size of the vectors (indicating the number of components) must already be set. Optionally, if useInitialModel is set to true, then the model given in the means, covariances, and weights parameters is used as the initial model, instead of using the InitialClusteringType::Cluster() option.

Parameters
observationsList of observations to train on.
probabilitiesProbability of each point being from this model.
meansVector to store trained means in.
covariancesVector to store trained covariances in.
weightsVector to store a priori weights in.
useInitialModelIf true, the given model is used for the initial clustering.
template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
void mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::InitialClustering ( const arma::mat &  observations,
std::vector< distribution::GaussianDistribution > &  dists,
arma::vec &  weights 
)
private

Run the clusterer, and then turn the cluster assignments into Gaussians.

This is a helper function for both overloads of Estimate(). The vectors must be already set to the number of clusters.

Parameters
observationsList of observations.
meansVector to store means in.
covariancesVector to store covariances in.
weightsVector to store a priori weights in.

Referenced by mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Tolerance().

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
double mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::LogLikelihood ( const arma::mat &  data,
const std::vector< distribution::GaussianDistribution > &  dists,
const arma::vec &  weights 
) const
private

Calculate the log-likelihood of a model.

Yes, this is reimplemented in the GMM code. Intuition suggests that the log-likelihood is not the best way to determine if the EM algorithm has converged.

Parameters
dataData matrix.
meansVector of means.
covariancesVector of covariance matrices.
weightsVector of a priori weights.

Referenced by mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Tolerance().

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
size_t mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::MaxIterations ( ) const
inline

Get the maximum number of iterations of the EM algorithm.

Definition at line 122 of file em_fit.hpp.

References mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::maxIterations.

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
size_t& mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::MaxIterations ( )
inline

Modify the maximum number of iterations of the EM algorithm.

Definition at line 124 of file em_fit.hpp.

References mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::maxIterations.

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
template<typename Archive >
void mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Serialize ( Archive &  ar,
const unsigned int  version 
)
template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
double mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Tolerance ( ) const
inline

Get the tolerance for the convergence of the EM algorithm.

Definition at line 127 of file em_fit.hpp.

References mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::tolerance.

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
double& mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Tolerance ( )
inline

Member Data Documentation

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
InitialClusteringType mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::clusterer
private

Object which will perform the clustering.

Definition at line 170 of file em_fit.hpp.

Referenced by mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Clusterer().

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
CovarianceConstraintPolicy mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::constraint
private

Object which applies constraints to the covariance matrix.

Definition at line 172 of file em_fit.hpp.

Referenced by mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Constraint().

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
size_t mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::maxIterations
private

Maximum iterations of EM algorithm.

Definition at line 166 of file em_fit.hpp.

Referenced by mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::MaxIterations().

template<typename InitialClusteringType = kmeans::KMeans<>, typename CovarianceConstraintPolicy = PositiveDefiniteConstraint>
double mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::tolerance
private

Tolerance for convergence of EM.

Definition at line 168 of file em_fit.hpp.

Referenced by mlpack::gmm::EMFit< InitialClusteringType, CovarianceConstraintPolicy >::Tolerance().


The documentation for this class was generated from the following file: