00001
00013 #ifndef KF_H
00014 #define KF_H
00015
00016
00017 #include "../stat/libFN.h"
00018 #include "../stat/libEF.h"
00019 #include "../math/chmat.h"
00020
00021 namespace bdm
00022 {
00023
00028 class KalmanFull
00029 {
00030 protected:
00031 int dimx, dimy, dimu;
00032 mat A, B, C, D, R, Q;
00033
00034
00035 mat _Pp, _Ry, _iRy, _K;
00036 public:
00037
00039 vec mu;
00041 mat P;
00042
00043 bool evalll;
00044 double ll;
00045 public:
00047 KalmanFull ( mat A, mat B, mat C, mat D, mat R, mat Q, mat P0, vec mu0 );
00049 void bayes ( const vec &dt );
00051 friend std::ostream &operator<< ( std::ostream &os, const KalmanFull &kf );
00053 KalmanFull() {};
00054 };
00055
00056
00064 template<class sq_T>
00065
00066 class Kalman : public BM
00067 {
00068 protected:
00070 RV rvy;
00072 RV rvu;
00074 int dimx;
00076 int dimy;
00078 int dimu;
00080 mat A;
00082 mat B;
00084 mat C;
00086 mat D;
00088 sq_T Q;
00090 sq_T R;
00091
00093 enorm<sq_T> est;
00095 enorm<sq_T> fy;
00096
00098 mat _K;
00100 vec& _yp;
00102 sq_T& _Ry;
00104 vec& _mu;
00106 sq_T& _P;
00107
00108 public:
00110 Kalman ( );
00112 Kalman ( const Kalman<sq_T> &K0 );
00114 void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const sq_T &Q0,const sq_T &R0 );
00116 void set_est ( const vec &mu0, const sq_T &P0 )
00117 {
00118 sq_T pom ( dimy );
00119 est.set_parameters ( mu0,P0 );
00120 P0.mult_sym ( C,pom );
00121 fy.set_parameters ( C*mu0, pom );
00122 };
00123
00125 void bayes ( const vec &dt );
00127 const epdf& posterior() const {return est;}
00128 const enorm<sq_T>* _e() const {return &est;}
00130 mat& __K() {return _K;}
00132 vec _dP() {return _P->getD();}
00133 };
00134
00141 class KalmanCh : public Kalman<chmat>
00142 {
00143 protected:
00145 mat preA;
00147 mat postA;
00148
00149 public:
00151 BM* _copy_() const
00152 {
00153 KalmanCh* K=new KalmanCh;
00154 K->set_parameters ( A,B,C,D,Q,R );
00155 K->set_statistics ( _mu,_P );
00156 return K;
00157 }
00159 void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const chmat &Q0,const chmat &R0 );
00160 void set_statistics ( const vec &mu0, const chmat &P0 )
00161 {
00162 est.set_parameters ( mu0,P0 );
00163 };
00164
00165
00179 void bayes ( const vec &dt );
00180 };
00181
00187 class EKFfull : public KalmanFull, public BM
00188 {
00189 protected:
00191 diffbifn* pfxu;
00193 diffbifn* phxu;
00194
00195 enorm<fsqmat> E;
00196 public:
00198 EKFfull ( );
00200 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 );
00202 void bayes ( const vec &dt );
00204 void set_statistics ( vec mu0, mat P0 ) {mu=mu0;P=P0;};
00206 const epdf& posterior() const{return E;};
00207 const enorm<fsqmat>* _e() const{return &E;};
00208 const mat _R() {return P;}
00209 };
00210
00216 template<class sq_T>
00217 class EKF : public Kalman<fsqmat>
00218 {
00220 diffbifn* pfxu;
00222 diffbifn* phxu;
00223 public:
00225 EKF ( RV rvx, RV rvy, RV rvu );
00227 EKF<sq_T>* _copy_() const { return new EKF<sq_T> ( this ); }
00229 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const sq_T Q0, const sq_T R0 );
00231 void bayes ( const vec &dt );
00232 };
00233
00240 class EKFCh : public KalmanCh
00241 {
00242 protected:
00244 diffbifn* pfxu;
00246 diffbifn* phxu;
00247 public:
00249 BM* _copy_() const
00250 {
00251 EKFCh* E=new EKFCh;
00252 E->set_parameters ( pfxu,phxu,Q,R );
00253 E->set_statistics ( _mu,_P );
00254 return E;
00255 }
00257 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 );
00259 void bayes ( const vec &dt );
00260 };
00261
00266 class KFcondQR : public Kalman<ldmat>
00267 {
00268
00269 public:
00270 void condition ( const vec &QR )
00271 {
00272 it_assert_debug ( QR.length() == ( dimx+dimy ),"KFcondRQ: conditioning by incompatible vector" );
00273
00274 Q.setD ( QR ( 0, dimx-1 ) );
00275 R.setD ( QR ( dimx, -1 ) );
00276 };
00277 };
00278
00283 class KFcondR : public Kalman<ldmat>
00284 {
00285
00286 public:
00288 KFcondR ( ) : Kalman<ldmat> ( ) {};
00289
00290 void condition ( const vec &R0 )
00291 {
00292 it_assert_debug ( R0.length() == ( dimy ),"KFcondR: conditioning by incompatible vector" );
00293
00294 R.setD ( R0 );
00295 };
00296
00297 };
00298
00300
00301 template<class sq_T>
00302 Kalman<sq_T>::Kalman ( const Kalman<sq_T> &K0 ) : BM ( ),rvy ( K0.rvy ),rvu ( K0.rvu ),
00303 dimx ( K0.dimx ), dimy ( K0.dimy ),dimu ( K0.dimu ),
00304 A ( K0.A ), B ( K0.B ), C ( K0.C ), D ( K0.D ),
00305 Q ( K0.Q ), R ( K0.R ),
00306 est ( K0.est ), fy ( K0.fy ), _yp ( fy._mu() ),_Ry ( fy._R() ), _mu ( est._mu() ), _P ( est._R() )
00307 {
00308
00309
00310
00311
00312
00313
00314
00315 }
00316
00317 template<class sq_T>
00318 Kalman<sq_T>::Kalman ( ) : BM (), est ( ), fy (), _yp ( fy._mu() ), _Ry ( fy._R() ), _mu ( est._mu() ), _P ( est._R() )
00319 {
00320 };
00321
00322 template<class sq_T>
00323 void Kalman<sq_T>::set_parameters ( const mat &A0,const mat &B0, const mat &C0, const mat &D0, const sq_T &Q0, const sq_T &R0 )
00324 {
00325 dimx = A0.rows();
00326 dimy = C0.rows();
00327 dimu = B0.cols();
00328
00329 it_assert_debug ( A0.cols() ==dimx, "Kalman: A is not square" );
00330 it_assert_debug ( B0.rows() ==dimx, "Kalman: B is not compatible" );
00331 it_assert_debug ( C0.cols() ==dimx, "Kalman: C is not square" );
00332 it_assert_debug ( ( D0.rows() ==dimy ) || ( D0.cols() ==dimu ), "Kalman: D is not compatible" );
00333 it_assert_debug ( ( R0.cols() ==dimy ) || ( R0.rows() ==dimy ), "Kalman: R is not compatible" );
00334 it_assert_debug ( ( Q0.cols() ==dimx ) || ( Q0.rows() ==dimx ), "Kalman: Q is not compatible" );
00335
00336 A = A0;
00337 B = B0;
00338 C = C0;
00339 D = D0;
00340 R = R0;
00341 Q = Q0;
00342 }
00343
00344 template<class sq_T>
00345 void Kalman<sq_T>::bayes ( const vec &dt )
00346 {
00347 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
00348
00349 sq_T iRy ( dimy );
00350 vec u = dt.get ( dimy,dimy+dimu-1 );
00351 vec y = dt.get ( 0,dimy-1 );
00352
00353 _mu = A* _mu + B*u;
00354
00355 _P.mult_sym ( A );
00356 _P +=Q;
00357
00358
00359
00360 _P.mult_sym ( C, _Ry );
00361 _Ry +=R;
00362
00363 mat Pfull = _P.to_mat();
00364
00365 _Ry.inv ( iRy );
00366 _K = Pfull*C.transpose() * ( iRy.to_mat() );
00367
00368 sq_T pom ( ( int ) Pfull.rows() );
00369 iRy.mult_sym_t ( C*Pfull,pom );
00370 ( _P ) -= pom;
00371 ( _yp ) = C* _mu +D*u;
00372 ( _mu ) += _K* ( y- _yp );
00373
00374
00375 if ( evalll==true )
00376 {
00377 ll=fy.evallog ( y );
00378 }
00379
00380
00381
00382 };
00383
00391 class MultiModel: public BM
00392 {
00393 protected:
00395 Array<EKFCh*> Models;
00397 vec w;
00399 vec _lls;
00401 int policy;
00403 enorm<chmat> est;
00404 public:
00405 void set_parameters ( Array<EKFCh*> A, int pol0=1 )
00406 {
00407 Models=A;
00408 w.set_length ( A.length() );
00409 _lls.set_length ( A.length() );
00410 policy=pol0;
00411
00412 est.set_rv(RV("MM",A(0)->posterior().dimension(),0));
00413 est.set_parameters(A(0)->_e()->mean(), A(0)->_e()->_R());
00414 }
00415 void bayes ( const vec &dt )
00416 {
00417 int n = Models.length();
00418 int i;
00419 for ( i=0;i<n;i++ )
00420 {
00421 Models ( i )->bayes ( dt );
00422 _lls ( i ) = Models ( i )->_ll();
00423 }
00424 double mlls=max ( _lls );
00425 w=exp ( _lls-mlls );
00426 w/=sum ( w );
00427
00428 switch ( policy )
00429 {
00430 case 1:
00431 {
00432 int mi=max_index ( w );
00433 const enorm<chmat>* st=(Models(mi)->_e());
00434 est.set_parameters(st->mean(), st->_R());
00435 }
00436 break;
00437 default: it_error ( "unknown policy" );
00438 }
00439
00440 for ( i=0;i<n;i++ )
00441 {
00442 Models ( i )->set_statistics ( est.mean(),est._R());
00443 }
00444 }
00445
00446 const enorm<chmat>* _e() const {return &est;}
00447
00448 const enorm<chmat>& posterior() const {return est;}
00449 };
00450
00451
00452
00453
00454 template<class sq_T>
00455 EKF<sq_T>::EKF ( RV rvx0, RV rvy0, RV rvu0 ) : Kalman<sq_T> ( rvx0,rvy0,rvu0 ) {}
00456
00457 template<class sq_T>
00458 void EKF<sq_T>::set_parameters ( diffbifn* pfxu0, diffbifn* phxu0,const sq_T Q0,const sq_T R0 )
00459 {
00460 pfxu = pfxu0;
00461 phxu = phxu0;
00462
00463
00464 pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true );
00465
00466 B.clear();
00467 phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true );
00468
00469 D.clear();
00470
00471 R = R0;
00472 Q = Q0;
00473 }
00474
00475 template<class sq_T>
00476 void EKF<sq_T>::bayes ( const vec &dt )
00477 {
00478 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
00479
00480 sq_T iRy ( dimy,dimy );
00481 vec u = dt.get ( dimy,dimy+dimu-1 );
00482 vec y = dt.get ( 0,dimy-1 );
00483
00484 _mu = pfxu->eval ( _mu, u );
00485 pfxu->dfdx_cond ( _mu,u,A,false );
00486
00487
00488 _P.mult_sym ( A );
00489 _P +=Q;
00490
00491
00492 phxu->dfdx_cond ( _mu,u,C,false );
00493
00494 _P.mult_sym ( C, _Ry );
00495 ( _Ry ) +=R;
00496
00497 mat Pfull = _P.to_mat();
00498
00499 _Ry.inv ( iRy );
00500 _K = Pfull*C.transpose() * ( iRy.to_mat() );
00501
00502 sq_T pom ( ( int ) Pfull.rows() );
00503 iRy.mult_sym_t ( C*Pfull,pom );
00504 ( _P ) -= pom;
00505 _yp = phxu->eval ( _mu,u );
00506 ( _mu ) += _K* ( y-_yp );
00507
00508 if ( evalll==true ) {ll+=fy.evallog ( y );}
00509 };
00510
00511
00512 }
00513 #endif // KF_H
00514
00515