| 330 | }; |
| 331 | |
| 332 | /*! |
| 333 | \brief Inverse-Gamma posterior density |
| 334 | |
| 335 | Multivariate inverse-Gamma density as product of independent univariate densities. |
| 336 | \f[ |
| 337 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
| 338 | \f] |
| 339 | |
| 340 | Inverse Gamma can be converted to Gamma using \[ |
| 341 | x\sim iG(a,b) => 1/x\sim G(a,1/b) |
| 342 | \] |
| 343 | This relation is used in sampling. |
| 344 | */ |
| 345 | |
| 346 | class eigamma : public eEF { |
| 347 | protected: |
| 348 | //! Vector \f$\alpha\f$ |
| 349 | vec* alpha; |
| 350 | //! Vector \f$\beta\f$ (in fact it is 1/beta as used in definition of iG) |
| 351 | vec* beta; |
| 352 | //!internal egamma |
| 353 | egamma eg; |
| 354 | public : |
| 355 | //! Default constructor |
| 356 | eigamma ( const RV &rv ) :eEF ( rv ), eg(rv) {eg._param(alpha,beta);}; |
| 357 | //! Sets parameters |
| 358 | void set_parameters ( const vec &a, const vec &b ) {*alpha=a,*beta=b;}; |
| 359 | vec sample() const {return 1.0/eg.sample();}; |
| 360 | //! TODO: is it used anywhere? |
| 361 | // mat sample ( int N ) const; |
| 362 | double evallog ( const vec &val ) const {return eg.evallog(val);}; |
| 363 | double lognc () const {return eg.lognc();}; |
| 364 | //! Returns poiter to alpha and beta. Potentially dangerous: use with care! |
| 365 | void _param ( vec* &a, vec* &b ) {a=alpha;b=beta;}; |
| 366 | vec mean() const {return elem_div(*beta,*alpha-1);} |
| 514 | }; |
| 515 | |
| 516 | /*! |
| 517 | \brief Inverse-Gamma random walk |
| 518 | |
| 519 | Mean value, \f$\mu\f$, of this density is given by \c rvc . |
| 520 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
| 521 | This is achieved by setting \f$\alpha=\mu/k+2\f$ and \f$\beta=\mu(\alpha-1)\f$. |
| 522 | |
| 523 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
| 524 | */ |
| 525 | class migamma : public mEF { |
| 526 | protected: |
| 527 | //! Internal epdf that arise by conditioning on \c rvc |
| 528 | eigamma epdf; |
| 529 | //! Constant \f$k\f$ |
| 530 | double k; |
| 531 | //! cache of epdf.beta |
| 532 | vec* _beta; |
| 533 | //! chaceh of epdf.alpha |
| 534 | vec* _alpha; |
| 535 | |
| 536 | public: |
| 537 | //! Constructor |
| 538 | migamma ( const RV &rv,const RV &rvc ): mEF ( rv,rvc ), epdf ( rv ) {epdf._param ( _alpha,_beta );ep=&epdf;}; |
| 539 | //! Set value of \c k |
| 540 | void set_parameters ( double k0 ){k=k0;*_alpha=1.0/(k*k)+2;}; |
| 541 | void condition ( const vec &val ) { |
| 542 | *_beta=elem_mult(val,(*_alpha-1)); |
| 543 | }; |
| 575 | |
| 576 | /*! |
| 577 | \brief Inverse-Gamma random walk around a fixed point |
| 578 | |
| 579 | 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 |
| 580 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
| 581 | |
| 582 | ==== Check == vv = |
| 583 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
| 584 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
| 585 | |
| 586 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
| 587 | */ |
| 588 | class migamma_fix : public migamma { |
| 589 | protected: |
| 590 | //! parameter l |
| 591 | double l; |
| 592 | //! reference vector |
| 593 | vec refl; |
| 594 | public: |
| 595 | //! Constructor |
| 596 | migamma_fix ( const RV &rv,const RV &rvc ) : migamma ( rv,rvc ),refl ( rv.count() ) {}; |
| 597 | //! Set value of \c k |
| 598 | void set_parameters ( double k0 , vec ref0, double l0 ) { |
| 599 | migamma::set_parameters ( k0 ); |
| 600 | refl=pow ( ref0,1.0-l0 );l=l0; |
| 601 | }; |
| 602 | |
| 603 | void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); migamma::condition(mean);}; |
| 604 | }; |