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]);
580 for (
int k = 0;k <
K;k++) sum +=
std::exp(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]);
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");
639 const int N = X_in.
size();