- Timestamp:
- 10/12/09 19:38:54 (15 years ago)
- Location:
- library/bdm/estim
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/kalman.cpp
r583 r653 16 16 mat &P = est->_R(); 17 17 mat& _Ry = fy._R(); 18 vec& yp = fy._mu(); 18 19 //Time update 19 20 mu = A * mu + B * u; … … 24 25 _K = P * C.transpose() * inv ( _Ry ); 25 26 P -= _K * C * P; // P = P -KCP; 26 mu += _K * ( y - C * mu - D * u ); 27 yp = C * mu + D * u; 28 mu += _K * ( y - yp ); 27 29 28 30 if ( evalll ) { 29 ll= est->evallog(y);31 ll=fy.evallog(y); 30 32 } 31 33 }; … … 65 67 mat &P = est->_R(); 66 68 mat& _Ry = fy._R(); 69 vec& yp = fy._mu(); 67 70 68 71 pfxu->dfdx_cond ( mu, zeros ( dimu ), A, true ); … … 77 80 _K = P * C.transpose() * inv ( _Ry ); 78 81 P -= _K * C * P; // P = P -KCP; 79 vecyp = phxu->eval ( mu, u );82 yp = phxu->eval ( mu, u ); 80 83 mu += _K * ( y - yp ); 81 84 -
library/bdm/estim/kalman.h
r625 r653 54 54 public: 55 55 StateSpace() : dimx (0), dimy (0), dimu (0), A(), B(), C(), D(), Q(), R() {} 56 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) {} 56 57 void set_parameters (const mat &A0, const mat &B0, const mat &C0, const mat &D0, const sq_T &Q0, const sq_T &R0); 57 58 void validate(); … … 114 115 enorm<sq_T> fy; 115 116 public: 116 Kalman() : BM(), StateSpace<sq_T>(), yrv(),urv(), _K(), est(new enorm<sq_T>){} 117 Kalman<sq_T>() : BM(), StateSpace<sq_T>(), yrv(),urv(), _K(), est(new enorm<sq_T>){} 118 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){} 117 119 void set_statistics (const vec &mu0, const mat &P0) {est->set_parameters (mu0, P0); }; 118 120 void set_statistics (const vec &mu0, const sq_T &P0) {est->set_parameters (mu0, P0); }; … … 152 154 //! Here dt = [yt;ut] of appropriate dimensions 153 155 void bayes (const vec &dt); 156 BM* _copy_() const { 157 KalmanFull* K = new KalmanFull; 158 K->set_parameters (A, B, C, D, Q, R); 159 K->set_statistics (est->_mu(), est->_R()); 160 return K; 161 } 154 162 }; 155 163 UIREGISTER(KalmanFull); … … 236 244 const mat _R() { 237 245 return est->_R().to_mat(); 246 } 247 void from_setting (const Setting &set) { 248 shared_ptr<diffbifn> IM = UI::build<diffbifn> ( set, "IM", UI::compulsory ); 249 shared_ptr<diffbifn> OM = UI::build<diffbifn> ( set, "OM", UI::compulsory ); 250 251 //statistics 252 int dim = IM->dimension(); 253 vec mu0; 254 if ( !UI::get ( mu0, set, "mu0" ) ) 255 mu0 = zeros ( dim ); 256 257 mat P0; 258 vec dP0; 259 if ( UI::get ( dP0, set, "dP0" ) ) 260 P0 = diag ( dP0 ); 261 else if ( !UI::get ( P0, set, "P0" ) ) 262 P0 = eye ( dim ); 263 264 set_statistics ( mu0, P0 ); 265 266 //parameters 267 vec dQ, dR; 268 UI::get ( dQ, set, "dQ", UI::compulsory ); 269 UI::get ( dR, set, "dR", UI::compulsory ); 270 set_parameters ( IM, OM, diag ( dQ ), diag ( dR ) ); 271 272 //connect 273 shared_ptr<RV> drv = UI::build<RV> ( set, "drv", UI::compulsory ); 274 set_drv ( *drv ); 275 shared_ptr<RV> rv = UI::build<RV> ( set, "rv", UI::compulsory ); 276 set_rv ( *rv ); 277 278 string options; 279 if ( UI::get ( options, set, "options" ) ) 280 set_options ( options ); 281 // pfxu = UI::build<diffbifn>(set, "IM", UI::compulsory); 282 // phxu = UI::build<diffbifn>(set, "OM", UI::compulsory); 283 // 284 // mat R0; 285 // UI::get(R0, set, "R",UI::compulsory); 286 // mat Q0; 287 // UI::get(Q0, set, "Q",UI::compulsory); 288 // 289 // 290 // mat P0; vec mu0; 291 // UI::get(mu0, set, "mu0", UI::optional); 292 // UI::get(P0, set, "P0", UI::optional); 293 // set_statistics(mu0,P0); 294 // // Initial values 295 // UI::get (yrv, set, "yrv", UI::optional); 296 // UI::get (urv, set, "urv", UI::optional); 297 // set_drv(concat(yrv,urv)); 298 // 299 // // setup StateSpace 300 // pfxu->dfdu_cond(mu0, zeros(pfxu->_dimu()), A,true); 301 // phxu->dfdu_cond(mu0, zeros(pfxu->_dimu()), C,true); 302 // 303 validate(); 304 } 305 void validate() { 306 // check stats and IM and OM 238 307 } 239 308 }; … … 448 517 void StateSpace<sq_T>::set_parameters (const mat &A0, const mat &B0, const mat &C0, const mat &D0, const sq_T &Q0, const sq_T &R0) 449 518 { 450 dimx = A0.rows();451 dimu = B0.cols();452 dimy = C0.rows();453 519 454 520 A = A0; … … 463 529 template<class sq_T> 464 530 void StateSpace<sq_T>::validate(){ 531 dimx = A.rows(); 532 dimu = B.cols(); 533 dimy = C.rows(); 465 534 bdm_assert (A.cols() == dimx, "KalmanFull: A is not square"); 466 535 bdm_assert (B.rows() == dimx, "KalmanFull: B is not compatible"); 467 536 bdm_assert (C.cols() == dimx, "KalmanFull: C is not square"); 468 bdm_assert ( (D.rows() == dimy) ||(D.cols() == dimu), "KalmanFull: D is not compatible");469 bdm_assert ( (Q.cols() == dimx) ||(Q.rows() == dimx), "KalmanFull: Q is not compatible");470 bdm_assert ( (R.cols() == dimy) ||(R.rows() == dimy), "KalmanFull: R is not compatible");537 bdm_assert ( (D.rows() == dimy) && (D.cols() == dimu), "KalmanFull: D is not compatible"); 538 bdm_assert ( (Q.cols() == dimx) && (Q.rows() == dimx), "KalmanFull: Q is not compatible"); 539 bdm_assert ( (R.cols() == dimy) && (R.rows() == dimy), "KalmanFull: R is not compatible"); 471 540 } 472 541 -
library/bdm/estim/particles.h
r638 r653 190 190 est.resample(ind,resmethod); 191 191 } 192 Array<vec>& __samples(){return _samples;} 192 193 }; 193 194 UIREGISTER(PF); … … 201 202 202 203 class MPF : public BM { 203 shared_ptr<PF> pf; 204 protected: 205 shared_ptr<PF> pf; 204 206 Array<BM*> BMs; 205 207