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 | |
---|
25 | //! Global Uniform_RNG |
---|
26 | extern Uniform_RNG UniRNG; |
---|
27 | //! Global Normal_RNG |
---|
28 | extern Normal_RNG NorRNG; |
---|
29 | //! Global Gamma_RNG |
---|
30 | extern Gamma_RNG GamRNG; |
---|
31 | |
---|
32 | /*! |
---|
33 | * \brief General conjugate exponential family posterior density. |
---|
34 | |
---|
35 | * More?... |
---|
36 | */ |
---|
37 | |
---|
38 | class eEF : public epdf |
---|
39 | { |
---|
40 | public: |
---|
41 | // eEF() :epdf() {}; |
---|
42 | //! default constructor |
---|
43 | eEF () : epdf () {}; |
---|
44 | //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ |
---|
45 | virtual double lognc() const = 0; |
---|
46 | //!TODO decide if it is really needed |
---|
47 | virtual void dupdate (mat &v) {it_error ("Not implemented");}; |
---|
48 | //!Evaluate normalized log-probability |
---|
49 | virtual double evallog_nn (const vec &val) const{it_error ("Not implemented");return 0.0;}; |
---|
50 | //!Evaluate normalized log-probability |
---|
51 | virtual double evallog (const vec &val) const { |
---|
52 | double tmp; |
---|
53 | tmp = evallog_nn (val) - lognc(); |
---|
54 | // it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); |
---|
55 | return tmp; |
---|
56 | } |
---|
57 | //!Evaluate normalized log-probability for many samples |
---|
58 | virtual vec evallog_m (const mat &Val) const { |
---|
59 | vec x (Val.cols()); |
---|
60 | for (int i = 0;i < Val.cols();i++) {x (i) = evallog_nn (Val.get_col (i)) ;} |
---|
61 | return x -lognc(); |
---|
62 | } |
---|
63 | //!Evaluate normalized log-probability for many samples |
---|
64 | virtual vec evallog_m (const Array<vec> &Val) const { |
---|
65 | vec x (Val.length()); |
---|
66 | for (int i = 0;i < Val.length();i++) {x (i) = evallog_nn (Val (i)) ;} |
---|
67 | return x -lognc(); |
---|
68 | } |
---|
69 | //!Power of the density, used e.g. to flatten the density |
---|
70 | virtual void pow (double p) {it_error ("Not implemented");}; |
---|
71 | }; |
---|
72 | |
---|
73 | |
---|
74 | //! Estimator for Exponential family |
---|
75 | class BMEF : public BM |
---|
76 | { |
---|
77 | protected: |
---|
78 | //! forgetting factor |
---|
79 | double frg; |
---|
80 | //! cached value of lognc() in the previous step (used in evaluation of \c ll ) |
---|
81 | double last_lognc; |
---|
82 | public: |
---|
83 | //! Default constructor (=empty constructor) |
---|
84 | BMEF (double frg0 = 1.0) : BM (), frg (frg0) {} |
---|
85 | //! Copy constructor |
---|
86 | BMEF (const BMEF &B) : BM (B), frg (B.frg), last_lognc (B.last_lognc) {} |
---|
87 | //!get statistics from another model |
---|
88 | virtual void set_statistics (const BMEF* BM0) {it_error ("Not implemented");}; |
---|
89 | //! Weighted update of sufficient statistics (Bayes rule) |
---|
90 | virtual void bayes (const vec &data, const double w) {}; |
---|
91 | //original Bayes |
---|
92 | void bayes (const vec &dt); |
---|
93 | //!Flatten the posterior according to the given BMEF (of the same type!) |
---|
94 | virtual void flatten (const BMEF * B) {it_error ("Not implemented");} |
---|
95 | //!Flatten the posterior as if to keep nu0 data |
---|
96 | // virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );} |
---|
97 | |
---|
98 | BMEF* _copy_ () const {it_error ("function _copy_ not implemented for this BM"); return NULL;}; |
---|
99 | }; |
---|
100 | |
---|
101 | template<class sq_T, template <typename> class TEpdf> |
---|
102 | class mlnorm; |
---|
103 | |
---|
104 | /*! |
---|
105 | * \brief Gaussian density with positive definite (decomposed) covariance matrix. |
---|
106 | |
---|
107 | * More?... |
---|
108 | */ |
---|
109 | template<class sq_T> |
---|
110 | class enorm : public eEF |
---|
111 | { |
---|
112 | protected: |
---|
113 | //! mean value |
---|
114 | vec mu; |
---|
115 | //! Covariance matrix in decomposed form |
---|
116 | sq_T R; |
---|
117 | public: |
---|
118 | //!\name Constructors |
---|
119 | //!@{ |
---|
120 | |
---|
121 | enorm () : eEF (), mu (), R () {}; |
---|
122 | enorm (const vec &mu, const sq_T &R) {set_parameters (mu, R);} |
---|
123 | void set_parameters (const vec &mu, const sq_T &R); |
---|
124 | void from_setting (const Setting &root); |
---|
125 | void validate() { |
---|
126 | it_assert (mu.length() == R.rows(), "parameters mismatch"); |
---|
127 | dim = mu.length(); |
---|
128 | } |
---|
129 | //!@} |
---|
130 | |
---|
131 | //! \name Mathematical operations |
---|
132 | //!@{ |
---|
133 | |
---|
134 | //! dupdate in exponential form (not really handy) |
---|
135 | void dupdate (mat &v, double nu = 1.0); |
---|
136 | |
---|
137 | vec sample() const; |
---|
138 | |
---|
139 | double evallog_nn (const vec &val) const; |
---|
140 | double lognc () const; |
---|
141 | vec mean() const {return mu;} |
---|
142 | vec variance() const {return diag (R.to_mat());} |
---|
143 | // mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why? |
---|
144 | shared_ptr<mpdf> condition ( const RV &rvn ) const; |
---|
145 | |
---|
146 | // target not typed to mlnorm<sq_T, enorm<sq_T> > & |
---|
147 | // because that doesn't compile (perhaps because we |
---|
148 | // haven't finished defining enorm yet), but the type |
---|
149 | // is required |
---|
150 | void condition ( const RV &rvn, mpdf &target ) const; |
---|
151 | |
---|
152 | shared_ptr<epdf> marginal (const RV &rvn ) const; |
---|
153 | void marginal ( const RV &rvn, enorm<sq_T> &target ) const; |
---|
154 | //!@} |
---|
155 | |
---|
156 | //! \name Access to attributes |
---|
157 | //!@{ |
---|
158 | |
---|
159 | vec& _mu() {return mu;} |
---|
160 | void set_mu (const vec mu0) { mu = mu0;} |
---|
161 | sq_T& _R() {return R;} |
---|
162 | const sq_T& _R() const {return R;} |
---|
163 | //!@} |
---|
164 | |
---|
165 | }; |
---|
166 | UIREGISTER (enorm<chmat>); |
---|
167 | UIREGISTER (enorm<ldmat>); |
---|
168 | UIREGISTER (enorm<fsqmat>); |
---|
169 | |
---|
170 | |
---|
171 | /*! |
---|
172 | * \brief Gauss-inverse-Wishart density stored in LD form |
---|
173 | |
---|
174 | * For \f$p\f$-variate densities, given rv.count() should be \f$p\times\f$ V.rows(). |
---|
175 | * |
---|
176 | */ |
---|
177 | class egiw : public eEF |
---|
178 | { |
---|
179 | protected: |
---|
180 | //! Extended information matrix of sufficient statistics |
---|
181 | ldmat V; |
---|
182 | //! Number of data records (degrees of freedom) of sufficient statistics |
---|
183 | double nu; |
---|
184 | //! Dimension of the output |
---|
185 | int dimx; |
---|
186 | //! Dimension of the regressor |
---|
187 | int nPsi; |
---|
188 | public: |
---|
189 | //!\name Constructors |
---|
190 | //!@{ |
---|
191 | egiw() : eEF() {}; |
---|
192 | egiw (int dimx0, ldmat V0, double nu0 = -1.0) : eEF() {set_parameters (dimx0, V0, nu0);}; |
---|
193 | |
---|
194 | void set_parameters (int dimx0, ldmat V0, double nu0 = -1.0) { |
---|
195 | dimx = dimx0; |
---|
196 | nPsi = V0.rows() - dimx; |
---|
197 | dim = dimx * (dimx + nPsi); // size(R) + size(Theta) |
---|
198 | |
---|
199 | V = V0; |
---|
200 | if (nu0 < 0) { |
---|
201 | nu = 0.1 + nPsi + 2 * dimx + 2; // +2 assures finite expected value of R |
---|
202 | // terms before that are sufficient for finite normalization |
---|
203 | } else { |
---|
204 | nu = nu0; |
---|
205 | } |
---|
206 | } |
---|
207 | //!@} |
---|
208 | |
---|
209 | vec sample() const; |
---|
210 | vec mean() const; |
---|
211 | vec variance() const; |
---|
212 | |
---|
213 | //! LS estimate of \f$\theta\f$ |
---|
214 | vec est_theta() const; |
---|
215 | |
---|
216 | //! Covariance of the LS estimate |
---|
217 | ldmat est_theta_cov() const; |
---|
218 | |
---|
219 | void mean_mat (mat &M, mat&R) const; |
---|
220 | //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] |
---|
221 | double evallog_nn (const vec &val) const; |
---|
222 | double lognc () const; |
---|
223 | void pow (double p) {V *= p;nu *= p;}; |
---|
224 | |
---|
225 | //! \name Access attributes |
---|
226 | //!@{ |
---|
227 | |
---|
228 | ldmat& _V() {return V;} |
---|
229 | const ldmat& _V() const {return V;} |
---|
230 | double& _nu() {return nu;} |
---|
231 | const double& _nu() const {return nu;} |
---|
232 | void from_setting (const Setting &set) { |
---|
233 | UI::get (nu, set, "nu", UI::compulsory); |
---|
234 | UI::get (dimx, set, "dimx", UI::compulsory); |
---|
235 | mat V; |
---|
236 | UI::get (V, set, "V", UI::compulsory); |
---|
237 | set_parameters (dimx, V, nu); |
---|
238 | shared_ptr<RV> rv = UI::build<RV> (set, "rv", UI::compulsory); |
---|
239 | set_rv (*rv); |
---|
240 | } |
---|
241 | //!@} |
---|
242 | }; |
---|
243 | UIREGISTER (egiw); |
---|
244 | |
---|
245 | /*! \brief Dirichlet posterior density |
---|
246 | |
---|
247 | Continuous Dirichlet density of \f$n\f$-dimensional variable \f$x\f$ |
---|
248 | \f[ |
---|
249 | f(x|\beta) = \frac{\Gamma[\gamma]}{\prod_{i=1}^{n}\Gamma(\beta_i)} \prod_{i=1}^{n}x_i^{\beta_i-1} |
---|
250 | \f] |
---|
251 | where \f$\gamma=\sum_i \beta_i\f$. |
---|
252 | */ |
---|
253 | class eDirich: public eEF |
---|
254 | { |
---|
255 | protected: |
---|
256 | //!sufficient statistics |
---|
257 | vec beta; |
---|
258 | public: |
---|
259 | //!\name Constructors |
---|
260 | //!@{ |
---|
261 | |
---|
262 | eDirich () : eEF () {}; |
---|
263 | eDirich (const eDirich &D0) : eEF () {set_parameters (D0.beta);}; |
---|
264 | eDirich (const vec &beta0) {set_parameters (beta0);}; |
---|
265 | void set_parameters (const vec &beta0) { |
---|
266 | beta = beta0; |
---|
267 | dim = beta.length(); |
---|
268 | } |
---|
269 | //!@} |
---|
270 | |
---|
271 | vec sample() const {it_error ("Not implemented");return vec_1 (0.0);}; |
---|
272 | vec mean() const {return beta / sum (beta);}; |
---|
273 | vec variance() const {double gamma = sum (beta); return elem_mult (beta, (beta + 1)) / (gamma* (gamma + 1));} |
---|
274 | //! In this instance, val is ... |
---|
275 | double evallog_nn (const vec &val) const { |
---|
276 | double tmp; tmp = (beta - 1) * log (val); |
---|
277 | // it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); |
---|
278 | return tmp; |
---|
279 | }; |
---|
280 | double lognc () const { |
---|
281 | double tmp; |
---|
282 | double gam = sum (beta); |
---|
283 | double lgb = 0.0; |
---|
284 | for (int i = 0;i < beta.length();i++) {lgb += lgamma (beta (i));} |
---|
285 | tmp = lgb - lgamma (gam); |
---|
286 | // it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); |
---|
287 | return tmp; |
---|
288 | }; |
---|
289 | //!access function |
---|
290 | vec& _beta() {return beta;} |
---|
291 | //!Set internal parameters |
---|
292 | }; |
---|
293 | |
---|
294 | //! \brief Estimator for Multinomial density |
---|
295 | class multiBM : public BMEF |
---|
296 | { |
---|
297 | protected: |
---|
298 | //! Conjugate prior and posterior |
---|
299 | eDirich est; |
---|
300 | //! Pointer inside est to sufficient statistics |
---|
301 | vec β |
---|
302 | public: |
---|
303 | //!Default constructor |
---|
304 | multiBM () : BMEF (), est (), beta (est._beta()) { |
---|
305 | if (beta.length() > 0) {last_lognc = est.lognc();} |
---|
306 | else{last_lognc = 0.0;} |
---|
307 | } |
---|
308 | //!Copy constructor |
---|
309 | multiBM (const multiBM &B) : BMEF (B), est (B.est), beta (est._beta()) {} |
---|
310 | //! Sets sufficient statistics to match that of givefrom mB0 |
---|
311 | void set_statistics (const BM* mB0) {const multiBM* mB = dynamic_cast<const multiBM*> (mB0); beta = mB->beta;} |
---|
312 | void bayes (const vec &dt) { |
---|
313 | if (frg < 1.0) {beta *= frg;last_lognc = est.lognc();} |
---|
314 | beta += dt; |
---|
315 | if (evalll) {ll = est.lognc() - last_lognc;} |
---|
316 | } |
---|
317 | double logpred (const vec &dt) const { |
---|
318 | eDirich pred (est); |
---|
319 | vec &beta = pred._beta(); |
---|
320 | |
---|
321 | double lll; |
---|
322 | if (frg < 1.0) |
---|
323 | {beta *= frg;lll = pred.lognc();} |
---|
324 | else |
---|
325 | if (evalll) {lll = last_lognc;} |
---|
326 | else{lll = pred.lognc();} |
---|
327 | |
---|
328 | beta += dt; |
---|
329 | return pred.lognc() - lll; |
---|
330 | } |
---|
331 | void flatten (const BMEF* B) { |
---|
332 | const multiBM* E = dynamic_cast<const multiBM*> (B); |
---|
333 | // sum(beta) should be equal to sum(B.beta) |
---|
334 | const vec &Eb = E->beta;//const_cast<multiBM*> ( E )->_beta(); |
---|
335 | beta *= (sum (Eb) / sum (beta)); |
---|
336 | if (evalll) {last_lognc = est.lognc();} |
---|
337 | } |
---|
338 | const epdf& posterior() const {return est;}; |
---|
339 | const eDirich* _e() const {return &est;}; |
---|
340 | void set_parameters (const vec &beta0) { |
---|
341 | est.set_parameters (beta0); |
---|
342 | if (evalll) {last_lognc = est.lognc();} |
---|
343 | } |
---|
344 | }; |
---|
345 | |
---|
346 | /*! |
---|
347 | \brief Gamma posterior density |
---|
348 | |
---|
349 | Multivariate Gamma density as product of independent univariate densities. |
---|
350 | \f[ |
---|
351 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
352 | \f] |
---|
353 | */ |
---|
354 | |
---|
355 | class egamma : public eEF |
---|
356 | { |
---|
357 | protected: |
---|
358 | //! Vector \f$\alpha\f$ |
---|
359 | vec alpha; |
---|
360 | //! Vector \f$\beta\f$ |
---|
361 | vec beta; |
---|
362 | public : |
---|
363 | //! \name Constructors |
---|
364 | //!@{ |
---|
365 | egamma () : eEF (), alpha (0), beta (0) {}; |
---|
366 | egamma (const vec &a, const vec &b) {set_parameters (a, b);}; |
---|
367 | void set_parameters (const vec &a, const vec &b) {alpha = a, beta = b;dim = alpha.length();}; |
---|
368 | //!@} |
---|
369 | |
---|
370 | vec sample() const; |
---|
371 | //! TODO: is it used anywhere? |
---|
372 | // mat sample ( int N ) const; |
---|
373 | double evallog (const vec &val) const; |
---|
374 | double lognc () const; |
---|
375 | //! Returns poiter to alpha and beta. Potentially dengerous: use with care! |
---|
376 | vec& _alpha() {return alpha;} |
---|
377 | vec& _beta() {return beta;} |
---|
378 | vec mean() const {return elem_div (alpha, beta);} |
---|
379 | vec variance() const {return elem_div (alpha, elem_mult (beta, beta)); } |
---|
380 | |
---|
381 | //! Load from structure with elements: |
---|
382 | //! \code |
---|
383 | //! { alpha = [...]; // vector of alpha |
---|
384 | //! beta = [...]; // vector of beta |
---|
385 | //! rv = {class="RV",...} // description |
---|
386 | //! } |
---|
387 | //! \endcode |
---|
388 | //!@} |
---|
389 | void from_setting (const Setting &set) { |
---|
390 | epdf::from_setting (set); // reads rv |
---|
391 | UI::get (alpha, set, "alpha", UI::compulsory); |
---|
392 | UI::get (beta, set, "beta", UI::compulsory); |
---|
393 | validate(); |
---|
394 | } |
---|
395 | void validate() { |
---|
396 | it_assert (alpha.length() == beta.length(), "parameters do not match"); |
---|
397 | dim = alpha.length(); |
---|
398 | } |
---|
399 | }; |
---|
400 | UIREGISTER (egamma); |
---|
401 | /*! |
---|
402 | \brief Inverse-Gamma posterior density |
---|
403 | |
---|
404 | Multivariate inverse-Gamma density as product of independent univariate densities. |
---|
405 | \f[ |
---|
406 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
407 | \f] |
---|
408 | |
---|
409 | Vector \f$\beta\f$ has different meaning (in fact it is 1/beta as used in definition of iG) |
---|
410 | |
---|
411 | Inverse Gamma can be converted to Gamma using \f[ |
---|
412 | x\sim iG(a,b) => 1/x\sim G(a,1/b) |
---|
413 | \f] |
---|
414 | This relation is used in sampling. |
---|
415 | */ |
---|
416 | |
---|
417 | class eigamma : public egamma |
---|
418 | { |
---|
419 | protected: |
---|
420 | public : |
---|
421 | //! \name Constructors |
---|
422 | //! All constructors are inherited |
---|
423 | //!@{ |
---|
424 | //!@} |
---|
425 | |
---|
426 | vec sample() const {return 1.0 / egamma::sample();}; |
---|
427 | //! Returns poiter to alpha and beta. Potentially dangerous: use with care! |
---|
428 | vec mean() const {return elem_div (beta, alpha - 1);} |
---|
429 | vec variance() const {vec mea = mean(); return elem_div (elem_mult (mea, mea), alpha - 2);} |
---|
430 | }; |
---|
431 | /* |
---|
432 | //! Weighted mixture of epdfs with external owned components. |
---|
433 | class emix : public epdf { |
---|
434 | protected: |
---|
435 | int n; |
---|
436 | vec &w; |
---|
437 | Array<epdf*> Coms; |
---|
438 | public: |
---|
439 | //! Default constructor |
---|
440 | emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {}; |
---|
441 | void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;} |
---|
442 | vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;}; |
---|
443 | vec sample() {it_error ( "Not implemented" );return 0;} |
---|
444 | }; |
---|
445 | */ |
---|
446 | |
---|
447 | //! Uniform distributed density on a rectangular support |
---|
448 | |
---|
449 | class euni: public epdf |
---|
450 | { |
---|
451 | protected: |
---|
452 | //! lower bound on support |
---|
453 | vec low; |
---|
454 | //! upper bound on support |
---|
455 | vec high; |
---|
456 | //! internal |
---|
457 | vec distance; |
---|
458 | //! normalizing coefficients |
---|
459 | double nk; |
---|
460 | //! cache of log( \c nk ) |
---|
461 | double lnk; |
---|
462 | public: |
---|
463 | //! \name Constructors |
---|
464 | //!@{ |
---|
465 | euni () : epdf () {} |
---|
466 | euni (const vec &low0, const vec &high0) {set_parameters (low0, high0);} |
---|
467 | void set_parameters (const vec &low0, const vec &high0) { |
---|
468 | distance = high0 - low0; |
---|
469 | it_assert_debug (min (distance) > 0.0, "bad support"); |
---|
470 | low = low0; |
---|
471 | high = high0; |
---|
472 | nk = prod (1.0 / distance); |
---|
473 | lnk = log (nk); |
---|
474 | dim = low.length(); |
---|
475 | } |
---|
476 | //!@} |
---|
477 | |
---|
478 | double eval (const vec &val) const {return nk;} |
---|
479 | double evallog (const vec &val) const { |
---|
480 | if (any (val < low) && any (val > high)) {return inf;} |
---|
481 | else return lnk; |
---|
482 | } |
---|
483 | vec sample() const { |
---|
484 | vec smp (dim); |
---|
485 | #pragma omp critical |
---|
486 | UniRNG.sample_vector (dim , smp); |
---|
487 | return low + elem_mult (distance, smp); |
---|
488 | } |
---|
489 | //! set values of \c low and \c high |
---|
490 | vec mean() const {return (high -low) / 2.0;} |
---|
491 | vec variance() const {return (pow (high, 2) + pow (low, 2) + elem_mult (high, low)) / 3.0;} |
---|
492 | //! Load from structure with elements: |
---|
493 | //! \code |
---|
494 | //! { high = [...]; // vector of upper bounds |
---|
495 | //! low = [...]; // vector of lower bounds |
---|
496 | //! rv = {class="RV",...} // description of RV |
---|
497 | //! } |
---|
498 | //! \endcode |
---|
499 | //!@} |
---|
500 | void from_setting (const Setting &set) { |
---|
501 | epdf::from_setting (set); // reads rv and rvc |
---|
502 | |
---|
503 | UI::get (high, set, "high", UI::compulsory); |
---|
504 | UI::get (low, set, "low", UI::compulsory); |
---|
505 | } |
---|
506 | }; |
---|
507 | |
---|
508 | |
---|
509 | /*! |
---|
510 | \brief Normal distributed linear function with linear function of mean value; |
---|
511 | |
---|
512 | Mean value \f$mu=A*rvc+mu_0\f$. |
---|
513 | */ |
---|
514 | template < class sq_T, template <typename> class TEpdf = enorm > |
---|
515 | class mlnorm : public mpdf_internal< TEpdf<sq_T> > |
---|
516 | { |
---|
517 | protected: |
---|
518 | //! Internal epdf that arise by conditioning on \c rvc |
---|
519 | mat A; |
---|
520 | vec mu_const; |
---|
521 | // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? |
---|
522 | public: |
---|
523 | //! \name Constructors |
---|
524 | //!@{ |
---|
525 | mlnorm() : mpdf_internal< TEpdf<sq_T> >() {}; |
---|
526 | mlnorm (const mat &A, const vec &mu0, const sq_T &R) : mpdf_internal< TEpdf<sq_T> >() { |
---|
527 | set_parameters (A, mu0, R); |
---|
528 | } |
---|
529 | |
---|
530 | //! Set \c A and \c R |
---|
531 | void set_parameters (const mat &A0, const vec &mu0, const sq_T &R0) { |
---|
532 | it_assert_debug (A0.rows() == mu0.length(), ""); |
---|
533 | it_assert_debug (A0.rows() == R0.rows(), ""); |
---|
534 | |
---|
535 | this->iepdf.set_parameters (zeros (A0.rows()), R0); |
---|
536 | A = A0; |
---|
537 | mu_const = mu0; |
---|
538 | this->dimc = A0.cols(); |
---|
539 | } |
---|
540 | //!@} |
---|
541 | //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. |
---|
542 | void condition (const vec &cond) { |
---|
543 | this->iepdf._mu() = A * cond + mu_const; |
---|
544 | //R is already assigned; |
---|
545 | } |
---|
546 | |
---|
547 | //!access function |
---|
548 | vec& _mu_const() {return mu_const;} |
---|
549 | //!access function |
---|
550 | mat& _A() {return A;} |
---|
551 | //!access function |
---|
552 | mat _R() { return this->iepdf._R().to_mat(); } |
---|
553 | |
---|
554 | template<typename sq_M> |
---|
555 | friend std::ostream &operator<< (std::ostream &os, mlnorm<sq_M, enorm> &ml); |
---|
556 | |
---|
557 | void from_setting (const Setting &set) { |
---|
558 | mpdf::from_setting (set); |
---|
559 | |
---|
560 | UI::get (A, set, "A", UI::compulsory); |
---|
561 | UI::get (mu_const, set, "const", UI::compulsory); |
---|
562 | mat R0; |
---|
563 | UI::get (R0, set, "R", UI::compulsory); |
---|
564 | set_parameters (A, mu_const, R0); |
---|
565 | }; |
---|
566 | }; |
---|
567 | UIREGISTER (mlnorm<ldmat>); |
---|
568 | UIREGISTER (mlnorm<fsqmat>); |
---|
569 | UIREGISTER (mlnorm<chmat>); |
---|
570 | |
---|
571 | //! Mpdf with general function for mean value |
---|
572 | template<class sq_T> |
---|
573 | class mgnorm : public mpdf_internal< enorm< sq_T > > |
---|
574 | { |
---|
575 | private: |
---|
576 | // vec μ WHY NOT? |
---|
577 | shared_ptr<fnc> g; |
---|
578 | |
---|
579 | public: |
---|
580 | //!default constructor |
---|
581 | mgnorm() : mpdf_internal<enorm<sq_T> >() { } |
---|
582 | //!set mean function |
---|
583 | inline void set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0); |
---|
584 | inline void condition (const vec &cond); |
---|
585 | |
---|
586 | |
---|
587 | /*! UI for mgnorm |
---|
588 | |
---|
589 | The mgnorm is constructed from a structure with fields: |
---|
590 | \code |
---|
591 | system = { |
---|
592 | type = "mgnorm"; |
---|
593 | // function for mean value evolution |
---|
594 | g = {type="fnc"; ... } |
---|
595 | |
---|
596 | // variance |
---|
597 | R = [1, 0, |
---|
598 | 0, 1]; |
---|
599 | // --OR -- |
---|
600 | dR = [1, 1]; |
---|
601 | |
---|
602 | // == OPTIONAL == |
---|
603 | |
---|
604 | // description of y variables |
---|
605 | y = {type="rv"; names=["y", "u"];}; |
---|
606 | // description of u variable |
---|
607 | u = {type="rv"; names=[];} |
---|
608 | }; |
---|
609 | \endcode |
---|
610 | |
---|
611 | Result if |
---|
612 | */ |
---|
613 | |
---|
614 | void from_setting (const Setting &set) { |
---|
615 | shared_ptr<fnc> g = UI::build<fnc> (set, "g", UI::compulsory); |
---|
616 | |
---|
617 | mat R; |
---|
618 | vec dR; |
---|
619 | if (UI::get (dR, set, "dR")) |
---|
620 | R = diag (dR); |
---|
621 | else |
---|
622 | UI::get (R, set, "R", UI::compulsory); |
---|
623 | |
---|
624 | set_parameters (g, R); |
---|
625 | } |
---|
626 | }; |
---|
627 | |
---|
628 | UIREGISTER (mgnorm<chmat>); |
---|
629 | |
---|
630 | |
---|
631 | /*! (Approximate) Student t density with linear function of mean value |
---|
632 | |
---|
633 | The internal epdf of this class is of the type of a Gaussian (enorm). |
---|
634 | However, each conditioning is trying to assure the best possible approximation by taking into account the zeta function. See [] for reference. |
---|
635 | |
---|
636 | Perhaps a moment-matching technique? |
---|
637 | */ |
---|
638 | class mlstudent : public mlnorm<ldmat, enorm> |
---|
639 | { |
---|
640 | protected: |
---|
641 | ldmat Lambda; |
---|
642 | ldmat &_R; |
---|
643 | ldmat Re; |
---|
644 | public: |
---|
645 | mlstudent () : mlnorm<ldmat, enorm> (), |
---|
646 | Lambda (), _R (iepdf._R()) {} |
---|
647 | void set_parameters (const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) { |
---|
648 | it_assert_debug (A0.rows() == mu0.length(), ""); |
---|
649 | it_assert_debug (R0.rows() == A0.rows(), ""); |
---|
650 | |
---|
651 | iepdf.set_parameters (mu0, Lambda); // |
---|
652 | A = A0; |
---|
653 | mu_const = mu0; |
---|
654 | Re = R0; |
---|
655 | Lambda = Lambda0; |
---|
656 | } |
---|
657 | void condition (const vec &cond) { |
---|
658 | iepdf._mu() = A * cond + mu_const; |
---|
659 | double zeta; |
---|
660 | //ugly hack! |
---|
661 | if ( (cond.length() + 1) == Lambda.rows()) { |
---|
662 | zeta = Lambda.invqform (concat (cond, vec_1 (1.0))); |
---|
663 | } else { |
---|
664 | zeta = Lambda.invqform (cond); |
---|
665 | } |
---|
666 | _R = Re; |
---|
667 | _R *= (1 + zeta);// / ( nu ); << nu is in Re!!!!!! |
---|
668 | }; |
---|
669 | |
---|
670 | }; |
---|
671 | /*! |
---|
672 | \brief Gamma random walk |
---|
673 | |
---|
674 | Mean value, \f$\mu\f$, of this density is given by \c rvc . |
---|
675 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
676 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
677 | |
---|
678 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
679 | */ |
---|
680 | class mgamma : public mpdf_internal<egamma> |
---|
681 | { |
---|
682 | protected: |
---|
683 | |
---|
684 | //! Constant \f$k\f$ |
---|
685 | double k; |
---|
686 | |
---|
687 | //! cache of iepdf.beta |
---|
688 | vec &_beta; |
---|
689 | |
---|
690 | public: |
---|
691 | //! Constructor |
---|
692 | mgamma() : mpdf_internal<egamma>(), k (0), |
---|
693 | _beta (iepdf._beta()) { |
---|
694 | } |
---|
695 | |
---|
696 | //! Set value of \c k |
---|
697 | void set_parameters (double k, const vec &beta0); |
---|
698 | |
---|
699 | void condition (const vec &val) {_beta = k / val;}; |
---|
700 | //! Load from structure with elements: |
---|
701 | //! \code |
---|
702 | //! { alpha = [...]; // vector of alpha |
---|
703 | //! k = 1.1; // multiplicative constant k |
---|
704 | //! rv = {class="RV",...} // description of RV |
---|
705 | //! rvc = {class="RV",...} // description of RV in condition |
---|
706 | //! } |
---|
707 | //! \endcode |
---|
708 | //!@} |
---|
709 | void from_setting (const Setting &set) { |
---|
710 | mpdf::from_setting (set); // reads rv and rvc |
---|
711 | vec betatmp; // ugly but necessary |
---|
712 | UI::get (betatmp, set, "beta", UI::compulsory); |
---|
713 | UI::get (k, set, "k", UI::compulsory); |
---|
714 | set_parameters (k, betatmp); |
---|
715 | } |
---|
716 | }; |
---|
717 | UIREGISTER (mgamma); |
---|
718 | |
---|
719 | /*! |
---|
720 | \brief Inverse-Gamma random walk |
---|
721 | |
---|
722 | Mean value, \f$ \mu \f$, of this density is given by \c rvc . |
---|
723 | Standard deviation of the random walk is proportional to one \f$ k \f$-th the mean. |
---|
724 | This is achieved by setting \f$ \alpha=\mu/k^2+2 \f$ and \f$ \beta=\mu(\alpha-1)\f$. |
---|
725 | |
---|
726 | The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$. |
---|
727 | */ |
---|
728 | class migamma : public mpdf_internal<eigamma> |
---|
729 | { |
---|
730 | protected: |
---|
731 | //! Constant \f$k\f$ |
---|
732 | double k; |
---|
733 | |
---|
734 | //! cache of iepdf.alpha |
---|
735 | vec &_alpha; |
---|
736 | |
---|
737 | //! cache of iepdf.beta |
---|
738 | vec &_beta; |
---|
739 | |
---|
740 | public: |
---|
741 | //! \name Constructors |
---|
742 | //!@{ |
---|
743 | migamma() : mpdf_internal<eigamma>(), |
---|
744 | k (0), |
---|
745 | _alpha (iepdf._alpha()), |
---|
746 | _beta (iepdf._beta()) { |
---|
747 | } |
---|
748 | |
---|
749 | migamma (const migamma &m) : mpdf_internal<eigamma>(), |
---|
750 | k (0), |
---|
751 | _alpha (iepdf._alpha()), |
---|
752 | _beta (iepdf._beta()) { |
---|
753 | } |
---|
754 | //!@} |
---|
755 | |
---|
756 | //! Set value of \c k |
---|
757 | void set_parameters (int len, double k0) { |
---|
758 | k = k0; |
---|
759 | iepdf.set_parameters ( (1.0 / (k*k) + 2.0) *ones (len) /*alpha*/, ones (len) /*beta*/); |
---|
760 | dimc = dimension(); |
---|
761 | }; |
---|
762 | void condition (const vec &val) { |
---|
763 | _beta = elem_mult (val, (_alpha - 1.0)); |
---|
764 | }; |
---|
765 | }; |
---|
766 | |
---|
767 | |
---|
768 | /*! |
---|
769 | \brief Gamma random walk around a fixed point |
---|
770 | |
---|
771 | 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 |
---|
772 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
773 | |
---|
774 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
775 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
776 | |
---|
777 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
778 | */ |
---|
779 | class mgamma_fix : public mgamma |
---|
780 | { |
---|
781 | protected: |
---|
782 | //! parameter l |
---|
783 | double l; |
---|
784 | //! reference vector |
---|
785 | vec refl; |
---|
786 | public: |
---|
787 | //! Constructor |
---|
788 | mgamma_fix () : mgamma (), refl () {}; |
---|
789 | //! Set value of \c k |
---|
790 | void set_parameters (double k0 , vec ref0, double l0) { |
---|
791 | mgamma::set_parameters (k0, ref0); |
---|
792 | refl = pow (ref0, 1.0 - l0);l = l0; |
---|
793 | dimc = dimension(); |
---|
794 | }; |
---|
795 | |
---|
796 | void condition (const vec &val) {vec mean = elem_mult (refl, pow (val, l)); _beta = k / mean;}; |
---|
797 | }; |
---|
798 | |
---|
799 | |
---|
800 | /*! |
---|
801 | \brief Inverse-Gamma random walk around a fixed point |
---|
802 | |
---|
803 | 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 |
---|
804 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
805 | |
---|
806 | ==== Check == vv = |
---|
807 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
808 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
809 | |
---|
810 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
811 | */ |
---|
812 | class migamma_ref : public migamma |
---|
813 | { |
---|
814 | protected: |
---|
815 | //! parameter l |
---|
816 | double l; |
---|
817 | //! reference vector |
---|
818 | vec refl; |
---|
819 | public: |
---|
820 | //! Constructor |
---|
821 | migamma_ref () : migamma (), refl () {}; |
---|
822 | //! Set value of \c k |
---|
823 | void set_parameters (double k0 , vec ref0, double l0) { |
---|
824 | migamma::set_parameters (ref0.length(), k0); |
---|
825 | refl = pow (ref0, 1.0 - l0); |
---|
826 | l = l0; |
---|
827 | dimc = dimension(); |
---|
828 | }; |
---|
829 | |
---|
830 | void condition (const vec &val) { |
---|
831 | vec mean = elem_mult (refl, pow (val, l)); |
---|
832 | migamma::condition (mean); |
---|
833 | }; |
---|
834 | |
---|
835 | /*! UI for migamma_ref |
---|
836 | |
---|
837 | The migamma_ref is constructed from a structure with fields: |
---|
838 | \code |
---|
839 | system = { |
---|
840 | type = "migamma_ref"; |
---|
841 | ref = [1e-5; 1e-5; 1e-2 1e-3]; // reference vector |
---|
842 | l = 0.999; // constant l |
---|
843 | k = 0.1; // constant k |
---|
844 | |
---|
845 | // == OPTIONAL == |
---|
846 | // description of y variables |
---|
847 | y = {type="rv"; names=["y", "u"];}; |
---|
848 | // description of u variable |
---|
849 | u = {type="rv"; names=[];} |
---|
850 | }; |
---|
851 | \endcode |
---|
852 | |
---|
853 | Result if |
---|
854 | */ |
---|
855 | void from_setting (const Setting &set); |
---|
856 | |
---|
857 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
858 | }; |
---|
859 | |
---|
860 | |
---|
861 | UIREGISTER (migamma_ref); |
---|
862 | |
---|
863 | /*! Log-Normal probability density |
---|
864 | only allow diagonal covariances! |
---|
865 | |
---|
866 | Density of the form \f$ \log(x)\sim \mathcal{N}(\mu,\sigma^2) \f$ , i.e. |
---|
867 | \f[ |
---|
868 | x \sim \frac{1}{x\sigma\sqrt{2\pi}}\exp{-\frac{1}{2\sigma^2}(\log(x)-\mu)} |
---|
869 | \f] |
---|
870 | |
---|
871 | */ |
---|
872 | class elognorm: public enorm<ldmat> |
---|
873 | { |
---|
874 | public: |
---|
875 | vec sample() const {return exp (enorm<ldmat>::sample());}; |
---|
876 | vec mean() const {vec var = enorm<ldmat>::variance();return exp (mu - 0.5*var);}; |
---|
877 | |
---|
878 | }; |
---|
879 | |
---|
880 | /*! |
---|
881 | \brief Log-Normal random walk |
---|
882 | |
---|
883 | Mean value, \f$\mu\f$, is... |
---|
884 | |
---|
885 | ==== Check == vv = |
---|
886 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
887 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
888 | |
---|
889 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
890 | */ |
---|
891 | class mlognorm : public mpdf_internal<elognorm> |
---|
892 | { |
---|
893 | protected: |
---|
894 | //! parameter 1/2*sigma^2 |
---|
895 | double sig2; |
---|
896 | |
---|
897 | //! access |
---|
898 | vec μ |
---|
899 | public: |
---|
900 | //! Constructor |
---|
901 | mlognorm() : mpdf_internal<elognorm>(), |
---|
902 | sig2 (0), |
---|
903 | mu (iepdf._mu()) { |
---|
904 | } |
---|
905 | |
---|
906 | //! Set value of \c k |
---|
907 | void set_parameters (int size, double k) { |
---|
908 | sig2 = 0.5 * log (k * k + 1); |
---|
909 | iepdf.set_parameters (zeros (size), 2*sig2*eye (size)); |
---|
910 | |
---|
911 | dimc = size; |
---|
912 | }; |
---|
913 | |
---|
914 | void condition (const vec &val) { |
---|
915 | mu = log (val) - sig2;//elem_mult ( refl,pow ( val,l ) ); |
---|
916 | }; |
---|
917 | |
---|
918 | /*! UI for mlognorm |
---|
919 | |
---|
920 | The mlognorm is constructed from a structure with fields: |
---|
921 | \code |
---|
922 | system = { |
---|
923 | type = "mlognorm"; |
---|
924 | k = 0.1; // constant k |
---|
925 | mu0 = [1., 1.]; |
---|
926 | |
---|
927 | // == OPTIONAL == |
---|
928 | // description of y variables |
---|
929 | y = {type="rv"; names=["y", "u"];}; |
---|
930 | // description of u variable |
---|
931 | u = {type="rv"; names=[];} |
---|
932 | }; |
---|
933 | \endcode |
---|
934 | |
---|
935 | */ |
---|
936 | void from_setting (const Setting &set); |
---|
937 | |
---|
938 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
939 | |
---|
940 | }; |
---|
941 | |
---|
942 | UIREGISTER (mlognorm); |
---|
943 | |
---|
944 | /*! inverse Wishart density defined on Choleski decomposition |
---|
945 | |
---|
946 | */ |
---|
947 | class eWishartCh : public epdf |
---|
948 | { |
---|
949 | protected: |
---|
950 | //! Upper-Triagle of Choleski decomposition of \f$ \Psi \f$ |
---|
951 | chmat Y; |
---|
952 | //! dimension of matrix \f$ \Psi \f$ |
---|
953 | int p; |
---|
954 | //! degrees of freedom \f$ \nu \f$ |
---|
955 | double delta; |
---|
956 | public: |
---|
957 | void set_parameters (const mat &Y0, const double delta0) {Y = chmat (Y0);delta = delta0; p = Y.rows(); dim = p * p; } |
---|
958 | mat sample_mat() const { |
---|
959 | mat X = zeros (p, p); |
---|
960 | |
---|
961 | //sample diagonal |
---|
962 | for (int i = 0;i < p;i++) { |
---|
963 | GamRNG.setup (0.5* (delta - i) , 0.5); // no +1 !! index if from 0 |
---|
964 | #pragma omp critical |
---|
965 | X (i, i) = sqrt (GamRNG()); |
---|
966 | } |
---|
967 | //do the rest |
---|
968 | for (int i = 0;i < p;i++) { |
---|
969 | for (int j = i + 1;j < p;j++) { |
---|
970 | #pragma omp critical |
---|
971 | X (i, j) = NorRNG.sample(); |
---|
972 | } |
---|
973 | } |
---|
974 | return X*Y._Ch();// return upper triangular part of the decomposition |
---|
975 | } |
---|
976 | vec sample () const { |
---|
977 | return vec (sample_mat()._data(), p*p); |
---|
978 | } |
---|
979 | //! fast access function y0 will be copied into Y.Ch. |
---|
980 | void setY (const mat &Ch0) {copy_vector (dim, Ch0._data(), Y._Ch()._data());} |
---|
981 | //! fast access function y0 will be copied into Y.Ch. |
---|
982 | void _setY (const vec &ch0) {copy_vector (dim, ch0._data(), Y._Ch()._data()); } |
---|
983 | //! access function |
---|
984 | const chmat& getY() const {return Y;} |
---|
985 | }; |
---|
986 | |
---|
987 | class eiWishartCh: public epdf |
---|
988 | { |
---|
989 | protected: |
---|
990 | eWishartCh W; |
---|
991 | int p; |
---|
992 | double delta; |
---|
993 | public: |
---|
994 | void set_parameters (const mat &Y0, const double delta0) { |
---|
995 | delta = delta0; |
---|
996 | W.set_parameters (inv (Y0), delta0); |
---|
997 | dim = W.dimension(); p = Y0.rows(); |
---|
998 | } |
---|
999 | vec sample() const {mat iCh; iCh = inv (W.sample_mat()); return vec (iCh._data(), dim);} |
---|
1000 | void _setY (const vec &y0) { |
---|
1001 | mat Ch (p, p); |
---|
1002 | mat iCh (p, p); |
---|
1003 | copy_vector (dim, y0._data(), Ch._data()); |
---|
1004 | |
---|
1005 | iCh = inv (Ch); |
---|
1006 | W.setY (iCh); |
---|
1007 | } |
---|
1008 | virtual double evallog (const vec &val) const { |
---|
1009 | chmat X (p); |
---|
1010 | const chmat& Y = W.getY(); |
---|
1011 | |
---|
1012 | copy_vector (p*p, val._data(), X._Ch()._data()); |
---|
1013 | chmat iX (p);X.inv (iX); |
---|
1014 | // compute |
---|
1015 | // \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)}, |
---|
1016 | mat M = Y.to_mat() * iX.to_mat(); |
---|
1017 | |
---|
1018 | double log1 = 0.5 * p * (2 * Y.logdet()) - 0.5 * (delta + p + 1) * (2 * X.logdet()) - 0.5 * trace (M); |
---|
1019 | //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!! |
---|
1020 | |
---|
1021 | /* if (0) { |
---|
1022 | mat XX=X.to_mat(); |
---|
1023 | mat YY=Y.to_mat(); |
---|
1024 | |
---|
1025 | double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX)); |
---|
1026 | cout << log1 << "," << log2 << endl; |
---|
1027 | }*/ |
---|
1028 | return log1; |
---|
1029 | }; |
---|
1030 | |
---|
1031 | }; |
---|
1032 | |
---|
1033 | class rwiWishartCh : public mpdf_internal<eiWishartCh> |
---|
1034 | { |
---|
1035 | protected: |
---|
1036 | //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions |
---|
1037 | double sqd; |
---|
1038 | //reference point for diagonal |
---|
1039 | vec refl; |
---|
1040 | double l; |
---|
1041 | int p; |
---|
1042 | |
---|
1043 | public: |
---|
1044 | rwiWishartCh() : sqd (0), l (0), p (0) {} |
---|
1045 | |
---|
1046 | void set_parameters (int p0, double k, vec ref0, double l0) { |
---|
1047 | p = p0; |
---|
1048 | double delta = 2 / (k * k) + p + 3; |
---|
1049 | sqd = sqrt (delta - p - 1); |
---|
1050 | l = l0; |
---|
1051 | refl = pow (ref0, 1 - l); |
---|
1052 | |
---|
1053 | iepdf.set_parameters (eye (p), delta); |
---|
1054 | dimc = iepdf.dimension(); |
---|
1055 | } |
---|
1056 | void condition (const vec &c) { |
---|
1057 | vec z = c; |
---|
1058 | int ri = 0; |
---|
1059 | for (int i = 0;i < p*p;i += (p + 1)) {//trace diagonal element |
---|
1060 | z (i) = pow (z (i), l) * refl (ri); |
---|
1061 | ri++; |
---|
1062 | } |
---|
1063 | |
---|
1064 | iepdf._setY (sqd*z); |
---|
1065 | } |
---|
1066 | }; |
---|
1067 | |
---|
1068 | //! Switch between various resampling methods. |
---|
1069 | enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; |
---|
1070 | /*! |
---|
1071 | \brief Weighted empirical density |
---|
1072 | |
---|
1073 | Used e.g. in particle filters. |
---|
1074 | */ |
---|
1075 | class eEmp: public epdf |
---|
1076 | { |
---|
1077 | protected : |
---|
1078 | //! Number of particles |
---|
1079 | int n; |
---|
1080 | //! Sample weights \f$w\f$ |
---|
1081 | vec w; |
---|
1082 | //! Samples \f$x^{(i)}, i=1..n\f$ |
---|
1083 | Array<vec> samples; |
---|
1084 | public: |
---|
1085 | //! \name Constructors |
---|
1086 | //!@{ |
---|
1087 | eEmp () : epdf (), w (), samples () {}; |
---|
1088 | //! copy constructor |
---|
1089 | eEmp (const eEmp &e) : epdf (e), w (e.w), samples (e.samples) {}; |
---|
1090 | //!@} |
---|
1091 | |
---|
1092 | //! Set samples and weights |
---|
1093 | void set_statistics (const vec &w0, const epdf &pdf0); |
---|
1094 | //! Set samples and weights |
---|
1095 | void set_statistics (const epdf &pdf0 , int n) {set_statistics (ones (n) / n, pdf0);}; |
---|
1096 | //! Set sample |
---|
1097 | void set_samples (const epdf* pdf0); |
---|
1098 | //! Set sample |
---|
1099 | void set_parameters (int n0, bool copy = true) {n = n0; w.set_size (n0, copy);samples.set_size (n0, copy);}; |
---|
1100 | //! Potentially dangerous, use with care. |
---|
1101 | vec& _w() {return w;}; |
---|
1102 | //! Potentially dangerous, use with care. |
---|
1103 | const vec& _w() const {return w;}; |
---|
1104 | //! access function |
---|
1105 | Array<vec>& _samples() {return samples;}; |
---|
1106 | //! access function |
---|
1107 | const Array<vec>& _samples() const {return samples;}; |
---|
1108 | //! 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. |
---|
1109 | ivec resample (RESAMPLING_METHOD method = SYSTEMATIC); |
---|
1110 | //! inherited operation : NOT implemneted |
---|
1111 | vec sample() const {it_error ("Not implemented");return 0;} |
---|
1112 | //! inherited operation : NOT implemneted |
---|
1113 | double evallog (const vec &val) const {it_error ("Not implemented");return 0.0;} |
---|
1114 | vec mean() const { |
---|
1115 | vec pom = zeros (dim); |
---|
1116 | for (int i = 0;i < n;i++) {pom += samples (i) * w (i);} |
---|
1117 | return pom; |
---|
1118 | } |
---|
1119 | vec variance() const { |
---|
1120 | vec pom = zeros (dim); |
---|
1121 | for (int i = 0;i < n;i++) {pom += pow (samples (i), 2) * w (i);} |
---|
1122 | return pom -pow (mean(), 2); |
---|
1123 | } |
---|
1124 | //! For this class, qbounds are minimum and maximum value of the population! |
---|
1125 | void qbounds (vec &lb, vec &ub, double perc = 0.95) const { |
---|
1126 | // lb in inf so than it will be pushed below; |
---|
1127 | lb.set_size (dim); |
---|
1128 | ub.set_size (dim); |
---|
1129 | lb = std::numeric_limits<double>::infinity(); |
---|
1130 | ub = -std::numeric_limits<double>::infinity(); |
---|
1131 | int j; |
---|
1132 | for (int i = 0;i < n;i++) { |
---|
1133 | for (j = 0;j < dim; j++) { |
---|
1134 | if (samples (i) (j) < lb (j)) {lb (j) = samples (i) (j);} |
---|
1135 | if (samples (i) (j) > ub (j)) {ub (j) = samples (i) (j);} |
---|
1136 | } |
---|
1137 | } |
---|
1138 | } |
---|
1139 | }; |
---|
1140 | |
---|
1141 | |
---|
1142 | //////////////////////// |
---|
1143 | |
---|
1144 | template<class sq_T> |
---|
1145 | void enorm<sq_T>::set_parameters (const vec &mu0, const sq_T &R0) |
---|
1146 | { |
---|
1147 | //Fixme test dimensions of mu0 and R0; |
---|
1148 | mu = mu0; |
---|
1149 | R = R0; |
---|
1150 | validate(); |
---|
1151 | }; |
---|
1152 | |
---|
1153 | template<class sq_T> |
---|
1154 | void enorm<sq_T>::from_setting (const Setting &set) |
---|
1155 | { |
---|
1156 | epdf::from_setting (set); //reads rv |
---|
1157 | |
---|
1158 | UI::get (mu, set, "mu", UI::compulsory); |
---|
1159 | mat Rtmp;// necessary for conversion |
---|
1160 | UI::get (Rtmp, set, "R", UI::compulsory); |
---|
1161 | R = Rtmp; // conversion |
---|
1162 | validate(); |
---|
1163 | } |
---|
1164 | |
---|
1165 | template<class sq_T> |
---|
1166 | void enorm<sq_T>::dupdate (mat &v, double nu) |
---|
1167 | { |
---|
1168 | // |
---|
1169 | }; |
---|
1170 | |
---|
1171 | // template<class sq_T> |
---|
1172 | // void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) { |
---|
1173 | // // |
---|
1174 | // }; |
---|
1175 | |
---|
1176 | template<class sq_T> |
---|
1177 | vec enorm<sq_T>::sample() const |
---|
1178 | { |
---|
1179 | vec x (dim); |
---|
1180 | #pragma omp critical |
---|
1181 | NorRNG.sample_vector (dim, x); |
---|
1182 | vec smp = R.sqrt_mult (x); |
---|
1183 | |
---|
1184 | smp += mu; |
---|
1185 | return smp; |
---|
1186 | }; |
---|
1187 | |
---|
1188 | // template<class sq_T> |
---|
1189 | // double enorm<sq_T>::eval ( const vec &val ) const { |
---|
1190 | // double pdfl,e; |
---|
1191 | // pdfl = evallog ( val ); |
---|
1192 | // e = exp ( pdfl ); |
---|
1193 | // return e; |
---|
1194 | // }; |
---|
1195 | |
---|
1196 | template<class sq_T> |
---|
1197 | double enorm<sq_T>::evallog_nn (const vec &val) const |
---|
1198 | { |
---|
1199 | // 1.83787706640935 = log(2pi) |
---|
1200 | double tmp = -0.5 * (R.invqform (mu - val));// - lognc(); |
---|
1201 | return tmp; |
---|
1202 | }; |
---|
1203 | |
---|
1204 | template<class sq_T> |
---|
1205 | inline double enorm<sq_T>::lognc () const |
---|
1206 | { |
---|
1207 | // 1.83787706640935 = log(2pi) |
---|
1208 | double tmp = 0.5 * (R.cols() * 1.83787706640935 + R.logdet()); |
---|
1209 | return tmp; |
---|
1210 | }; |
---|
1211 | |
---|
1212 | |
---|
1213 | // template<class sq_T> |
---|
1214 | // vec mlnorm<sq_T>::samplecond (const vec &cond, double &lik ) { |
---|
1215 | // this->condition ( cond ); |
---|
1216 | // vec smp = epdf.sample(); |
---|
1217 | // lik = epdf.eval ( smp ); |
---|
1218 | // return smp; |
---|
1219 | // } |
---|
1220 | |
---|
1221 | // template<class sq_T> |
---|
1222 | // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) { |
---|
1223 | // int i; |
---|
1224 | // int dim = rv.count(); |
---|
1225 | // mat Smp ( dim,n ); |
---|
1226 | // vec smp ( dim ); |
---|
1227 | // this->condition ( cond ); |
---|
1228 | // |
---|
1229 | // for ( i=0; i<n; i++ ) { |
---|
1230 | // smp = epdf.sample(); |
---|
1231 | // lik ( i ) = epdf.eval ( smp ); |
---|
1232 | // Smp.set_col ( i ,smp ); |
---|
1233 | // } |
---|
1234 | // |
---|
1235 | // return Smp; |
---|
1236 | // } |
---|
1237 | |
---|
1238 | |
---|
1239 | template<class sq_T> |
---|
1240 | shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const |
---|
1241 | { |
---|
1242 | enorm<sq_T> *tmp = new enorm<sq_T> (); |
---|
1243 | shared_ptr<epdf> narrow(tmp); |
---|
1244 | marginal ( rvn, *tmp ); |
---|
1245 | return narrow; |
---|
1246 | } |
---|
1247 | |
---|
1248 | template<class sq_T> |
---|
1249 | void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const |
---|
1250 | { |
---|
1251 | it_assert_debug (isnamed(), "rv description is not assigned"); |
---|
1252 | ivec irvn = rvn.dataind (rv); |
---|
1253 | |
---|
1254 | sq_T Rn (R, irvn); // select rows and columns of R |
---|
1255 | |
---|
1256 | target.set_rv ( rvn ); |
---|
1257 | target.set_parameters (mu (irvn), Rn); |
---|
1258 | } |
---|
1259 | |
---|
1260 | template<class sq_T> |
---|
1261 | shared_ptr<mpdf> enorm<sq_T>::condition ( const RV &rvn ) const |
---|
1262 | { |
---|
1263 | mlnorm<sq_T> *tmp = new mlnorm<sq_T> (); |
---|
1264 | shared_ptr<mpdf> narrow(tmp); |
---|
1265 | condition ( rvn, *tmp ); |
---|
1266 | return narrow; |
---|
1267 | } |
---|
1268 | |
---|
1269 | template<class sq_T> |
---|
1270 | void enorm<sq_T>::condition ( const RV &rvn, mpdf &target ) const |
---|
1271 | { |
---|
1272 | typedef mlnorm<sq_T> TMlnorm; |
---|
1273 | |
---|
1274 | it_assert_debug (isnamed(), "rvs are not assigned"); |
---|
1275 | TMlnorm &uptarget = dynamic_cast<TMlnorm &>(target); |
---|
1276 | |
---|
1277 | RV rvc = rv.subt (rvn); |
---|
1278 | it_assert_debug ( (rvc._dsize() + rvn._dsize() == rv._dsize()), "wrong rvn"); |
---|
1279 | //Permutation vector of the new R |
---|
1280 | ivec irvn = rvn.dataind (rv); |
---|
1281 | ivec irvc = rvc.dataind (rv); |
---|
1282 | ivec perm = concat (irvn , irvc); |
---|
1283 | sq_T Rn (R, perm); |
---|
1284 | |
---|
1285 | //fixme - could this be done in general for all sq_T? |
---|
1286 | mat S = Rn.to_mat(); |
---|
1287 | //fixme |
---|
1288 | int n = rvn._dsize() - 1; |
---|
1289 | int end = R.rows() - 1; |
---|
1290 | mat S11 = S.get (0, n, 0, n); |
---|
1291 | mat S12 = S.get (0, n , rvn._dsize(), end); |
---|
1292 | mat S22 = S.get (rvn._dsize(), end, rvn._dsize(), end); |
---|
1293 | |
---|
1294 | vec mu1 = mu (irvn); |
---|
1295 | vec mu2 = mu (irvc); |
---|
1296 | mat A = S12 * inv (S22); |
---|
1297 | sq_T R_n (S11 - A *S12.T()); |
---|
1298 | |
---|
1299 | uptarget.set_rv (rvn); |
---|
1300 | uptarget.set_rvc (rvc); |
---|
1301 | uptarget.set_parameters (A, mu1 - A*mu2, R_n); |
---|
1302 | } |
---|
1303 | |
---|
1304 | //// |
---|
1305 | /////// |
---|
1306 | template<class sq_T> |
---|
1307 | void mgnorm<sq_T >::set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0) { |
---|
1308 | g = g0; |
---|
1309 | this->iepdf.set_parameters (zeros (g->dimension()), R0); |
---|
1310 | } |
---|
1311 | |
---|
1312 | template<class sq_T> |
---|
1313 | void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);}; |
---|
1314 | |
---|
1315 | template<class sq_T> |
---|
1316 | std::ostream &operator<< (std::ostream &os, mlnorm<sq_T> &ml) |
---|
1317 | { |
---|
1318 | os << "A:" << ml.A << endl; |
---|
1319 | os << "mu:" << ml.mu_const << endl; |
---|
1320 | os << "R:" << ml._R() << endl; |
---|
1321 | return os; |
---|
1322 | }; |
---|
1323 | |
---|
1324 | } |
---|
1325 | #endif //EF_H |
---|