Changeset 723 for library/bdm/estim/kalman.h
- Timestamp:
- 11/15/09 23:02:02 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/kalman.h
r703 r723 27 27 * \brief Basic elements of linear state-space model 28 28 29 Parameter evolution model:\f[ x_ t = A x_{t-1} + B u_t + Q^{1/2} e_t \f]30 Observation model: \f[ y_t = C x_{t -1} + C u_t + Q^{1/2} w_t. \f]31 Where $e_t$ and $w_t$ are independent vectorsNormal(0,1)-distributed disturbances.29 Parameter evolution model:\f[ x_{t+1} = A x_{t} + B u_t + Q^{1/2} e_t \f] 30 Observation model: \f[ y_t = C x_{t} + C u_t + R^{1/2} w_t. \f] 31 Where $e_t$ and $w_t$ are mutually independent vectors of Normal(0,1)-distributed disturbances. 32 32 */ 33 33 template<class sq_T> … … 61 61 UI::get (C, set, "C", UI::compulsory); 62 62 UI::get (D, set, "D", UI::compulsory); 63 mat Qtm, Rtm; 63 mat Qtm, Rtm; // full matrices 64 64 if(!UI::get(Qtm, set, "Q", UI::optional)){ 65 65 vec dq; … … 72 72 Rtm=diag(dr); 73 73 } 74 R=Rtm; // automatic conversion 74 R=Rtm; // automatic conversion to square-root form 75 75 Q=Qtm; 76 76 … … 519 519 520 520 */ 521 class StateFromARX: public StateSpace< fsqmat>{521 class StateFromARX: public StateSpace<chmat>{ 522 522 protected: 523 523 //! remember connection from theta ->A 524 524 datalink_part th2A; 525 //! xrv526 RV xrv;525 //! remember connection from theta ->B 526 datalink_part th2B; 527 527 //!function adds n diagonal elements from given starting point r,c 528 528 void diagonal_part(mat &A, int r, int c, int n){ 529 529 for(int i=0; i<n; i++){A(r,c)=1.0; r++;c++;} 530 530 }; 531 //! similar to ARX.have_constant 532 bool have_constant; 531 533 public: 532 534 //! set up this object to match given mlnorm 533 void connect_mlnorm(const mlnorm<fsqmat> &ml){ 535 //! Note that state-space and common mpdf use different meaning of \f$ _t \f$ in \f$ u_t \f$. 536 //!While mlnorm typically assumes that \f$ u_t \rightarrow y_t \f$ in state space it is \f$ u_{t-1} \rightarrow y_t \f$ 537 //! For consequences in notation of internal variable xt see arx2statespace_notes.lyx. 538 void connect_mlnorm(const mlnorm<chmat> &ml, RV &xrv, RV &urv){ 539 534 540 //get ids of yrv 535 541 const RV &yrv = ml._rv(); … … 537 543 const RV &rgr = ml._rvc(); 538 544 RV rgr0 = rgr.remove_time(); 539 RVurv = rgr0.subt(yrv);545 urv = rgr0.subt(yrv); 540 546 541 547 // create names for state variables 542 548 xrv=yrv; 543 549 544 int y_multiplicity = rgr.mint(yrv);545 int y_block_size = yrv.length()*(y_multiplicity +1); // current yt + all delayed yts546 for (int m=0;m<y_multiplicity ;m++){550 int y_multiplicity = -rgr.mint(yrv); 551 int y_block_size = yrv.length()*(y_multiplicity); // current yt + all delayed yts 552 for (int m=0;m<y_multiplicity - 1;m++){ // ========= -1 is important see arx2statespace_notes 547 553 xrv.add(yrv.copy_t(-m-1)); //add delayed yt 548 554 } 555 //! temporary RV for connection to ml.rvc, since notation of xrv and ml.rvc does not match 556 RV xrv_ml = xrv.copy_t(-1); 557 549 558 // add regressors 550 559 ivec u_block_sizes(urv.length()); // no_blocks = yt + unique rgr … … 552 561 RV R=urv.subselect(vec_1(r)); //non-delayed element of rgr 553 562 int r_size=urv.size(r); 554 int r_multiplicity= rgr.mint(R);563 int r_multiplicity=-rgr.mint(R); 555 564 u_block_sizes(r)= r_size*r_multiplicity ; 556 565 for(int m=0;m<r_multiplicity;m++){ 557 566 xrv.add(R.copy_t(-m-1)); //add delayed yt 567 xrv_ml.add(R.copy_t(-m-1)); //add delayed yt 558 568 } 559 569 } 560 570 // add constant 571 if (any(ml._mu_const()!=0.0)){ 572 have_constant=true; 573 xrv.add(RV("bdm_reserved_constant_one",1)); 574 } else {have_constant=false;} 575 576 561 577 // get mapp 562 th2A.set_connection(xrv, ml._rvc()); 578 th2A.set_connection(xrv_ml, ml._rvc()); 579 th2B.set_connection(urv, ml._rvc()); 563 580 564 581 //set matrix sizes … … 577 594 this->C=zeros(yrv._dsize(), xrv._dsize()); 578 595 this->C.set_submatrix(0,0,eye(yrv._dsize())); 579 580 this->R = zeros(yrv._dsize(),yrv._dsize());581 this->Q = zeros(xrv._dsize(),xrv._dsize());582 583 584 585 596 this->D=zeros(yrv._dsize(),urv._dsize()); 597 this->R.setCh(zeros(yrv._dsize(),yrv._dsize())); 598 this->Q.setCh(zeros(xrv._dsize(),xrv._dsize())); 599 // Q is set by update 600 601 update_from(ml); 602 validate(); 586 603 }; 587 604 //! fast function to update parameters from ml - not checked for compatibility!! 588 void update_from(const mlnorm<fsqmat> &ml){ 589 590 vec theta = ml._A().get_row(0); // this 591 this->Q._M().set_submatrix(0,0,ml._R()); 592 593 } 605 void update_from(const mlnorm<chmat> &ml){ 606 607 vec Arow=zeros(A.cols()); 608 vec Brow=zeros(B.cols()); 609 // ROW- WISE EVALUATION ===== 610 for(int i=0;i<ml._rv()._dsize(); i++){ 611 612 vec theta = ml._A().get_row(i); 613 614 th2A.filldown(theta,Arow); 615 if (have_constant){ 616 // constant is always at the end no need for datalink 617 Arow(A.cols()-1) = ml._mu_const()(i); 618 } 619 this->A.set_row(i,Arow); 620 621 th2B.filldown(theta,Brow); 622 this->B.set_row(i,Brow); 623 } 624 this->Q._Ch().set_submatrix(0,0,ml.__R()._Ch()); 625 626 }; 627 //! access function 628 bool _have_constant() const {return have_constant;} 594 629 }; 595 630