Changeset 737 for library/bdm/estim/particles.h
- Timestamp:
- 11/25/09 12:14:38 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/particles.h
r700 r737 41 41 //! internal structure storing loglikelihood of predictions 42 42 vec lls; 43 43 44 44 //! which resampling method will be used 45 45 RESAMPLING_METHOD resmethod; … … 47 47 //! For example, for 0.5 resampling is performed when the numebr of active aprticles drops belo 50%. 48 48 double res_threshold; 49 49 50 50 //! \name Options 51 51 //!@{ … … 57 57 PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ) { 58 58 }; 59 60 void set_parameters ( int n0, double res_th0=0.5, RESAMPLING_METHOD rm = SYSTEMATIC ) {59 60 void set_parameters ( int n0, double res_th0 = 0.5, RESAMPLING_METHOD rm = SYSTEMATIC ) { 61 61 n = n0; 62 62 res_threshold = res_th0; 63 63 resmethod = rm; 64 64 }; 65 void set_model ( shared_ptr<pdf> par0, shared_ptr<pdf> obs0 ) {65 void set_model ( shared_ptr<pdf> par0, shared_ptr<pdf> obs0 ) { 66 66 par = par0; 67 67 obs = obs0; 68 68 // set values for posterior 69 est.set_rv (par->_rv());69 est.set_rv ( par->_rv() ); 70 70 }; 71 71 void set_statistics ( const vec w0, const epdf &epdf0 ) { … … 73 73 }; 74 74 void set_statistics ( const eEmp &epdf0 ) { 75 bdm_assert_debug (epdf0._rv().equal(par->_rv()),"Incompatibel input");76 est =epdf0;75 bdm_assert_debug ( epdf0._rv().equal ( par->_rv() ), "Incompatibel input" ); 76 est = epdf0; 77 77 }; 78 78 //!@} … … 85 85 } 86 86 //! bayes I - generate samples and add their weights to lls 87 virtual void bayes_gensmp (const vec &ut);88 //! bayes II - compute weights of the 87 virtual void bayes_gensmp ( const vec &ut ); 88 //! bayes II - compute weights of the 89 89 virtual void bayes_weights(); 90 90 //! important part of particle filtering - decide if it is time to perform resampling 91 virtual bool do_resampling() {91 virtual bool do_resampling() { 92 92 double eff = 1.0 / ( _w * _w ); 93 93 return eff < ( res_threshold*n ); … … 95 95 void bayes ( const vec &yt, const vec &cond ); 96 96 //!access function 97 vec& __w() { return _w; } 97 vec& __w() { 98 return _w; 99 } 98 100 //!access function 99 vec& _lls() { return lls; } 101 vec& _lls() { 102 return lls; 103 } 100 104 //!access function 101 RESAMPLING_METHOD _resmethod() const { return resmethod; } 105 RESAMPLING_METHOD _resmethod() const { 106 return resmethod; 107 } 102 108 //! return correctly typed posterior (covariant return) 103 const eEmp& posterior() const {return est;} 104 109 const eEmp& posterior() const { 110 return est; 111 } 112 105 113 /*! configuration structure for basic PF 106 114 \code … … 115 123 \endcode 116 124 */ 117 void from_setting (const Setting &set){118 BM::from_setting (set);119 par = UI::build<pdf> (set,"parameter_pdf",UI::compulsory);120 obs = UI::build<pdf> (set,"observation_pdf",UI::compulsory);121 122 prior_from_set (set);123 resmethod_from_set (set);125 void from_setting ( const Setting &set ) { 126 BM::from_setting ( set ); 127 par = UI::build<pdf> ( set, "parameter_pdf", UI::compulsory ); 128 obs = UI::build<pdf> ( set, "observation_pdf", UI::compulsory ); 129 130 prior_from_set ( set ); 131 resmethod_from_set ( set ); 124 132 // set resampling method 125 133 //set drv 126 134 //find potential input - what remains in rvc when we subtract rv 127 RV u = par->_rvc().remove_time().subt ( par->_rv() );135 RV u = par->_rvc().remove_time().subt ( par->_rv() ); 128 136 //find potential input - what remains in rvc when we subtract x_t 129 RV obs_u = obs->_rvc().remove_time().subt ( par->_rv() );130 131 u.add (obs_u); // join both u, and check if they do not overlap132 133 set_yrv (obs->_rv() );137 RV obs_u = obs->_rvc().remove_time().subt ( par->_rv() ); 138 139 u.add ( obs_u ); // join both u, and check if they do not overlap 140 141 set_yrv ( obs->_rv() ); 134 142 rvc = u; 135 143 } 136 144 //! auxiliary function reading parameter 'resmethod' from configuration file 137 void resmethod_from_set (const Setting &set){145 void resmethod_from_set ( const Setting &set ) { 138 146 string resmeth; 139 if ( UI::get(resmeth,set,"resmethod",UI::optional)){140 if ( resmeth=="systematic") {141 resmethod = SYSTEMATIC;147 if ( UI::get ( resmeth, set, "resmethod", UI::optional ) ) { 148 if ( resmeth == "systematic" ) { 149 resmethod = SYSTEMATIC; 142 150 } else { 143 if ( resmeth=="multinomial"){144 resmethod =MULTINOMIAL;151 if ( resmeth == "multinomial" ) { 152 resmethod = MULTINOMIAL; 145 153 } else { 146 if ( resmeth=="stratified"){147 resmethod = STRATIFIED;154 if ( resmeth == "stratified" ) { 155 resmethod = STRATIFIED; 148 156 } else { 149 bdm_error ("Unknown resampling method");157 bdm_error ( "Unknown resampling method" ); 150 158 } 151 159 } 152 160 } 153 161 } else { 154 resmethod =SYSTEMATIC;162 resmethod = SYSTEMATIC; 155 163 }; 156 if (!UI::get(res_threshold, set, "res_threshold", UI::optional)){157 res_threshold =0.5;164 if ( !UI::get ( res_threshold, set, "res_threshold", UI::optional ) ) { 165 res_threshold = 0.5; 158 166 } 159 167 //validate(); 160 168 } 161 169 //! load prior information from set and set internal structures accordingly 162 void prior_from_set (const Setting & set){163 shared_ptr<epdf> pri = UI::build<epdf> (set,"prior",UI::compulsory);164 165 eEmp *test_emp =dynamic_cast<eEmp*>(&(*pri));166 if ( test_emp) { // given pdf is sampled167 est =*test_emp;170 void prior_from_set ( const Setting & set ) { 171 shared_ptr<epdf> pri = UI::build<epdf> ( set, "prior", UI::compulsory ); 172 173 eEmp *test_emp = dynamic_cast<eEmp*> ( & ( *pri ) ); 174 if ( test_emp ) { // given pdf is sampled 175 est = *test_emp; 168 176 } else { 169 177 //int n; 170 if (!UI::get(n,set,"n",UI::optional)){n=10;} 178 if ( !UI::get ( n, set, "n", UI::optional ) ) { 179 n = 10; 180 } 171 181 // sample from prior 172 set_statistics (ones(n)/n, *pri);182 set_statistics ( ones ( n ) / n, *pri ); 173 183 } 174 184 n = est._w().length(); 175 185 //validate(); 176 186 } 177 178 void validate() {179 bdm_assert (par,"PF::parameter_pdf not specified.");180 n =_w.length();181 lls =zeros(n);182 183 if ( par->_rv()._dsize()>0) {184 bdm_assert (par->_rv()._dsize()==est.dimension(),"Mismatch of RV and dimension of posterior" );187 188 void validate() { 189 bdm_assert ( par, "PF::parameter_pdf not specified." ); 190 n = _w.length(); 191 lls = zeros ( n ); 192 193 if ( par->_rv()._dsize() > 0 ) { 194 bdm_assert ( par->_rv()._dsize() == est.dimension(), "Mismatch of RV and dimension of posterior" ); 185 195 } 186 196 } 187 197 //! resample posterior density (from outside - see MPF) 188 void resample (ivec &ind){189 est.resample (ind,resmethod);198 void resample ( ivec &ind ) { 199 est.resample ( ind, resmethod ); 190 200 } 191 201 //! access function 192 Array<vec>& __samples(){return _samples;} 202 Array<vec>& __samples() { 203 return _samples; 204 } 193 205 }; 194 UIREGISTER (PF);206 UIREGISTER ( PF ); 195 207 196 208 /*! … … 202 214 203 215 class MPF : public BM { 204 205 216 protected: 217 //! particle filter on non-linear variable 206 218 shared_ptr<PF> pf; 207 219 //! Array of Bayesian models … … 217 229 public: 218 230 //! constructor 219 mpfepdf ( shared_ptr<PF> &pf0, Array<BM*> &BMs0): epdf(), pf(pf0), BMs(BMs0) { };231 mpfepdf ( shared_ptr<PF> &pf0, Array<BM*> &BMs0 ) : epdf(), pf ( pf0 ), BMs ( BMs0 ) { }; 220 232 //! a variant of set parameters - this time, parameters are read from BMs and pf 221 void read_parameters() {222 rv = concat (pf->posterior()._rv(), BMs(0)->posterior()._rv());223 dim = pf->posterior().dimension() + BMs (0)->posterior().dimension();224 bdm_assert_debug (dim == rv._dsize(), "Wrong name ");233 void read_parameters() { 234 rv = concat ( pf->posterior()._rv(), BMs ( 0 )->posterior()._rv() ); 235 dim = pf->posterior().dimension() + BMs ( 0 )->posterior().dimension(); 236 bdm_assert_debug ( dim == rv._dsize(), "Wrong name " ); 225 237 } 226 238 vec mean() const { 227 239 const vec &w = pf->posterior()._w(); 228 vec pom = zeros ( BMs (0)->posterior ().dimension() );240 vec pom = zeros ( BMs ( 0 )->posterior ().dimension() ); 229 241 //compute mean of BMs 230 242 for ( int i = 0; i < w.length(); i++ ) { … … 235 247 vec variance() const { 236 248 const vec &w = pf->posterior()._w(); 237 238 vec pom = zeros ( BMs (0)->posterior ().dimension() );239 vec pom2 = zeros ( BMs (0)->posterior ().dimension() );249 250 vec pom = zeros ( BMs ( 0 )->posterior ().dimension() ); 251 vec pom2 = zeros ( BMs ( 0 )->posterior ().dimension() ); 240 252 vec mea; 241 253 242 254 for ( int i = 0; i < w.length(); i++ ) { 243 // save current mean 255 // save current mean 244 256 mea = BMs ( i )->posterior().mean(); 245 257 pom += mea * w ( i ); … … 249 261 return concat ( pf->posterior().variance(), pom2 - pow ( pom, 2 ) ); 250 262 } 251 263 252 264 void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const { 253 265 //bounds on particles … … 305 317 //!datalink from PF part to BM 306 318 datalink_part pf2bm; 307 319 308 320 public: 309 321 //! Default constructor. 310 MPF () : jest ( pf,BMs) {};322 MPF () : jest ( pf, BMs ) {}; 311 323 //! set all parameters at once 312 324 void set_parameters ( shared_ptr<pdf> par0, shared_ptr<pdf> obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { 313 pf->set_model ( par0, obs0 );314 pf->set_parameters (n0, rm );325 pf->set_model ( par0, obs0 ); 326 pf->set_parameters ( n0, rm ); 315 327 BMs.set_length ( n0 ); 316 328 } … … 318 330 void set_BM ( const BM &BMcond0 ) { 319 331 320 int n =pf->__w().length();321 BMs.set_length (n);332 int n = pf->__w().length(); 333 BMs.set_length ( n ); 322 334 // copy 323 335 //BMcond0 .condition ( pf->posterior()._sample ( 0 ) ); … … 331 343 return jest; 332 344 } 333 //! Extends options understood by BM::set_options by option 334 //! \li logmeans - meaning 345 //! Extends options understood by BM::set_options by option 346 //! \li logmeans - meaning 335 347 void set_options ( const string &opt ) { 336 BM::set_options (opt);348 BM::set_options ( opt ); 337 349 } 338 350 … … 341 353 return BMs ( i ); 342 354 } 343 355 344 356 /*! configuration structure for basic PF 345 357 \code … … 352 364 // resampling method 353 365 \endcode 354 */ 355 void from_setting (const Setting &set){356 shared_ptr<pdf> par = UI::build<pdf> (set,"parameter_pdf",UI::compulsory);357 shared_ptr<pdf> obs = new pdf(); // not used!!366 */ 367 void from_setting ( const Setting &set ) { 368 shared_ptr<pdf> par = UI::build<pdf> ( set, "parameter_pdf", UI::compulsory ); 369 shared_ptr<pdf> obs = new pdf(); // not used!! 358 370 359 371 pf = new PF; 360 372 // prior must be set before BM 361 pf->prior_from_set (set);362 pf->resmethod_from_set (set);363 pf->set_model (par,obs);364 365 shared_ptr<BM> BM0 = UI::build<BM>(set,"BM",UI::compulsory);366 set_BM (*BM0);367 373 pf->prior_from_set ( set ); 374 pf->resmethod_from_set ( set ); 375 pf->set_model ( par, obs ); 376 377 shared_ptr<BM> BM0 = UI::build<BM> ( set, "BM", UI::compulsory ); 378 set_BM ( *BM0 ); 379 368 380 string opt; 369 if ( UI::get(opt,set,"options",UI::optional)){370 set_options (opt);381 if ( UI::get ( opt, set, "options", UI::optional ) ) { 382 set_options ( opt ); 371 383 } 372 384 //set drv 373 385 //??set_yrv(concat(BM0->_yrv(),u) ); 374 set_yrv (BM0->_yrv() );375 rvc = BM0->_rvc().subt (par->_rv());386 set_yrv ( BM0->_yrv() ); 387 rvc = BM0->_rvc().subt ( par->_rv() ); 376 388 //find potential input - what remains in rvc when we subtract rv 377 RV u = par->_rvc().subt ( par->_rv().copy_t(-1) );378 rvc.add (u);389 RV u = par->_rvc().subt ( par->_rv().copy_t ( -1 ) ); 390 rvc.add ( u ); 379 391 dimc = rvc._dsize(); 380 392 validate(); 381 393 } 382 void validate() {383 try {394 void validate() { 395 try { 384 396 pf->validate(); 385 } catch ( std::exception &e){386 throw UIException ("Error in PF part of MPF:");397 } catch ( std::exception &e ) { 398 throw UIException ( "Error in PF part of MPF:" ); 387 399 } 388 400 jest.read_parameters(); 389 this2bm.set_connection (BMs(0)->_yrv(), BMs(0)->_rvc(), yrv, rvc);390 this2pf.set_connection (pf->_yrv(), pf->_rvc(), yrv, rvc);391 pf2bm.set_connection (BMs(0)->_rvc(), pf->posterior()._rv());401 this2bm.set_connection ( BMs ( 0 )->_yrv(), BMs ( 0 )->_rvc(), yrv, rvc ); 402 this2pf.set_connection ( pf->_yrv(), pf->_rvc(), yrv, rvc ); 403 pf2bm.set_connection ( BMs ( 0 )->_rvc(), pf->posterior()._rv() ); 392 404 } 393 405 394 406 }; 395 UIREGISTER (MPF);407 UIREGISTER ( MPF ); 396 408 397 409 }