IT++ Logo
mog_diag.cpp
Go to the documentation of this file.
1
30#include <itpp/stat/mog_diag.h>
31#include <cstdlib>
32
33
34namespace itpp
35{
36
37double MOG_diag::log_lhood_single_gaus_internal(const double * c_x_in, const int k) const
38{
39
40 const double * c_mean = c_means[k];
41 const double * c_diag_cov_inv_etc = c_diag_covs_inv_etc[k];
42
43 double acc = 0.0;
44
45 for (int d = 0; d < D; d++) {
46 double tmp_val = c_x_in[d] - c_mean[d];
47 acc += (tmp_val * tmp_val) * c_diag_cov_inv_etc[d];
48 }
49 return(c_log_det_etc[k] - acc);
50}
51
52
53double MOG_diag::log_lhood_single_gaus_internal(const vec &x_in, const int k) const
54{
55 return log_lhood_single_gaus_internal(x_in._data(), k);
56}
57
58
59double MOG_diag::log_lhood_single_gaus(const double * c_x_in, const int k) const
60{
61 if (do_checks) {
62 it_assert(valid, "MOG_diag::log_lhood_single_gaus(): model not valid");
63 it_assert(((k >= 0) && (k < K)), "MOG::log_lhood_single_gaus(): k specifies a non-existant Gaussian");
64 }
65 return log_lhood_single_gaus_internal(c_x_in, k);
66}
67
68
69double MOG_diag::log_lhood_single_gaus(const vec &x_in, const int k) const
70{
71 if (do_checks) {
72 it_assert(valid, "MOG_diag::log_lhood_single_gaus(): model not valid");
73 it_assert(check_size(x_in), "MOG_diag::log_lhood_single_gaus(): x has wrong dimensionality");
74 it_assert(((k >= 0) && (k < K)), "MOG::log_lhood_single_gaus(): k specifies a non-existant Gaussian");
75 }
76 return log_lhood_single_gaus_internal(x_in._data(), k);
77}
78
79
80double MOG_diag::log_lhood_internal(const double * c_x_in)
81{
82
83 bool danger = paranoid;
84
85 for (int k = 0;k < K;k++) {
86 double tmp = c_log_weights[k] + log_lhood_single_gaus_internal(c_x_in, k);
87 c_tmpvecK[k] = tmp;
88
89 if (tmp >= log_max_K) danger = true;
90 }
91
92
93 if (danger) {
94 double log_sum = c_tmpvecK[0];
95 for (int k = 1; k < K; k++) log_sum = log_add(log_sum, c_tmpvecK[k]);
96 return(log_sum);
97 }
98 else {
99 double sum = 0.0;
100 for (int k = 0;k < K;k++) sum += std::exp(c_tmpvecK[k]);
101 return(std::log(sum));
102 }
103}
104
105
106double MOG_diag::log_lhood_internal(const vec &x_in)
107{
108 return log_lhood_internal(x_in._data());
109}
110
111
112double MOG_diag::log_lhood(const vec &x_in)
113{
114 if (do_checks) {
115 it_assert(valid, "MOG_diag::log_lhood(): model not valid");
116 it_assert(check_size(x_in), "MOG_diag::log_lhood(): x has wrong dimensionality");
117 }
118 return log_lhood_internal(x_in._data());
119}
120
121
122double MOG_diag::log_lhood(const double * c_x_in)
123{
124 if (do_checks) {
125 it_assert(valid, "MOG_diag::log_lhood(): model not valid");
126 it_assert((c_x_in != 0), "MOG_diag::log_lhood(): c_x_in is a null pointer");
127 }
128
129 return log_lhood_internal(c_x_in);
130}
131
132
133double MOG_diag::lhood_internal(const double * c_x_in)
134{
135
136 bool danger = paranoid;
137
138 for (int k = 0;k < K;k++) {
139 double tmp = c_log_weights[k] + log_lhood_single_gaus_internal(c_x_in, k);
140 c_tmpvecK[k] = tmp;
141
142 if (tmp >= log_max_K) danger = true;
143 }
144
145
146 if (danger) {
147 double log_sum = c_tmpvecK[0];
148 for (int k = 1; k < K; k++) log_sum = log_add(log_sum, c_tmpvecK[k]);
149 return(trunc_exp(log_sum));
150 }
151 else {
152 double sum = 0.0;
153 for (int k = 0;k < K;k++) sum += std::exp(c_tmpvecK[k]);
154 return(sum);
155 }
156}
157
158double MOG_diag::lhood_internal(const vec &x_in) { return lhood_internal(x_in._data()); }
159
160double MOG_diag::lhood(const vec &x_in)
161{
162 if (do_checks) {
163 it_assert(valid, "MOG_diag::lhood(): model not valid");
164 it_assert(check_size(x_in), "MOG_diag::lhood(): x has wrong dimensionality");
165 }
166 return lhood_internal(x_in._data());
167}
168
169
170double MOG_diag::lhood(const double * c_x_in)
171{
172 if (do_checks) {
173 it_assert(valid, "MOG_diag::lhood(): model not valid");
174 it_assert((c_x_in != 0), "MOG_diag::lhood(): c_x_in is a null pointer");
175 }
176
177 return lhood_internal(c_x_in);
178}
179
180
181double MOG_diag::avg_log_lhood(const double ** c_x_in, const int N)
182{
183 if (do_checks) {
184 it_assert(valid, "MOG_diag::avg_log_lhood(): model not valid");
185 it_assert((c_x_in != 0), "MOG_diag::avg_log_lhood(): c_x_in is a null pointer");
186 it_assert((N >= 0), "MOG_diag::avg_log_lhood(): N is zero or negative");
187 }
188
189 double acc = 0.0;
190 for (int n = 0;n < N;n++) acc += log_lhood_internal(c_x_in[n]);
191 return(acc / N);
192}
193
194
196{
197 if (do_checks) {
198 it_assert(valid, "MOG_diag::avg_log_lhood(): model not valid");
199 it_assert(check_size(X_in), "MOG_diag::avg_log_lhood(): X is empty or at least one vector has the wrong dimensionality");
200 }
201 const int N = X_in.size();
202 double acc = 0.0;
203 for (int n = 0;n < N;n++) acc += log_lhood_internal(X_in(n)._data());
204 return(acc / N);
205}
206
208{
209 c_means = 0;
210 c_diag_covs = 0;
212 c_weights = 0;
213 c_log_weights = 0;
214 c_log_det_etc = 0;
215 c_tmpvecK = 0;
216}
217
218
220{
227 c_tmpvecK = disable_c_access(c_tmpvecK);
228}
229
230
232{
236}
237
238
240{
242 if (full) return;
243
247
251}
252
253
255{
257
260
263}
264
265
267{
268 disable_c_access(c_tmpvecK);
269 tmpvecK.set_size(K);
270 c_tmpvecK = enable_c_access(tmpvecK);
271
274}
275
276
277void MOG_diag::load(const std::string &name_in)
278{
279 MOG_generic::load(name_in);
280 if (full) convert_to_diag();
281}
282
283
285{
286 int rows = A_in.size();
287 double ** A = (double **)std::malloc(rows * sizeof(double *));
288 if (A) for (int row = 0;row < rows;row++) A[row] = A_in(row)._data();
289 return(A);
290}
291
293{
294 int rows = A_in.size();
295 int ** A = (int **)std::malloc(rows * sizeof(int *));
296 if (A) for (int row = 0;row < rows;row++) A[row] = A_in(row)._data();
297 return(A);
298}
299
300double ** MOG_diag::disable_c_access(double ** A_in) { if (A_in) std::free(A_in); return(0); }
301int ** MOG_diag::disable_c_access(int ** A_in) { if (A_in) std::free(A_in); return(0); }
302
303double * MOG_diag::enable_c_access(vec & v_in) { return v_in._data(); }
304int * MOG_diag::enable_c_access(ivec & v_in) { return v_in._data(); }
305
306double * MOG_diag::disable_c_access(double *) { return(0); }
307int * MOG_diag::disable_c_access(int *) { return(0); }
308
309}
int size() const
Returns the number of data elements in the array object.
Definition: array.h:155
void setup_means()
additional processing of mean vectors, done as the last step of mean initialisation
Definition: mog_diag.cpp:231
void setup_misc()
additional processing of miscellaneous parameters, done as the last step of overall initialisation
Definition: mog_diag.cpp:266
void setup_weights()
additional processing of the weight vector, done as the last step of weight initialisation
Definition: mog_diag.cpp:254
double log_lhood(const double *c_x_in)
calculate the log likelihood of C vector c_x_in
Definition: mog_diag.cpp:122
double ** c_diag_covs_inv_etc
pointers to the inverted covariance vectors
Definition: mog_diag.h:203
void load(const std::string &name_in)
Initialise the model by loading the parameters from a model file.
Definition: mog_diag.cpp:277
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
double lhood_internal(const double *c_x_in)
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:133
double log_lhood_single_gaus_internal(const double *c_x_in, const int k) const
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:37
double log_lhood_internal(const double *c_x_in)
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:80
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
void zero_all_ptrs()
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:207
double ** c_means
pointers to the mean vectors
Definition: mog_diag.h:197
double ** disable_c_access(double **A_in)
Disable C style access to an Array of vectors (vec)
Definition: mog_diag.cpp:300
double ** enable_c_access(Array< vec > &A_in)
Enable C style access to an Array of vectors (vec)
Definition: mog_diag.cpp:284
void setup_covs()
additional processing of covariance vectors/matrices, done as the last step of covariance initialisat...
Definition: mog_diag.cpp:239
double * c_log_det_etc
pointer to the log_det_etc vector
Definition: mog_diag.h:212
double * c_log_weights
pointer to the log version of the weight vector
Definition: mog_diag.h:209
void free_all_ptrs()
ADD DOCUMENTATION HERE.
Definition: mog_diag.cpp:219
double lhood(const double *c_x_in)
calculate the likelihood of C vector c_x_in
Definition: mog_diag.cpp:170
double * c_weights
pointer to the weight vector
Definition: mog_diag.h:206
double ** c_diag_covs
pointers to the covariance vectors
Definition: mog_diag.h:200
void convert_to_diag_internal()
ADD DOCUMENTATION HERE.
virtual void setup_weights()
additional processing of the weight vector, done as the last step of weight initialisation
bool check_size(const vec &x_in) const
Check if vector x_in has the same dimensionality as the model.
virtual void convert_to_diag()
Convert the model to use diagonal covariances.
bool do_checks
indicates whether checks on input data are done
Definition: mog_generic.h:280
virtual void setup_covs()
additional processing of covariance vectors/matrices, done as the last step of covariance initialisat...
int K
number of gaussians
Definition: mog_generic.h:292
virtual void setup_means()
additional processing of mean vectors, done as the last step of mean initialisation
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
Definition: mog_generic.h:283
vec weights
weights
Definition: mog_generic.h:307
Array< vec > diag_covs
diagonal covariance matrices, stored as vectors
Definition: mog_generic.h:301
int D
dimensionality
Definition: mog_generic.h:295
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
vec log_weights
Pre-calculated log versions of the weights.
Definition: mog_generic.h:320
vec log_det_etc
Gaussian specific pre-calcualted constants.
Definition: mog_generic.h:317
Array< vec > means
means
Definition: mog_generic.h:298
bool full
indicates whether we are using full or diagonal covariance matrices
Definition: mog_generic.h:286
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
virtual void load(const std::string &name_in)
Initialise the model by loading the parameters from a model file.
bool paranoid
indicates whether we are paranoid about numerical stability
Definition: mog_generic.h:289
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
double log_add(double log_a, double log_b)
Safe substitute for log(exp(log_a) + exp(log_b))
Definition: log_exp.cpp:40
vec log(const vec &x)
The natural logarithm of the elements.
Definition: log_exp.h:241
double trunc_exp(double x)
Truncated exponential function.
Definition: log_exp.h:137
vec exp(const vec &x)
Exp of the elements of a vector x.
Definition: log_exp.h:155
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition: matfunc.h:59
Logarithmic and exponenential functions - header file.
Diagonal Mixture of Gaussians class - header file.
itpp namespace
Definition: itmex.h:37
SourceForge Logo

Generated on Sun Jun 5 2022 21:26:42 for IT++ by Doxygen 1.9.3