1 | /*! |
---|
2 | \file |
---|
3 | \brief Probability distributions for Exponential Family models. |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | ----------------------------------- |
---|
7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | |
---|
9 | Using IT++ for numerical operations |
---|
10 | ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef EF_H |
---|
14 | #define EF_H |
---|
15 | |
---|
16 | |
---|
17 | #include "../shared_ptr.h" |
---|
18 | #include "../base/bdmbase.h" |
---|
19 | #include "../math/chmat.h" |
---|
20 | |
---|
21 | namespace bdm { |
---|
22 | |
---|
23 | |
---|
24 | //! Global Uniform_RNG |
---|
25 | extern Uniform_RNG UniRNG; |
---|
26 | //! Global Normal_RNG |
---|
27 | extern Normal_RNG NorRNG; |
---|
28 | //! Global Gamma_RNG |
---|
29 | extern Gamma_RNG GamRNG; |
---|
30 | |
---|
31 | /*! |
---|
32 | * \brief General conjugate exponential family posterior density. |
---|
33 | |
---|
34 | * More?... |
---|
35 | */ |
---|
36 | |
---|
37 | class eEF : public epdf { |
---|
38 | public: |
---|
39 | // eEF() :epdf() {}; |
---|
40 | //! default constructor |
---|
41 | eEF () : epdf () {}; |
---|
42 | //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ |
---|
43 | virtual double lognc() const = 0; |
---|
44 | |
---|
45 | //!Evaluate normalized log-probability |
---|
46 | virtual double evallog_nn ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
47 | |
---|
48 | //!Evaluate normalized log-probability |
---|
49 | virtual double evallog ( const vec &val ) const { |
---|
50 | double tmp; |
---|
51 | tmp = evallog_nn ( val ) - lognc(); |
---|
52 | return tmp; |
---|
53 | } |
---|
54 | //!Evaluate normalized log-probability for many samples |
---|
55 | virtual vec evallog_mat ( const mat &Val ) const { |
---|
56 | vec x ( Val.cols() ); |
---|
57 | for ( int i = 0; i < Val.cols(); i++ ) { |
---|
58 | x ( i ) = evallog_nn ( Val.get_col ( i ) ) ; |
---|
59 | } |
---|
60 | return x - lognc(); |
---|
61 | } |
---|
62 | //!Evaluate normalized log-probability for many samples |
---|
63 | virtual vec evallog_mat ( const Array<vec> &Val ) const { |
---|
64 | vec x ( Val.length() ); |
---|
65 | for ( int i = 0; i < Val.length(); i++ ) { |
---|
66 | x ( i ) = evallog_nn ( Val ( i ) ) ; |
---|
67 | } |
---|
68 | return x - lognc(); |
---|
69 | } |
---|
70 | |
---|
71 | //!Power of the density, used e.g. to flatten the density |
---|
72 | virtual void pow ( double p ) NOT_IMPLEMENTED_VOID; |
---|
73 | }; |
---|
74 | |
---|
75 | |
---|
76 | //! Estimator for Exponential family |
---|
77 | class BMEF : public BM { |
---|
78 | protected: |
---|
79 | //! forgetting factor |
---|
80 | double frg; |
---|
81 | //! cached value of lognc() in the previous step (used in evaluation of \c ll ) |
---|
82 | double last_lognc; |
---|
83 | public: |
---|
84 | //! Default constructor (=empty constructor) |
---|
85 | BMEF ( double frg0 = 1.0 ) : BM (), frg ( frg0 ) {} |
---|
86 | //! Copy constructor |
---|
87 | BMEF ( const BMEF &B ) : BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {} |
---|
88 | //!get statistics from another model |
---|
89 | virtual void set_statistics ( const BMEF* BM0 ) NOT_IMPLEMENTED_VOID; |
---|
90 | |
---|
91 | //! Weighted update of sufficient statistics (Bayes rule) |
---|
92 | virtual void bayes_weighted ( const vec &data, const vec &cond = empty_vec, const double w = 1.0 ) {}; |
---|
93 | //original Bayes |
---|
94 | void bayes ( const vec &yt, const vec &cond = empty_vec ); |
---|
95 | |
---|
96 | //!Flatten the posterior according to the given BMEF (of the same type!) |
---|
97 | virtual void flatten ( const BMEF * B ) NOT_IMPLEMENTED_VOID; |
---|
98 | |
---|
99 | double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0); |
---|
100 | |
---|
101 | virtual epdf* epredictor() const NOT_IMPLEMENTED(NULL); |
---|
102 | |
---|
103 | virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); |
---|
104 | |
---|
105 | void to_setting ( Setting &set ) const |
---|
106 | { |
---|
107 | BM::to_setting( set ); |
---|
108 | UI::save(frg, set, "frg"); |
---|
109 | // TODO DOPLNIT? ALE MOMENTALNE CHYBI FROM_SETTING PRO INSPIRACI |
---|
110 | } |
---|
111 | void from_setting( const Setting &set) { |
---|
112 | BM::from_setting(set); |
---|
113 | if ( !UI::get ( frg, set, "frg" ) ) |
---|
114 | frg = 1.0; |
---|
115 | |
---|
116 | } |
---|
117 | }; |
---|
118 | |
---|
119 | /*! Dirac delta density with predefined transformation |
---|
120 | |
---|
121 | Density of the type:\f[ f(x_t | y_t) = \delta (x_t - g(y_t)) \f] |
---|
122 | where \f$ x_t \f$ is the \c rv, \f$ y_t \f$ is the \c rvc and g is a deterministic transformation of class fn. |
---|
123 | */ |
---|
124 | class mgdirac: public pdf{ |
---|
125 | protected: |
---|
126 | shared_ptr<fnc> g; |
---|
127 | public: |
---|
128 | vec samplecond(const vec &cond) { |
---|
129 | bdm_assert_debug(cond.length()==g->dimensionc(),"given cond in not compatible with g"); |
---|
130 | vec tmp = g->eval(cond); |
---|
131 | return tmp; |
---|
132 | } |
---|
133 | double evallogcond ( const vec &yt, const vec &cond ){ |
---|
134 | return std::numeric_limits< double >::max(); |
---|
135 | } |
---|
136 | void from_setting(const Setting& set){ |
---|
137 | pdf::from_setting(set); |
---|
138 | g=UI::build<fnc>(set,"g",UI::compulsory); |
---|
139 | validate(); |
---|
140 | } |
---|
141 | void to_setting(Setting &set) const{ |
---|
142 | pdf::to_setting(set); |
---|
143 | UI::save(g.get(), set, "g"); |
---|
144 | } |
---|
145 | void validate() { |
---|
146 | dim = g->dimension(); |
---|
147 | dimc = g->dimensionc(); |
---|
148 | } |
---|
149 | }; |
---|
150 | UIREGISTER(mgdirac); |
---|
151 | |
---|
152 | |
---|
153 | template<class sq_T, template <typename> class TEpdf> |
---|
154 | class mlnorm; |
---|
155 | |
---|
156 | /*! |
---|
157 | * \brief Gaussian density with positive definite (decomposed) covariance matrix. |
---|
158 | |
---|
159 | * More?... |
---|
160 | */ |
---|
161 | template<class sq_T> |
---|
162 | class enorm : public eEF { |
---|
163 | protected: |
---|
164 | //! mean value |
---|
165 | vec mu; |
---|
166 | //! Covariance matrix in decomposed form |
---|
167 | sq_T R; |
---|
168 | public: |
---|
169 | //!\name Constructors |
---|
170 | //!@{ |
---|
171 | |
---|
172 | enorm () : eEF (), mu (), R () {}; |
---|
173 | enorm ( const vec &mu, const sq_T &R ) { |
---|
174 | set_parameters ( mu, R ); |
---|
175 | } |
---|
176 | void set_parameters ( const vec &mu, const sq_T &R ); |
---|
177 | /*! Create Normal density |
---|
178 | \f[ f(rv) = N(\mu, R) \f] |
---|
179 | from structure |
---|
180 | \code |
---|
181 | class = 'enorm<ldmat>', (OR) 'enorm<chmat>', (OR) 'enorm<fsqmat>'; |
---|
182 | mu = []; // mean value |
---|
183 | R = []; // variance, square matrix of appropriate dimension |
---|
184 | \endcode |
---|
185 | */ |
---|
186 | void from_setting ( const Setting &root ); |
---|
187 | void to_setting ( Setting &root ) const ; |
---|
188 | |
---|
189 | void validate() { |
---|
190 | bdm_assert ( mu.length() == R.rows(), "mu and R parameters do not match" ); |
---|
191 | dim = mu.length(); |
---|
192 | } |
---|
193 | //!@} |
---|
194 | |
---|
195 | //! \name Mathematical operations |
---|
196 | //!@{ |
---|
197 | |
---|
198 | //! dupdate in exponential form (not really handy) |
---|
199 | void dupdate ( mat &v, double nu = 1.0 ); |
---|
200 | |
---|
201 | //! evaluate bhattacharya distance |
---|
202 | double bhattacharyya(const enorm<sq_T> &e2){ |
---|
203 | bdm_assert(dim == e2.dimension(), "enorms of differnt dimensions"); |
---|
204 | sq_T P=R; |
---|
205 | P.add(e2._R()); |
---|
206 | |
---|
207 | double tmp = 0.125*P.invqform(mu - e2._mu()) + 0.5*(P.logdet() - 0.5*(R.logdet() + e2._R().logdet())); |
---|
208 | return tmp; |
---|
209 | } |
---|
210 | |
---|
211 | vec sample() const; |
---|
212 | |
---|
213 | double evallog_nn ( const vec &val ) const; |
---|
214 | double lognc () const; |
---|
215 | vec mean() const { |
---|
216 | return mu; |
---|
217 | } |
---|
218 | vec variance() const { |
---|
219 | return diag ( R.to_mat() ); |
---|
220 | } |
---|
221 | mat covariance() const { |
---|
222 | return R.to_mat(); |
---|
223 | } |
---|
224 | // mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why? |
---|
225 | shared_ptr<pdf> condition ( const RV &rvn ) const; |
---|
226 | |
---|
227 | // target not typed to mlnorm<sq_T, enorm<sq_T> > & |
---|
228 | // because that doesn't compile (perhaps because we |
---|
229 | // haven't finished defining enorm yet), but the type |
---|
230 | // is required |
---|
231 | void condition ( const RV &rvn, pdf &target ) const; |
---|
232 | |
---|
233 | shared_ptr<epdf> marginal ( const RV &rvn ) const; |
---|
234 | void marginal ( const RV &rvn, enorm<sq_T> &target ) const; |
---|
235 | //!@} |
---|
236 | |
---|
237 | //! \name Access to attributes |
---|
238 | //!@{ |
---|
239 | |
---|
240 | vec& _mu() { |
---|
241 | return mu; |
---|
242 | } |
---|
243 | const vec& _mu() const { |
---|
244 | return mu; |
---|
245 | } |
---|
246 | void set_mu ( const vec mu0 ) { |
---|
247 | mu = mu0; |
---|
248 | } |
---|
249 | sq_T& _R() { |
---|
250 | return R; |
---|
251 | } |
---|
252 | const sq_T& _R() const { |
---|
253 | return R; |
---|
254 | } |
---|
255 | //!@} |
---|
256 | |
---|
257 | }; |
---|
258 | UIREGISTER2 ( enorm, chmat ); |
---|
259 | SHAREDPTR2 ( enorm, chmat ); |
---|
260 | UIREGISTER2 ( enorm, ldmat ); |
---|
261 | SHAREDPTR2 ( enorm, ldmat ); |
---|
262 | UIREGISTER2 ( enorm, fsqmat ); |
---|
263 | SHAREDPTR2 ( enorm, fsqmat ); |
---|
264 | |
---|
265 | //forward declaration |
---|
266 | class mstudent; |
---|
267 | |
---|
268 | /*! distribution of multivariate Student t density |
---|
269 | |
---|
270 | Based on article by Genest and Zidek, |
---|
271 | */ |
---|
272 | template<class sq_T> |
---|
273 | class estudent : public eEF{ |
---|
274 | protected: |
---|
275 | //! mena value |
---|
276 | vec mu; |
---|
277 | //! matrix H |
---|
278 | sq_T H; |
---|
279 | //! degrees of freedom |
---|
280 | double delta; |
---|
281 | public: |
---|
282 | double evallog_nn(const vec &val) const{ |
---|
283 | double tmp = -0.5*H.logdet() - 0.5*(delta + dim) * log(1+ H.invqform(val - mu)/delta); |
---|
284 | return tmp; |
---|
285 | } |
---|
286 | double lognc() const { |
---|
287 | //log(pi) = 1.14472988584940 |
---|
288 | double tmp = -lgamma(0.5*(delta+dim))+lgamma(0.5*delta) + 0.5*dim*(log(delta) + 1.14472988584940); |
---|
289 | return tmp; |
---|
290 | } |
---|
291 | void marginal (const RV &rvm, estudent<sq_T> &marg) const { |
---|
292 | ivec ind = rvm.findself_ids(rv); // indeces of rvm in rv |
---|
293 | marg._mu() = mu(ind); |
---|
294 | marg._H() = sq_T(H,ind); |
---|
295 | marg._delta() = delta; |
---|
296 | marg.validate(); |
---|
297 | } |
---|
298 | shared_ptr<epdf> marginal(const RV &rvm) const { |
---|
299 | shared_ptr<estudent<sq_T> > tmp = new estudent<sq_T>; |
---|
300 | marginal(rvm, *tmp); |
---|
301 | return tmp; |
---|
302 | } |
---|
303 | vec sample() const NOT_IMPLEMENTED(vec(0)) |
---|
304 | |
---|
305 | vec mean() const {return mu;} |
---|
306 | mat covariance() const {return delta/(delta-2)*H.to_mat();} |
---|
307 | vec variance() const {return diag(covariance());} |
---|
308 | //! \name access |
---|
309 | //! @{ |
---|
310 | //! access function |
---|
311 | vec& _mu() {return mu;} |
---|
312 | //! access function |
---|
313 | sq_T& _H() {return H;} |
---|
314 | //! access function |
---|
315 | double& _delta() {return delta;} |
---|
316 | //!@} |
---|
317 | //! todo |
---|
318 | void from_setting(const Setting &set){ |
---|
319 | epdf::from_setting(set); |
---|
320 | mat H0; |
---|
321 | UI::get(H0,set, "H"); |
---|
322 | H= H0; // conversion!! |
---|
323 | UI::get(delta,set,"delta"); |
---|
324 | UI::get(mu,set,"mu"); |
---|
325 | } |
---|
326 | void to_setting(Setting &set) const{ |
---|
327 | epdf::to_setting(set); |
---|
328 | UI::save(H.to_mat(), set, "H"); |
---|
329 | UI::save(delta, set, "delta"); |
---|
330 | UI::save(mu, set, "mu"); |
---|
331 | } |
---|
332 | void validate() { |
---|
333 | dim = H.rows(); |
---|
334 | } |
---|
335 | }; |
---|
336 | UIREGISTER2(estudent,fsqmat); |
---|
337 | UIREGISTER2(estudent,ldmat); |
---|
338 | UIREGISTER2(estudent,chmat); |
---|
339 | |
---|
340 | /*! |
---|
341 | * \brief Gauss-inverse-Wishart density stored in LD form |
---|
342 | |
---|
343 | * For \f$p\f$-variate densities, given rv.count() should be \f$p\times\f$ V.rows(). |
---|
344 | * |
---|
345 | */ |
---|
346 | class egiw : public eEF { |
---|
347 | protected: |
---|
348 | //! Extended information matrix of sufficient statistics |
---|
349 | ldmat V; |
---|
350 | //! Number of data records (degrees of freedom) of sufficient statistics |
---|
351 | double nu; |
---|
352 | //! Dimension of the output |
---|
353 | int dimx; |
---|
354 | //! Dimension of the regressor |
---|
355 | int nPsi; |
---|
356 | public: |
---|
357 | //!\name Constructors |
---|
358 | //!@{ |
---|
359 | egiw() : eEF() {}; |
---|
360 | egiw ( int dimx0, ldmat V0, double nu0 = -1.0 ) : eEF() { |
---|
361 | set_parameters ( dimx0, V0, nu0 ); |
---|
362 | }; |
---|
363 | |
---|
364 | void set_parameters ( int dimx0, ldmat V0, double nu0 = -1.0 ); |
---|
365 | //!@} |
---|
366 | |
---|
367 | vec sample() const; |
---|
368 | mat sample_mat ( int n ) const; |
---|
369 | vec mean() const; |
---|
370 | vec variance() const; |
---|
371 | void sample_mat ( mat &Mi, chmat &Ri ) const; |
---|
372 | |
---|
373 | void factorize ( mat &M, ldmat &Vz, ldmat &Lam ) const; |
---|
374 | //! LS estimate of \f$\theta\f$ |
---|
375 | vec est_theta() const; |
---|
376 | |
---|
377 | //! Covariance of the LS estimate |
---|
378 | ldmat est_theta_cov() const; |
---|
379 | |
---|
380 | //! expected values of the linear coefficient and the covariance matrix are written to \c M and \c R , respectively |
---|
381 | void mean_mat ( mat &M, mat&R ) const; |
---|
382 | //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] |
---|
383 | double evallog_nn ( const vec &val ) const; |
---|
384 | double lognc () const; |
---|
385 | void pow ( double p ) { |
---|
386 | V *= p; |
---|
387 | nu *= p; |
---|
388 | }; |
---|
389 | |
---|
390 | //! marginal density (only student for now) |
---|
391 | shared_ptr<epdf> marginal(const RV &rvm) const { |
---|
392 | bdm_assert(dimx==1, "Not supported"); |
---|
393 | //TODO - this is too trivial!!! |
---|
394 | ivec ind = rvm.findself_ids(rv); |
---|
395 | if (min(ind)==0) { //assume it si |
---|
396 | shared_ptr<estudent<ldmat> > tmp = new estudent<ldmat>; |
---|
397 | mat M; |
---|
398 | ldmat Vz; |
---|
399 | ldmat Lam; |
---|
400 | factorize(M,Vz,Lam); |
---|
401 | |
---|
402 | tmp->_mu() = M.get_col(0); |
---|
403 | ldmat H; |
---|
404 | Vz.inv(H); |
---|
405 | H *=Lam._D()(0)/nu; |
---|
406 | tmp->_H() = H; |
---|
407 | tmp->_delta() = nu; |
---|
408 | tmp->validate(); |
---|
409 | return tmp; |
---|
410 | } |
---|
411 | return NULL; |
---|
412 | } |
---|
413 | //! \name Access attributes |
---|
414 | //!@{ |
---|
415 | |
---|
416 | ldmat& _V() { |
---|
417 | return V; |
---|
418 | } |
---|
419 | const ldmat& _V() const { |
---|
420 | return V; |
---|
421 | } |
---|
422 | double& _nu() { |
---|
423 | return nu; |
---|
424 | } |
---|
425 | const double& _nu() const { |
---|
426 | return nu; |
---|
427 | } |
---|
428 | const int & _dimx() const { |
---|
429 | return dimx; |
---|
430 | } |
---|
431 | |
---|
432 | /*! Create Gauss-inverse-Wishart density |
---|
433 | \f[ f(rv) = GiW(V,\nu) \f] |
---|
434 | from structure |
---|
435 | \code |
---|
436 | class = 'egiw'; |
---|
437 | V = []; // square matrix |
---|
438 | dV = []; // vector of diagonal of V (when V not given) |
---|
439 | nu = []; // scalar \nu ((almost) degrees of freedom) |
---|
440 | // when missing, it will be computed to obtain proper pdf |
---|
441 | dimx = []; // dimension of the wishart part |
---|
442 | rv = RV({'name'}) // description of RV |
---|
443 | rvc = RV({'name'}) // description of RV in condition |
---|
444 | \endcode |
---|
445 | */ |
---|
446 | |
---|
447 | void from_setting ( const Setting &set ) { |
---|
448 | epdf::from_setting ( set ); |
---|
449 | UI::get ( dimx, set, "dimx", UI::compulsory ); |
---|
450 | if ( !UI::get ( nu, set, "nu", UI::optional ) ) { |
---|
451 | nu = -1; |
---|
452 | } |
---|
453 | mat V; |
---|
454 | if ( !UI::get ( V, set, "V", UI::optional ) ) { |
---|
455 | vec dV; |
---|
456 | UI::get ( dV, set, "dV", UI::compulsory ); |
---|
457 | set_parameters ( dimx, ldmat ( dV ), nu ); |
---|
458 | |
---|
459 | } else { |
---|
460 | set_parameters ( dimx, V, nu ); |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | void to_setting ( Setting& set ) const { |
---|
465 | epdf::to_setting ( set ); |
---|
466 | UI::save ( dimx, set, "dimx" ); |
---|
467 | UI::save ( V.to_mat(), set, "V" ); |
---|
468 | UI::save ( nu, set, "nu" ); |
---|
469 | }; |
---|
470 | |
---|
471 | void validate() { |
---|
472 | // check sizes, rvs etc. |
---|
473 | // also check if RV are meaningful!!! |
---|
474 | // meaningful = rv for theta and rv for r are split! |
---|
475 | } |
---|
476 | void log_register ( bdm::logger& L, const string& prefix ); |
---|
477 | |
---|
478 | void log_write() const; |
---|
479 | //!@} |
---|
480 | }; |
---|
481 | UIREGISTER ( egiw ); |
---|
482 | SHAREDPTR ( egiw ); |
---|
483 | |
---|
484 | /*! \brief Dirichlet posterior density |
---|
485 | |
---|
486 | Continuous Dirichlet density of \f$n\f$-dimensional variable \f$x\f$ |
---|
487 | \f[ |
---|
488 | f(x|\beta) = \frac{\Gamma[\gamma]}{\prod_{i=1}^{n}\Gamma(\beta_i)} \prod_{i=1}^{n}x_i^{\beta_i-1} |
---|
489 | \f] |
---|
490 | where \f$\gamma=\sum_i \beta_i\f$. |
---|
491 | */ |
---|
492 | class eDirich: public eEF { |
---|
493 | protected: |
---|
494 | //!sufficient statistics |
---|
495 | vec beta; |
---|
496 | public: |
---|
497 | //!\name Constructors |
---|
498 | //!@{ |
---|
499 | |
---|
500 | eDirich () : eEF () {}; |
---|
501 | eDirich ( const eDirich &D0 ) : eEF () { |
---|
502 | set_parameters ( D0.beta ); |
---|
503 | }; |
---|
504 | eDirich ( const vec &beta0 ) { |
---|
505 | set_parameters ( beta0 ); |
---|
506 | }; |
---|
507 | void set_parameters ( const vec &beta0 ) { |
---|
508 | beta = beta0; |
---|
509 | dim = beta.length(); |
---|
510 | } |
---|
511 | //!@} |
---|
512 | |
---|
513 | //! using sampling procedure from wikipedia |
---|
514 | vec sample() const { |
---|
515 | vec y ( beta.length() ); |
---|
516 | for ( int i = 0; i < beta.length(); i++ ) { |
---|
517 | GamRNG.setup ( beta ( i ), 1 ); |
---|
518 | #pragma omp critical |
---|
519 | y ( i ) = GamRNG(); |
---|
520 | } |
---|
521 | return y / sum ( y ); |
---|
522 | } |
---|
523 | |
---|
524 | vec mean() const { |
---|
525 | return beta / sum ( beta ); |
---|
526 | }; |
---|
527 | vec variance() const { |
---|
528 | double gamma = sum ( beta ); |
---|
529 | return elem_mult ( beta, ( gamma - beta ) ) / ( gamma*gamma* ( gamma + 1 ) ); |
---|
530 | } |
---|
531 | //! In this instance, val is ... |
---|
532 | double evallog_nn ( const vec &val ) const { |
---|
533 | double tmp; |
---|
534 | tmp = ( beta - 1 ) * log ( val ); |
---|
535 | return tmp; |
---|
536 | } |
---|
537 | |
---|
538 | double lognc () const { |
---|
539 | double tmp; |
---|
540 | double gam = sum ( beta ); |
---|
541 | double lgb = 0.0; |
---|
542 | for ( int i = 0; i < beta.length(); i++ ) { |
---|
543 | lgb += lgamma ( beta ( i ) ); |
---|
544 | } |
---|
545 | tmp = lgb - lgamma ( gam ); |
---|
546 | return tmp; |
---|
547 | } |
---|
548 | |
---|
549 | //!access function |
---|
550 | vec& _beta() { |
---|
551 | return beta; |
---|
552 | } |
---|
553 | /*! configuration structure |
---|
554 | \code |
---|
555 | class = 'eDirich'; |
---|
556 | beta = []; //parametr beta |
---|
557 | \endcode |
---|
558 | */ |
---|
559 | void from_setting ( const Setting &set ) { |
---|
560 | epdf::from_setting ( set ); |
---|
561 | UI::get ( beta, set, "beta", UI::compulsory ); |
---|
562 | validate(); |
---|
563 | } |
---|
564 | void validate() { |
---|
565 | //check rv |
---|
566 | dim = beta.length(); |
---|
567 | } |
---|
568 | |
---|
569 | void to_setting ( Setting &set ) const |
---|
570 | { |
---|
571 | eEF::to_setting( set ); |
---|
572 | UI::save( beta, set, "beta" ); |
---|
573 | } |
---|
574 | }; |
---|
575 | UIREGISTER ( eDirich ); |
---|
576 | |
---|
577 | /*! Random Walk on Dirichlet |
---|
578 | Using simple assignment |
---|
579 | \f[ \beta = rvc / k + \beta_c \f] |
---|
580 | hence, mean value = rvc, variance = (k+1)*mean*mean; |
---|
581 | |
---|
582 | The greater k is, the greater is the variance of the random walk; |
---|
583 | |
---|
584 | \f$ \beta_c \f$ is used as regularizing element to avoid corner cases, i.e. when one element of rvc is zero. |
---|
585 | By default is it set to 0.1; |
---|
586 | */ |
---|
587 | |
---|
588 | class mDirich: public pdf_internal<eDirich> { |
---|
589 | protected: |
---|
590 | //! constant \f$ k \f$ of the random walk |
---|
591 | double k; |
---|
592 | //! cache of beta_i |
---|
593 | vec &_beta; |
---|
594 | //! stabilizing coefficient \f$ \beta_c \f$ |
---|
595 | vec betac; |
---|
596 | public: |
---|
597 | mDirich() : pdf_internal<eDirich>(), _beta ( iepdf._beta() ) {}; |
---|
598 | void condition ( const vec &val ) { |
---|
599 | _beta = val / k + betac; |
---|
600 | }; |
---|
601 | /*! Create Dirichlet random walk |
---|
602 | \f[ f(rv|rvc) = Di(rvc*k) \f] |
---|
603 | from structure |
---|
604 | \code |
---|
605 | class = 'mDirich'; |
---|
606 | k = 1; // multiplicative constant k |
---|
607 | --- optional --- |
---|
608 | rv = RV({'name'},size) // description of RV |
---|
609 | beta0 = []; // initial value of beta |
---|
610 | betac = []; // initial value of beta |
---|
611 | \endcode |
---|
612 | */ |
---|
613 | void from_setting ( const Setting &set ) { |
---|
614 | pdf::from_setting ( set ); // reads rv and rvc |
---|
615 | if ( _rv()._dsize() > 0 ) { |
---|
616 | rvc = _rv().copy_t ( -1 ); |
---|
617 | } |
---|
618 | vec beta0; |
---|
619 | if ( !UI::get ( beta0, set, "beta0", UI::optional ) ) { |
---|
620 | beta0 = ones ( _rv()._dsize() ); |
---|
621 | } |
---|
622 | if ( !UI::get ( betac, set, "betac", UI::optional ) ) { |
---|
623 | betac = 0.1 * ones ( _rv()._dsize() ); |
---|
624 | } |
---|
625 | _beta = beta0; |
---|
626 | |
---|
627 | UI::get ( k, set, "k", UI::compulsory ); |
---|
628 | validate(); |
---|
629 | } |
---|
630 | void validate() { |
---|
631 | pdf_internal<eDirich>::validate(); |
---|
632 | bdm_assert ( _beta.length() == betac.length(), "beta0 and betac are not compatible" ); |
---|
633 | if ( _rv()._dsize() > 0 ) { |
---|
634 | bdm_assert ( ( _rv()._dsize() == dimension() ) , "Size of rv does not match with beta" ); |
---|
635 | } |
---|
636 | dimc = _beta.length(); |
---|
637 | }; |
---|
638 | }; |
---|
639 | UIREGISTER ( mDirich ); |
---|
640 | |
---|
641 | //! \brief Estimator for Multinomial density |
---|
642 | class multiBM : public BMEF { |
---|
643 | protected: |
---|
644 | //! Conjugate prior and posterior |
---|
645 | eDirich est; |
---|
646 | //! Pointer inside est to sufficient statistics |
---|
647 | vec β |
---|
648 | public: |
---|
649 | //!Default constructor |
---|
650 | multiBM () : BMEF (), est (), beta ( est._beta() ) { |
---|
651 | if ( beta.length() > 0 ) { |
---|
652 | last_lognc = est.lognc(); |
---|
653 | } else { |
---|
654 | last_lognc = 0.0; |
---|
655 | } |
---|
656 | } |
---|
657 | //!Copy constructor |
---|
658 | multiBM ( const multiBM &B ) : BMEF ( B ), est ( B.est ), beta ( est._beta() ) {} |
---|
659 | //! Sets sufficient statistics to match that of givefrom mB0 |
---|
660 | void set_statistics ( const BM* mB0 ) { |
---|
661 | const multiBM* mB = dynamic_cast<const multiBM*> ( mB0 ); |
---|
662 | beta = mB->beta; |
---|
663 | } |
---|
664 | void bayes ( const vec &yt, const vec &cond = empty_vec ); |
---|
665 | |
---|
666 | double logpred ( const vec &yt ) const; |
---|
667 | |
---|
668 | void flatten ( const BMEF* B ); |
---|
669 | |
---|
670 | //! return correctly typed posterior (covariant return) |
---|
671 | const eDirich& posterior() const { |
---|
672 | return est; |
---|
673 | }; |
---|
674 | //! constructor function |
---|
675 | void set_parameters ( const vec &beta0 ) { |
---|
676 | est.set_parameters ( beta0 ); |
---|
677 | est.validate(); |
---|
678 | if ( evalll ) { |
---|
679 | last_lognc = est.lognc(); |
---|
680 | } |
---|
681 | } |
---|
682 | |
---|
683 | void to_setting ( Setting &set ) const { |
---|
684 | BMEF::to_setting ( set ); |
---|
685 | UI::save( &est, set, "prior" ); |
---|
686 | } |
---|
687 | }; |
---|
688 | UIREGISTER( multiBM ); |
---|
689 | |
---|
690 | /*! |
---|
691 | \brief Gamma posterior density |
---|
692 | |
---|
693 | Multivariate Gamma density as product of independent univariate densities. |
---|
694 | \f[ |
---|
695 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
696 | \f] |
---|
697 | */ |
---|
698 | |
---|
699 | class egamma : public eEF { |
---|
700 | protected: |
---|
701 | //! Vector \f$\alpha\f$ |
---|
702 | vec alpha; |
---|
703 | //! Vector \f$\beta\f$ |
---|
704 | vec beta; |
---|
705 | public : |
---|
706 | //! \name Constructors |
---|
707 | //!@{ |
---|
708 | egamma () : eEF (), alpha ( 0 ), beta ( 0 ) {}; |
---|
709 | egamma ( const vec &a, const vec &b ) { |
---|
710 | set_parameters ( a, b ); |
---|
711 | }; |
---|
712 | void set_parameters ( const vec &a, const vec &b ) { |
---|
713 | alpha = a, beta = b; |
---|
714 | dim = alpha.length(); |
---|
715 | }; |
---|
716 | //!@} |
---|
717 | |
---|
718 | vec sample() const; |
---|
719 | double evallog ( const vec &val ) const; |
---|
720 | double lognc () const; |
---|
721 | //! Returns pointer to internal alpha. Potentially dengerous: use with care! |
---|
722 | vec& _alpha() { |
---|
723 | return alpha; |
---|
724 | } |
---|
725 | //! Returns pointer to internal beta. Potentially dengerous: use with care! |
---|
726 | vec& _beta() { |
---|
727 | return beta; |
---|
728 | } |
---|
729 | vec mean() const { |
---|
730 | return elem_div ( alpha, beta ); |
---|
731 | } |
---|
732 | vec variance() const { |
---|
733 | return elem_div ( alpha, elem_mult ( beta, beta ) ); |
---|
734 | } |
---|
735 | |
---|
736 | /*! Create Gamma density |
---|
737 | \f[ f(rv|rvc) = \Gamma(\alpha, \beta) \f] |
---|
738 | from structure |
---|
739 | \code |
---|
740 | class = 'egamma'; |
---|
741 | alpha = [...]; // vector of alpha |
---|
742 | beta = [...]; // vector of beta |
---|
743 | rv = RV({'name'}) // description of RV |
---|
744 | \endcode |
---|
745 | */ |
---|
746 | void from_setting ( const Setting &set ) { |
---|
747 | epdf::from_setting ( set ); // reads rv |
---|
748 | UI::get ( alpha, set, "alpha", UI::compulsory ); |
---|
749 | UI::get ( beta, set, "beta", UI::compulsory ); |
---|
750 | validate(); |
---|
751 | } |
---|
752 | void validate() { |
---|
753 | bdm_assert ( alpha.length() == beta.length(), "parameters do not match" ); |
---|
754 | dim = alpha.length(); |
---|
755 | } |
---|
756 | }; |
---|
757 | UIREGISTER ( egamma ); |
---|
758 | SHAREDPTR ( egamma ); |
---|
759 | |
---|
760 | /*! |
---|
761 | \brief Inverse-Gamma posterior density |
---|
762 | |
---|
763 | Multivariate inverse-Gamma density as product of independent univariate densities. |
---|
764 | \f[ |
---|
765 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
766 | \f] |
---|
767 | |
---|
768 | Vector \f$\beta\f$ has different meaning (in fact it is 1/beta as used in definition of iG) |
---|
769 | |
---|
770 | Inverse Gamma can be converted to Gamma using \f[ |
---|
771 | x\sim iG(a,b) => 1/x\sim G(a,1/b) |
---|
772 | \f] |
---|
773 | This relation is used in sampling. |
---|
774 | */ |
---|
775 | |
---|
776 | class eigamma : public egamma { |
---|
777 | protected: |
---|
778 | public : |
---|
779 | //! \name Constructors |
---|
780 | //! All constructors are inherited |
---|
781 | //!@{ |
---|
782 | //!@} |
---|
783 | |
---|
784 | vec sample() const { |
---|
785 | return 1.0 / egamma::sample(); |
---|
786 | }; |
---|
787 | //! Returns poiter to alpha and beta. Potentially dangerous: use with care! |
---|
788 | vec mean() const { |
---|
789 | return elem_div ( beta, alpha - 1 ); |
---|
790 | } |
---|
791 | vec variance() const { |
---|
792 | vec mea = mean(); |
---|
793 | return elem_div ( elem_mult ( mea, mea ), alpha - 2 ); |
---|
794 | } |
---|
795 | }; |
---|
796 | /* |
---|
797 | //! Weighted mixture of epdfs with external owned components. |
---|
798 | class emix : public epdf { |
---|
799 | protected: |
---|
800 | int n; |
---|
801 | vec &w; |
---|
802 | Array<epdf*> Coms; |
---|
803 | public: |
---|
804 | //! Default constructor |
---|
805 | emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {}; |
---|
806 | void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;} |
---|
807 | vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;}; |
---|
808 | }; |
---|
809 | */ |
---|
810 | |
---|
811 | //! Uniform distributed density on a rectangular support |
---|
812 | |
---|
813 | class euni: public epdf { |
---|
814 | protected: |
---|
815 | //! lower bound on support |
---|
816 | vec low; |
---|
817 | //! upper bound on support |
---|
818 | vec high; |
---|
819 | //! internal |
---|
820 | vec distance; |
---|
821 | //! normalizing coefficients |
---|
822 | double nk; |
---|
823 | //! cache of log( \c nk ) |
---|
824 | double lnk; |
---|
825 | public: |
---|
826 | //! \name Constructors |
---|
827 | //!@{ |
---|
828 | euni () : epdf () {} |
---|
829 | euni ( const vec &low0, const vec &high0 ) { |
---|
830 | set_parameters ( low0, high0 ); |
---|
831 | } |
---|
832 | void set_parameters ( const vec &low0, const vec &high0 ) { |
---|
833 | distance = high0 - low0; |
---|
834 | low = low0; |
---|
835 | high = high0; |
---|
836 | nk = prod ( 1.0 / distance ); |
---|
837 | lnk = log ( nk ); |
---|
838 | dim = low.length(); |
---|
839 | } |
---|
840 | //!@} |
---|
841 | |
---|
842 | double evallog ( const vec &val ) const { |
---|
843 | if ( any ( val < low ) && any ( val > high ) ) { |
---|
844 | return -inf; |
---|
845 | } else return lnk; |
---|
846 | } |
---|
847 | vec sample() const { |
---|
848 | vec smp ( dim ); |
---|
849 | #pragma omp critical |
---|
850 | UniRNG.sample_vector ( dim , smp ); |
---|
851 | return low + elem_mult ( distance, smp ); |
---|
852 | } |
---|
853 | //! set values of \c low and \c high |
---|
854 | vec mean() const { |
---|
855 | return ( high - low ) / 2.0; |
---|
856 | } |
---|
857 | vec variance() const { |
---|
858 | return ( pow ( high, 2 ) + pow ( low, 2 ) + elem_mult ( high, low ) ) / 3.0; |
---|
859 | } |
---|
860 | /*! Create Uniform density |
---|
861 | \f[ f(rv) = U(low,high) \f] |
---|
862 | from structure |
---|
863 | \code |
---|
864 | class = 'euni' |
---|
865 | high = [...]; // vector of upper bounds |
---|
866 | low = [...]; // vector of lower bounds |
---|
867 | rv = RV({'name'}); // description of RV |
---|
868 | \endcode |
---|
869 | */ |
---|
870 | void from_setting ( const Setting &set ) { |
---|
871 | epdf::from_setting ( set ); // reads rv and rvc |
---|
872 | |
---|
873 | UI::get ( high, set, "high", UI::compulsory ); |
---|
874 | UI::get ( low, set, "low", UI::compulsory ); |
---|
875 | set_parameters ( low, high ); |
---|
876 | validate(); |
---|
877 | } |
---|
878 | void validate() { |
---|
879 | bdm_assert ( high.length() == low.length(), "Incompatible high and low vectors" ); |
---|
880 | dim = high.length(); |
---|
881 | bdm_assert ( min ( distance ) > 0.0, "bad support" ); |
---|
882 | } |
---|
883 | }; |
---|
884 | UIREGISTER ( euni ); |
---|
885 | |
---|
886 | //! Uniform density with conditional mean value |
---|
887 | class mguni : public pdf_internal<euni> { |
---|
888 | //! function of the mean value |
---|
889 | shared_ptr<fnc> mean; |
---|
890 | //! distance from mean to both sides |
---|
891 | vec delta; |
---|
892 | public: |
---|
893 | void condition ( const vec &cond ) { |
---|
894 | vec mea = mean->eval ( cond ); |
---|
895 | iepdf.set_parameters ( mea - delta, mea + delta ); |
---|
896 | } |
---|
897 | //! load from |
---|
898 | void from_setting ( const Setting &set ) { |
---|
899 | pdf::from_setting ( set ); //reads rv and rvc |
---|
900 | UI::get ( delta, set, "delta", UI::compulsory ); |
---|
901 | mean = UI::build<fnc> ( set, "mean", UI::compulsory ); |
---|
902 | |
---|
903 | iepdf.set_parameters ( -delta, delta ); |
---|
904 | dimc = mean->dimensionc(); |
---|
905 | validate(); |
---|
906 | } |
---|
907 | }; |
---|
908 | UIREGISTER ( mguni ); |
---|
909 | /*! |
---|
910 | \brief Normal distributed linear function with linear function of mean value; |
---|
911 | |
---|
912 | Mean value \f$ \mu=A*\mbox{rvc}+\mu_0 \f$. |
---|
913 | */ |
---|
914 | template < class sq_T, template <typename> class TEpdf = enorm > |
---|
915 | class mlnorm : public pdf_internal< TEpdf<sq_T> > { |
---|
916 | protected: |
---|
917 | //! Internal epdf that arise by conditioning on \c rvc |
---|
918 | mat A; |
---|
919 | //! Constant additive term |
---|
920 | vec mu_const; |
---|
921 | // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? |
---|
922 | public: |
---|
923 | //! \name Constructors |
---|
924 | //!@{ |
---|
925 | mlnorm() : pdf_internal< TEpdf<sq_T> >() {}; |
---|
926 | mlnorm ( const mat &A, const vec &mu0, const sq_T &R ) : pdf_internal< TEpdf<sq_T> >() { |
---|
927 | set_parameters ( A, mu0, R ); |
---|
928 | } |
---|
929 | |
---|
930 | //! Set \c A and \c R |
---|
931 | void set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 ) { |
---|
932 | this->iepdf.set_parameters ( zeros ( A0.rows() ), R0 ); |
---|
933 | A = A0; |
---|
934 | mu_const = mu0; |
---|
935 | this->dimc = A0.cols(); |
---|
936 | } |
---|
937 | //!@} |
---|
938 | //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. |
---|
939 | void condition ( const vec &cond ) { |
---|
940 | this->iepdf._mu() = A * cond + mu_const; |
---|
941 | //R is already assigned; |
---|
942 | } |
---|
943 | |
---|
944 | //!access function |
---|
945 | const vec& _mu_const() const { |
---|
946 | return mu_const; |
---|
947 | } |
---|
948 | //!access function |
---|
949 | const mat& _A() const { |
---|
950 | return A; |
---|
951 | } |
---|
952 | //!access function |
---|
953 | mat _R() const { |
---|
954 | return this->iepdf._R().to_mat(); |
---|
955 | } |
---|
956 | //!access function |
---|
957 | sq_T __R() const { |
---|
958 | return this->iepdf._R(); |
---|
959 | } |
---|
960 | |
---|
961 | //! Debug stream |
---|
962 | template<typename sq_M> |
---|
963 | friend std::ostream &operator<< ( std::ostream &os, mlnorm<sq_M, enorm> &ml ); |
---|
964 | |
---|
965 | /*! Create Normal density with linear function of mean value |
---|
966 | \f[ f(rv|rvc) = N(A*rvc+const, R) \f] |
---|
967 | from structure |
---|
968 | \code |
---|
969 | class = 'mlnorm<ldmat>', (OR) 'mlnorm<chmat>', (OR) 'mlnorm<fsqmat>'; |
---|
970 | A = []; // matrix or vector of appropriate dimension |
---|
971 | const = []; // vector of constant term |
---|
972 | R = []; // square matrix of appropriate dimension |
---|
973 | \endcode |
---|
974 | */ |
---|
975 | void from_setting ( const Setting &set ) { |
---|
976 | pdf::from_setting ( set ); |
---|
977 | |
---|
978 | UI::get ( A, set, "A", UI::compulsory ); |
---|
979 | UI::get ( mu_const, set, "const", UI::compulsory ); |
---|
980 | mat R0; |
---|
981 | UI::get ( R0, set, "R", UI::compulsory ); |
---|
982 | set_parameters ( A, mu_const, R0 ); |
---|
983 | validate(); |
---|
984 | }; |
---|
985 | void to_setting (Setting &set) const { |
---|
986 | pdf::to_setting(set); |
---|
987 | UI::save ( A, set, "A"); |
---|
988 | UI::save ( mu_const, set, "const"); |
---|
989 | UI::save ( _R(), set, "R"); |
---|
990 | } |
---|
991 | void validate() { |
---|
992 | pdf_internal<TEpdf<sq_T> >::validate(); |
---|
993 | bdm_assert ( A.rows() == mu_const.length(), "mlnorm: A vs. mu mismatch" ); |
---|
994 | bdm_assert ( A.rows() == _R().rows(), "mlnorm: A vs. R mismatch" ); |
---|
995 | |
---|
996 | } |
---|
997 | }; |
---|
998 | UIREGISTER2 ( mlnorm, ldmat ); |
---|
999 | SHAREDPTR2 ( mlnorm, ldmat ); |
---|
1000 | UIREGISTER2 ( mlnorm, fsqmat ); |
---|
1001 | SHAREDPTR2 ( mlnorm, fsqmat ); |
---|
1002 | UIREGISTER2 ( mlnorm, chmat ); |
---|
1003 | SHAREDPTR2 ( mlnorm, chmat ); |
---|
1004 | |
---|
1005 | //! pdf with general function for mean value |
---|
1006 | template<class sq_T> |
---|
1007 | class mgnorm : public pdf_internal< enorm< sq_T > > { |
---|
1008 | private: |
---|
1009 | // vec μ WHY NOT? |
---|
1010 | shared_ptr<fnc> g; |
---|
1011 | |
---|
1012 | public: |
---|
1013 | //!default constructor |
---|
1014 | mgnorm() : pdf_internal<enorm<sq_T> >() { } |
---|
1015 | //!set mean function |
---|
1016 | inline void set_parameters ( const shared_ptr<fnc> &g0, const sq_T &R0 ); |
---|
1017 | inline void condition ( const vec &cond ); |
---|
1018 | |
---|
1019 | |
---|
1020 | /*! Create Normal density with given function of mean value |
---|
1021 | \f[ f(rv|rvc) = N( g(rvc), R) \f] |
---|
1022 | from structure |
---|
1023 | \code |
---|
1024 | class = 'mgnorm'; |
---|
1025 | g.class = 'fnc'; // function for mean value evolution |
---|
1026 | g._fields_of_fnc = ...; |
---|
1027 | |
---|
1028 | R = [1, 0; // covariance matrix |
---|
1029 | 0, 1]; |
---|
1030 | --OR -- |
---|
1031 | dR = [1, 1]; // diagonal of cavariance matrix |
---|
1032 | |
---|
1033 | rv = RV({'name'}) // description of RV |
---|
1034 | rvc = RV({'name'}) // description of RV in condition |
---|
1035 | \endcode |
---|
1036 | */ |
---|
1037 | |
---|
1038 | void from_setting ( const Setting &set ) { |
---|
1039 | pdf::from_setting ( set ); |
---|
1040 | shared_ptr<fnc> g = UI::build<fnc> ( set, "g", UI::compulsory ); |
---|
1041 | |
---|
1042 | mat R; |
---|
1043 | vec dR; |
---|
1044 | if ( UI::get ( dR, set, "dR" ) ) |
---|
1045 | R = diag ( dR ); |
---|
1046 | else |
---|
1047 | UI::get ( R, set, "R", UI::compulsory ); |
---|
1048 | |
---|
1049 | set_parameters ( g, R ); |
---|
1050 | validate(); |
---|
1051 | } |
---|
1052 | void validate() { |
---|
1053 | bdm_assert ( g->dimension() == this->dimension(), "incompatible function" ); |
---|
1054 | } |
---|
1055 | }; |
---|
1056 | |
---|
1057 | UIREGISTER2 ( mgnorm, chmat ); |
---|
1058 | SHAREDPTR2 ( mgnorm, chmat ); |
---|
1059 | |
---|
1060 | |
---|
1061 | /*! (Approximate) Student t density with linear function of mean value |
---|
1062 | |
---|
1063 | The internal epdf of this class is of the type of a Gaussian (enorm). |
---|
1064 | However, each conditioning is trying to assure the best possible approximation by taking into account the zeta function. See [] for reference. |
---|
1065 | |
---|
1066 | Perhaps a moment-matching technique? |
---|
1067 | */ |
---|
1068 | class mlstudent : public mlnorm<ldmat, enorm> { |
---|
1069 | protected: |
---|
1070 | //! Variable \f$ \Lambda \f$ from theory |
---|
1071 | ldmat Lambda; |
---|
1072 | //! Reference to variable \f$ R \f$ |
---|
1073 | ldmat &_R; |
---|
1074 | //! Variable \f$ R_e \f$ |
---|
1075 | ldmat Re; |
---|
1076 | public: |
---|
1077 | mlstudent () : mlnorm<ldmat, enorm> (), |
---|
1078 | Lambda (), _R ( iepdf._R() ) {} |
---|
1079 | //! constructor function |
---|
1080 | void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 ) { |
---|
1081 | iepdf.set_parameters ( mu0, R0 );// was Lambda, why? |
---|
1082 | A = A0; |
---|
1083 | mu_const = mu0; |
---|
1084 | Re = R0; |
---|
1085 | Lambda = Lambda0; |
---|
1086 | } |
---|
1087 | |
---|
1088 | void condition ( const vec &cond ); |
---|
1089 | |
---|
1090 | void validate() { |
---|
1091 | bdm_assert ( A.rows() == mu_const.length(), "mlstudent: A vs. mu mismatch" ); |
---|
1092 | bdm_assert ( _R.rows() == A.rows(), "mlstudent: A vs. R mismatch" ); |
---|
1093 | |
---|
1094 | } |
---|
1095 | }; |
---|
1096 | /*! |
---|
1097 | \brief Gamma random walk |
---|
1098 | |
---|
1099 | Mean value, \f$\mu\f$, of this density is given by \c rvc . |
---|
1100 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
1101 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
1102 | |
---|
1103 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
1104 | */ |
---|
1105 | class mgamma : public pdf_internal<egamma> { |
---|
1106 | protected: |
---|
1107 | |
---|
1108 | //! Constant \f$k\f$ |
---|
1109 | double k; |
---|
1110 | |
---|
1111 | //! cache of iepdf.beta |
---|
1112 | vec &_beta; |
---|
1113 | |
---|
1114 | public: |
---|
1115 | //! Constructor |
---|
1116 | mgamma() : pdf_internal<egamma>(), k ( 0 ), |
---|
1117 | _beta ( iepdf._beta() ) { |
---|
1118 | } |
---|
1119 | |
---|
1120 | //! Set value of \c k |
---|
1121 | void set_parameters ( double k, const vec &beta0 ); |
---|
1122 | |
---|
1123 | void condition ( const vec &val ) { |
---|
1124 | _beta = k / val; |
---|
1125 | }; |
---|
1126 | /*! Create Gamma density with conditional mean value |
---|
1127 | \f[ f(rv|rvc) = \Gamma(k, k/rvc) \f] |
---|
1128 | from structure |
---|
1129 | \code |
---|
1130 | class = 'mgamma'; |
---|
1131 | beta = [...]; // vector of initial alpha |
---|
1132 | k = 1.1; // multiplicative constant k |
---|
1133 | rv = RV({'name'}) // description of RV |
---|
1134 | rvc = RV({'name'}) // description of RV in condition |
---|
1135 | \endcode |
---|
1136 | */ |
---|
1137 | void from_setting ( const Setting &set ) { |
---|
1138 | pdf::from_setting ( set ); // reads rv and rvc |
---|
1139 | vec betatmp; // ugly but necessary |
---|
1140 | UI::get ( betatmp, set, "beta", UI::compulsory ); |
---|
1141 | UI::get ( k, set, "k", UI::compulsory ); |
---|
1142 | set_parameters ( k, betatmp ); |
---|
1143 | validate(); |
---|
1144 | } |
---|
1145 | void validate() { |
---|
1146 | pdf_internal<egamma>::validate(); |
---|
1147 | |
---|
1148 | dim = _beta.length(); |
---|
1149 | dimc = _beta.length(); |
---|
1150 | } |
---|
1151 | }; |
---|
1152 | UIREGISTER ( mgamma ); |
---|
1153 | SHAREDPTR ( mgamma ); |
---|
1154 | |
---|
1155 | /*! |
---|
1156 | \brief Inverse-Gamma random walk |
---|
1157 | |
---|
1158 | Mean value, \f$ \mu \f$, of this density is given by \c rvc . |
---|
1159 | Standard deviation of the random walk is proportional to one \f$ k \f$-th the mean. |
---|
1160 | This is achieved by setting \f$ \alpha=\mu/k^2+2 \f$ and \f$ \beta=\mu(\alpha-1)\f$. |
---|
1161 | |
---|
1162 | The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$. |
---|
1163 | */ |
---|
1164 | class migamma : public pdf_internal<eigamma> { |
---|
1165 | protected: |
---|
1166 | //! Constant \f$k\f$ |
---|
1167 | double k; |
---|
1168 | |
---|
1169 | //! cache of iepdf.alpha |
---|
1170 | vec &_alpha; |
---|
1171 | |
---|
1172 | //! cache of iepdf.beta |
---|
1173 | vec &_beta; |
---|
1174 | |
---|
1175 | public: |
---|
1176 | //! \name Constructors |
---|
1177 | //!@{ |
---|
1178 | migamma() : pdf_internal<eigamma>(), |
---|
1179 | k ( 0 ), |
---|
1180 | _alpha ( iepdf._alpha() ), |
---|
1181 | _beta ( iepdf._beta() ) { |
---|
1182 | } |
---|
1183 | |
---|
1184 | migamma ( const migamma &m ) : pdf_internal<eigamma>(), |
---|
1185 | k ( 0 ), |
---|
1186 | _alpha ( iepdf._alpha() ), |
---|
1187 | _beta ( iepdf._beta() ) { |
---|
1188 | } |
---|
1189 | //!@} |
---|
1190 | |
---|
1191 | //! Set value of \c k |
---|
1192 | void set_parameters ( int len, double k0 ) { |
---|
1193 | k = k0; |
---|
1194 | iepdf.set_parameters ( ( 1.0 / ( k*k ) + 2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ ); |
---|
1195 | dimc = dimension(); |
---|
1196 | }; |
---|
1197 | void condition ( const vec &val ) { |
---|
1198 | _beta = elem_mult ( val, ( _alpha - 1.0 ) ); |
---|
1199 | }; |
---|
1200 | }; |
---|
1201 | |
---|
1202 | |
---|
1203 | /*! |
---|
1204 | \brief Gamma random walk around a fixed point |
---|
1205 | |
---|
1206 | Mean value, \f$\mu\f$, of this density is given by a geometric combination of \c rvc and given fixed point, \f$p\f$. \f$l\f$ is the coefficient of the geometric combimation |
---|
1207 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
1208 | |
---|
1209 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
1210 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
1211 | |
---|
1212 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
1213 | */ |
---|
1214 | class mgamma_fix : public mgamma { |
---|
1215 | protected: |
---|
1216 | //! parameter l |
---|
1217 | double l; |
---|
1218 | //! reference vector |
---|
1219 | vec refl; |
---|
1220 | public: |
---|
1221 | //! Constructor |
---|
1222 | mgamma_fix () : mgamma (), refl () {}; |
---|
1223 | //! Set value of \c k |
---|
1224 | void set_parameters ( double k0 , vec ref0, double l0 ) { |
---|
1225 | mgamma::set_parameters ( k0, ref0 ); |
---|
1226 | refl = pow ( ref0, 1.0 - l0 ); |
---|
1227 | l = l0; |
---|
1228 | dimc = dimension(); |
---|
1229 | }; |
---|
1230 | |
---|
1231 | void condition ( const vec &val ) { |
---|
1232 | vec mean = elem_mult ( refl, pow ( val, l ) ); |
---|
1233 | _beta = k / mean; |
---|
1234 | }; |
---|
1235 | }; |
---|
1236 | |
---|
1237 | |
---|
1238 | /*! |
---|
1239 | \brief Inverse-Gamma random walk around a fixed point |
---|
1240 | |
---|
1241 | Mean value, \f$\mu\f$, of this density is given by a geometric combination of \c rvc and given fixed point, \f$p\f$. \f$l\f$ is the coefficient of the geometric combimation |
---|
1242 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
1243 | |
---|
1244 | ==== Check == vv = |
---|
1245 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
1246 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
1247 | |
---|
1248 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
1249 | */ |
---|
1250 | class migamma_ref : public migamma { |
---|
1251 | protected: |
---|
1252 | //! parameter l |
---|
1253 | double l; |
---|
1254 | //! reference vector |
---|
1255 | vec refl; |
---|
1256 | public: |
---|
1257 | //! Constructor |
---|
1258 | migamma_ref () : migamma (), refl () {}; |
---|
1259 | |
---|
1260 | //! Set value of \c k |
---|
1261 | void set_parameters ( double k0 , vec ref0, double l0 ) { |
---|
1262 | migamma::set_parameters ( ref0.length(), k0 ); |
---|
1263 | refl = pow ( ref0, 1.0 - l0 ); |
---|
1264 | l = l0; |
---|
1265 | dimc = dimension(); |
---|
1266 | }; |
---|
1267 | |
---|
1268 | void condition ( const vec &val ) { |
---|
1269 | vec mean = elem_mult ( refl, pow ( val, l ) ); |
---|
1270 | migamma::condition ( mean ); |
---|
1271 | }; |
---|
1272 | |
---|
1273 | |
---|
1274 | /*! Create inverse-Gamma density with conditional mean value |
---|
1275 | \f[ f(rv|rvc) = i\Gamma(k, k/(rvc^l \circ ref^{(1-l)}) \f] |
---|
1276 | from structure |
---|
1277 | \code |
---|
1278 | class = 'migamma_ref'; |
---|
1279 | ref = [1e-5; 1e-5; 1e-2 1e-3]; // reference vector |
---|
1280 | l = 0.999; // constant l |
---|
1281 | k = 0.1; // constant k |
---|
1282 | rv = RV({'name'}) // description of RV |
---|
1283 | rvc = RV({'name'}) // description of RV in condition |
---|
1284 | \endcode |
---|
1285 | */ |
---|
1286 | void from_setting ( const Setting &set ); |
---|
1287 | |
---|
1288 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
1289 | }; |
---|
1290 | |
---|
1291 | |
---|
1292 | UIREGISTER ( migamma_ref ); |
---|
1293 | SHAREDPTR ( migamma_ref ); |
---|
1294 | |
---|
1295 | /*! Log-Normal probability density |
---|
1296 | only allow diagonal covariances! |
---|
1297 | |
---|
1298 | Density of the form \f$ \log(x)\sim \mathcal{N}(\mu,\sigma^2) \f$ , i.e. |
---|
1299 | \f[ |
---|
1300 | x \sim \frac{1}{x\sigma\sqrt{2\pi}}\exp{-\frac{1}{2\sigma^2}(\log(x)-\mu)} |
---|
1301 | \f] |
---|
1302 | |
---|
1303 | Function from_setting loads mu and R in the same way as it does for enorm<>! |
---|
1304 | */ |
---|
1305 | class elognorm: public enorm<ldmat> { |
---|
1306 | public: |
---|
1307 | vec sample() const { |
---|
1308 | return exp ( enorm<ldmat>::sample() ); |
---|
1309 | }; |
---|
1310 | vec mean() const { |
---|
1311 | vec var = enorm<ldmat>::variance(); |
---|
1312 | return exp ( mu - 0.5*var ); |
---|
1313 | }; |
---|
1314 | |
---|
1315 | }; |
---|
1316 | |
---|
1317 | /*! |
---|
1318 | \brief Log-Normal random walk |
---|
1319 | |
---|
1320 | Mean value, \f$\mu\f$, is... |
---|
1321 | |
---|
1322 | */ |
---|
1323 | class mlognorm : public pdf_internal<elognorm> { |
---|
1324 | protected: |
---|
1325 | //! parameter 1/2*sigma^2 |
---|
1326 | double sig2; |
---|
1327 | |
---|
1328 | //! access |
---|
1329 | vec μ |
---|
1330 | public: |
---|
1331 | //! Constructor |
---|
1332 | mlognorm() : pdf_internal<elognorm>(), |
---|
1333 | sig2 ( 0 ), |
---|
1334 | mu ( iepdf._mu() ) { |
---|
1335 | } |
---|
1336 | |
---|
1337 | //! Set value of \c k |
---|
1338 | void set_parameters ( int size, double k ) { |
---|
1339 | sig2 = 0.5 * log ( k * k + 1 ); |
---|
1340 | iepdf.set_parameters ( zeros ( size ), 2*sig2*eye ( size ) ); |
---|
1341 | |
---|
1342 | dimc = size; |
---|
1343 | }; |
---|
1344 | |
---|
1345 | void condition ( const vec &val ) { |
---|
1346 | mu = log ( val ) - sig2;//elem_mult ( refl,pow ( val,l ) ); |
---|
1347 | }; |
---|
1348 | |
---|
1349 | /*! Create logNormal random Walk |
---|
1350 | \f[ f(rv|rvc) = log\mathcal{N}( \log(rvc)-0.5\log(k^2+1), k I) \f] |
---|
1351 | from structure |
---|
1352 | \code |
---|
1353 | class = 'mlognorm'; |
---|
1354 | k = 0.1; // "variance" k |
---|
1355 | mu0 = 0.1; // Initial value of mean |
---|
1356 | rv = RV({'name'}) // description of RV |
---|
1357 | rvc = RV({'name'}) // description of RV in condition |
---|
1358 | \endcode |
---|
1359 | */ |
---|
1360 | void from_setting ( const Setting &set ); |
---|
1361 | |
---|
1362 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
1363 | |
---|
1364 | }; |
---|
1365 | |
---|
1366 | UIREGISTER ( mlognorm ); |
---|
1367 | SHAREDPTR ( mlognorm ); |
---|
1368 | |
---|
1369 | /*! inverse Wishart density defined on Choleski decomposition |
---|
1370 | |
---|
1371 | */ |
---|
1372 | class eWishartCh : public epdf { |
---|
1373 | protected: |
---|
1374 | //! Upper-Triagle of Choleski decomposition of \f$ \Psi \f$ |
---|
1375 | chmat Y; |
---|
1376 | //! dimension of matrix \f$ \Psi \f$ |
---|
1377 | int p; |
---|
1378 | //! degrees of freedom \f$ \nu \f$ |
---|
1379 | double delta; |
---|
1380 | public: |
---|
1381 | //! Set internal structures |
---|
1382 | void set_parameters ( const mat &Y0, const double delta0 ) { |
---|
1383 | Y = chmat ( Y0 ); |
---|
1384 | delta = delta0; |
---|
1385 | p = Y.rows(); |
---|
1386 | dim = p * p; |
---|
1387 | } |
---|
1388 | //! Set internal structures |
---|
1389 | void set_parameters ( const chmat &Y0, const double delta0 ) { |
---|
1390 | Y = Y0; |
---|
1391 | delta = delta0; |
---|
1392 | p = Y.rows(); |
---|
1393 | dim = p * p; |
---|
1394 | } |
---|
1395 | //! Sample matrix argument |
---|
1396 | mat sample_mat() const { |
---|
1397 | mat X = zeros ( p, p ); |
---|
1398 | |
---|
1399 | //sample diagonal |
---|
1400 | for ( int i = 0; i < p; i++ ) { |
---|
1401 | GamRNG.setup ( 0.5* ( delta - i ) , 0.5 ); // no +1 !! index if from 0 |
---|
1402 | #pragma omp critical |
---|
1403 | X ( i, i ) = sqrt ( GamRNG() ); |
---|
1404 | } |
---|
1405 | //do the rest |
---|
1406 | for ( int i = 0; i < p; i++ ) { |
---|
1407 | for ( int j = i + 1; j < p; j++ ) { |
---|
1408 | #pragma omp critical |
---|
1409 | X ( i, j ) = NorRNG.sample(); |
---|
1410 | } |
---|
1411 | } |
---|
1412 | return X*Y._Ch();// return upper triangular part of the decomposition |
---|
1413 | } |
---|
1414 | |
---|
1415 | vec sample () const { |
---|
1416 | return vec ( sample_mat()._data(), p*p ); |
---|
1417 | } |
---|
1418 | |
---|
1419 | virtual vec mean() const NOT_IMPLEMENTED(0); |
---|
1420 | |
---|
1421 | //! return expected variance (not covariance!) |
---|
1422 | virtual vec variance() const NOT_IMPLEMENTED(0); |
---|
1423 | |
---|
1424 | virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
1425 | |
---|
1426 | //! fast access function y0 will be copied into Y.Ch. |
---|
1427 | void setY ( const mat &Ch0 ) { |
---|
1428 | copy_vector ( dim, Ch0._data(), Y._Ch()._data() ); |
---|
1429 | } |
---|
1430 | |
---|
1431 | //! fast access function y0 will be copied into Y.Ch. |
---|
1432 | void _setY ( const vec &ch0 ) { |
---|
1433 | copy_vector ( dim, ch0._data(), Y._Ch()._data() ); |
---|
1434 | } |
---|
1435 | |
---|
1436 | //! access function |
---|
1437 | const chmat& getY() const { |
---|
1438 | return Y; |
---|
1439 | } |
---|
1440 | }; |
---|
1441 | |
---|
1442 | //! Inverse Wishart on Choleski decomposition |
---|
1443 | /*! Being computed by conversion from `standard' Wishart |
---|
1444 | */ |
---|
1445 | class eiWishartCh: public epdf { |
---|
1446 | protected: |
---|
1447 | //! Internal instance of Wishart density |
---|
1448 | eWishartCh W; |
---|
1449 | //! size of Ch |
---|
1450 | int p; |
---|
1451 | //! parameter delta |
---|
1452 | double delta; |
---|
1453 | public: |
---|
1454 | //! constructor function |
---|
1455 | void set_parameters ( const mat &Y0, const double delta0 ) { |
---|
1456 | delta = delta0; |
---|
1457 | W.set_parameters ( inv ( Y0 ), delta0 ); |
---|
1458 | p = Y0.rows(); |
---|
1459 | } |
---|
1460 | |
---|
1461 | virtual void validate (){ |
---|
1462 | dim = W.dimension(); |
---|
1463 | } |
---|
1464 | |
---|
1465 | |
---|
1466 | vec sample() const { |
---|
1467 | mat iCh; |
---|
1468 | iCh = inv ( W.sample_mat() ); |
---|
1469 | return vec ( iCh._data(), dim ); |
---|
1470 | } |
---|
1471 | //! access function |
---|
1472 | void _setY ( const vec &y0 ) { |
---|
1473 | mat Ch ( p, p ); |
---|
1474 | mat iCh ( p, p ); |
---|
1475 | copy_vector ( dim, y0._data(), Ch._data() ); |
---|
1476 | |
---|
1477 | iCh = inv ( Ch ); |
---|
1478 | W.setY ( iCh ); |
---|
1479 | } |
---|
1480 | |
---|
1481 | virtual double evallog ( const vec &val ) const { |
---|
1482 | chmat X ( p ); |
---|
1483 | const chmat& Y = W.getY(); |
---|
1484 | |
---|
1485 | copy_vector ( p*p, val._data(), X._Ch()._data() ); |
---|
1486 | chmat iX ( p ); |
---|
1487 | X.inv ( iX ); |
---|
1488 | // compute |
---|
1489 | // \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)}, |
---|
1490 | mat M = Y.to_mat() * iX.to_mat(); |
---|
1491 | |
---|
1492 | double log1 = 0.5 * p * ( 2 * Y.logdet() ) - 0.5 * ( delta + p + 1 ) * ( 2 * X.logdet() ) - 0.5 * trace ( M ); |
---|
1493 | //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!! |
---|
1494 | |
---|
1495 | /* if (0) { |
---|
1496 | mat XX=X.to_mat(); |
---|
1497 | mat YY=Y.to_mat(); |
---|
1498 | |
---|
1499 | double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX)); |
---|
1500 | cout << log1 << "," << log2 << endl; |
---|
1501 | }*/ |
---|
1502 | return log1; |
---|
1503 | }; |
---|
1504 | |
---|
1505 | virtual vec mean() const NOT_IMPLEMENTED(0); |
---|
1506 | |
---|
1507 | //! return expected variance (not covariance!) |
---|
1508 | virtual vec variance() const NOT_IMPLEMENTED(0); |
---|
1509 | }; |
---|
1510 | |
---|
1511 | //! Random Walk on inverse Wishart |
---|
1512 | class rwiWishartCh : public pdf_internal<eiWishartCh> { |
---|
1513 | protected: |
---|
1514 | //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions |
---|
1515 | double sqd; |
---|
1516 | //!reference point for diagonal |
---|
1517 | vec refl; |
---|
1518 | //! power of the reference |
---|
1519 | double l; |
---|
1520 | //! dimension |
---|
1521 | int p; |
---|
1522 | |
---|
1523 | public: |
---|
1524 | rwiWishartCh() : sqd ( 0 ), l ( 0 ), p ( 0 ) {} |
---|
1525 | //! constructor function |
---|
1526 | void set_parameters ( int p0, double k, vec ref0, double l0 ) { |
---|
1527 | p = p0; |
---|
1528 | double delta = 2 / ( k * k ) + p + 3; |
---|
1529 | sqd = sqrt ( delta - p - 1 ); |
---|
1530 | l = l0; |
---|
1531 | refl = pow ( ref0, 1 - l ); |
---|
1532 | |
---|
1533 | iepdf.set_parameters ( eye ( p ), delta ); |
---|
1534 | dimc = iepdf.dimension(); |
---|
1535 | } |
---|
1536 | void condition ( const vec &c ) { |
---|
1537 | vec z = c; |
---|
1538 | int ri = 0; |
---|
1539 | for ( int i = 0; i < p*p; i += ( p + 1 ) ) {//trace diagonal element |
---|
1540 | z ( i ) = pow ( z ( i ), l ) * refl ( ri ); |
---|
1541 | ri++; |
---|
1542 | } |
---|
1543 | |
---|
1544 | iepdf._setY ( sqd*z ); |
---|
1545 | } |
---|
1546 | }; |
---|
1547 | |
---|
1548 | //! Switch between various resampling methods. |
---|
1549 | enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; |
---|
1550 | /*! |
---|
1551 | \brief Weighted empirical density |
---|
1552 | |
---|
1553 | Used e.g. in particle filters. |
---|
1554 | */ |
---|
1555 | class eEmp: public epdf { |
---|
1556 | protected : |
---|
1557 | //! Number of particles |
---|
1558 | int n; |
---|
1559 | //! Sample weights \f$w\f$ |
---|
1560 | vec w; |
---|
1561 | //! Samples \f$x^{(i)}, i=1..n\f$ |
---|
1562 | Array<vec> samples; |
---|
1563 | public: |
---|
1564 | //! \name Constructors |
---|
1565 | //!@{ |
---|
1566 | eEmp () : epdf (), w (), samples () {}; |
---|
1567 | //! copy constructor |
---|
1568 | eEmp ( const eEmp &e ) : epdf ( e ), w ( e.w ), samples ( e.samples ) {}; |
---|
1569 | //!@} |
---|
1570 | |
---|
1571 | //! Set samples and weights |
---|
1572 | void set_statistics ( const vec &w0, const epdf &pdf0 ); |
---|
1573 | //! Set samples and weights |
---|
1574 | void set_statistics ( const epdf &pdf0 , int n ) { |
---|
1575 | set_statistics ( ones ( n ) / n, pdf0 ); |
---|
1576 | }; |
---|
1577 | //! Set sample |
---|
1578 | void set_samples ( const epdf* pdf0 ); |
---|
1579 | //! Set sample |
---|
1580 | void set_parameters ( int n0, bool copy = true ) { |
---|
1581 | n = n0; |
---|
1582 | w.set_size ( n0, copy ); |
---|
1583 | samples.set_size ( n0, copy ); |
---|
1584 | }; |
---|
1585 | //! Set samples |
---|
1586 | void set_parameters ( const Array<vec> &Av ) { |
---|
1587 | n = Av.size(); |
---|
1588 | w = 1 / n * ones ( n ); |
---|
1589 | samples = Av; |
---|
1590 | }; |
---|
1591 | virtual void validate (){ |
---|
1592 | bdm_assert (samples.length()==w.length(),"samples and weigths are of different lengths"); |
---|
1593 | n = w.length(); |
---|
1594 | if (n>0) |
---|
1595 | pdf::dim = samples ( 0 ).length(); |
---|
1596 | } |
---|
1597 | //! Potentially dangerous, use with care. |
---|
1598 | vec& _w() { |
---|
1599 | return w; |
---|
1600 | }; |
---|
1601 | //! Potentially dangerous, use with care. |
---|
1602 | const vec& _w() const { |
---|
1603 | return w; |
---|
1604 | }; |
---|
1605 | //! access function |
---|
1606 | Array<vec>& _samples() { |
---|
1607 | return samples; |
---|
1608 | }; |
---|
1609 | //! access function |
---|
1610 | const vec& _sample ( int i ) const { |
---|
1611 | return samples ( i ); |
---|
1612 | }; |
---|
1613 | //! access function |
---|
1614 | const Array<vec>& _samples() const { |
---|
1615 | return samples; |
---|
1616 | }; |
---|
1617 | //! Function performs resampling, i.e. removal of low-weight samples and duplication of high-weight samples such that the new samples represent the same density. |
---|
1618 | //! The vector with indeces of new samples is returned in variable \c index. |
---|
1619 | void resample ( ivec &index, RESAMPLING_METHOD method = SYSTEMATIC ); |
---|
1620 | |
---|
1621 | //! Resampling without returning index of new particles. |
---|
1622 | void resample ( RESAMPLING_METHOD method = SYSTEMATIC ) { |
---|
1623 | ivec ind; |
---|
1624 | resample ( ind, method ); |
---|
1625 | }; |
---|
1626 | |
---|
1627 | //! inherited operation : NOT implemented |
---|
1628 | vec sample() const NOT_IMPLEMENTED(0); |
---|
1629 | |
---|
1630 | //! inherited operation : NOT implemented |
---|
1631 | double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
1632 | |
---|
1633 | vec mean() const { |
---|
1634 | vec pom = zeros ( dim ); |
---|
1635 | for ( int i = 0; i < n; i++ ) { |
---|
1636 | pom += samples ( i ) * w ( i ); |
---|
1637 | } |
---|
1638 | return pom; |
---|
1639 | } |
---|
1640 | vec variance() const { |
---|
1641 | vec pom = zeros ( dim ); |
---|
1642 | for ( int i = 0; i < n; i++ ) { |
---|
1643 | pom += pow ( samples ( i ), 2 ) * w ( i ); |
---|
1644 | } |
---|
1645 | return pom - pow ( mean(), 2 ); |
---|
1646 | } |
---|
1647 | //! For this class, qbounds are minimum and maximum value of the population! |
---|
1648 | void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const; |
---|
1649 | |
---|
1650 | void to_setting ( Setting &set ) const { |
---|
1651 | epdf::to_setting( set ); |
---|
1652 | UI::save ( samples, set, "samples" ); |
---|
1653 | UI::save ( w, set, "w" ); |
---|
1654 | } |
---|
1655 | |
---|
1656 | void from_setting ( const Setting &set ) { |
---|
1657 | epdf::from_setting( set ); |
---|
1658 | |
---|
1659 | UI::get( samples, set, "samples", UI::compulsory ); |
---|
1660 | UI::get ( w, set, "w", UI::compulsory ); |
---|
1661 | validate(); |
---|
1662 | } |
---|
1663 | |
---|
1664 | }; |
---|
1665 | UIREGISTER(eEmp); |
---|
1666 | |
---|
1667 | |
---|
1668 | //////////////////////// |
---|
1669 | |
---|
1670 | template<class sq_T> |
---|
1671 | void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) { |
---|
1672 | //Fixme test dimensions of mu0 and R0; |
---|
1673 | mu = mu0; |
---|
1674 | R = R0; |
---|
1675 | validate(); |
---|
1676 | }; |
---|
1677 | |
---|
1678 | template<class sq_T> |
---|
1679 | void enorm<sq_T>::from_setting ( const Setting &set ) { |
---|
1680 | epdf::from_setting ( set ); //reads rv |
---|
1681 | |
---|
1682 | UI::get ( mu, set, "mu", UI::compulsory ); |
---|
1683 | mat Rtmp;// necessary for conversion |
---|
1684 | UI::get ( Rtmp, set, "R", UI::compulsory ); |
---|
1685 | R = Rtmp; // conversion |
---|
1686 | validate(); |
---|
1687 | } |
---|
1688 | |
---|
1689 | template<class sq_T> |
---|
1690 | void enorm<sq_T>::to_setting ( Setting &set ) const { |
---|
1691 | epdf::to_setting ( set ); //reads rv |
---|
1692 | UI::save ( mu, set, "mu"); |
---|
1693 | UI::save ( R.to_mat(), set, "R"); |
---|
1694 | } |
---|
1695 | |
---|
1696 | template<class sq_T> |
---|
1697 | void enorm<sq_T>::dupdate ( mat &v, double nu ) { |
---|
1698 | // |
---|
1699 | }; |
---|
1700 | |
---|
1701 | // template<class sq_T> |
---|
1702 | // void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) { |
---|
1703 | // // |
---|
1704 | // }; |
---|
1705 | |
---|
1706 | template<class sq_T> |
---|
1707 | vec enorm<sq_T>::sample() const { |
---|
1708 | vec x ( dim ); |
---|
1709 | #pragma omp critical |
---|
1710 | NorRNG.sample_vector ( dim, x ); |
---|
1711 | vec smp = R.sqrt_mult ( x ); |
---|
1712 | |
---|
1713 | smp += mu; |
---|
1714 | return smp; |
---|
1715 | }; |
---|
1716 | |
---|
1717 | // template<class sq_T> |
---|
1718 | // double enorm<sq_T>::eval ( const vec &val ) const { |
---|
1719 | // double pdfl,e; |
---|
1720 | // pdfl = evallog ( val ); |
---|
1721 | // e = exp ( pdfl ); |
---|
1722 | // return e; |
---|
1723 | // }; |
---|
1724 | |
---|
1725 | template<class sq_T> |
---|
1726 | double enorm<sq_T>::evallog_nn ( const vec &val ) const { |
---|
1727 | // 1.83787706640935 = log(2pi) |
---|
1728 | double tmp = -0.5 * ( R.invqform ( mu - val ) );// - lognc(); |
---|
1729 | return tmp; |
---|
1730 | }; |
---|
1731 | |
---|
1732 | template<class sq_T> |
---|
1733 | inline double enorm<sq_T>::lognc () const { |
---|
1734 | // 1.83787706640935 = log(2pi) |
---|
1735 | double tmp = 0.5 * ( R.cols() * 1.83787706640935 + R.logdet() ); |
---|
1736 | return tmp; |
---|
1737 | }; |
---|
1738 | |
---|
1739 | |
---|
1740 | // template<class sq_T> |
---|
1741 | // vec mlnorm<sq_T>::samplecond (const vec &cond, double &lik ) { |
---|
1742 | // this->condition ( cond ); |
---|
1743 | // vec smp = epdf.sample(); |
---|
1744 | // lik = epdf.eval ( smp ); |
---|
1745 | // return smp; |
---|
1746 | // } |
---|
1747 | |
---|
1748 | // template<class sq_T> |
---|
1749 | // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) { |
---|
1750 | // int i; |
---|
1751 | // int dim = rv.count(); |
---|
1752 | // mat Smp ( dim,n ); |
---|
1753 | // vec smp ( dim ); |
---|
1754 | // this->condition ( cond ); |
---|
1755 | // |
---|
1756 | // for ( i=0; i<n; i++ ) { |
---|
1757 | // smp = epdf.sample(); |
---|
1758 | // lik ( i ) = epdf.eval ( smp ); |
---|
1759 | // Smp.set_col ( i ,smp ); |
---|
1760 | // } |
---|
1761 | // |
---|
1762 | // return Smp; |
---|
1763 | // } |
---|
1764 | |
---|
1765 | |
---|
1766 | template<class sq_T> |
---|
1767 | shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const { |
---|
1768 | enorm<sq_T> *tmp = new enorm<sq_T> (); |
---|
1769 | shared_ptr<epdf> narrow ( tmp ); |
---|
1770 | marginal ( rvn, *tmp ); |
---|
1771 | return narrow; |
---|
1772 | } |
---|
1773 | |
---|
1774 | template<class sq_T> |
---|
1775 | void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const { |
---|
1776 | bdm_assert ( isnamed(), "rv description is not assigned" ); |
---|
1777 | ivec irvn = rvn.dataind ( rv ); |
---|
1778 | |
---|
1779 | sq_T Rn ( R, irvn ); // select rows and columns of R |
---|
1780 | |
---|
1781 | target.set_rv ( rvn ); |
---|
1782 | target.set_parameters ( mu ( irvn ), Rn ); |
---|
1783 | } |
---|
1784 | |
---|
1785 | template<class sq_T> |
---|
1786 | shared_ptr<pdf> enorm<sq_T>::condition ( const RV &rvn ) const { |
---|
1787 | mlnorm<sq_T> *tmp = new mlnorm<sq_T> (); |
---|
1788 | shared_ptr<pdf> narrow ( tmp ); |
---|
1789 | condition ( rvn, *tmp ); |
---|
1790 | return narrow; |
---|
1791 | } |
---|
1792 | |
---|
1793 | template<class sq_T> |
---|
1794 | void enorm<sq_T>::condition ( const RV &rvn, pdf &target ) const { |
---|
1795 | typedef mlnorm<sq_T> TMlnorm; |
---|
1796 | |
---|
1797 | bdm_assert ( isnamed(), "rvs are not assigned" ); |
---|
1798 | TMlnorm &uptarget = dynamic_cast<TMlnorm &> ( target ); |
---|
1799 | |
---|
1800 | RV rvc = rv.subt ( rvn ); |
---|
1801 | bdm_assert ( ( rvc._dsize() + rvn._dsize() == rv._dsize() ), "wrong rvn" ); |
---|
1802 | //Permutation vector of the new R |
---|
1803 | ivec irvn = rvn.dataind ( rv ); |
---|
1804 | ivec irvc = rvc.dataind ( rv ); |
---|
1805 | ivec perm = concat ( irvn , irvc ); |
---|
1806 | sq_T Rn ( R, perm ); |
---|
1807 | |
---|
1808 | //fixme - could this be done in general for all sq_T? |
---|
1809 | mat S = Rn.to_mat(); |
---|
1810 | //fixme |
---|
1811 | int n = rvn._dsize() - 1; |
---|
1812 | int end = R.rows() - 1; |
---|
1813 | mat S11 = S.get ( 0, n, 0, n ); |
---|
1814 | mat S12 = S.get ( 0, n , rvn._dsize(), end ); |
---|
1815 | mat S22 = S.get ( rvn._dsize(), end, rvn._dsize(), end ); |
---|
1816 | |
---|
1817 | vec mu1 = mu ( irvn ); |
---|
1818 | vec mu2 = mu ( irvc ); |
---|
1819 | mat A = S12 * inv ( S22 ); |
---|
1820 | sq_T R_n ( S11 - A *S12.T() ); |
---|
1821 | |
---|
1822 | uptarget.set_rv ( rvn ); |
---|
1823 | uptarget.set_rvc ( rvc ); |
---|
1824 | uptarget.set_parameters ( A, mu1 - A*mu2, R_n ); |
---|
1825 | uptarget.validate(); |
---|
1826 | } |
---|
1827 | |
---|
1828 | //// |
---|
1829 | /////// |
---|
1830 | template<class sq_T> |
---|
1831 | void mgnorm<sq_T >::set_parameters ( const shared_ptr<fnc> &g0, const sq_T &R0 ) { |
---|
1832 | g = g0; |
---|
1833 | this->dim = g->dimension(); |
---|
1834 | this->dimc = g->dimensionc(); |
---|
1835 | this->iepdf.set_parameters ( zeros ( g->dimension() ), R0 ); |
---|
1836 | } |
---|
1837 | |
---|
1838 | template<class sq_T> |
---|
1839 | void mgnorm<sq_T >::condition ( const vec &cond ) { |
---|
1840 | this->iepdf._mu() = g->eval ( cond ); |
---|
1841 | }; |
---|
1842 | |
---|
1843 | //! \todo unify this stuff with to_string() |
---|
1844 | template<class sq_T> |
---|
1845 | std::ostream &operator<< ( std::ostream &os, mlnorm<sq_T> &ml ) { |
---|
1846 | os << "A:" << ml.A << endl; |
---|
1847 | os << "mu:" << ml.mu_const << endl; |
---|
1848 | os << "R:" << ml._R() << endl; |
---|
1849 | return os; |
---|
1850 | }; |
---|
1851 | |
---|
1852 | } |
---|
1853 | #endif //EF_H |
---|