Gaussian Expectation-Maximisation algorithm. More...
#include <EM.hpp>
Public Member Functions | |
EM (unsigned int number_components) | |
Constructs an EM ready to fit. More... | |
void | set_seed (unsigned int seed) |
Sets PRNG seed. More... | |
void | set_absolute_tolerance (double absolute_tolerance) |
Sets absolute tolerance for convergence test. More... | |
void | set_relative_tolerance (double relative_tolerance) |
Sets relative tolerance for convergence test. More... | |
void | set_maximum_steps (unsigned int maximum_steps) |
Sets maximum number of E-M steps. More... | |
void | set_means_initialiser (std::shared_ptr< const Clustering::CentroidsInitialiser > means_initialiser) |
Sets means initialiser. More... | |
void | set_responsibilities_initialiser (std::shared_ptr< const Clustering::ResponsibilitiesInitialiser > responsibilities_initialiser) |
Sets responsibilities initialiser. More... | |
void | set_verbose (bool verbose) |
Switches between verbose and quiet mode. More... | |
void | set_maximise_first (bool maximise_first) |
Switches between starting with E or M step first. More... | |
bool | fit (Eigen::Ref< const Eigen::MatrixXd > data) override |
Fits the model. More... | |
auto | number_components () const |
Returns the number of components. | |
unsigned int | number_clusters () const override |
Returns the number of clusters. More... | |
const auto & | means () const |
Returns a const reference to matrix containing fitted component means. More... | |
const Eigen::MatrixXd & | centroids () const override |
Returns a const reference to the matrix of cluster centroids (in columns). More... | |
const auto & | covariances () const |
Returns a const reference to fitted component covariance matrices. More... | |
const Eigen::MatrixXd & | covariance (unsigned int k) const |
Returns a const reference to fitted k-th component's covariance matrix. More... | |
const auto & | mixing_probabilities () const |
Returns a const reference to fitted component mixing probabilities. More... | |
const auto & | responsibilities () const |
Returns a const reference to resulting component responsibilities. More... | |
double | log_likelihood () const |
Returns a const reference to maximised log-likelihood of training data. | |
std::shared_ptr< const Clustering::CentroidsInitialiser > | means_initialiser () const |
Returns a shared pointer to means initialiser implementation. | |
void | assign_responsibilities (Eigen::Ref< const Eigen::VectorXd > x, Eigen::Ref< Eigen::VectorXd > u) const |
Given a data point x, calculate each component's responsibilities for x and save them in u. More... | |
const std::vector< unsigned int > & | labels () const override |
Returns a const reference to resulting cluster labels for each datapoint. Value make sense only if fitting converged successfully. | |
bool | converged () const override |
Reports if the model converged. | |
![]() | |
virtual | ~Model () |
Virtual destructor. | |
Gaussian Expectation-Maximisation algorithm.
Iterates until log-likelihood converges.
ml::EM::EM | ( | unsigned int | number_components | ) |
Constructs an EM ready to fit.
[in] | number_components | Number of Gaussian components. |
If | number_components == 0 . |
void ml::EM::set_seed | ( | unsigned int | seed | ) |
Sets PRNG seed.
[in] | seed | PRNG seed. |
void ml::EM::set_absolute_tolerance | ( | double | absolute_tolerance | ) |
Sets absolute tolerance for convergence test.
[in] | absolute_tolerance | Absolute tolerance. |
std::domain_error | If absolute_tolerance < 0 . |
void ml::EM::set_relative_tolerance | ( | double | relative_tolerance | ) |
Sets relative tolerance for convergence test.
[in] | relative_tolerance | Relative tolerance. |
std::domain_error | If relative_tolerance < 0 . |
void ml::EM::set_maximum_steps | ( | unsigned int | maximum_steps | ) |
Sets maximum number of E-M steps.
[in] | maximum_steps | Maximum number of E-M steps. |
std::invalid_argument | If maximum_steps < 2 . |
void ml::EM::set_means_initialiser | ( | std::shared_ptr< const Clustering::CentroidsInitialiser > | means_initialiser | ) |
Sets means initialiser.
[in] | means_initialiser | Pointer to Clustering::CentroidsInitialiser implementation. |
std::invalid_argument | If means_initialiser is null. |
void ml::EM::set_responsibilities_initialiser | ( | std::shared_ptr< const Clustering::ResponsibilitiesInitialiser > | responsibilities_initialiser | ) |
Sets responsibilities initialiser.
[in] | responsibilities_initialiser | Pointer to ResponsibilitiesInitialiser implementation. |
std::invalid_argument | If responsibilities_initialiser is null. |
|
inline |
Switches between verbose and quiet mode.
[in] | verbose | true if we want verbose output. |
|
inline |
Switches between starting with E or M step first.
[in] | maximise_first | true if we want to start with the E step. |
|
overridevirtual |
Fits the model.
[in] | data | Matrix (column-major order) with a data point in every column. |
true
if fitting converged successfully. Implements ml::Clustering::Model.
|
inlineoverridevirtual |
Returns the number of clusters.
Value make sense only if fitting converged successfully.
Implements ml::Clustering::Model.
|
inline |
Returns a const reference to matrix containing fitted component means.
number_dimensions
x number_components() matrix.
|
inlineoverridevirtual |
Returns a const reference to the matrix of cluster centroids (in columns).
A centroid represent the central location of the cluster. It is e.g. a mean of all points in the cluster. Value make sense only if fitting converged successfully.
Implements ml::Clustering::Model.
|
inline |
Returns a const reference to fitted component covariance matrices.
means().rows()
x means().rows()
. const Eigen::MatrixXd& ml::EM::covariance | ( | unsigned int | k | ) | const |
|
inline |
Returns a const reference to fitted component mixing probabilities.
|
inline |
Returns a const reference to resulting component responsibilities.
sample_size
x number_components() matrix. void ml::EM::assign_responsibilities | ( | Eigen::Ref< const Eigen::VectorXd > | x, |
Eigen::Ref< Eigen::VectorXd > | u | ||
) | const |
Given a data point x, calculate each component's responsibilities for x and save them in u.
[in] | x | Data point with correct dimension. |
[out] | u | Vector for responsibilities with length equal to number_components(). |
std::invalid_argument | If x.size() != means().rows() or u.size() != number_components() . |