Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
itpp
stat
mog_diag.cpp
Go to the documentation of this file.
1
29
#include <
itpp/base/math/log_exp.h
>
30
#include <
itpp/stat/mog_diag.h
>
31
#include <cstdlib>
32
33
34
namespace
itpp
35
{
36
37
double
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
53
double
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
59
double
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
69
double
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
80
double
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
106
double
MOG_diag::log_lhood_internal
(
const
vec &x_in)
107
{
108
return
log_lhood_internal
(x_in._data());
109
}
110
111
112
double
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
122
double
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
133
double
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
158
double
MOG_diag::lhood_internal
(
const
vec &x_in) {
return
lhood_internal
(x_in._data()); }
159
160
double
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
170
double
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
181
double
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
195
double
MOG_diag::avg_log_lhood
(
const
Array<vec>
&X_in)
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
207
void
MOG_diag::zero_all_ptrs
()
208
{
209
c_means
= 0;
210
c_diag_covs
= 0;
211
c_diag_covs_inv_etc
= 0;
212
c_weights
= 0;
213
c_log_weights
= 0;
214
c_log_det_etc
= 0;
215
c_tmpvecK = 0;
216
}
217
218
219
void
MOG_diag::free_all_ptrs
()
220
{
221
c_means
=
disable_c_access
(
c_means
);
222
c_diag_covs
=
disable_c_access
(
c_diag_covs
);
223
c_diag_covs_inv_etc
=
disable_c_access
(
c_diag_covs_inv_etc
);
224
c_weights
=
disable_c_access
(
c_weights
);
225
c_log_weights
=
disable_c_access
(
c_log_weights
);
226
c_log_det_etc
=
disable_c_access
(
c_log_det_etc
);
227
c_tmpvecK =
disable_c_access
(c_tmpvecK);
228
}
229
230
231
void
MOG_diag::setup_means
()
232
{
233
MOG_generic::setup_means
();
234
disable_c_access
(
c_means
);
235
c_means
=
enable_c_access
(
means
);
236
}
237
238
239
void
MOG_diag::setup_covs
()
240
{
241
MOG_generic::setup_covs
();
242
if
(
full
)
return
;
243
244
disable_c_access
(
c_diag_covs
);
245
disable_c_access
(
c_diag_covs_inv_etc
);
246
disable_c_access
(
c_log_det_etc
);
247
248
c_diag_covs
=
enable_c_access
(
diag_covs
);
249
c_diag_covs_inv_etc
=
enable_c_access
(
diag_covs_inv_etc
);
250
c_log_det_etc
=
enable_c_access
(
log_det_etc
);
251
}
252
253
254
void
MOG_diag::setup_weights
()
255
{
256
MOG_generic::setup_weights
();
257
258
disable_c_access
(
c_weights
);
259
disable_c_access
(
c_log_weights
);
260
261
c_weights
=
enable_c_access
(
weights
);
262
c_log_weights
=
enable_c_access
(
log_weights
);
263
}
264
265
266
void
MOG_diag::setup_misc
()
267
{
268
disable_c_access
(c_tmpvecK);
269
tmpvecK.set_size(
K
);
270
c_tmpvecK =
enable_c_access
(tmpvecK);
271
272
MOG_generic::setup_misc
();
273
if
(
full
)
convert_to_diag_internal
();
274
}
275
276
277
void
MOG_diag::load
(
const
std::string &name_in)
278
{
279
MOG_generic::load
(name_in);
280
if
(
full
)
convert_to_diag
();
281
}
282
283
284
double
**
MOG_diag::enable_c_access
(
Array<vec>
& A_in)
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
292
int
**
MOG_diag::enable_c_access
(
Array<ivec>
& A_in)
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
300
double
**
MOG_diag::disable_c_access
(
double
** A_in) {
if
(A_in) std::free(A_in);
return
(0); }
301
int
**
MOG_diag::disable_c_access
(
int
** A_in) {
if
(A_in) std::free(A_in);
return
(0); }
302
303
double
*
MOG_diag::enable_c_access
(vec & v_in) {
return
v_in._data(); }
304
int
*
MOG_diag::enable_c_access
(ivec & v_in) {
return
v_in._data(); }
305
306
double
*
MOG_diag::disable_c_access
(
double
*) {
return
(0); }
307
int
*
MOG_diag::disable_c_access
(
int
*) {
return
(0); }
308
309
}
Generated on Sat Jul 6 2013 10:54:25 for IT++ by
Doxygen
1.8.2