Changeset 679 for library/bdm/estim/kalman.h
- Timestamp:
- 10/23/09 00:05:25 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/kalman.h
r675 r679 34 34 { 35 35 protected: 36 //! cache of rv.count()37 int dimx;38 //! cache of rvy.count()39 int dimy;40 //! cache of rvu.count()41 int dimu;42 36 //! Matrix A 43 37 mat A; … … 53 47 sq_T R; 54 48 public: 55 StateSpace() : dimx (0), dimy (0), dimu (0),A(), B(), C(), D(), Q(), R() {}49 StateSpace() : A(), B(), C(), D(), Q(), R() {} 56 50 //!copy constructor 57 StateSpace(const StateSpace<sq_T> &S0) : dimx (S0.dimx), dimy (S0.dimy), dimu (S0.dimu),A(S0.A), B(S0.B), C(S0.C), D(S0.D), Q(S0.Q), R(S0.R) {}51 StateSpace(const StateSpace<sq_T> &S0) : A(S0.A), B(S0.B), C(S0.C), D(S0.D), Q(S0.Q), R(S0.R) {} 58 52 //! set all matrix parameters 59 53 void set_parameters (const mat &A0, const mat &B0, const mat &C0, const mat &D0, const sq_T &Q0, const sq_T &R0); … … 83 77 } 84 78 //! access function 85 int _dimx(){return dimx;}86 //! access function87 int _dimy(){return dimy;}88 //! access function89 int _dimu(){return dimu;}90 //! access function91 79 const mat& _A() const {return A;} 92 80 //! access function … … 109 97 //! id of output 110 98 RV yrv; 111 //! id of input112 RV urv;113 99 //! Kalman gain 114 100 mat _K; 115 101 //!posterior 116 shared_ptr<enorm<sq_T>> est;102 enorm<sq_T> est; 117 103 //!marginal on data f(y|y) 118 104 enorm<sq_T> fy; 119 105 public: 120 Kalman<sq_T>() : BM(), StateSpace<sq_T>(), yrv(), urv(), _K(), est(new enorm<sq_T>){}106 Kalman<sq_T>() : BM(), StateSpace<sq_T>(), yrv(), _K(), est(){} 121 107 //! Copy constructor 122 Kalman<sq_T>(const Kalman<sq_T> &K0) : BM(K0), StateSpace<sq_T>(K0), yrv(K0.yrv), urv(K0.urv), _K(K0._K), est(new enorm<sq_T>(*K0.est)), fy(K0.fy){}108 Kalman<sq_T>(const Kalman<sq_T> &K0) : BM(K0), StateSpace<sq_T>(K0), yrv(K0.yrv), _K(K0._K), est(K0.est), fy(K0.fy){} 123 109 //!set statistics of the posterior 124 void set_statistics (const vec &mu0, const mat &P0) {est ->set_parameters (mu0, P0); };110 void set_statistics (const vec &mu0, const mat &P0) {est.set_parameters (mu0, P0); }; 125 111 //!set statistics of the posterior 126 void set_statistics (const vec &mu0, const sq_T &P0) {est ->set_parameters (mu0, P0); };112 void set_statistics (const vec &mu0, const sq_T &P0) {est.set_parameters (mu0, P0); }; 127 113 //! return correctly typed posterior (covariant return) 128 const enorm<sq_T>& posterior() const {return *est.get();} 129 //! shared posterior 130 shared_ptr<epdf> shared_posterior() {return est;} 114 const enorm<sq_T>& posterior() const {return est;} 131 115 //! load basic elements of Kalman from structure 132 116 void from_setting (const Setting &set) { … … 139 123 // Initial values 140 124 UI::get (yrv, set, "yrv", UI::optional); 141 UI::get ( urv, set, "urv", UI::optional);142 set_ drv(concat(yrv,urv));125 UI::get (rvc, set, "urv", UI::optional); 126 set_yrv(concat(yrv,rvc)); 143 127 144 128 validate(); … … 147 131 void validate() { 148 132 StateSpace<sq_T>::validate(); 149 bdm_assert(est->dimension(), "Statistics and model parameters mismatch"); 133 dimy = this->C.rows(); 134 dimc = this->B.cols(); 135 set_dim(this->A.rows()); 136 137 bdm_assert(est.dimension(), "Statistics and model parameters mismatch"); 150 138 } 151 139 }; … … 160 148 KalmanFull() :Kalman<fsqmat>(){}; 161 149 //! Here dt = [yt;ut] of appropriate dimensions 162 void bayes (const vec & dt);150 void bayes (const vec &yt, const vec &cond=empty_vec); 163 151 BM* _copy_() const { 164 152 KalmanFull* K = new KalmanFull; 165 153 K->set_parameters (A, B, C, D, Q, R); 166 K->set_statistics (est ->_mu(), est->_R());154 K->set_statistics (est._mu(), est._R()); 167 155 return K; 168 156 } … … 192 180 KalmanCh* K = new KalmanCh; 193 181 K->set_parameters (A, B, C, D, Q, R); 194 K->set_statistics (est ->_mu(), est->_R());182 K->set_statistics (est._mu(), est._R()); 195 183 return K; 196 184 } … … 213 201 Thus this object evaluates only predictors! Not filtering densities. 214 202 */ 215 void bayes (const vec & dt);203 void bayes (const vec &yt, const vec &cond=empty_vec); 216 204 217 205 void from_setting(const Setting &set){ 218 206 Kalman<chmat>::from_setting(set); 207 validate(); 208 } 209 void validate() { 210 Kalman<chmat>::validate(); 219 211 initialize(); 220 212 } … … 244 236 245 237 //! Here dt = [yt;ut] of appropriate dimensions 246 void bayes (const vec & dt);238 void bayes (const vec &yt, const vec &cond=empty_vec); 247 239 //! set estimates 248 240 void set_statistics (const vec &mu0, const mat &P0) { 249 est ->set_parameters (mu0, P0);241 est.set_parameters (mu0, P0); 250 242 }; 251 243 //! access function 252 244 const mat _R() { 253 return est ->_R().to_mat();245 return est._R().to_mat(); 254 246 } 255 247 void from_setting (const Setting &set) { … … 280 272 //connect 281 273 shared_ptr<RV> drv = UI::build<RV> ( set, "drv", UI::compulsory ); 282 set_ drv ( *drv );274 set_yrv ( *drv ); 283 275 shared_ptr<RV> rv = UI::build<RV> ( set, "rv", UI::compulsory ); 284 276 set_rv ( *rv ); … … 337 329 EKFCh* E = new EKFCh; 338 330 E->set_parameters (pfxu, phxu, Q, R); 339 E->set_statistics (est ->_mu(), est->_R());331 E->set_statistics (est._mu(), est._R()); 340 332 return E; 341 333 } … … 344 336 345 337 //! Here dt = [yt;ut] of appropriate dimensions 346 void bayes (const vec & dt);338 void bayes (const vec &yt, const vec &cond=empty_vec); 347 339 348 340 void from_setting (const Setting &set); … … 389 381 est.set_parameters (A (0)->posterior().mean(), A (0)->posterior()._R()); 390 382 } 391 void bayes (const vec & dt) {383 void bayes (const vec &yt, const vec &cond=empty_vec) { 392 384 int n = Models.length(); 393 385 int i; 394 386 for (i = 0; i < n; i++) { 395 Models (i)->bayes ( dt);387 Models (i)->bayes (yt); 396 388 _lls (i) = Models (i)->_ll(); 397 389 } … … 475 467 Crv.add(urv.copy_t(t)); 476 468 } 477 478 this->dimx = xrv._dsize(); 479 this->dimy = yrv._dsize(); 480 this->dimu = urv._dsize(); 481 469 482 470 // get mapp 483 471 th2A.set_connection(xrv, ml._rvc()); … … 486 474 487 475 //set matrix sizes 488 this->A=zeros( dimx,dimx);489 for (int j=1; j< dimx; j++){A(j,j-1)=1.0;} // off diagonal490 this->B=zeros( dimx,1);476 this->A=zeros(xrv._dsize(),xrv._dsize()); 477 for (int j=1; j<xrv._dsize(); j++){A(j,j-1)=1.0;} // off diagonal 478 this->B=zeros(xrv._dsize(),1); 491 479 this->B(0) = 1.0; 492 this->C=zeros(1, dimx);480 this->C=zeros(1,xrv._dsize()); 493 481 this->D=zeros(1,urv._dsize()); 494 this->Q = zeros( dimx,dimx);482 this->Q = zeros(xrv._dsize(),xrv._dsize()); 495 483 // R is set by update 496 484 … … 538 526 template<class sq_T> 539 527 void StateSpace<sq_T>::validate(){ 540 dimx = A.rows(); 541 dimu = B.cols(); 542 dimy = C.rows(); 543 bdm_assert (A.cols() == dimx, "KalmanFull: A is not square"); 544 bdm_assert (B.rows() == dimx, "KalmanFull: B is not compatible"); 545 bdm_assert (C.cols() == dimx, "KalmanFull: C is not square"); 546 bdm_assert ( (D.rows() == dimy) && (D.cols() == dimu), "KalmanFull: D is not compatible"); 547 bdm_assert ( (Q.cols() == dimx) && (Q.rows() == dimx), "KalmanFull: Q is not compatible"); 548 bdm_assert ( (R.cols() == dimy) && (R.rows() == dimy), "KalmanFull: R is not compatible"); 528 bdm_assert (A.cols() == A.rows(), "KalmanFull: A is not square"); 529 bdm_assert (B.rows() == A.rows(), "KalmanFull: B is not compatible"); 530 bdm_assert (C.cols() == A.rows(), "KalmanFull: C is not compatible"); 531 bdm_assert ( (D.rows() == C.rows()) && (D.cols() == B.cols()), "KalmanFull: D is not compatible"); 532 bdm_assert ( (Q.cols() == A.rows()) && (Q.rows() == A.rows()), "KalmanFull: Q is not compatible"); 533 bdm_assert ( (R.cols() == C.rows()) && (R.rows() == C.rows()), "KalmanFull: R is not compatible"); 549 534 } 550 535