Changeset 225 for bdm/stat/libEF.h

Show
Ignore:
Timestamp:
01/12/09 13:28:18 (15 years ago)
Author:
smidl
Message:

prestavba mpf_test do inv-gamma

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libEF.h

    r215 r225  
    317317public : 
    318318        //! Default constructor 
    319         egamma ( const RV &rv ) :eEF ( rv ) {}; 
     319        egamma ( const RV &rv ) :eEF ( rv ), alpha(rv.count()), beta(rv.count()) {}; 
    320320        //! Sets parameters 
    321321        void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;}; 
     
    328328        void _param ( vec* &a, vec* &b ) {a=αb=β}; 
    329329        vec mean() const {vec pom ( alpha ); pom/=beta; return pom;} 
     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\] 
     343This relation is used in sampling. 
     344 */ 
     345 
     346class 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);} 
    330367}; 
    331368/* 
     
    471508public: 
    472509        //! Constructor 
    473         mgamma ( const RV &rv,const RV &rvc ); 
     510        mgamma ( const RV &rv,const RV &rvc ): mEF ( rv,rvc ), epdf ( rv ) {vec* tmp; epdf._param ( tmp,_beta );ep=&epdf;}; 
    474511        //! Set value of \c k 
    475512        void set_parameters ( double k ); 
    476513        void condition ( const vec &val ) {*_beta=k/val;}; 
     514}; 
     515 
     516/*! 
     517\brief  Inverse-Gamma random walk 
     518 
     519Mean value, \f$\mu\f$, of this density is given by \c rvc . 
     520Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. 
     521This is achieved by setting \f$\alpha=\mu/k+2\f$ and \f$\beta=\mu(\alpha-1)\f$. 
     522 
     523The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 
     524 */ 
     525class 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                }; 
    477544}; 
    478545 
     
    506573}; 
    507574 
     575 
     576/*! 
     577\brief  Inverse-Gamma random walk around a fixed point 
     578 
     579Mean 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 = 
     583Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. 
     584This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. 
     585 
     586The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 
     587 */ 
     588class 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}; 
    508605//! Switch between various resampling methods. 
    509606enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };