Changeset 96
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.h
r85 r96 129 129 //! Returns the required moment of the epdf 130 130 // virtual vec moment ( const int order = 1 ); 131 //! Returns a sample from the density, \f$x \simepdf(rv)\f$131 //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ 132 132 virtual vec sample () const =0; 133 133 //! Compute probability of argument \c val … … 224 224 RV rv; 225 225 //!Logarithm of marginalized data likelihood. 226 226 double ll; 227 227 //! 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. 228 228 bool evalll; -
bdm/stat/libEF.cpp
r60 r96 12 12 using std::cout; 13 13 14 vec egiw::sample() const { 15 it_warning("Function not implemented"); 16 return vec_1(0.0); 17 } 18 19 double 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 31 double 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 37 return lgamma(0.5*nu) + 0.5*((1.0-nu)*log(D(0)) - V.logdet() + (nu+nPsi)*0.693147180559945286226763983 + nPsi*1.144729885849400163877476189); 38 } 39 40 vec 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 14 55 vec egamma::sample() const { 15 56 vec smp ( rv.count() ); … … 40 81 41 82 double 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 93 double egamma::lognc() const { 42 94 double res = 0.0; //will be added 43 95 int i; 44 96 45 97 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 ) ) ; 47 99 } 48 100 -
bdm/stat/libEF.h
r85 r96 37 37 38 38 class eEF : public epdf { 39 40 39 public: 41 40 // eEF() :epdf() {}; 42 41 //! default constructor 43 42 eEF ( const RV &rv ) :epdf ( rv ) {}; 43 //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ 44 virtual double lognc()const =0; 44 45 //!TODO decide if it is really needed 45 46 virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; … … 92 93 double eval ( const vec &val ) const ; 93 94 double evalpdflog ( const vec &val ) const; 95 double lognc () const; 94 96 vec mean() const {return mu;} 95 97 … … 106 108 //! access method 107 109 mat getR () {return R.to_mat();} 110 }; 111 112 /*! 113 * \brief Gauss-inverse-Wishart density stored in LD form 114 115 * More?... 116 */ 117 class egiw : public eEF { 118 protected: 119 //! Extended information matrix of sufficient statistics 120 ldmat V; 121 //! Number of data records (degrees of freedom) of sufficient statistics 122 double nu; 123 public: 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 108 140 }; 109 141 … … 132 164 mat sample ( int N ) const; 133 165 double evalpdflog ( const vec &val ) const; 166 double lognc () const; 134 167 //! Returns poiter to alpha and beta. Potentially dengerous: use with care! 135 168 void _param ( vec* &a, vec* &b ) {a=αb=β}; … … 255 288 class mgamma_fix : public mgamma { 256 289 protected: 290 //! parameter l 257 291 double l; 292 //! reference vector 258 293 vec refl; 259 294 public: … … 367 402 double enorm<sq_T>::evalpdflog ( const vec &val ) const { 368 403 // 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 407 template<class sq_T> 408 inline double enorm<sq_T>::lognc () const { 409 // 1.83787706640935 = log(2pi) 410 return -0.5* ( R.cols() * 1.83787706640935 +R.logdet()); 411 }; 372 412 373 413 template<class sq_T>