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