1 | #include <math.h> |
---|
2 | |
---|
3 | #include <itpp/base/bessel.h> |
---|
4 | #include "exp_family.h" |
---|
5 | |
---|
6 | namespace bdm { |
---|
7 | |
---|
8 | Uniform_RNG UniRNG; |
---|
9 | Normal_RNG NorRNG; |
---|
10 | Gamma_RNG GamRNG; |
---|
11 | |
---|
12 | using std::cout; |
---|
13 | |
---|
14 | /////////// |
---|
15 | |
---|
16 | void BMEF::bayes( const vec &yt, const vec &cond ) { |
---|
17 | this->bayes_weighted ( yt, cond, 1.0 ); |
---|
18 | }; |
---|
19 | |
---|
20 | void egiw::set_parameters (int dimx0, ldmat V0, double nu0) { |
---|
21 | dimx = dimx0; |
---|
22 | nPsi = V0.rows() - dimx; |
---|
23 | dim = dimx * (dimx + nPsi); // size(R) + size(Theta) |
---|
24 | |
---|
25 | V = V0; |
---|
26 | if (nu0 < 0) { |
---|
27 | nu = 0.1 + nPsi + 2 * dimx + 2; // +2 assures finite expected value of R |
---|
28 | // terms before that are sufficient for finite normalization |
---|
29 | } else { |
---|
30 | nu = nu0; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | vec egiw::sample() const { |
---|
35 | mat M; |
---|
36 | chmat R; |
---|
37 | sample_mat(M,R); |
---|
38 | |
---|
39 | return concat (cvectorize(M),cvectorize(R.to_mat())); |
---|
40 | } |
---|
41 | |
---|
42 | mat egiw::sample_mat(int n) const { |
---|
43 | // TODO - correct approach - convert to product of norm * Wishart |
---|
44 | mat M; |
---|
45 | ldmat Vz; |
---|
46 | ldmat Lam; |
---|
47 | factorize(M,Vz,Lam); |
---|
48 | |
---|
49 | chmat ChLam(Lam.to_mat()); |
---|
50 | chmat iChLam; |
---|
51 | ChLam.inv(iChLam); |
---|
52 | |
---|
53 | eWishartCh Omega; //inverse Wishart, result is R, |
---|
54 | Omega.set_parameters(iChLam,nu-2*nPsi-dimx); // 2*nPsi is there to match numercial simulations - check if analytically correct |
---|
55 | |
---|
56 | mat OmChi; |
---|
57 | mat Z(M.rows(),M.cols()); |
---|
58 | |
---|
59 | mat Mi; |
---|
60 | mat RChiT; |
---|
61 | mat tmp(dimension(), n); |
---|
62 | for (int i=0; i<n;i++){ |
---|
63 | OmChi=Omega.sample_mat(); |
---|
64 | RChiT=inv(OmChi); |
---|
65 | Z=randn(M.rows(), M.cols()); |
---|
66 | Mi = M + RChiT * Z * inv(Vz._L().T() *diag(sqrt(Vz._D()))); |
---|
67 | |
---|
68 | tmp.set_col(i,concat (cvectorize(Mi),cvectorize(RChiT*RChiT.T()))); |
---|
69 | } |
---|
70 | return tmp; |
---|
71 | } |
---|
72 | |
---|
73 | void egiw::sample_mat(mat &Mi, chmat &Ri)const{ |
---|
74 | |
---|
75 | // TODO - correct approach - convert to product of norm * Wishart |
---|
76 | mat M; |
---|
77 | ldmat Vz; |
---|
78 | ldmat Lam; |
---|
79 | factorize(M,Vz,Lam); |
---|
80 | |
---|
81 | chmat Ch; |
---|
82 | Ch.setCh(Lam._L()*diag(sqrt(Lam._D()))); |
---|
83 | chmat iCh; |
---|
84 | Ch.inv(iCh); |
---|
85 | |
---|
86 | eWishartCh Omega; //inverse Wishart, result is R, |
---|
87 | Omega.set_parameters(iCh,nu-2*nPsi-dimx); // 2*nPsi is there to match numercial simulations - check if analytically correct |
---|
88 | |
---|
89 | chmat Omi; |
---|
90 | Omi.setCh(Omega.sample_mat()); |
---|
91 | |
---|
92 | mat Z=randn(M.rows(), M.cols()); |
---|
93 | Mi = M+ Omi._Ch() * Z * inv(Vz._L()*diag(sqrt(Vz._D()))); |
---|
94 | Omi.inv(Ri); |
---|
95 | } |
---|
96 | |
---|
97 | double egiw::evallog_nn ( const vec &val ) const { |
---|
98 | int vend = val.length() - 1; |
---|
99 | |
---|
100 | if ( dimx == 1 ) { //same as the following, just quicker. |
---|
101 | double r = val ( vend ); //last entry! |
---|
102 | if ( r < 0 ) return -inf; |
---|
103 | vec Psi ( nPsi + dimx ); |
---|
104 | Psi ( 0 ) = -1.0; |
---|
105 | Psi.set_subvector ( 1, val ( 0, vend - 1 ) ); // fill the rest |
---|
106 | |
---|
107 | double Vq = V.qform ( Psi ); |
---|
108 | return -0.5* ( nu*log ( r ) + Vq / r ); |
---|
109 | } else { |
---|
110 | mat Th = reshape ( val ( 0, nPsi * dimx - 1 ), nPsi, dimx ); |
---|
111 | fsqmat R ( reshape ( val ( nPsi*dimx, vend ), dimx, dimx ) ); |
---|
112 | double ldetR = R.logdet(); |
---|
113 | if ( ldetR ) return -inf; |
---|
114 | mat Tmp = concat_vertical ( -eye ( dimx ), Th ); |
---|
115 | fsqmat iR ( dimx ); |
---|
116 | R.inv ( iR ); |
---|
117 | |
---|
118 | return -0.5* ( nu*ldetR + trace ( iR.to_mat() *Tmp.T() *V.to_mat() *Tmp ) ); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | double egiw::lognc() const { |
---|
123 | const vec& D = V._D(); |
---|
124 | |
---|
125 | double m = nu - nPsi - dimx - 1; |
---|
126 | #define log2 0.693147180559945286226763983 |
---|
127 | #define logpi 1.144729885849400163877476189 |
---|
128 | #define log2pi 1.83787706640935 |
---|
129 | #define Inf std::numeric_limits<double>::infinity() |
---|
130 | |
---|
131 | double nkG = 0.5 * dimx * ( -nPsi * log2pi + sum ( log ( D ( dimx, D.length() - 1 ) ) ) ); |
---|
132 | // temporary for lgamma in Wishart |
---|
133 | double lg = 0; |
---|
134 | for ( int i = 0; i < dimx; i++ ) { |
---|
135 | lg += lgamma ( 0.5 * ( m - i ) ); |
---|
136 | } |
---|
137 | |
---|
138 | double nkW = 0.5 * ( m * sum ( log ( D ( 0, dimx - 1 ) ) ) ) \ |
---|
139 | - 0.5 * dimx * ( m * log2 + 0.5 * ( dimx - 1 ) * log2pi ) - lg; |
---|
140 | |
---|
141 | // bdm_assert_debug ( ( ( -nkG - nkW ) > -Inf ) && ( ( -nkG - nkW ) < Inf ), "ARX improper" ); |
---|
142 | if (-nkG - nkW==Inf){ |
---|
143 | cout << "??" <<endl; |
---|
144 | } |
---|
145 | return -nkG - nkW; |
---|
146 | } |
---|
147 | |
---|
148 | vec egiw::est_theta() const { |
---|
149 | if ( dimx == 1 ) { |
---|
150 | const mat &L = V._L(); |
---|
151 | int end = L.rows() - 1; |
---|
152 | |
---|
153 | mat iLsub = ltuinv ( L ( dimx, end, dimx, end ) ); |
---|
154 | |
---|
155 | vec L0 = L.get_col ( 0 ); |
---|
156 | |
---|
157 | return iLsub * L0 ( 1, end ); |
---|
158 | } else { |
---|
159 | bdm_error ( "ERROR: est_theta() not implemented for dimx>1" ); |
---|
160 | return vec(); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | void egiw::factorize(mat &M, ldmat &Vz, ldmat &Lam) const{ |
---|
165 | const mat &L = V._L(); |
---|
166 | const vec &D = V._D(); |
---|
167 | int end = L.rows() - 1; |
---|
168 | |
---|
169 | Vz=ldmat(L ( dimx, end, dimx, end ), D(dimx,end)); |
---|
170 | mat iLsub = ltuinv ( Vz._L()); |
---|
171 | // set mean value |
---|
172 | mat Lpsi = L ( dimx, end, 0, dimx - 1 ); |
---|
173 | M = iLsub * Lpsi; |
---|
174 | |
---|
175 | Lam =ldmat ( L (0, dimx-1, 0, dimx-1 ), D (0, dimx-1 ) ); //exp val of R |
---|
176 | if (1){ // test with Peterka |
---|
177 | mat VF=V.to_mat(); |
---|
178 | mat Vf=VF(0,dimx-1,0, dimx-1); |
---|
179 | mat Vzf = VF(dimx,end,0,dimx-1); |
---|
180 | mat VZ = VF(dimx,end,dimx,end); |
---|
181 | |
---|
182 | mat Lam2 = Vf-Vzf.T()*inv(VZ)*Vzf; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | ldmat egiw::est_theta_cov() const { |
---|
187 | if ( dimx == 1 ) { |
---|
188 | const mat &L = V._L(); |
---|
189 | const vec &D = V._D(); |
---|
190 | int end = D.length() - 1; |
---|
191 | |
---|
192 | mat Lsub = L ( 1, end, 1, end ); |
---|
193 | // mat Dsub = diag ( D ( 1, end ) ); |
---|
194 | |
---|
195 | ldmat LD(inv(Lsub).T(), 1.0/D(1,end)); |
---|
196 | return LD; |
---|
197 | |
---|
198 | } else { |
---|
199 | bdm_error ( "ERROR: est_theta_cov() not implemented for dimx>1" ); |
---|
200 | return ldmat(); |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | vec egiw::mean() const { |
---|
206 | |
---|
207 | if ( dimx == 1 ) { |
---|
208 | const vec &D = V._D(); |
---|
209 | int end = D.length() - 1; |
---|
210 | |
---|
211 | vec m ( dim ); |
---|
212 | m.set_subvector ( 0, est_theta() ); |
---|
213 | m ( end ) = D ( 0 ) / ( nu - nPsi - 2 * dimx - 2 ); |
---|
214 | return m; |
---|
215 | } else { |
---|
216 | mat M; |
---|
217 | mat R; |
---|
218 | mean_mat ( M, R ); |
---|
219 | return concat( cvectorize ( M),cvectorize( R ) ); |
---|
220 | } |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | vec egiw::variance() const { |
---|
225 | int l = V.rows(); |
---|
226 | // cut out rest of lower-right part of V |
---|
227 | const ldmat tmp ( V, linspace ( dimx, l - 1 ) ); |
---|
228 | // invert it |
---|
229 | ldmat itmp ( l ); |
---|
230 | tmp.inv ( itmp ); |
---|
231 | |
---|
232 | // following Wikipedia notation |
---|
233 | // m=nu-nPsi-dimx-1, p=dimx |
---|
234 | double mp1p=nu-nPsi-2*dimx; // m-p+1 |
---|
235 | double mp1m=mp1p-2; // m-p-1 |
---|
236 | |
---|
237 | if ( dimx == 1 ) { |
---|
238 | double cove = V._D() ( 0 ) / mp1m ; |
---|
239 | |
---|
240 | vec var ( l ); |
---|
241 | var.set_subvector ( 0, diag ( itmp.to_mat() ) *cove ); |
---|
242 | var ( l - 1 ) = cove * cove / (mp1m-2); |
---|
243 | return var; |
---|
244 | } else { |
---|
245 | ldmat Vll( V, linspace(0,dimx-1)); // top-left part of V |
---|
246 | mat Y=Vll.to_mat(); |
---|
247 | mat varY(Y.rows(), Y.cols()); |
---|
248 | |
---|
249 | double denom = (mp1p-1)*mp1m*mp1m*(mp1m-2); // (m-p)(m-p-1)^2(m-p-3) |
---|
250 | |
---|
251 | int i,j; |
---|
252 | for ( i=0; i<Y.rows(); i++){ |
---|
253 | for ( j=0; j<Y.cols(); j++){ |
---|
254 | varY(i,j) = (mp1p*Y(i,j)*Y(i,j) + mp1m * Y(i,i)* Y(j,j)) /denom; |
---|
255 | } |
---|
256 | } |
---|
257 | vec mean_dR = diag(Y)/mp1m; // corresponds to cove |
---|
258 | vec var_th=diag ( itmp.to_mat() ); |
---|
259 | vec var_Th ( mean_dR.length()*var_th.length() ); |
---|
260 | // diagonal of diag(mean_dR) \kron diag(var_th) |
---|
261 | for (int i=0; i<mean_dR.length(); i++){ |
---|
262 | var_Th.set_subvector(i*var_th.length(), var_th*mean_dR(i)); |
---|
263 | } |
---|
264 | |
---|
265 | return concat(var_Th, cvectorize(varY)); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void egiw::mean_mat ( mat &M, mat&R ) const { |
---|
270 | const mat &L = V._L(); |
---|
271 | const vec &D = V._D(); |
---|
272 | int end = L.rows() - 1; |
---|
273 | |
---|
274 | ldmat ldR ( L ( 0, dimx - 1, 0, dimx - 1 ), D ( 0, dimx - 1 ) / ( nu - nPsi - 2*dimx - 2 ) ); //exp val of R |
---|
275 | mat iLsub = ltuinv ( L ( dimx, end, dimx, end ) ); |
---|
276 | |
---|
277 | // set mean value |
---|
278 | mat Lpsi = L ( dimx, end, 0, dimx - 1 ); |
---|
279 | M = iLsub * Lpsi; |
---|
280 | R = ldR.to_mat() ; |
---|
281 | } |
---|
282 | |
---|
283 | vec egamma::sample() const { |
---|
284 | vec smp ( dim ); |
---|
285 | int i; |
---|
286 | |
---|
287 | for ( i = 0; i < dim; i++ ) { |
---|
288 | if ( beta ( i ) > std::numeric_limits<double>::epsilon() ) { |
---|
289 | GamRNG.setup ( alpha ( i ), beta ( i ) ); |
---|
290 | } else { |
---|
291 | GamRNG.setup ( alpha ( i ), std::numeric_limits<double>::epsilon() ); |
---|
292 | } |
---|
293 | #pragma omp critical |
---|
294 | smp ( i ) = GamRNG(); |
---|
295 | } |
---|
296 | |
---|
297 | return smp; |
---|
298 | } |
---|
299 | |
---|
300 | // mat egamma::sample ( int N ) const { |
---|
301 | // mat Smp ( rv.count(),N ); |
---|
302 | // int i,j; |
---|
303 | // |
---|
304 | // for ( i=0; i<rv.count(); i++ ) { |
---|
305 | // GamRNG.setup ( alpha ( i ),beta ( i ) ); |
---|
306 | // |
---|
307 | // for ( j=0; j<N; j++ ) { |
---|
308 | // Smp ( i,j ) = GamRNG(); |
---|
309 | // } |
---|
310 | // } |
---|
311 | // |
---|
312 | // return Smp; |
---|
313 | // } |
---|
314 | |
---|
315 | double egamma::evallog ( const vec &val ) const { |
---|
316 | double res = 0.0; //the rest will be added |
---|
317 | int i; |
---|
318 | |
---|
319 | if ( any ( val <= 0. ) ) return -inf; |
---|
320 | if ( any ( beta <= 0. ) ) return -inf; |
---|
321 | for ( i = 0; i < dim; i++ ) { |
---|
322 | res += ( alpha ( i ) - 1 ) * std::log ( val ( i ) ) - beta ( i ) * val ( i ); |
---|
323 | } |
---|
324 | double tmp = res - lognc();; |
---|
325 | bdm_assert_debug ( std::isfinite ( tmp ), "Infinite value" ); |
---|
326 | return tmp; |
---|
327 | } |
---|
328 | |
---|
329 | double egamma::lognc() const { |
---|
330 | double res = 0.0; //will be added |
---|
331 | int i; |
---|
332 | |
---|
333 | for ( i = 0; i < dim; i++ ) { |
---|
334 | res += lgamma ( alpha ( i ) ) - alpha ( i ) * std::log ( beta ( i ) ) ; |
---|
335 | } |
---|
336 | |
---|
337 | return res; |
---|
338 | } |
---|
339 | |
---|
340 | void mgamma::set_parameters ( double k0, const vec &beta0 ) { |
---|
341 | k = k0; |
---|
342 | iepdf.set_parameters ( k * ones ( beta0.length() ), beta0 ); |
---|
343 | dimc = iepdf.dimension(); |
---|
344 | dim = iepdf.dimension(); |
---|
345 | } |
---|
346 | |
---|
347 | void eEmp::resample ( ivec &ind, RESAMPLING_METHOD method ) { |
---|
348 | ind = zeros_i ( n ); |
---|
349 | ivec N_babies = zeros_i ( n ); |
---|
350 | vec cumDist = cumsum ( w ); |
---|
351 | vec u ( n ); |
---|
352 | int i, j, parent; |
---|
353 | double u0; |
---|
354 | |
---|
355 | switch ( method ) { |
---|
356 | case MULTINOMIAL: |
---|
357 | u ( n - 1 ) = pow ( UniRNG.sample(), 1.0 / n ); |
---|
358 | |
---|
359 | for ( i = n - 2; i >= 0; i-- ) { |
---|
360 | u ( i ) = u ( i + 1 ) * pow ( UniRNG.sample(), 1.0 / ( i + 1 ) ); |
---|
361 | } |
---|
362 | |
---|
363 | break; |
---|
364 | |
---|
365 | case STRATIFIED: |
---|
366 | |
---|
367 | for ( i = 0; i < n; i++ ) { |
---|
368 | u ( i ) = ( i + UniRNG.sample() ) / n; |
---|
369 | } |
---|
370 | |
---|
371 | break; |
---|
372 | |
---|
373 | case SYSTEMATIC: |
---|
374 | u0 = UniRNG.sample(); |
---|
375 | |
---|
376 | for ( i = 0; i < n; i++ ) { |
---|
377 | u ( i ) = ( i + u0 ) / n; |
---|
378 | } |
---|
379 | |
---|
380 | break; |
---|
381 | |
---|
382 | default: |
---|
383 | bdm_error ( "PF::resample(): Unknown resampling method" ); |
---|
384 | } |
---|
385 | |
---|
386 | // U is now full |
---|
387 | j = 0; |
---|
388 | |
---|
389 | for ( i = 0; i < n; i++ ) { |
---|
390 | while ( u ( i ) > cumDist ( j ) ) j++; |
---|
391 | |
---|
392 | N_babies ( j ) ++; |
---|
393 | } |
---|
394 | // We have assigned new babies for each Particle |
---|
395 | // Now, we fill the resulting index such that: |
---|
396 | // * particles with at least one baby should not move * |
---|
397 | // This assures that reassignment can be done inplace; |
---|
398 | |
---|
399 | // find the first parent; |
---|
400 | parent = 0; |
---|
401 | while ( N_babies ( parent ) == 0 ) parent++; |
---|
402 | |
---|
403 | // Build index |
---|
404 | for ( i = 0; i < n; i++ ) { |
---|
405 | if ( N_babies ( i ) > 0 ) { |
---|
406 | ind ( i ) = i; |
---|
407 | N_babies ( i ) --; //this index was now replicated; |
---|
408 | } else { |
---|
409 | // test if the parent has been fully replicated |
---|
410 | // if yes, find the next one |
---|
411 | while ( ( N_babies ( parent ) == 0 ) || ( N_babies ( parent ) == 1 && parent > i ) ) parent++; |
---|
412 | |
---|
413 | // Replicate parent |
---|
414 | ind ( i ) = parent; |
---|
415 | |
---|
416 | N_babies ( parent ) --; //this index was now replicated; |
---|
417 | } |
---|
418 | |
---|
419 | } |
---|
420 | |
---|
421 | // copy the internals according to ind |
---|
422 | for ( i = 0; i < n; i++ ) { |
---|
423 | if ( ind ( i ) != i ) { |
---|
424 | samples ( i ) = samples ( ind ( i ) ); |
---|
425 | } |
---|
426 | w ( i ) = 1.0 / n; |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | void eEmp::set_statistics ( const vec &w0, const epdf &epdf0 ) { |
---|
431 | dim = epdf0.dimension(); |
---|
432 | w = w0; |
---|
433 | w /= sum ( w0 );//renormalize |
---|
434 | n = w.length(); |
---|
435 | samples.set_size ( n ); |
---|
436 | |
---|
437 | for ( int i = 0; i < n; i++ ) { |
---|
438 | samples ( i ) = epdf0.sample(); |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | void eEmp::set_samples ( const epdf* epdf0 ) { |
---|
443 | w = 1; |
---|
444 | w /= sum ( w );//renormalize |
---|
445 | |
---|
446 | for ( int i = 0; i < n; i++ ) { |
---|
447 | samples ( i ) = epdf0->sample(); |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | void migamma_ref::from_setting ( const Setting &set ) { |
---|
452 | vec ref; |
---|
453 | UI::get ( ref, set, "ref" , UI::compulsory ); |
---|
454 | set_parameters ( set["k"], ref, set["l"] ); |
---|
455 | } |
---|
456 | |
---|
457 | void mlognorm::from_setting ( const Setting &set ) { |
---|
458 | vec mu0; |
---|
459 | UI::get ( mu0, set, "mu0", UI::compulsory ); |
---|
460 | set_parameters ( mu0.length(), set["k"] ); |
---|
461 | condition ( mu0 ); |
---|
462 | } |
---|
463 | |
---|
464 | }; |
---|