- Timestamp:
- 09/24/10 21:36:43 (14 years ago)
- Location:
- library
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/particles.cpp
r1187 r1196 46 46 log_level.store( logweights, w); 47 47 } 48 if (log_level[logNeff]) { 49 log_level.store( logNeff, 1.0 / ( w * w )); 50 } 48 51 49 52 if ( do_resampling() ) { -
library/bdm/estim/particles.h
r1192 r1196 258 258 //! \var log_level_enums logmeans 259 259 //! means of particles will be logged 260 LOG_LEVEL(PF,logweights,logmeans,logvars );260 LOG_LEVEL(PF,logweights,logmeans,logvars,logNeff); 261 261 262 262 class pf_mix: public emix_base { … … 392 392 void log_register ( bdm::logger& L, const string& prefix ) { 393 393 BM::log_register(L,prefix); 394 if (log_level[logweights]) { 395 L.add_vector( log_level, logweights, RV ( particles.length()), prefix); 396 } 397 if (log_level[logmeans]) { 394 if (log_level[logweights]) { 395 L.add_vector( log_level, logweights, RV ( particles.length()), prefix); 396 } 397 if (log_level[logNeff]) { 398 L.add_vector( log_level, logNeff, RV ( 1), prefix); 399 } 400 if (log_level[logmeans]) { 398 401 for (int i=0; i<particles.length(); i++) { 399 402 L.add_vector( log_level, logmeans, RV ( particles(i)->dimension() ), prefix , i); … … 548 551 //shared_ptr<epdf> pred_v=bm->epredictor(); 549 552 550 //vec vt=pred_v->sample(); 551 vec vt = bm->samplepred(); 553 vec xt; 554 vec xthat; 555 //vec vt=pred_v->sample(); 556 // vec vt = bm->samplepred(); 557 if (1){ 558 //vec vt=pred_v->sample(); 559 int dim_x = bm->dimensiony(); 560 int dim_y = dimensiony(); 561 562 vec post_mean =bm->posterior().mean(); 563 mat SigV; 564 if (post_mean.length()==dim_x){ 565 SigV=diag(vec(post_mean._data(), dim_x)); 566 } else { 567 SigV=mat(post_mean._data(), dim_x, dim_x); 568 } 569 570 vec &xtm=est_emp.point; 571 vec g_args(g->dimensionc()); 572 x2g.filldown(xtm,g_args); 573 cond2g.filldown(cond,g_args); 574 xthat = g->eval(g_args); 575 576 mat IC = eye(dim_x+dim_y); 577 IC.set_submatrix(0,0,eye(dim_x)); 578 mat SigJo=eye(dim_x + dim_y); 579 SigJo.set_submatrix(0,0,SigV); 580 mlnorm<ldmat>* mlfy=dynamic_cast<mlnorm<ldmat>* >(fy.get()); 581 582 SigJo.set_submatrix(dim_x,dim_x, mlfy->_R()); 583 IC.set_submatrix(dim_x,0,-mlfy->_A()); 584 585 mat iIC=inv(IC); 586 enorm<chmat> Jo; 587 Jo._mu()=iIC*concat(xthat, zeros(dim_y)); 588 Jo._R()=iIC*SigJo*iIC.T(); 589 Jo.set_rv(concat(bm->_yrv(),yrv)); 590 Jo.validate(); 591 shared_ptr<pdf> Cond=Jo.condition(bm->_yrv()); 592 //new sample 593 594 xt = Cond->samplecond(dt);// + vt; 595 } else{ 596 552 597 553 598 //new sample … … 556 601 x2g.filldown(xtm,g_args); 557 602 cond2g.filldown(cond,g_args); 558 vec xt = g->eval(g_args) + vt; 603 xthat = g->eval(g_args); 604 605 xt = xthat + bm->samplepred(); 606 607 } 559 608 est_emp.point=xt; 560 609 561 610 // the vector [v_t] updates bm, 562 bm->bayes( vt);611 bm->bayes(xt-xthat); 563 612 564 613 // residue of observation … … 635 684 */ 636 685 class NoiseParticleXY : public BM { 686 protected: 637 687 //! discrte particle 638 688 dirac est_emp; … … 804 854 UIREGISTER(NoiseParticleXY); 805 855 856 class NoiseParticleXYprop: public NoiseParticleXY{ 857 public: 858 BM* _copy() const { 859 return new NoiseParticleXYprop(*this); 860 }; 861 void bayes(const vec &dt, const vec &cond) { 862 int dim_x = bmx->dimensiony(); 863 int dim_y = bmy->dimensiony(); 864 865 //vec vt=pred_v->sample(); 866 mat SigV=mat(bmx->posterior().mean()._data(), dim_x, dim_x); 867 mat SigW=mat(bmy->posterior().mean()._data(), dim_y, dim_y); 868 869 vec &xtm=est_emp.point; 870 vec g_args(g->dimensionc()); 871 x2g.filldown(xtm,g_args); 872 cond2g.filldown(cond,g_args); 873 vec xthat = g->eval(g_args); 874 875 mat IC = eye(dim_x+dim_y); 876 IC.set_submatrix(0,0,eye(dim_x)); 877 mat SigJo=eye(dim_x + dim_y); 878 SigJo.set_submatrix(0,0,SigV); 879 SigJo.set_submatrix(dim_x,dim_x, 10*SigW); 880 881 diffbifn* hb=dynamic_cast<bilinfn*>(h.get()); 882 mat C=zeros(dim_y,dim_x); 883 if (hb){ 884 hb->dfdx_cond(xtm, cond, C,true); 885 IC.set_submatrix(dim_x,0,-C); 886 } 887 888 mat iIC=inv(IC); 889 enorm<chmat> Jo; 890 Jo._mu()=iIC*concat(xthat, zeros(dim_y)); 891 Jo._R()=iIC*SigJo*iIC.T(); 892 Jo.set_rv(concat(bmx->_yrv(), bmy->_yrv())); 893 Jo.validate(); 894 shared_ptr<pdf> Cond=Jo.condition(bmx->_yrv()); 895 //new sample 896 897 vec xt = Cond->samplecond(dt);// + vt; 898 899 est_emp.point=xt; 900 901 // the vector [v_t] updates bm, 902 bmx->bayes(xt-xthat); 903 904 // residue of observation 905 vec h_args(h->dimensionc()); 906 x2h.filldown(xt,h_args); 907 cond2h.filldown(cond,h_args); 908 909 vec z_y =h->eval(h_args)-dt; 910 // ARX *abm = dynamic_cast<ARX*>(bmy.get()); 911 /* double ll2; 912 i f (abm){ //ARX* 913 shared_ptr<epdf> pr_y(abm->epredictor_student(empty_vec)); 914 ll2=pr_y->evallog(z_y); 915 } else{ 916 shared_ptr<epdf> pr_y(bmy->epredictor(empty_vec)); 917 ll2=pr_y->evallog(z_y); 918 }*/ 919 920 bmy->bayes(z_y); 921 // test _lls 922 ll= bmy->_ll(); 923 } 924 }; 925 UIREGISTER(NoiseParticleXYprop); 926 806 927 /*! Marginalized particle for state-space models with unknown parameters of residues distribution 807 928 -
library/bdm/math/functions.h
r1184 r1196 184 184 bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." ); 185 185 bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." ); 186 return A*x0 + B*u0;186 return dimu>0 ? A*x0 + B*u0 : A*x0; 187 187 } 188 188 … … 196 196 if ( full ) F = B; //else : nothing has changed no need to regenerate 197 197 } 198 //!@} 199 }; 198 void from_setting(const Setting &set) { 199 diffbifn::from_setting(set); 200 UI::get(A,set,"A",UI::compulsory); 201 UI::get(B,set,"B",UI::compulsory); 202 } 203 void validate() { 204 dimy = A.rows(); 205 dimx = A.cols(); 206 dimu = B.cols(); 207 dimc =dimx+dimu; 208 } 209 //!@} 210 }; 211 UIREGISTER(bilinfn); 200 212 201 213 } //namespace -
library/bdm/stat/exp_family.h
r1189 r1196 384 384 UIREGISTER2(estudent,ldmat); 385 385 UIREGISTER2(estudent,chmat); 386 387 // template < class sq_T, template <typename> class TEpdf = estudent > 388 // class mstudent : public pdf_internal< TEpdf<sq_T> > { 389 // protected: 390 // //! Internal epdf that arise by conditioning on \c rvc 391 // mat A; 392 // //! Constant additive term 393 // vec mu_const; 394 // 395 // public: 396 // void condition( const vec &cond ) { 397 // iepdf._mu()=A*val*mu_const; 398 // } 399 // 400 // }; 386 401 387 402 /*! … … 1136 1151 class mlnorm : public pdf_internal< TEpdf<sq_T> > { 1137 1152 protected: 1138 1139 1140 1141 1142 // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT?1153 //! Internal epdf that arise by conditioning on \c rvc 1154 mat A; 1155 //! Constant additive term 1156 vec mu_const; 1157 // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? 1143 1158 public: 1144 1159 //! \name Constructors -
library/tests/testsuite/enorm.cfg
r810 r1196 25 25 }; 26 26 tolerance = 0.3; 27 }, 28 { 29 class = "epdf_harness"; 30 epdf = { 31 class = "enorm<ldmat>"; 32 mu = [ 1.1, -2.0, 3.0]; 33 R = ( "matrix", 3, 3, [ 1.0, -0.5, 0.8, -0.5, 2.0, 0.3, 0.8, 0.3, 3.0 ] ); 34 rv : 35 { 36 class = "RV"; 37 names = ( "x", "y", "z" ); 38 }; 39 }; 40 mean = [ 1.1, -2.0, 3.0 ]; 41 variance = [ 1, 2, 3 ]; 42 support = { 43 class = "rectangular_support"; 44 ranges = ( [ -5.0, 5.0 ], [-5.0, 5.0 ], [-1.0, 9.0] ); 45 gridsizes = [ 50, 50, 50 ]; 46 }; 47 R = ( "matrix", 3, 3, [ 1.0, -0.5, 0.8, -0.5, 2.0, 0.3, 0.8, 0.3, 3.0 ] ); 48 marginal_rv = { 49 class = "RV"; 50 names = ( "z" ); 51 }; 52 tolerance = 0.1; 27 53 }, 28 54 {