mog_diag.cpp
Go to the documentation of this file.
#include <itpp/base/math/log_exp.h>
#include <itpp/stat/mog_diag.h>
#include <cstdlib>
namespace itpp
{
double MOG_diag::log_lhood_single_gaus_internal(const double * c_x_in, const int k) const
{
const double * c_mean = c_means[k];
const double * c_diag_cov_inv_etc = c_diag_covs_inv_etc[k];
double acc = 0.0;
for (int d = 0; d < D; d++) {
double tmp_val = c_x_in[d] - c_mean[d];
acc += (tmp_val * tmp_val) * c_diag_cov_inv_etc[d];
}
return(c_log_det_etc[k] - acc);
}
double MOG_diag::log_lhood_single_gaus_internal(const vec &x_in, const int k) const
{
return log_lhood_single_gaus_internal(x_in._data(), k);
}
double MOG_diag::log_lhood_single_gaus(const double * c_x_in, const int k) const
{
if (do_checks) {
it_assert(valid, "MOG_diag::log_lhood_single_gaus(): model not valid");
it_assert(((k >= 0) && (k < K)), "MOG::log_lhood_single_gaus(): k specifies a non-existant Gaussian");
}
return log_lhood_single_gaus_internal(c_x_in, k);
}
double MOG_diag::log_lhood_single_gaus(const vec &x_in, const int k) const
{
if (do_checks) {
it_assert(valid, "MOG_diag::log_lhood_single_gaus(): model not valid");
it_assert(check_size(x_in), "MOG_diag::log_lhood_single_gaus(): x has wrong dimensionality");
it_assert(((k >= 0) && (k < K)), "MOG::log_lhood_single_gaus(): k specifies a non-existant Gaussian");
}
return log_lhood_single_gaus_internal(x_in._data(), k);
}
double MOG_diag::log_lhood_internal(const double * c_x_in)
{
bool danger = paranoid;
for (int k = 0;k < K;k++) {
double tmp = c_log_weights[k] + log_lhood_single_gaus_internal(c_x_in, k);
c_tmpvecK[k] = tmp;
if (tmp >= log_max_K) danger = true;
}
if (danger) {
double log_sum = c_tmpvecK[0];
for (int k = 1; k < K; k++) log_sum = log_add(log_sum, c_tmpvecK[k]);
return(log_sum);
}
else {
double sum = 0.0;
for (int k = 0;k < K;k++) sum += std::exp(c_tmpvecK[k]);
return(std::log(sum));
}
}
double MOG_diag::log_lhood_internal(const vec &x_in)
{
return log_lhood_internal(x_in._data());
}
double MOG_diag::log_lhood(const vec &x_in)
{
if (do_checks) {
it_assert(valid, "MOG_diag::log_lhood(): model not valid");
it_assert(check_size(x_in), "MOG_diag::log_lhood(): x has wrong dimensionality");
}
return log_lhood_internal(x_in._data());
}
double MOG_diag::log_lhood(const double * c_x_in)
{
if (do_checks) {
it_assert(valid, "MOG_diag::log_lhood(): model not valid");
it_assert((c_x_in != 0), "MOG_diag::log_lhood(): c_x_in is a null pointer");
}
return log_lhood_internal(c_x_in);
}
double MOG_diag::lhood_internal(const double * c_x_in)
{
bool danger = paranoid;
for (int k = 0;k < K;k++) {
double tmp = c_log_weights[k] + log_lhood_single_gaus_internal(c_x_in, k);
c_tmpvecK[k] = tmp;
if (tmp >= log_max_K) danger = true;
}
if (danger) {
double log_sum = c_tmpvecK[0];
for (int k = 1; k < K; k++) log_sum = log_add(log_sum, c_tmpvecK[k]);
return(trunc_exp(log_sum));
}
else {
double sum = 0.0;
for (int k = 0;k < K;k++) sum += std::exp(c_tmpvecK[k]);
return(sum);
}
}
double MOG_diag::lhood_internal(const vec &x_in) { return lhood_internal(x_in._data()); }
double MOG_diag::lhood(const vec &x_in)
{
if (do_checks) {
it_assert(valid, "MOG_diag::lhood(): model not valid");
it_assert(check_size(x_in), "MOG_diag::lhood(): x has wrong dimensionality");
}
return lhood_internal(x_in._data());
}
double MOG_diag::lhood(const double * c_x_in)
{
if (do_checks) {
it_assert(valid, "MOG_diag::lhood(): model not valid");
it_assert((c_x_in != 0), "MOG_diag::lhood(): c_x_in is a null pointer");
}
return lhood_internal(c_x_in);
}
double MOG_diag::avg_log_lhood(const double ** c_x_in, const int N)
{
if (do_checks) {
it_assert(valid, "MOG_diag::avg_log_lhood(): model not valid");
it_assert((c_x_in != 0), "MOG_diag::avg_log_lhood(): c_x_in is a null pointer");
it_assert((N >= 0), "MOG_diag::avg_log_lhood(): N is zero or negative");
}
double acc = 0.0;
for (int n = 0;n < N;n++) acc += log_lhood_internal(c_x_in[n]);
return(acc / N);
}
double MOG_diag::avg_log_lhood(const Array<vec> &X_in)
{
if (do_checks) {
it_assert(valid, "MOG_diag::avg_log_lhood(): model not valid");
it_assert(check_size(X_in), "MOG_diag::avg_log_lhood(): X is empty or at least one vector has the wrong dimensionality");
}
const int N = X_in.size();
double acc = 0.0;
for (int n = 0;n < N;n++) acc += log_lhood_internal(X_in(n)._data());
return(acc / N);
}
void MOG_diag::zero_all_ptrs()
{
c_means = 0;
c_diag_covs = 0;
c_diag_covs_inv_etc = 0;
c_weights = 0;
c_log_weights = 0;
c_log_det_etc = 0;
c_tmpvecK = 0;
}
void MOG_diag::free_all_ptrs()
{
c_means = disable_c_access(c_means);
c_diag_covs = disable_c_access(c_diag_covs);
c_diag_covs_inv_etc = disable_c_access(c_diag_covs_inv_etc);
c_weights = disable_c_access(c_weights);
c_log_weights = disable_c_access(c_log_weights);
c_log_det_etc = disable_c_access(c_log_det_etc);
c_tmpvecK = disable_c_access(c_tmpvecK);
}
void MOG_diag::setup_means()
{
MOG_generic::setup_means();
disable_c_access(c_means);
c_means = enable_c_access(means);
}
void MOG_diag::setup_covs()
{
MOG_generic::setup_covs();
if (full) return;
disable_c_access(c_diag_covs);
disable_c_access(c_diag_covs_inv_etc);
disable_c_access(c_log_det_etc);
c_diag_covs = enable_c_access(diag_covs);
c_diag_covs_inv_etc = enable_c_access(diag_covs_inv_etc);
c_log_det_etc = enable_c_access(log_det_etc);
}
void MOG_diag::setup_weights()
{
MOG_generic::setup_weights();
disable_c_access(c_weights);
disable_c_access(c_log_weights);
c_weights = enable_c_access(weights);
c_log_weights = enable_c_access(log_weights);
}
void MOG_diag::setup_misc()
{
disable_c_access(c_tmpvecK);
tmpvecK.set_size(K);
c_tmpvecK = enable_c_access(tmpvecK);
MOG_generic::setup_misc();
if (full) convert_to_diag_internal();
}
void MOG_diag::load(const std::string &name_in)
{
MOG_generic::load(name_in);
if (full) convert_to_diag();
}
double ** MOG_diag::enable_c_access(Array<vec> & A_in)
{
int rows = A_in.size();
double ** A = (double **)std::malloc(rows * sizeof(double *));
if (A) for (int row = 0;row < rows;row++) A[row] = A_in(row)._data();
return(A);
}
int ** MOG_diag::enable_c_access(Array<ivec> & A_in)
{
int rows = A_in.size();
int ** A = (int **)std::malloc(rows * sizeof(int *));
if (A) for (int row = 0;row < rows;row++) A[row] = A_in(row)._data();
return(A);
}
double ** MOG_diag::disable_c_access(double ** A_in) { if (A_in) std::free(A_in); return(0); }
int ** MOG_diag::disable_c_access(int ** A_in) { if (A_in) std::free(A_in); return(0); }
double * MOG_diag::enable_c_access(vec & v_in) { return v_in._data(); }
int * MOG_diag::enable_c_access(ivec & v_in) { return v_in._data(); }
double * MOG_diag::disable_c_access(double *) { return(0); }
int * MOG_diag::disable_c_access(int *) { return(0); }
}
mog_diag.h
Diagonal Mixture of Gaussians class - header file.
itpp::log_add
double log_add(double log_a, double log_b)
Safe substitute for log(exp(log_a) + exp(log_b))
Definition: log_exp.cpp:40
itpp::MOG_generic::check_size
bool check_size(const vec &x_in) const
Check if vector x_in has the same dimensionality as the model.
Definition: mog_generic.cpp:130
itpp::MOG_generic::convert_to_diag_internal
void convert_to_diag_internal()
ADD DOCUMENTATION HERE.
Definition: mog_generic.cpp:486
itpp::MOG_generic::D
int D
dimensionality
Definition: mog_generic.h:295
itpp::MOG_generic::setup_covs
virtual void setup_covs()
additional processing of covariance vectors/matrices, done as the last step of covariance initialisat...
Definition: mog_generic.cpp:279
itpp::Array< vec >
itpp::MOG_generic::means
Array< vec > means
means
Definition: mog_generic.h:298
itpp::MOG_diag::load
void load(const std::string &name_in)
Initialise the model by loading the parameters from a model file.
Definition: mog_diag.cpp:277
itpp::MOG_diag::c_log_weights
double * c_log_weights
pointer to the log version of the weight vector
Definition: mog_diag.h:209
itpp::MOG_diag::c_log_det_etc
double * c_log_det_etc
pointer to the log_det_etc vector
Definition: mog_diag.h:212
itpp::MOG_diag::setup_misc
void setup_misc()
additional processing of miscellaneous parameters, done as the last step of overall initialisation ...
Definition: mog_diag.cpp:266
itpp::MOG_diag::c_diag_covs
double ** c_diag_covs
pointers to the covariance vectors
Definition: mog_diag.h:200
itpp::sum
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition: matfunc.h:59
itpp::Array::size
int size() const
Returns the number of data elements in the array object.
Definition: array.h:155
itpp::MOG_diag::log_lhood_internal
double log_lhood_internal(const double *c_x_in)
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:80
itpp::MOG_diag::log_lhood_single_gaus_internal
double log_lhood_single_gaus_internal(const double *c_x_in, const int k) const
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:37
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
log_exp.h
Logarithmic and exponenential functions - header file.
itpp::MOG_generic::setup_misc
virtual void setup_misc()
additional processing of miscellaneous parameters, done as the last step of overall initialisation ...
Definition: mog_generic.cpp:321
itpp::MOG_diag::c_means
double ** c_means
pointers to the mean vectors
Definition: mog_diag.h:197
itpp::log
vec log(const vec &x)
The natural logarithm of the elements.
Definition: log_exp.h:241
itpp::MOG_generic::setup_weights
virtual void setup_weights()
additional processing of the weight vector, done as the last step of weight initialisation ...
Definition: mog_generic.cpp:314
itpp::MOG_diag::lhood_internal
double lhood_internal(const double *c_x_in)
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:133
itpp::MOG_diag::lhood
double lhood(const double *c_x_in)
calculate the likelihood of C vector c_x_in
Definition: mog_diag.cpp:170
itpp::MOG_generic::log_weights
vec log_weights
Pre-calculated log versions of the weights.
Definition: mog_generic.h:320
itpp::exp
vec exp(const vec &x)
Exp of the elements of a vector x.
Definition: log_exp.h:155
itpp::MOG_generic::full
bool full
indicates whether we are using full or diagonal covariance matrices
Definition: mog_generic.h:286
itpp::MOG_generic::diag_covs_inv_etc
Array< vec > diag_covs_inv_etc
Pre-calcuated inverted version of each diagonal covariance vector, where the covariance elements are ...
Definition: mog_generic.h:326
itpp::MOG_diag::free_all_ptrs
void free_all_ptrs()
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:219
itpp::MOG_generic::do_checks
bool do_checks
indicates whether checks on input data are done
Definition: mog_generic.h:280
itpp::MOG_diag::disable_c_access
double ** disable_c_access(double **A_in)
Disable C style access to an Array of vectors (vec)
Definition: mog_diag.cpp:300
itpp::MOG_generic::convert_to_diag
virtual void convert_to_diag()
Convert the model to use diagonal covariances.
Definition: mog_generic.cpp:499
itpp
itpp namespace
Definition: itmex.h:36
itpp::MOG_diag::c_diag_covs_inv_etc
double ** c_diag_covs_inv_etc
pointers to the inverted covariance vectors
Definition: mog_diag.h:203
itpp::MOG_generic::setup_means
virtual void setup_means()
additional processing of mean vectors, done as the last step of mean initialisation ...
Definition: mog_generic.cpp:277
itpp::MOG_diag::enable_c_access
double ** enable_c_access(Array< vec > &A_in)
Enable C style access to an Array of vectors (vec)
Definition: mog_diag.cpp:284
itpp::MOG_diag::setup_covs
void setup_covs()
additional processing of covariance vectors/matrices, done as the last step of covariance initialisat...
Definition: mog_diag.cpp:239
itpp::MOG_diag::setup_means
void setup_means()
additional processing of mean vectors, done as the last step of mean initialisation ...
Definition: mog_diag.cpp:231
itpp::MOG_generic::load
virtual void load(const std::string &name_in)
Initialise the model by loading the parameters from a model file.
Definition: mog_generic.cpp:407
itpp::MOG_generic::log_det_etc
vec log_det_etc
Gaussian specific pre-calcualted constants.
Definition: mog_generic.h:317
itpp::MOG_diag::avg_log_lhood
double avg_log_lhood(const double **c_x_in, int N)
calculate the average log likelihood of an array of C vectors ( c_x_in )
Definition: mog_diag.cpp:181
itpp::trunc_exp
double trunc_exp(double x)
Truncated exponential function.
Definition: log_exp.h:137
itpp::MOG_generic::paranoid
bool paranoid
indicates whether we are paranoid about numerical stability
Definition: mog_generic.h:289
itpp::MOG_diag::setup_weights
void setup_weights()
additional processing of the weight vector, done as the last step of weight initialisation ...
Definition: mog_diag.cpp:254
itpp::MOG_generic::K
int K
number of gaussians
Definition: mog_generic.h:292
itpp::MOG_generic::valid
bool valid
indicates whether the parameters are valid
Definition: mog_generic.h:283
itpp::MOG_generic::diag_covs
Array< vec > diag_covs
diagonal covariance matrices, stored as vectors
Definition: mog_generic.h:301
itpp::MOG_diag::log_lhood_single_gaus
double log_lhood_single_gaus(const double *c_x_in, const int k) const
calculate the log likelihood of C vector c_x_in using only Gaussian k
Definition: mog_diag.cpp:59
itpp::MOG_generic::log_max_K
double log_max_K
Pre-calcualted std::log(std::numeric_limits<double>::max() / K), where K is the number of Gaussians...
Definition: mog_generic.h:310
itpp::MOG_diag::c_weights
double * c_weights
pointer to the weight vector
Definition: mog_diag.h:206
itpp::MOG_generic::weights
vec weights
weights
Definition: mog_generic.h:307
itpp::MOG_diag::log_lhood
double log_lhood(const double *c_x_in)
calculate the log likelihood of C vector c_x_in
Definition: mog_diag.cpp:122
itpp::MOG_diag::zero_all_ptrs
void zero_all_ptrs()
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:207

Generated on Thu Aug 20 2015 21:02:51 for IT++ by Doxygen 1.8.9.1