Changeset 811
- Timestamp:
- 02/21/10 20:58:51 (15 years ago)
- Files:
-
- 4 added
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/bdmtoolbox/mex/estimator.cpp
r801 r811 105 105 106 106 //DBG 107 //Cfg.writeFile ( "estimator.cfg" );107 Cfg.writeFile ( "estimator.cfg" ); 108 108 109 109 #else -
library/bdm/estim/particles.cpp
r766 r811 175 175 176 176 177 void MPF_ARXg::bayes ( const vec &yt, const vec &cond ) { 178 // follows PF::bayes in most places!!! 179 int i; 180 int n = pf->__w().length(); 181 vec &lls = pf->_lls(); 182 Array<vec> &samples = pf->__samples(); 183 184 // generate samples - time step 185 for (int i =0;i<n; i++){ 186 mat M; chmat R; 187 BMsp(i)->posterior().sample_mat(M,R); 188 vec diff=R._Ch().T()*randn(samples(i).length()); 189 samples(i) = g->eval(samples(i)) + diff; 190 //////////// 191 // samples(i) = cond; 192 ///////// 193 BMsp(i)->bayes(diff); 194 } 195 // weight them - data step 196 enorm<ldmat> ooo; 197 ooo.set_parameters(zeros(2),0.1*eye(2)); 198 ooo.validate(); 199 200 #pragma parallel for 201 for ( i = 0; i < n; i++ ) { 202 vec tmp=yt - h->eval(samples(i)); 203 BMso ( i ) -> bayes ( tmp ); 204 lls ( i ) += BMso ( i )->_ll(); 205 ///// 206 ooo._mu()=h->eval(samples(i)); 207 lls(i) = ooo.evallog_nn(yt); 208 } 209 210 pf->bayes_weights(); 211 212 ivec ind; 213 if ( pf->do_resampling() ) { 214 pf->resample ( ind ); 215 216 #pragma omp parallel for 217 for ( i = 0; i < n; i++ ) { 218 if ( ind ( i ) != i ) {//replace the current Bm by a new one 219 delete BMso ( i ); 220 BMso ( i ) = (ARX*) BMso ( ind ( i ) )->_copy(); //copy constructor 221 delete BMsp ( i ); 222 BMsp ( i ) = (ARX*) BMsp ( ind ( i ) )->_copy(); //copy constructor 223 } 224 }; 225 } 226 }; 227 228 void MPF_ARXg::from_setting(const libconfig::Setting& set) { 229 BM::from_setting(set); 230 231 pf = new PF; 232 // prior must be set before BM 233 pf->prior_from_set ( set ); 234 pf->resmethod_from_set ( set ); 235 236 string opt; 237 if ( UI::get ( opt, set, "options", UI::optional ) ) { 238 set_options ( opt ); 239 } 240 241 // read functions g and h 242 g=UI::build<fnc>(set,"g"); 243 h=UI::build<fnc>(set,"h"); 244 245 set_dim( g->dimension()); 246 dimy = h->dimension(); 247 248 shared_ptr<ARX> arxo=UI::build<ARX>(set,"arxo"); 249 shared_ptr<ARX> arxp=UI::build<ARX>(set,"arxp"); 250 int n = pf->__samples().length(); 251 BMso.set_length(n); 252 BMsp.set_length(n); 253 for(int i=0; i<n; i++){ 254 BMso(i)=arxo->_copy(); 255 BMsp(i)=arxp->_copy(); 256 } 257 258 yrv = arxo->_yrv(); 259 //rvc = arxo->_rvc(); 260 261 validate(); 262 263 } 264 265 177 266 } 178 267 //MPF::MPF:{} -
library/bdm/estim/particles.h
r766 r811 15 15 16 16 17 #include "../ stat/exp_family.h"17 #include "../estim/arx_ext.h" 18 18 19 19 namespace bdm { … … 183 183 } 184 184 n = est._w().length(); 185 //validate();185 lls=zeros(n); 186 186 } 187 187 … … 363 363 UIREGISTER ( MPF ); 364 364 365 /*! ARXg for estimation of state-space variances 366 */ 367 class MPF_ARXg :public BM{ 368 protected: 369 shared_ptr<PF> pf; 370 //! pointer to Array of BMs 371 Array<ARX*> BMso; 372 //! pointer to Array of BMs 373 Array<ARX*> BMsp; 374 //!parameter evolution 375 shared_ptr<fnc> g; 376 //!observation function 377 shared_ptr<fnc> h; 378 379 public: 380 void bayes(const vec &yt, const vec &cond ); 381 void from_setting(const Setting &set) ; 382 void validate() { 383 bdm_assert(g->dimensionc()==g->dimension(),"not supported yet"); 384 bdm_assert(h->dimensionc()==g->dimension(),"not supported yet"); 385 } 386 387 double logpred(const vec &cond) const NOT_IMPLEMENTED(0.0); 388 epdf* epredictor() const NOT_IMPLEMENTED(NULL); 389 pdf* predictor() const NOT_IMPLEMENTED(NULL); 390 const epdf& posterior() const {return pf->posterior();}; 391 392 void log_register( logger &L, const string &prefix ){ 393 BM::log_register(L,prefix); 394 logrec->ids.set_size ( 3 ); 395 logrec->ids(1)= L.add_vector(RV("Q",dimension()*dimension()), prefix+L.prefix_sep()+"Q"); 396 logrec->ids(2)= L.add_vector(RV("R",dimensiony()*dimensiony()), prefix+L.prefix_sep()+"R"); 397 398 }; 399 void log_write() const { 400 BM::log_write(); 401 mat mQ=zeros(dimension(),dimension()); 402 mat pom=zeros(dimension(),dimension()); 403 mat mR=zeros(dimensiony(),dimensiony()); 404 mat pom2=zeros(dimensiony(),dimensiony()); 405 mat dum; 406 const vec w=pf->posterior()._w(); 407 for (int i=0; i<w.length(); i++){ 408 BMsp(i)->posterior().mean_mat(dum,pom); 409 mQ += w(i) * pom; 410 BMso(i)->posterior().mean_mat(dum,pom2); 411 mR += w(i) * pom2; 412 413 } 414 logrec->L.log_vector ( logrec->ids ( 1 ), cvectorize(mQ) ); 415 logrec->L.log_vector ( logrec->ids ( 2 ), cvectorize(mR) ); 416 417 } 418 }; 419 UIREGISTER(MPF_ARXg); 420 421 365 422 } 366 423 #endif // KF_H -
library/bdm/math/chmat.cpp
r738 r811 73 73 vec chmat::sqrt_mult ( const vec &v ) const { 74 74 vec pom; 75 pom = Ch * v;75 pom = Ch.T() * v; 76 76 return pom; 77 77 } -
library/bdm/stat/exp_family.h
r809 r811 1094 1094 } 1095 1095 }; 1096 1096 1097 /*! 1097 1098 \brief Gamma random walk