- Timestamp:
- 05/25/10 20:55:06 (15 years ago)
- Location:
- library/bdm
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.cpp
r965 r979 421 421 root::log_register ( L, prefix ); 422 422 423 if (dimension()==0) return; 424 423 425 RV r; 424 426 if ( isnamed() ) { -
library/bdm/estim/arx.cpp
r973 r979 223 223 BMEF::from_setting(set); 224 224 225 RV rrv; 226 UI::get (rrv, set, "rgr", UI::compulsory ); 225 UI::get (rgr, set, "rgr", UI::compulsory ); 227 226 228 227 dimy = yrv._dsize(); 229 228 bdm_assert(dimy>0,"ARX::yrv should not be empty"); 230 229 // rgrlen - including constant!!! 231 rgrlen = r rv._dsize();230 rgrlen = rgr._dsize(); 232 231 233 232 int constant; … … 238 237 } 239 238 dimc = rgrlen; 240 rvc = r rv;239 rvc = rgr; 241 240 rgrlen += int ( have_constant == true ); 242 241 -
library/bdm/estim/arx.h
r973 r979 47 47 //! vector of dyadic update 48 48 vec dyad; 49 //! RV of regressor 50 RV rgr; 49 51 //! length of the regressor 50 52 int rgrlen; … … 165 167 { 166 168 BMEF::to_setting( set ); // takes care of rv, yrv, rvc 169 UI::save(rgr, set, "rgr"); 167 170 int constant = have_constant ? 1 : 0; 168 171 UI::save(constant, set, "constant"); 169 UI::save(&est, set, "prior");170 172 UI::save(&alter_est, set, "alternative"); 171 173 UI::save(&posterior(), set, "posterior"); 172 174 173 175 } -
library/bdm/estim/particles.h
r974 r979 465 465 UIREGISTER ( PF ); 466 466 467 /*! Marginalized particle for state-space models with unknown parameters of distribuution of residues on \f$v_t\f$. 468 469 \f{eqnarray*}{ 470 x_t &=& g(x_{t-1}) + v_t,\\ 471 y_t &\sim &fy(x_t), 472 \f} 473 474 This particle is a only a shell creating the residues calling internal estimator of their parameters. The internal estimator can be of any compatible type, e.g. ARX for Gaussian residues with unknown mean and variance. 475 476 */ 477 class NoiseParticleX : public MarginalizedParticleBase{ 478 protected: 479 //! function transforming xt, ut -> x_t+1 480 shared_ptr<fnc> g; // pdf for non-linear part 481 //! function transforming xt,ut -> yt 482 shared_ptr<pdf> fy; // pdf for non-linear part 483 484 RV rvx; 485 RV rvxc; 486 RV rvyc; 487 488 //!link from condition to f 489 datalink_part cond2g; 490 //!link from condition to h 491 datalink_part cond2fy; 492 //!link from xt to f 493 datalink_part x2g; 494 //!link from xt to h 495 datalink_part x2fy; 496 497 public: 498 BM* _copy() const{return new NoiseParticleX(*this);}; 499 void bayes(const vec &dt, const vec &cond){ 500 shared_ptr<epdf> pred_v=bm->epredictor(); 501 502 vec vt=pred_v->sample(); 503 504 //new sample 505 vec &xtm=est_emp.point; 506 vec g_args(g->dimensionc()); 507 x2g.filldown(xtm,g_args); 508 cond2g.filldown(cond,g_args); 509 vec xt = g->eval(g_args) + vt; 510 est_emp.point=xt; 511 512 // the vector [v_t] updates bm, 513 bm->bayes(vt); 514 515 // residue of observation 516 vec fy_args(fy->dimensionc()); 517 x2fy.filldown(xt,fy_args); 518 cond2fy.filldown(cond,fy_args); 519 520 ll=bm->_ll() + fy->evallogcond(dt,fy_args); 521 } 522 void from_setting(const Setting &set){ 523 MarginalizedParticleBase::from_setting(set); //reads bm, yrv,rvc, bm_rv, etc... 524 525 g=UI::build<fnc>(set,"g",UI::compulsory); 526 fy=UI::build<pdf>(set,"fy",UI::compulsory); 527 UI::get(rvx,set,"rvx",UI::compulsory); 528 est_emp.set_rv(rvx); 529 530 UI::get(rvxc,set,"rvxc",UI::compulsory); 531 UI::get(rvyc,set,"rvyc",UI::compulsory); 532 533 } 534 void validate(){ 535 MarginalizedParticleBase::validate(); 536 537 dimy = fy->dimension(); 538 bm->set_yrv(rvx); 539 540 est_emp.set_rv(rvx); 541 est_emp.set_dim(rvx._dsize()); 542 est.validate(); 543 // 544 //check dimensions 545 rvc = rvxc.subt(rvx.copy_t(-1)); 546 rvc.add( rvyc); 547 rvc=rvc.subt(rvx); 548 549 bdm_assert(g->dimension()==rvx._dsize(),"rvx is not described"); 550 bdm_assert(g->dimensionc()==rvxc._dsize(),"rvxc is not described"); 551 bdm_assert(fy->dimensionc()==rvyc._dsize(),"rvyc is not described"); 552 553 bdm_assert(bm->dimensiony()==g->dimension(), 554 "Incompatible noise estimator of dimension " + 555 num2str(bm->dimensiony()) + " does not match dimension of g , " + 556 num2str(g->dimension())); 557 558 dimc = rvc._dsize(); 559 560 //establish datalinks 561 x2g.set_connection(rvxc, rvx.copy_t(-1)); 562 cond2g.set_connection(rvxc, rvc); 563 564 x2fy.set_connection(rvyc, rvx); 565 cond2fy.set_connection(rvyc, rvc); 566 } 567 }; 568 UIREGISTER(NoiseParticleX); 569 467 570 /*! Marginalized particle for state-space models with unknown parameters of residues distribution 468 571 469 \f[ 470 \begin{eqnarray} 471 x_t = g(x_{t-1}) + v_t,\\ 472 z_t = h(x_{t-1}) + w_t,\\ 473 \end{eqnarray} 474 \f] 475 476 This particle is a only a shell creating the residues calling internal estimator of their parameters. The internal estimator can be of any compatible type, e.g. ARX for Gaussian residues with unknown mean and variance. 477 478 */ 572 \f{eqnarray*}{ 573 x_t &=& g(x_{t-1}) + v_t,\\ 574 z_t &= &h(x_{t-1}) + w_t, 575 \f} 576 577 This particle is a only a shell creating the residues calling internal estimator of their parameters. The internal estimator can be of any compatible type, e.g. ARX for Gaussian residues with unknown mean and variance. 578 579 */ 479 580 class NoiseParticle : public MarginalizedParticleBase{ 480 581 protected: … … 496 597 //!link from xt to h 497 598 datalink_part x2h; 498 599 499 600 public: 500 601 BM* _copy() const{return new NoiseParticle(*this);}; … … 524 625 void from_setting(const Setting &set){ 525 626 MarginalizedParticleBase::from_setting(set); //reads bm, yrv,rvc, bm_rv, etc... 526 627 527 628 UI::get(g,set,"g",UI::compulsory); 528 629 UI::get(h,set,"h",UI::compulsory); … … 532 633 UI::get(rvxc,set,"rvxc",UI::compulsory); 533 634 UI::get(rvyc,set,"rvyc",UI::compulsory); 635 534 636 } 535 637 void validate(){ … … 554 656 bdm_assert(bm->dimensiony()==g->dimension()+h->dimension(), 555 657 "Incompatible noise estimator of dimension " + 556 557 558 559 dimc = rvc._dsize();560 561 //establish datalinks562 x2g.set_connection(rvxc, rvx.copy_t(-1));563 cond2g.set_connection(rvxc, rvc);564 565 x2h.set_connection(rvyc, rvx);566 cond2h.set_connection(rvyc, rvc);658 num2str(bm->dimensiony()) + " does not match dimension of g and h, " + 659 num2str(g->dimension())+" and "+ num2str(h->dimension()) ); 660 661 dimc = rvc._dsize(); 662 663 //establish datalinks 664 x2g.set_connection(rvxc, rvx.copy_t(-1)); 665 cond2g.set_connection(rvxc, rvc); 666 667 x2h.set_connection(rvyc, rvx); 668 cond2h.set_connection(rvyc, rvc); 567 669 } 568 670 }; … … 570 672 571 673 572 573 674 } 574 675 #endif // KF_H -
library/bdm/stat/emix.h
r956 r979 278 278 Array<datalink*> dls; 279 279 //! interface for a factor 280 public: 280 281 virtual const epdf* factor(int i) const NOT_IMPLEMENTED(NULL); 281 282 //!number of factors 282 virtual const int no_factors() const =0; 283 public: 283 virtual const int no_factors() const NOT_IMPLEMENTED(0); 284 284 //! Default constructor 285 285 eprod_base () : dls (0) {}; … … 394 394 UIREGISTER ( mmix ); 395 395 396 397 //! Base class for all BM running as parallel update of internal BMs 398 399 class ProdBMBase : public BM { 400 protected : 401 Array<vec_from_vec> bm_yt; 402 Array<vec_from_2vec> bm_cond; 403 class eprod_bm : public eprod_base { 404 ProdBMBase & pb; 405 public : 406 eprod_bm(ProdBMBase &pb0): pb(pb0){} 407 const epdf* factor(int i ) const {return &(pb.bm(i)->posterior());} 408 const int no_factors() const {return pb.no_bms();} 409 } est; 410 public: 411 ProdBMBase():est(*this){} 412 virtual BM* bm(int i) NOT_IMPLEMENTED(NULL); 413 virtual int no_bms() const {return 0;} 414 const epdf& posterior() const {return est;} 415 void set_prior(const epdf *pri){ 416 const eprod_base* ep=dynamic_cast<const eprod_base*>(pri); 417 if (ep){ 418 bdm_assert(ep->no_factors()!=no_bms() , "Given prior has "+ num2str(ep->no_factors()) + " while this ProdBM has "+ 419 num2str(no_bms()) + "BMs"); 420 for (int i=0; i<no_bms(); i++){ 421 bm(i)->set_prior(ep->factor(i)); 422 } 423 } 424 } 425 426 void validate() { 427 est.validate(); 428 BM::validate(); 429 // set links 430 bm_yt.set_length(no_bms()); 431 bm_cond.set_length(no_bms()); 432 433 // 434 435 for (int i=0; i<no_bms(); i++){ 436 yrv.add(bm(i)->_yrv()); 437 rvc.add(bm(i)->_rvc()); 438 } 439 rvc=rvc.subt(yrv); 440 441 dimy = yrv._dsize(); 442 dimc = rvc._dsize(); 443 444 for (int i=0; i<no_bms(); i++){ 445 bm_yt(i).connect(bm(i)->_yrv(), yrv); 446 bm_cond(i).connect(bm(i)->_rvc(), yrv, rvc); 447 } 448 } 449 void bayes(const vec &dt, const vec &cond){ 450 ll=0; 451 for(int i=0;i<no_bms(); i++){ 452 bm_yt(i).update(dt); 453 bm_cond(i).update(dt,cond); 454 bm(i)->bayes(bm_yt(i), bm_cond(i)); 455 } 456 } 457 458 }; 459 460 class ProdBM: public ProdBMBase{ 461 protected: 462 Array<shared_ptr<BM> > BMs; 463 public: 464 virtual BM* bm(int i) {return BMs(i).get();} 465 virtual int no_bms() const {return BMs.length();} 466 void from_setting(const Setting &set){ 467 BM::from_setting(set); 468 UI::get(BMs,set,"BMs"); 469 } 470 void to_setting(Setting &set) const{ 471 BM::to_setting(set); 472 UI::save(BMs,set,"BMs"); 473 } 474 475 }; 476 UIREGISTER(ProdBM); 477 396 478 } 397 479 #endif //MX_H