Changeset 170
- Timestamp:
- 09/24/08 13:07:50 (16 years ago)
- Files:
-
- 23 modified
Legend:
- Unmodified
- Added
- Removed
-
CMakeLists.txt
r169 r170 27 27 SET(CMAKE_BUILD_TYPE Debug) 28 28 SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wno-unknown-pragmas") 29 SET(ITPP_DIR "/home/smidl/work/git/trunk/itpp/.libs")30 29 ENDIF(WIN32) 31 30 -
bdm/estim/arx.cpp
r162 r170 3 3 using namespace itpp; 4 4 5 void ARX::bayes ( const vec &dt ) {5 void ARX::bayes ( const vec &dt, const double w ) { 6 6 double lnc; 7 7 8 if (frg<1.0) { 9 V*=frg; 10 nu*=frg; 11 if (evalll) { 8 if ( frg<1.0 ) { 9 est.pow ( frg ); 10 if ( evalll ) { 12 11 last_lognc = est.lognc(); 13 12 } 14 13 } 15 V.opupdt ( dt, 1.0);16 nu+= 1.0;14 V.opupdt ( dt,w ); 15 nu+=w; 17 16 18 17 if ( evalll ) { … … 20 19 ll = lnc - last_lognc; 21 20 last_lognc = lnc; 22 tll +=ll;23 21 } 24 22 } 25 23 24 double ARX::logpred ( const vec &dt ) const { 25 egiw pred ( est ); 26 ldmat &V=pred._V(); 27 double &nu=pred._nu(); 28 29 double lll; 30 31 if ( frg<1.0 ) { 32 pred.pow ( frg ); 33 lll = pred.lognc(); 34 } 35 else 36 if(evalll){lll=last_lognc;}else{lll=pred.lognc();} 37 38 V.opupdt ( dt,1.0 ); 39 nu+=1.0; 40 41 return pred.lognc()-lll; 42 } 43 44 BM* ARX::_copy_ ( bool changerv) { 45 ARX* Tmp=new ARX ( *this ); 46 if ( changerv ) {Tmp->rv.newids(); Tmp->est._renewrv(Tmp->rv);} 47 return Tmp; 48 } 49 50 void ARX::set_statistics ( const BMEF* B0 ) { 51 const ARX* A0=dynamic_cast<const ARX*> ( B0 ); 52 53 it_assert_debug ( V.rows() ==A0->V.rows(),"ARX::set_statistics Statistics differ" ); 54 set_parameters ( A0->V,A0->nu ); 55 } 26 56 /*! \brief Return the best structure 27 57 @param Eg a copy of GiW density that is being examined … … 48 78 49 79 50 cout << "bb:(" << indeces <<") ll=" << Egll <<endl;80 cout << "bb:(" << indeces <<") ll=" << Egll <<endl; 51 81 52 82 //try to remove only one rv … … 62 92 tmpll = Eg.lognc()-Eg0.lognc(); // likelihood is difference of norm. coefs. 63 93 64 cout << "i=(" << i <<") ll=" << tmpll <<endl;65 94 cout << "i=(" << i <<") ll=" << tmpll <<endl; 95 66 96 // 67 97 if ( tmpll > Egll ) { //increase of the likelihood -
bdm/estim/arx.h
r168 r170 34 34 Extension for time-variant parameters \f$\theta_t,r_t\f$ may be achived using exponential forgetting (Kulhavy and Zarrop, 1993). In such a case, the forgetting factor \c frg \f$\in <0,1>\f$ should be given in the constructor. Time-invariant parameters are estimated for \c frg = 1. 35 35 */ 36 class ARX: public BM {36 class ARX: public BMEF { 37 37 protected: 38 38 //! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density … … 42 42 //! cached value of est.nu 43 43 double ν 44 //! forgetting factor45 double frg;46 //! cached value of lognc() in the previous step (used in evaluation of \c ll )47 double last_lognc;48 //! total likelihood49 double tll;50 44 public: 51 45 //! Full constructor 52 ARX (const RV &rv, const mat &V0, const double &nu0, const double frg0=1.0) : BM(rv),est(rv,V0,nu0), V(est._V()), nu(est._nu()), frg(frg0){last_lognc=est.lognc();tll=0.0;}; 46 ARX ( const RV &rv, const mat &V0, const double &nu0, const double frg0=1.0 ) : BMEF ( rv,frg0 ),est ( rv,V0,nu0 ), V ( est._V() ), nu ( est._nu() ) 47 {last_lognc=est.lognc();}; 48 49 //!Copy constructor 50 ARX ( const ARX &A0 ) : BMEF ( A0),est ( rv,A0.V,A0.nu ), V ( est._V() ), nu ( est._nu() ) {}; 51 52 //!Auxiliary function 53 BM* _copy_(bool changerv=false); 54 55 // //! Set parameters given by moments, \c mu (mean of theta), \c R (mean of R) and \c C (variance of theta) 56 // void set_parameters ( const vec &mu, const mat &R, const mat &C, double dfm){}; 53 57 //! Set sufficient statistics 54 void set_parameters(const mat &V0, const double &nu0){est._V()=V0;est._nu()=nu0;last_lognc=est.lognc();tll=last_lognc;} 58 void set_parameters ( const ldmat &V0, const double &nu0 ) 59 {est._V() =V0;est._nu() =nu0;last_lognc=est.lognc();} 60 void set_statistics ( const BMEF* BM0 ); 55 61 //! Returns sufficient statistics 56 void get_parameters (mat &V0, double &nu0){V0=est._V().to_mat(); nu0=est._nu();}62 void get_parameters ( mat &V0, double &nu0 ) {V0=est._V().to_mat(); nu0=est._nu();} 57 63 //! Here \f$dt = [y_t psi_t] \f$. 58 void bayes ( const vec &dt ); 59 epdf& _epdf() {return est;} 64 void bayes ( const vec &dt, const double w ); 65 void bayes ( const vec &dt ) {bayes ( dt,1.0 );}; 66 const epdf& _epdf() const {return est;} 67 double logpred ( const vec &dt ) const; 68 void flatten (BMEF* B ) { 69 ARX* A=dynamic_cast<ARX*>(B); 70 // nu should be equal to B.nu 71 est.pow ( A->nu/nu); 72 if(evalll){last_lognc=est.lognc();} 73 } 74 60 75 //! Brute force structure estimation.\return indeces of accepted regressors. 61 ivec structure_est(egiw Eg0); 62 //!access function 63 double _tll(){return tll;} 76 ivec structure_est ( egiw Eg0 ); 64 77 }; 65 78 -
bdm/estim/libKF.h
r132 r170 121 121 void bayes ( const vec &dt ); 122 122 //!access function 123 epdf& _epdf(){return est;}123 const epdf& _epdf() const {return est;} 124 124 //!access function 125 125 mat& __K() {return _K;} … … 186 186 void set_est (vec mu0, mat P0){mu=mu0;P=P0;}; 187 187 //!dummy! 188 epdf& _epdf(){return E;};188 const epdf& _epdf()const{return E;}; 189 189 }; 190 190 -
bdm/estim/libPF.h
r141 r170 71 71 eEmp &E; 72 72 vec &_w; 73 Array< epdf*> Coms;73 Array<const epdf*> Coms; 74 74 public: 75 75 mpfepdf ( eEmp &E0, const RV &rvc ) : … … 80 80 }; 81 81 82 void set_elements ( int &i, double wi, epdf* ep )82 void set_elements ( int &i, double wi, const epdf* ep ) 83 83 {_w ( i ) =wi; Coms ( i ) =ep;}; 84 84 … … 112 112 for ( int i=0;i<n;i++ ) { 113 113 Bms[i] = new BM_T ( BMcond0 ); //copy constructor 114 epdf& pom=Bms[i]->_epdf();114 const epdf& pom=Bms[i]->_epdf(); 115 115 jest.set_elements ( i,1.0/n,&pom ); 116 116 } … … 121 121 122 122 void bayes ( const vec &dt ); 123 epdf& _epdf(){return jest;}123 const epdf& _epdf() const {return jest;} 124 124 //! Set postrior of \c rvc to samples from epdf0. Statistics of Bms are not re-computed! Use only for initialization! 125 125 void set_est ( const epdf& epdf0 ) { … … 197 197 delete Bms[i]; 198 198 Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor 199 epdf& pom=Bms[i]->_epdf();199 const epdf& pom=Bms[i]->_epdf(); 200 200 jest.set_elements ( i,1.0/n,&pom ); 201 201 } -
bdm/math/libDC.cpp
r168 r170 157 157 double x = 0.0, sum; 158 158 int i,j; 159 vec s(v.length());160 vec S=L*v;161 159 162 160 for ( i=0; i<D.length(); i++ ) { //rows of L … … 164 162 for ( j=0; j<=i; j++ ){sum+=L( i,j )*v( j );} 165 163 x +=D( i )*sum*sum; 166 s(i)=sum;167 164 }; 168 165 return x; -
bdm/stat/emix.h
r168 r170 139 139 protected: 140 140 //! Components (epdfs) 141 Array< epdf*> epdfs;141 Array<const epdf*> epdfs; 142 142 //! Array of indeces 143 143 Array<ivec> rvinds; 144 144 public: 145 eprod ( const Array< epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) {145 eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) { 146 146 bool independent=true; 147 147 for ( int i=0;i<epdfs.length();i++ ) { … … 175 175 return tmp; 176 176 } 177 //!access function 178 const epdf* operator () (int i) const {it_assert_debug(i<epdfs.length(),"wrong index");return epdfs(i);} 177 179 }; 178 180 -
bdm/stat/libBM.cpp
r168 r170 180 180 return pom; 181 181 } 182 183 void RV::newids(){ids=linspace ( RVcounter+1, RVcounter+len ),RVcounter+=len;} 184 185 void BM::bayesB(const mat &Data){ 186 for(int t=0;t<Data.cols();t++){bayes(Data.get_col(t));} 187 } -
bdm/stat/libBM.h
r168 r170 103 103 //!access function 104 104 std::string name ( int at ) {return names ( at );}; 105 //!Assign unused ids to this rv 106 void newids(); 105 107 }; 106 108 … … 144 146 epdf ( const RV &rv0 ) :rv ( rv0 ) {}; 145 147 146 //! Returns the required moment of the epdf148 // //! Returns the required moment of the epdf 147 149 // virtual vec moment ( const int order = 1 ); 150 148 151 //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ 149 152 virtual vec sample () const =0; … … 156 159 virtual double evalpdflog ( const vec &val ) const =0; 157 160 161 //! Compute log-probability of multiple values argument \c val 162 virtual vec evalpdflog ( const mat &Val ) const { 163 vec x ( Val.cols() ); 164 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evalpdflog( Val.get_col(i) ) ;} 165 return x; 166 } 167 158 168 //! return expected value 159 169 virtual vec mean() const =0; … … 162 172 virtual ~epdf() {}; 163 173 //! access function, possibly dangerous! 164 RV& _rv() {return rv;} 174 const RV& _rv() const {return rv;} 175 //! modifier function - useful when copying epdfs 176 void _renewrv(const RV &in_rv){rv=in_rv;} 165 177 }; 166 178 … … 270 282 271 283 //!Default constructor 272 BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true) {//Fixme: test rv284 BM ( const RV &rv0, double ll0=0,bool evalll0=true ) :rv ( rv0 ), ll ( ll0 ),evalll ( evalll0) {//Fixme: test rv 273 285 }; 286 //!Copy constructor 287 BM (const BM &B) : rv(B.rv), ll(B.ll), evalll(B.evalll) {} 274 288 275 289 /*! \brief Incremental Bayes rule … … 278 292 virtual void bayes ( const vec &dt ) = 0; 279 293 //! Batch Bayes rule (columns of Dt are observations) 280 v oid bayes ( matDt );294 virtual void bayesB (const mat &Dt ); 281 295 //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! 282 virtual epdf& _epdf() =0; 283 296 virtual const epdf& _epdf() const =0; 297 298 //! Evaluates predictive log-likelihood of the given data record 299 //! I.e. marginal likelihood of the data with the posterior integrated out. 300 virtual double logpred(const vec &dt)const{it_error("Not implemented");return 0.0;} 301 284 302 //! Destructor for future use; 285 303 virtual ~BM() {}; … … 290 308 //!access function 291 309 void set_evalll(bool evl0){evalll=evl0;} 310 311 //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 312 //! Prototype: BM* _copy_(){BM Tmp*=new Tmp(this*); return Tmp; } 313 virtual BM* _copy_(bool changerv=false){it_error("function _copy_ not implemented for this BM"); return NULL;}; 292 314 }; 293 315 -
bdm/stat/libEF.cpp
r168 r170 12 12 using std::cout; 13 13 14 void BMEF::bayes ( const vec &dt ) {this->bayes ( dt,1.0 );}; 15 14 16 vec egiw::sample() const { 15 17 it_warning ( "Function not implemented" ); … … 17 19 } 18 20 19 double egiw::evalpdflog ( const vec &val ) const { 21 double egiw::evalpdflog_nn ( const vec &val ) const { 22 int vend = val.length()-1; 20 23 21 24 if ( xdim==1 ) { //same as the following, just quicker. 22 double r = val ( v al.length()-1); //last entry!25 double r = val ( vend ); //last entry! 23 26 vec Psi ( nPsi+xdim ); 24 27 Psi ( 0 ) = -1.0; 25 Psi.set_subvector ( 1,val ); // fill the rest 26 27 return -0.5* ( nu*log ( r ) + V.qform ( Psi ) /r ) + lognc(); 28 Psi.set_subvector ( 1,val ( 0,vend-1 ) ); // fill the rest 29 30 double Vq=V.qform ( Psi ); 31 return -0.5* ( nu*log ( r ) + Vq /r ); 28 32 } 29 33 else { 30 int vend = val.length()-1;31 34 mat Th= reshape ( val ( 0,nPsi*xdim-1 ),nPsi,xdim ); 32 35 fsqmat R ( reshape ( val ( nPsi*xdim,vend ),xdim,xdim ) ); … … 34 37 fsqmat iR ( xdim ); 35 38 R.inv ( iR ); 36 37 return -0.5* ( nu*R.logdet() + trace ( iR.to_mat() *Tmp.T() *V.to_mat() *Tmp ) ) + lognc();39 40 return -0.5* ( nu*R.logdet() + trace ( iR.to_mat() *Tmp.T() *V.to_mat() *Tmp ) ); 38 41 } 39 42 } … … 55 58 - 0.5*xdim* ( m*log2 + 0.5* ( xdim-1 ) *log2pi ) - lg; 56 59 57 return nkG+nkW;60 return -nkG-nkW; 58 61 } 59 62 60 63 vec egiw::mean() const { 64 65 if ( xdim==1 ) { 66 const mat &L= V._L(); 67 const vec &D= V._D(); 68 int end = L.rows()-1; 69 70 vec m ( rv.count() ); 71 mat iLsub = ltuinv ( L ( xdim,end,xdim,end ) ); 72 73 vec L0 = L.get_col ( 0 ); 74 75 m.set_subvector ( 0,iLsub*L0 ( 1,end ) ); 76 m ( end ) = D ( 0 ) / ( nu -nPsi -2*xdim -2 ); 77 return m; 78 } else { 79 mat M; 80 mat R; 81 mean_mat(M,R); 82 return cvectorize (concat_vertical(M,R)); 83 } 84 85 } 86 void egiw::mean_mat(mat &M, mat&R) const { 61 87 const mat &L= V._L(); 62 88 const vec &D= V._D(); 63 89 int end = L.rows()-1; 64 65 vec m(rv.count());66 mat iLsub =ltuinv ( L ( xdim,end,xdim,end ) );90 91 ldmat ldR ( L ( 0,xdim-1,0,xdim-1 ), D ( 0,xdim-1 ) / ( nu -nPsi -2*xdim -2 ) ); //exp val of R 92 mat iLsub=ltuinv ( L ( xdim,end,xdim,end ) ); 67 93 68 if ( xdim==1 ) { 69 vec L0 = L.get_col ( 0 ); 70 71 m.set_subvector ( 0,iLsub*L0 ( 1,end ) ); 72 m ( end ) = D ( 0 ) / ( nu -nPsi -2*xdim -2); 73 } 74 else { 75 ldmat ldR(L(0,xdim,0,xdim), D(0,xdim)/( nu -nPsi -2*xdim -2)); //exp val of R 76 mat iLsub=ltuinv ( L ( xdim,end,xdim,end ) ); 77 mat M = iLsub*L(xdim,end,0,xdim); 78 79 m = cvectorize(concat_vertical(M,ldR.to_mat())); 80 } 81 return m; 82 } 94 // set mean value 95 M= iLsub*L ( xdim,end,0,xdim-1 ); 96 R= ldR.to_mat() ; 97 } 98 99 void egiw::pow(double p){V*=p;nu*=p;} 83 100 84 101 vec egamma::sample() const { -
bdm/stat/libEF.h
r168 r170 41 41 //! default constructor 42 42 eEF ( const RV &rv ) :epdf ( rv ) {}; 43 //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ 44 virtual double lognc() const =0;43 //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ 44 virtual double lognc() const =0; 45 45 //!TODO decide if it is really needed 46 virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; 47 //!TODO decide if it is really needed 48 virtual void dupdate ( mat &v,double nu=1.0 ) {}; 46 virtual void dupdate ( mat &v ) {it_error ( "Not implemneted" );}; 47 //!Evaluate normalized log-probability 48 virtual double evalpdflog_nn ( const vec &val ) const{it_error ( "Not implemneted" );return 0.0;}; 49 //!Evaluate normalized log-probability 50 virtual double evalpdflog ( const vec &val ) const {return evalpdflog_nn ( val )-lognc();} 51 //!Evaluate normalized log-probability for many samples 52 virtual vec evalpdflog ( const mat &Val ) const { 53 vec x ( Val.cols() ); 54 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evalpdflog_nn ( Val.get_col ( i ) ) ;} 55 return x-lognc(); 56 } 57 //!Power of the density, used e.g. to flatten the density 58 virtual void pow ( double p ) {it_error ( "Not implemented" );}; 49 59 }; 50 60 … … 60 70 //! Default constructor 61 71 mEF ( const RV &rv0, const RV &rvc0 ) :mpdf ( rv0,rvc0 ) {}; 72 }; 73 74 //! Estimator for Exponential family 75 class BMEF : public BM { 76 protected: 77 //! forgetting factor 78 double frg; 79 //! cached value of lognc() in the previous step (used in evaluation of \c ll ) 80 double last_lognc; 81 public: 82 //! Default constructor 83 BMEF ( const RV &rv, double frg0=1.0 ) :BM ( rv ), frg ( frg0 ) {} 84 //! Copy constructor 85 BMEF ( const BMEF &B ) :BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {} 86 //!get statistics from another model 87 virtual void set_statistics ( const BMEF* BM0 ) {it_error ( "Not implemented" );}; 88 //! Weighted update of sufficient statistics (Bayes rule) 89 virtual void bayes ( const vec &data, const double w ) {}; 90 //original Bayes 91 void bayes ( const vec &dt ); 92 //!Flatten the posterior 93 virtual void flatten ( BMEF * B) {it_error ( "Not implemented" );} 62 94 }; 63 95 … … 99 131 //! returns a pointer to the internal mean value. Use with Care! 100 132 vec& _mu() {return mu;} 101 133 102 134 //! access function 103 void set_mu (const vec mu0) { mu=mu0;}135 void set_mu ( const vec mu0 ) { mu=mu0;} 104 136 105 137 //! returns pointers to the internal variance and its inverse. Use with Care! … … 128 160 public: 129 161 //!Default constructor, assuming 130 egiw(RV rv, mat V0, double nu0): eEF(rv), V(V0), nu(nu0) { 131 xdim = rv.count()/V.rows(); 132 it_assert_debug(rv.count()==xdim*V.rows(),"Incompatible V0."); 162 egiw ( RV rv, mat V0, double nu0 ) : eEF ( rv ), V ( V0 ), nu ( nu0 ) { 163 xdim = rv.count() /V.rows(); 164 it_assert_debug ( rv.count() ==xdim*V.rows(),"Incompatible V0." ); 165 nPsi = V.rows()-xdim; 166 } 167 //!Full constructor for V in ldmat form 168 egiw ( RV rv, ldmat V0, double nu0 ) : eEF ( rv ), V ( V0 ), nu ( nu0 ) { 169 xdim = rv.count() /V.rows(); 170 it_assert_debug ( rv.count() ==xdim*V.rows(),"Incompatible V0." ); 133 171 nPsi = V.rows()-xdim; 134 172 } … … 136 174 vec sample() const; 137 175 vec mean() const; 176 void mean_mat ( mat &M, mat&R ) const; 138 177 //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] 139 double evalpdflog ( const vec &val ) const;178 double evalpdflog_nn ( const vec &val ) const; 140 179 double lognc () const; 141 180 … … 145 184 //! returns a pointer to the internal statistics. Use with Care! 146 185 double& _nu() {return nu;} 147 186 void pow ( double p ); 187 }; 188 189 /*! \brief Dirichlet posterior density 190 Continuous Dirichlet density of \f$n\f$-dimensional variable \f$x\f$ 191 \f[ 192 f(x|\beta) = \frac{\Gamma[\gamma]}{\prod_{i=1}^{n}\Gamma(\beta_i)} \prod_{i=1}^{n]x_i^(\beta_i-1) 193 \f] 194 where \f$\gamma=\sum_i beta_i\f$. 195 */ 196 class eDirich: public eEF { 197 protected: 198 //!sufficient statistics 199 vec beta; 200 public: 201 //!Default constructor 202 eDirich ( const RV &rv, const vec &beta0 ) : eEF ( rv ),beta ( beta0 ) {it_assert_debug ( rv.count() ==beta.length(),"Incompatible statistics" ); }; 203 //! Copy constructor 204 eDirich ( const eDirich &D0 ) : eEF ( D0.rv ),beta ( D0.beta ) {}; 205 vec sample() const {it_error ( "Not implemented" );return vec_1 ( 0.0 );}; 206 vec mean() const {return beta/sum ( beta );}; 207 //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] 208 double evalpdflog_nn ( const vec &val ) const {return ( beta-1 ) *log ( val );}; 209 double lognc () const { 210 double gam=sum ( beta ); 211 double lgb=0.0; 212 for ( int i=0;i<beta.length();i++ ) {lgb+=lgamma ( beta ( i ) );} 213 return lgb-lgamma ( gam ); 214 }; 215 //!access function 216 vec& _beta() {return beta;} 217 }; 218 219 //! Estimator for Multinomial density 220 class multiBM : public BMEF { 221 protected: 222 //! Conjugate prior and posterior 223 eDirich est; 224 vec β 225 public: 226 //!Default constructor 227 multiBM ( const RV &rv, const vec beta0 ) : BMEF ( rv ),est ( rv,beta0 ),beta ( est._beta() ) {last_lognc=est.lognc();} 228 //!Copy constructor 229 multiBM ( const multiBM &B ) : BMEF ( B ),est ( rv,B.beta ),beta ( est._beta() ) {} 230 231 void set_statistics ( const BM* mB0 ) {const multiBM* mB=dynamic_cast<const multiBM*> ( mB0 ); beta=mB->beta;} 232 void bayes ( const vec &dt ) { 233 if ( frg<1.0 ) {beta*=frg;last_lognc=est.lognc();} 234 beta+=dt; 235 if ( evalll ) {ll=est.lognc()-last_lognc;} 236 } 237 double logpred ( const vec &dt ) const { 238 eDirich pred ( est ); 239 vec &beta = pred._beta(); 240 241 double lll; 242 if ( frg<1.0 ) 243 {beta*=frg;lll=pred.lognc();} 244 else 245 if ( evalll ) {lll=last_lognc;} 246 else{lll=pred.lognc();} 247 248 beta+=dt; 249 return pred.lognc()-lll; 250 } 251 void flatten (BMEF* B ) { 252 eDirich* E=dynamic_cast<eDirich*>(B); 253 // sum(beta) should be equal to sum(B.beta) 254 const vec &Eb=E->_beta(); 255 est.pow ( sum(beta)/sum(Eb) ); 256 if(evalll){last_lognc=est.lognc();} 257 } 258 const epdf& _epdf() const {return est;}; 259 //!access funct 148 260 }; 149 261 … … 151 263 \brief Gamma posterior density 152 264 153 Mult variate Gamma density as product of independent univariate densities.265 Multivariate Gamma density as product of independent univariate densities. 154 266 \f[ 155 267 f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) … … 213 325 double evalpdflog ( const vec &val ) const {return lnk;} 214 326 vec sample() const { 215 vec smp ( rv.count() ); 216 327 vec smp ( rv.count() ); 328 #pragma omp critical 217 329 UniRNG.sample_vector ( rv.count(),smp ); 218 return low+elem_mult (distance,smp);330 return low+elem_mult ( distance,smp ); 219 331 } 220 332 //! set values of \c low and \c high … … 408 520 double enorm<sq_T>::evalpdflog ( const vec &val ) const { 409 521 // 1.83787706640935 = log(2pi) 410 return -0.5* ( 522 return -0.5* ( +R.invqform ( mu-val ) ) - lognc(); 411 523 }; 412 524 … … 414 526 inline double enorm<sq_T>::lognc () const { 415 527 // 1.83787706640935 = log(2pi) 416 return -0.5* ( R.cols() * 1.83787706640935 +R.logdet()); 417 }; 418 419 template<class sq_T> 420 mlnorm<sq_T>::mlnorm ( RV &rv0,RV &rvc0 ) :mEF ( rv0,rvc0 ),epdf ( rv0 ),A ( rv0.count(),rv0.count() ),_mu(epdf._mu()) { ep =&epdf; 528 return -0.5* ( R.cols() * 1.83787706640935 +R.logdet() ); 529 }; 530 531 template<class sq_T> 532 mlnorm<sq_T>::mlnorm ( RV &rv0,RV &rvc0 ) :mEF ( rv0,rvc0 ),epdf ( rv0 ),A ( rv0.count(),rv0.count() ),_mu ( epdf._mu() ) { 533 ep =&epdf; 421 534 } 422 535 -
pmsm/pmsm_mix.cpp
r162 r170 83 83 M.set_est ( evolQR._epdf() ); 84 84 85 epdf& Efix_ep = Efix._epdf();86 epdf& M_ep = M._epdf();85 const epdf& Efix_ep = Efix._epdf(); 86 const epdf& M_ep = M._epdf(); 87 87 88 88 //LOG -
pmsm/pmsm_sim.cpp
r162 r170 72 72 // 73 73 74 epdf& KFEep = KFE._epdf();75 epdf& Mep = M._epdf();74 const epdf& KFEep = KFE._epdf(); 75 const epdf& Mep = M._epdf(); 76 76 77 77 mat Xt=zeros ( Ndat ,9 ); //true state from simulator -
pmsm/pmsm_sim2.cpp
r162 r170 107 107 evolQ.set_parameters ( 100000.0, Qdiag, 0.9999 ); 108 108 // 109 epdf& KFEep = KFE._epdf();110 epdf& Mep = M._epdf();109 const epdf& KFEep = KFE._epdf(); 110 const epdf& Mep = M._epdf(); 111 111 112 112 int X_log = L.add(rx,"X"); -
pmsm/pmsm_unkQ.cpp
r72 r170 62 62 } 63 63 64 epdf& KFEep = KFE._epdf();64 const epdf& KFEep = KFE._epdf(); 65 65 66 66 //simulator values -
pmsm/pmsm_unkQpf.cpp
r162 r170 62 62 MPF<EKF_unQ> M ( rx,rQ,evolQ,evolQ,100,KFE ); 63 63 64 epdf& KFEep = KFE._epdf();65 epdf& Mep = M._epdf();64 const epdf& KFEep = KFE._epdf(); 65 const epdf& Mep = M._epdf(); 66 66 // initialize 67 67 evolQ.set_parameters ( 1.0 ); //sigma = 1/10 mu 68 68 evolQ.condition ( "0.5 0.5" ); 69 epdf& pfinit=evolQ._epdf();69 const epdf& pfinit=evolQ._epdf(); 70 70 M.set_est ( pfinit ); 71 71 evolQ.set_parameters ( 1000.0 ); //sigma = 1/10 mu -
pmsm/sim_var.cpp
r162 r170 72 72 Edi.set_parameters ( &fxu,&hxu,Q,R); 73 73 74 epdf& Efix_ep = Efix._epdf();75 epdf& Eop_ep = Eop._epdf();76 epdf& Edi_ep = Edi._epdf();74 const epdf& Efix_ep = Efix._epdf(); 75 const epdf& Eop_ep = Eop._epdf(); 76 const epdf& Edi_ep = Edi._epdf(); 77 77 78 78 //LOG -
pmsm/sim_var_arx.cpp
r162 r170 45 45 ARX Ar_a ( rgr,V0,nu0 ,0.95 ); 46 46 ARX Ar_b ( rgr,V0,nu0 ,0.95 ); 47 epdf& pA= Ar_a._epdf();48 epdf& pB= Ar_b._epdf();47 const epdf& pA= Ar_a._epdf(); 48 const epdf& pB= Ar_b._epdf(); 49 49 50 50 RV rta ( "{th_a }",vec_1 ( rglen ) ); -
tests/arx_test.cpp
r162 r170 11 11 vec th("0.8 -0.3 0.4 0.01"); 12 12 int ord=th.length(); 13 double sqr= 1;13 double sqr=0.1; 14 14 15 15 //Test constructor 16 mat V0 = 0.00 1*eye(ord+1); V0(0.0)*= 10; //16 mat V0 = 0.00001*eye(ord+1); V0(0.0)*= 10; // 17 17 double nu0 = ord+1; 18 18 19 RV thr("{theta_and_r }" );19 RV thr("{theta_and_r }",vec_1(ord+1)); 20 20 ARX Ar(thr, V0, nu0); 21 epdf& Ar_ep = Ar._epdf();22 21 const epdf& Ar_ep = Ar._epdf(); 22 23 23 //Test estimation 24 int ndat = 1000 ;24 int ndat = 10000; 25 25 int t,j; 26 26 vec Yt(ndat); -
tests/test0.cpp
r162 r170 4 4 #include "../bdm/math/chmat.h" 5 5 6 #include <vector> 6 7 using namespace itpp; 7 8 -
tests/testKF.cpp
r162 r170 57 57 KF.set_parameters(A,B,C,D,chmat(R),chmat(Q)); 58 58 KF.set_est(mu0,chmat(P0) ); //prediction! 59 epdf& KFep = KF._epdf();59 const epdf& KFep = KF._epdf(); 60 60 mat Xt(dimx,Ndat); 61 61 Xt.set_col( 0,KFep.mean() ); … … 66 66 KFf.set_parameters(A,B,C,D,ldmat(R),ldmat(Q)); 67 67 KFf.set_est(mu0,ldmat(P0) ); 68 epdf& KFfep = KFf._epdf();68 const epdf& KFfep = KFf._epdf(); 69 69 mat Xtf(dimx,Ndat); 70 70 Xtf.set_col( 0,KFfep.mean() ); … … 82 82 KFE.set_parameters(&fxu,&hxu,Q,R); 83 83 KFE.set_est(mu0,chmat(P0)); 84 epdf& KFEep = KFE._epdf();84 const epdf& KFEep = KFE._epdf(); 85 85 mat XtE(dimx,Ndat); 86 86 XtE.set_col( 0,KFEep.mean() ); -
tests/testKF_QR.cpp
r162 r170 65 65 epdf& pfinit=evolQR._epdf(); 66 66 KF_QR.set_est(pfinit); 67 epdf& mpost=KF_QR._epdf();68 epdf& mposttr=KFtr._epdf();67 const epdf& mpost=KF_QR._epdf(); 68 const epdf& mposttr=KFtr._epdf(); 69 69 70 70 XQRt.set_col( 0,mpost.mean()); -
tests/testSmp.cpp
r162 r170 99 99 cout << "======= EProd ======== " << endl; 100 100 // we have to change eG.rv to y 101 eG._rv()= y; 101 egamma eGy(x); 102 eGy.set_parameters(a,b); 102 103 //create array 103 104 Array<mpdf*> A(2); 104 105 mepdf meN(eN); 105 mepdf meG(eG );106 mepdf meG(eGy); 106 107 A(0) = &meN; 107 108 A(1) = &meG; … … 114 115 vec v0=vec(0); 115 116 Smp = eP.samplecond(v0,lik,N); 116 disp(concat(eN.mean(),eG .mean()), epV,Smp);117 disp(concat(eN.mean(),eGy.mean()), epV,Smp); 117 118 118 119 //Exit program: