50 it_assert(
K_in >= 0,
"MOG_generic::init(): number of Gaussians must be greater than zero");
51 it_assert(
D_in >= 0,
"MOG_generic::init(): dimensionality must be greater than zero");
147 int cols = A(0).
size();
149 if (!rows || !cols)
return false;
151 for (
int row = 1;
row < rows;
row++)
if (A(
row).
size() != cols)
return false;
166 it_assert((
means_in.
size() ==
K),
"MOG_generic::set_means_internal(): number of vectors in 'means' is not equivalent to number of Gaussians");
168 for (
int k = 0; k <
K; k++)
169 it_assert((
means_in(k).
size() ==
D),
"MOG_generic::set_means_internal(): dimensionality mismatch between model and one or more vectors in 'means'");
171 for (
int k = 0; k <
K; k++)
172 for (
int d = 0; d <
D; d++)
173 it_assert(std::isfinite(
means_in(k)(d)),
"MOG_generic::set_means_internal(): 'means' has a non-finite value");
182 it_assert((
diag_covs_in.
size() ==
K),
"MOG_generic::set_diag_covs_internal(): number of vectors in 'diag_covs' does not match number of Gaussians");
184 for (
int k = 0; k <
K; k++)
185 it_assert((
diag_covs_in(k).
size() ==
D),
"MOG_generic::set_diag_covs_internal(): dimensionality mismatch between model and one or more vectors in 'diag_covs'");
187 for (
int k = 0; k <
K; k++)
188 for (
int d = 0; d <
D; d++) {
189 it_assert((
diag_covs_in(k)(d) > 0.0),
"MOG_generic::set_diag_covs_internal(): 'diag_covs' has a zero or negative value");
190 it_assert(std::isfinite(
diag_covs_in(k)(d)),
"MOG_generic::set_diag_covs_internal(): 'diag_covs' has a non-finite value");
202 it_assert((
full_covs_in.
size() ==
K),
"MOG_generic::set_full_covs_internal(): number of matrices in 'full_covs' does not match number of Gaussians");
204 for (
int k = 0; k <
K; k++)
205 it_assert(((
full_covs_in(k).rows() ==
D) && (
full_covs_in(k).cols() ==
D)),
"MOG_generic::set_full_covs_internal(): dimensionality mismatch between model and one or more matrices in 'full_covs'");
207 for (
int k = 0; k <
K; k++)
208 for (
int i = 0;
i <
D;
i++)
209 for (
int j = 0;
j <
D;
j++) {
210 it_assert(std::isfinite(
full_covs_in(k)(
i,
j)),
"MOG_generic::set_full_covs_internal(): 'full_covs' has a non-finite value");
213 "MOG_generic::set_full_covs_internal(): 'full_covs' has "
214 "a zero or negative value on a diagonal");
228 it_assert((
weights_in.
size() ==
K),
"MOG_generic::set_weights_internal(): number of elements in 'weights' does not match number of Gaussians");
230 for (
int k = 0; k <
K; k++) {
231 it_assert((
weights_in(k) >= 0),
"MOG_generic::set_weights_internal(): 'weights' has a negative value");
232 it_assert(std::isfinite(
weights_in(k)),
"MOG_generic::set_weights_internal(): 'weights' has a non-finite value");
258 for (
int k = 0; k <
K; k++) {
261 for (
int d = 0;d <
D;d++)
full_covs(k)(d, d) = 1.0;
296 for (
int k = 0;k <
K;k++) {
301 for (
int d = 0;d <
D;d++) {
323 log_max_K = std::log(std::numeric_limits<double>::max() /
K);
414 bool contents =
ff.exists(
"means") && (
ff.exists(
"diag_covs") ||
ff.exists(
"full_covs")) &&
ff.exists(
"weights");
422 if (
ff.exists(
"full_covs")) {
457 if (!
B_in.is_valid())
return;
459 it_assert((
full ==
B_in.is_full()),
"MOG_generic::join(): given model must be of the same type");
460 it_assert((
B_in.get_D() ==
D),
"MOG_generic::join(): given model has different dimensionality");
461 it_assert((
B_in.get_K() > 0),
"MOG_generic::join(): given model has no components");
532 for (
int d = 0;d <
D;d++) tmpvecD[d] =
x_in[d] -
mean[d];
541 for (
int d = 0; d <
D; d++) {
553 it_assert(
valid,
"MOG_generic::log_lhood_single_gaus(): model not valid");
555 it_assert(((k >= 0) && (k <
K)),
"MOG_generic::log_lhood_single_gaus(): k specifies a non-existant Gaussian");
566 for (
int k = 0;k <
K;k++) {
580 for (
int k = 0;k <
K;k++)
sum += std::exp(tmpvecK[k]);
581 return(std::log(
sum));
601 for (
int k = 0;k <
K;k++) {
615 for (
int k = 0;k <
K;k++)
sum += std::exp(tmpvecK[k]);
635 it_assert(
valid,
"MOG_generic::avg_log_lhood(): model not valid");
636 it_assert(
check_size(
X_in),
"MOG_generic::avg_log_lhood(): X is empty or at least one vector has the wrong dimensionality");
int size() const
Returns the number of data elements in the array object.
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Generic Mixture of Gaussians (MOG) class. Used as a base for other MOG classes.
virtual double lhood(const vec &x_in)
calculate the likelihood of vector x_in
void set_diag_covs_unity()
Set the diagonal covariance vectors to be unity.
void convert_to_diag_internal()
ADD DOCUMENTATION HERE.
void set_weights_uniform()
Set all the weights to 1/K, where K is the number of Gaussians.
Array< mat > full_covs_inv
Pre-calcuated inverted version of each full covariance matrix.
virtual void setup_weights()
additional processing of the weight vector, done as the last step of weight initialisation
void set_diag_covs_internal(Array< vec > &diag_covs_in)
ADD DOCUMENTATION HERE.
bool check_size(const vec &x_in) const
Check if vector x_in has the same dimensionality as the model.
Array< mat > full_covs
full covariance matrices
void set_weights_uniform_internal()
ADD DOCUMENTATION HERE.
virtual void cleanup()
Release memory used by the model. The model will be empty.
void set_full_covs_unity()
Set the full covariance matrices to be unity.
bool check_array_uniformity(const Array< vec > &A) const
Check if all vectors in Array X_in have the same dimensionality.
void init()
Initialise the model to be empty.
virtual void convert_to_diag()
Convert the model to use diagonal covariances.
void set_weights_internal(vec &_weigths)
ADD DOCUMENTATION HERE.
virtual double avg_log_lhood(const Array< vec > &X_in)
calculate the average log likelihood of an array of vectors X_in
void set_full_covs_internal(Array< mat > &full_covs_in)
ADD DOCUMENTATION HERE.
bool do_checks
indicates whether checks on input data are done
virtual void setup_covs()
additional processing of covariance vectors/matrices, done as the last step of covariance initialisat...
void convert_to_full_internal()
ADD DOCUMENTATION HERE.
virtual void setup_means()
additional processing of mean vectors, done as the last step of mean initialisation
void set_means_zero()
Set the means in the model to be zero.
virtual void setup_misc()
additional processing of miscellaneous parameters, done as the last step of overall initialisation
bool valid
indicates whether the parameters are valid
virtual double log_lhood(const vec &x_in)
calculate the log likelihood of vector x_in
virtual void convert_to_full()
Convert the model to have full covariance matrices.
void set_full_covs(Array< mat > &full_covs_in)
Set the full covariance matrices of the model.
void set_means(Array< vec > &means_in)
Set the means of the model.
virtual double log_lhood_single_gaus(const vec &x_in, const int k)
calculate the log likelihood of vector x_in using only Gaussian k
void set_means_internal(Array< vec > &means_in)
ADD DOCUMENTATION HERE.
virtual double log_lhood_internal(const vec &x_in)
ADD DOCUMENTATION HERE.
virtual void save(const std::string &name_in) const
Save the model's parameters to a model file.
Array< vec > diag_covs
diagonal covariance matrices, stored as vectors
void set_full_covs_unity_internal()
ADD DOCUMENTATION HERE.
void set_diag_covs(Array< vec > &diag_covs_in)
Set the diagonal covariance vectors of the model.
virtual void join(const MOG_generic &B_in)
Mathematically join the model with a user supplied model.
virtual double lhood_internal(const vec &x_in)
ADD DOCUMENTATION HERE.
virtual double log_lhood_single_gaus_internal(const vec &x_in, const int k)
ADD DOCUMENTATION HERE.
Array< vec > diag_covs_inv_etc
Pre-calcuated inverted version of each diagonal covariance vector, where the covariance elements are ...
vec log_weights
Pre-calculated log versions of the weights.
vec log_det_etc
Gaussian specific pre-calcualted constants.
void set_diag_covs_unity_internal()
ADD DOCUMENTATION HERE.
void set_means_zero_internal()
ADD DOCUMENTATION HERE.
bool full
indicates whether we are using full or diagonal covariance matrices
double log_max_K
Pre-calcualted std::log(std::numeric_limits<double>::max() / K), where K is the number of Gaussians.
virtual void load(const std::string &name_in)
Initialise the model by loading the parameters from a model file.
void set_weights(vec &weights_in)
Set the weight vector of the model.
bool paranoid
indicates whether we are paranoid about numerical stability
Automatic naming when saving.
The IT++ file format reading and writing class.
Definitions of determinant calculations.
double det(const mat &X)
Determinant of real square matrix.
Mat< T > diag(const Vec< T > &v, const int K=0)
Create a diagonal matrix using vector v as its diagonal.
#define it_assert(t, s)
Abort if t is not true.
bool inv(const mat &X, mat &Y)
Inverse of real square matrix.
bool exist(const std::string &name)
Checks if a file named name already exists on the disk.
double log_add(double log_a, double log_b)
Safe substitute for log(exp(log_a) + exp(log_b))
vec log(const vec &x)
The natural logarithm of the elements.
double trunc_exp(double x)
Truncated exponential function.
int size(const Vec< T > &v)
Length of vector.
T sum(const Vec< T > &v)
Sum of all elements in the vector.
double mean(const vec &v)
The mean value.
Definitions of matrix inversion routines.
IT++ compatibility types and functions.
Definition of classes for the IT++ file format.
Logarithmic and exponenential functions - header file.
Various functions on vectors and matrices - header file.
Miscellaneous functions - header file.
Generic Mixture of Gaussians (MOG) class - header file.
const double m_2pi
Constant 2*Pi.
const Array< T > concat(const Array< T > &a, const T &e)
Append element e to the end of the Array a.