Changeset 96

Show
Ignore:
Timestamp:
05/09/08 17:25:10 (16 years ago)
Author:
smidl
Message:

changes in EF - introduction of the lognc() function

Location:
bdm/stat
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.h

    r85 r96  
    129129        //! Returns the required moment of the epdf 
    130130//      virtual vec moment ( const int order = 1 ); 
    131         //! Returns a sample from the density, \f$x \sim epdf(rv)\f$ 
     131        //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ 
    132132        virtual vec sample () const =0; 
    133133        //! Compute probability of argument \c val 
     
    224224        RV rv; 
    225225        //!Logarithm of marginalized data likelihood. 
    226  double ll; 
     226        double ll; 
    227227        //!  If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save time. 
    228228        bool evalll; 
  • bdm/stat/libEF.cpp

    r60 r96  
    1212using std::cout; 
    1313 
     14vec egiw::sample() const { 
     15        it_warning("Function not implemented"); 
     16        return vec_1(0.0); 
     17} 
     18 
     19double egiw::evalpdflog( const vec &val ) const { 
     20        int nPsi = rv.count()-1; // assuming 1dim y 
     21        double k = nu + nPsi + 2; 
     22         
     23        double r = val(nPsi); //last entry! 
     24        vec Psi(nPsi+1); 
     25        Psi(0) = -1.0; 
     26        Psi.set_subvector(1,val); // fill the rest 
     27         
     28        return -0.5*( k*log(r) + V.qform(Psi)) - lognc(); 
     29} 
     30 
     31double egiw::lognc() const{ 
     32        const vec& D = V._D(); 
     33        int nPsi = D.length()-1; // assuming 1dim y 
     34 
     35// log(2) = 0.693147180559945286226763983 
     36// log(pi) = 1.144729885849400163877476189 
     37return lgamma(0.5*nu) + 0.5*((1.0-nu)*log(D(0)) - V.logdet() + (nu+nPsi)*0.693147180559945286226763983 + nPsi*1.144729885849400163877476189); 
     38} 
     39 
     40vec egiw::mean() const { 
     41        const mat &L= V._L(); 
     42        const vec &D= V._D(); 
     43         
     44        int end = L.rows()-1; 
     45        vec L0 = L.get_col(0); 
     46         
     47        vec m(D.length()); 
     48        mat iLsub = ltuinv(L(1,end,1,end)); 
     49        m.set_subvector(0,iLsub*L0(1,end)); 
     50        m(end)= D(0)/(nu-2.0); 
     51         
     52        return m; 
     53} 
     54 
    1455vec egamma::sample() const { 
    1556        vec smp ( rv.count() ); 
     
    4081 
    4182double egamma::evalpdflog ( const vec &val ) const { 
     83        double res = 0.0; //the rest will be added 
     84        int i; 
     85 
     86        for ( i=0; i<rv.count(); i++ ) { 
     87                res += ( alpha ( i ) - 1 ) *std::log ( val ( i ) ) - beta ( i ) *val ( i ); 
     88        } 
     89 
     90        return res-lognc(); 
     91} 
     92 
     93double egamma::lognc() const { 
    4294        double res = 0.0; //will be added 
    4395        int i; 
    4496 
    4597        for ( i=0; i<rv.count(); i++ ) { 
    46                 res += ( alpha ( i ) - 1 ) *std::log ( val ( i ) ) + alpha ( i ) *std::log ( beta ( i ) ) - beta ( i ) *val ( i ) - lgamma ( alpha ( i ) ); 
     98                res += lgamma ( alpha ( i ) ) - alpha ( i ) *std::log ( beta ( i ) ) ; 
    4799        } 
    48100 
  • bdm/stat/libEF.h

    r85 r96  
    3737 
    3838class eEF : public epdf { 
    39  
    4039public: 
    4140//      eEF() :epdf() {}; 
    4241        //! default constructor 
    4342        eEF ( const RV &rv ) :epdf ( rv ) {}; 
     43        //! logarithm of the normalizing constant, \f$\mathcal{I}\f$  
     44        virtual double lognc()const =0; 
    4445        //!TODO decide if it is really needed 
    4546        virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; 
     
    9293        double eval ( const vec &val ) const ; 
    9394        double evalpdflog ( const vec &val ) const; 
     95        double lognc () const; 
    9496        vec mean() const {return mu;} 
    9597 
     
    106108        //! access method 
    107109        mat getR () {return R.to_mat();} 
     110}; 
     111 
     112/*! 
     113* \brief Gauss-inverse-Wishart density stored in LD form 
     114 
     115* More?... 
     116*/ 
     117class egiw : public eEF { 
     118protected: 
     119        //! Extended information matrix of sufficient statistics 
     120        ldmat V; 
     121        //! Number of data records (degrees of freedom) of sufficient statistics 
     122        double nu; 
     123public: 
     124        //!Default constructor 
     125        egiw(RV rv, mat V0, double nu0): eEF(rv), V(V0), nu(nu0) { 
     126                it_assert_debug(rv.count()==V.rows(),"Incompatible V0."); 
     127        } 
     128 
     129        vec sample() const; 
     130        vec mean() const; 
     131        double evalpdflog ( const vec &val ) const; 
     132        double lognc () const; 
     133 
     134        //Access 
     135        //! returns a pointer to the internal statistics. Use with Care! 
     136        ldmat& _V() {return V;} 
     137        //! returns a pointer to the internal statistics. Use with Care! 
     138        double& _nu() {return nu;} 
     139 
    108140}; 
    109141 
     
    132164        mat sample ( int N ) const; 
    133165        double evalpdflog ( const vec &val ) const; 
     166        double lognc () const; 
    134167        //! Returns poiter to alpha and beta. Potentially dengerous: use with care! 
    135168        void _param ( vec* &a, vec* &b ) {a=&alpha;b=&beta;}; 
     
    255288class mgamma_fix : public mgamma { 
    256289protected: 
     290        //! parameter l 
    257291        double l; 
     292        //! reference vector 
    258293        vec refl; 
    259294public: 
     
    367402double enorm<sq_T>::evalpdflog ( const vec &val ) const { 
    368403        // 1.83787706640935 = log(2pi) 
    369         return -0.5* ( R.cols() * 1.83787706640935 +R.logdet() +R.invqform ( mu-val ) ); 
    370 }; 
    371  
     404        return  -0.5* (  +R.invqform ( mu-val ) ) - lognc(); 
     405}; 
     406 
     407template<class sq_T> 
     408inline double enorm<sq_T>::lognc () const { 
     409        // 1.83787706640935 = log(2pi) 
     410        return -0.5* ( R.cols() * 1.83787706640935 +R.logdet()); 
     411}; 
    372412 
    373413template<class sq_T>