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");
74 D = means_in(0).
size();
94 D = means_in(0).
size();
115 D = means_in(0).
size();
132 if (x_in.size() ==
D)
return true;
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++) {
302 double tmp = diag_cov(d);
303 diag_cov_inv_etc(d) = 1.0 / (2.0 * tmp);
411 it_assert(
exist(name_in),
"MOG_generic::load(): couldn't access file '" + name_in +
"'");
415 it_assert(contents,
"MOG_generic::load(): file '" + name_in +
"' doesn't appear to be a model file");
418 ff >>
Name(
"means") >> means_in;
420 ff >>
Name(
"weights") >> weights_in;
422 if (ff.
exists(
"full_covs")) {
424 ff >>
Name(
"full_covs") >> full_covs_in;
425 init(means_in, full_covs_in, weights_in);
429 ff >>
Name(
"diag_covs") >> diag_covs_in;
430 init(means_in, diag_covs_in, weights_in);
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");
463 int new_K =
K + B_in.
get_K();
464 vec new_weights(new_K);
467 double alpha = double(
K) / double(new_K);
468 double beta = double(B_in.
get_K()) /
double(new_K);
470 for (
int k = 0;k <
K;k++) new_weights(k) = alpha *
weights(k);
471 for (
int k =
K;k < new_K;k++) new_weights(k) = beta * B_in_weights(k);
477 init(new_means, new_full_covs, new_weights);
481 init(new_means, new_diag_covs, new_weights);
532 for (
int d = 0;d <
D;d++) tmpvecD[d] = x_in[d] -
mean[d];
541 for (
int d = 0; d <
D; d++) {
542 double tmpval = x_in[d] -
mean[d];
543 acc += (tmpval * tmpval) * diag_cov_inv_etc[d];
553 it_assert(
valid,
"MOG_generic::log_lhood_single_gaus(): model not valid");
554 it_assert(
check_size(x_in),
"MOG_generic::log_lhood_single_gaus(): x has wrong dimensionality");
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++) {
574 double log_sum = tmpvecK[0];
575 for (
int k = 1; k <
K; k++) log_sum =
log_add(log_sum, tmpvecK[k]);
601 for (
int k = 0;k <
K;k++) {
609 double log_sum = tmpvecK[0];
610 for (
int k = 1; k <
K; k++) log_sum =
log_add(log_sum, 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");
639 const int N = X_in.
size();
void set_means(Array< vec > &means_in)
Set the means of the model.
bool is_full() const
Returns true if the model has full covariance matrices.
virtual double log_lhood_internal(const vec &x_in)
ADD DOCUMENTATION HERE.
virtual double lhood(const vec &x_in)
calculate the likelihood of vector x_in
Various functions on vectors and matrices - header file.
void set_diag_covs_internal(Array< vec > &diag_covs_in)
ADD DOCUMENTATION HERE.
const double m_2pi
Constant 2*Pi.
virtual double log_lhood_single_gaus_internal(const vec &x_in, const int k)
ADD DOCUMENTATION HERE.
double log_add(double log_a, double log_b)
Safe substitute for log(exp(log_a) + exp(log_b))
Array< mat > get_full_covs() const
Obtain a copy of the array of full covariance matrices.
void set_weights_uniform()
Set all the weights to 1/K, where K is the number of Gaussians.
void convert_to_diag_internal()
ADD DOCUMENTATION HERE.
int size() const
Returns the number of data elements in the array object.
Miscellaneous functions - header file.
virtual void setup_covs()
additional processing of covariance vectors/matrices, done as the last step of covariance initialisat...
Array< vec > get_means() const
Obtain a copy of the array of mean vectors.
void set_means_zero()
Set the means in the model to be zero.
Array< vec > get_diag_covs() const
Obtain a copy of the array of diagonal covariance vectors.
Definition of classes for the IT++ file format.
Generic Mixture of Gaussians (MOG) class - header file.
virtual void join(const MOG_generic &B_in)
Mathematically join the model with a user supplied model.
Automatic naming when savingAn easy way to give a variable a name and optionally description when sav...
void set_weights_internal(vec &_weigths)
ADD DOCUMENTATION HERE.
T sum(const Vec< T > &v)
Sum of all elements in the vector.
virtual void save(const std::string &name_in) const
Save the model's parameters to a model file.
Definitions of matrix inversion routines.
bool is_valid() const
Returns true if the model's parameters are valid.
#define it_assert(t, s)
Abort if t is not true.
Logarithmic and exponenential functions - header file.
Array< mat > full_covs
full covariance matrices
void set_diag_covs_unity()
Set the diagonal covariance vectors to be unity.
double det(const mat &X)
Determinant of real square matrix.Calculate determinant of the real matrix .
void set_size(int n, bool copy=false)
Resizing an Array<T>.
double mean(const vec &v)
The mean value.
virtual void setup_misc()
additional processing of miscellaneous parameters, done as the last step of overall initialisation ...
void close()
Close the file.
bool check_size(const vec &x_in) const
Check if vector x_in has the same dimensionality as the model.
vec log(const vec &x)
The natural logarithm of the elements.
bool exists(const std::string &name)
Returns true if the variable name exists in the file.
void set_diag_covs_unity_internal()
ADD DOCUMENTATION HERE.
void set_full_covs_unity()
Set the full covariance matrices to be unity.
virtual void setup_weights()
additional processing of the weight vector, done as the last step of weight initialisation ...
vec log_weights
Pre-calculated log versions of the weights.
vec exp(const vec &x)
Exp of the elements of a vector x.
bool full
indicates whether we are using full or diagonal covariance matrices
Generic Mixture of Gaussians (MOG) class. Used as a base for other MOG classes.
T max(const Vec< T > &v)
Maximum value of vector.
void set_full_covs(Array< mat > &full_covs_in)
Set the full covariance matrices of the model.
void set_weights(vec &weights_in)
Set the weight vector of the model.
virtual double lhood_internal(const vec &x_in)
ADD DOCUMENTATION HERE.
Array< vec > diag_covs_inv_etc
Pre-calcuated inverted version of each diagonal covariance vector, where the covariance elements are ...
bool do_checks
indicates whether checks on input data are done
The IT++ file format reading and writing class.
int get_D() const
Return the dimensionality.
virtual void convert_to_diag()
Convert the model to use diagonal covariances.
virtual double avg_log_lhood(const Array< vec > &X_in)
calculate the average log likelihood of an array of vectors X_in
Definitions of determinant calculations.
int size(const Vec< T > &v)
Length of vector.
void convert_to_full_internal()
ADD DOCUMENTATION HERE.
void set_full_covs_unity_internal()
ADD DOCUMENTATION HERE.
Array< mat > full_covs_inv
Pre-calcuated inverted version of each full covariance matrix.
virtual void setup_means()
additional processing of mean vectors, done as the last step of mean initialisation ...
virtual void convert_to_full()
Convert the model to have full covariance matrices.
bool inv(const mat &X, mat &Y)
Inverse of real square matrix.Calculate the inverse of the real matrix .
void set_means_zero_internal()
ADD DOCUMENTATION HERE.
virtual void load(const std::string &name_in)
Initialise the model by loading the parameters from a model file.
IT++ compatibility types and functions.
void init()
Initialise the model to be empty.
vec log_det_etc
Gaussian specific pre-calcualted constants.
double trunc_exp(double x)
Truncated exponential function.
bool paranoid
indicates whether we are paranoid about numerical stability
vec get_weights() const
Obtain a copy of the weight vector.
void set_full_covs_internal(Array< mat > &full_covs_in)
ADD DOCUMENTATION HERE.
bool valid
indicates whether the parameters are valid
Mat< T > diag(const Vec< T > &v, const int K=0)
Create a diagonal matrix using vector v as its diagonal.
Array< vec > diag_covs
diagonal covariance matrices, stored as vectors
double log_max_K
Pre-calcualted std::log(std::numeric_limits<double>::max() / K), where K is the number of Gaussians...
void set_means_internal(Array< vec > &means_in)
ADD DOCUMENTATION HERE.
int get_K() const
Return the number of Gaussians.
bool check_array_uniformity(const Array< vec > &A) const
Check if all vectors in Array X_in have the same dimensionality.
void set_diag_covs(Array< vec > &diag_covs_in)
Set the diagonal covariance vectors of the model.
virtual void cleanup()
Release memory used by the model. The model will be empty.
bool exist(const std::string &name)
Checks if a file named name already exists on the disk.
void set_weights_uniform_internal()
ADD DOCUMENTATION HERE.
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
virtual double log_lhood(const vec &x_in)
calculate the log likelihood of vector x_in
const Array< T > concat(const Array< T > &a, const T &e)
Append element e to the end of the Array a.