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
00027 class KalmanFull {
00028 protected:
00029 int dimx, dimy, dimu;
00030 mat A, B, C, D, R, Q;
00031
00032
00033 mat _Pp, _Ry, _iRy, _K;
00034 public:
00035
00037 vec mu;
00039 mat P;
00040
00041 bool evalll;
00042 double ll;
00043 public:
00045 KalmanFull ( mat A, mat B, mat C, mat D, mat R, mat Q, mat P0, vec mu0 );
00047 void bayes ( const vec &dt );
00049 friend std::ostream &operator<< ( std::ostream &os, const KalmanFull &kf );
00051 KalmanFull(){};
00052 };
00053
00054
00062 template<class sq_T>
00063
00064 class Kalman : public BM {
00065 protected:
00067 RV rvy;
00069 RV rvu;
00071 int dimx;
00073 int dimy;
00075 int dimu;
00077 mat A;
00079 mat B;
00081 mat C;
00083 mat D;
00085 sq_T Q;
00087 sq_T R;
00088
00090 enorm<sq_T> est;
00092 enorm<sq_T> fy;
00093
00095 mat _K;
00097 vec& _yp;
00099 sq_T& _Ry;
00101 vec& _mu;
00103 sq_T& _P;
00104
00105 public:
00107 Kalman ( );
00109 Kalman ( const Kalman<sq_T> &K0 );
00111 void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const sq_T &Q0,const sq_T &R0 );
00113 void set_est ( const vec &mu0, const sq_T &P0 ) {
00114 sq_T pom(dimy);
00115 est.set_parameters ( mu0,P0 );
00116 P0.mult_sym(C,pom);
00117 fy.set_parameters ( C*mu0, pom );
00118 };
00119
00121 void bayes ( const vec &dt );
00123 const epdf& posterior() const {return est;}
00124 const enorm<sq_T>* _e() const {return &est;}
00126 mat& __K() {return _K;}
00128 vec _dP() {return _P->getD();}
00129 };
00130
00137 class KalmanCh : public Kalman<chmat>{
00138 protected:
00140 mat preA;
00142 mat postA;
00143
00144 public:
00146 KalmanCh ():Kalman<chmat>(),preA(),postA(){};
00148 void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const chmat &Q0,const chmat &R0 );
00149 void set_statistics ( const vec &mu0, const chmat &P0 ) {
00150 est.set_parameters ( mu0,P0 );
00151 };
00152
00153
00167 void bayes ( const vec &dt );
00168 };
00169
00175 class EKFfull : public KalmanFull, public BM {
00176
00178 diffbifn* pfxu;
00180 diffbifn* phxu;
00181
00182 enorm<fsqmat> E;
00183 public:
00185 EKFfull ( );
00187 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 );
00189 void bayes ( const vec &dt );
00191 void set_est (vec mu0, mat P0){mu=mu0;P=P0;};
00193 const epdf& posterior()const{return E;};
00194 const enorm<fsqmat>* _e()const{return &E;};
00195 const mat _R(){return P;}
00196 };
00197
00203 template<class sq_T>
00204 class EKF : public Kalman<fsqmat> {
00206 diffbifn* pfxu;
00208 diffbifn* phxu;
00209 public:
00211 EKF ( RV rvx, RV rvy, RV rvu );
00213 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const sq_T Q0, const sq_T R0 );
00215 void bayes ( const vec &dt );
00216 };
00217
00224 class EKFCh : public KalmanCh {
00225 protected:
00227 diffbifn* pfxu;
00229 diffbifn* phxu;
00230 public:
00232 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 );
00234 void bayes ( const vec &dt );
00235 };
00236
00241 class KFcondQR : public Kalman<ldmat>, public BMcond {
00242
00243 public:
00245 KFcondQR ( ) : Kalman<ldmat> ( ),BMcond ( ) {};
00246
00247 void condition ( const vec &RQ );
00248 };
00249
00254 class KFcondR : public Kalman<ldmat>, public BMcond {
00255
00256 public:
00258 KFcondR ( ) : Kalman<ldmat> ( ),BMcond ( ) {};
00259
00260 void condition ( const vec &R );
00261 };
00262
00264
00265 template<class sq_T>
00266 Kalman<sq_T>::Kalman ( const Kalman<sq_T> &K0 ) : BM ( ),rvy ( K0.rvy ),rvu ( K0.rvu ),
00267 dimx ( K0.dimx ), dimy ( K0.dimy ),dimu ( K0.dimu ),
00268 A ( K0.A ), B ( K0.B ), C ( K0.C ), D ( K0.D ),
00269 Q(K0.Q), R(K0.R),
00270 est ( K0.est ), fy ( K0.fy ), _yp(fy._mu()),_Ry(fy._R()), _mu(est._mu()), _P(est._R()) {
00271
00272
00273
00274
00275
00276
00277
00278 }
00279
00280 template<class sq_T>
00281 Kalman<sq_T>::Kalman ( ) : BM (), est ( ), fy (), _yp(fy._mu()), _Ry(fy._R()), _mu(est._mu()), _P(est._R()) {
00282 };
00283
00284 template<class sq_T>
00285 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 ) {
00286 dimx = A0.rows();
00287 dimy = C0.rows();
00288 dimu = B0.cols();
00289
00290 it_assert_debug ( A0.cols() ==dimx, "Kalman: A is not square" );
00291 it_assert_debug ( B0.rows() ==dimx, "Kalman: B is not compatible" );
00292 it_assert_debug ( C0.cols() ==dimx, "Kalman: C is not square" );
00293 it_assert_debug ( ( D0.rows() ==dimy ) || ( D0.cols() ==dimu ), "Kalman: D is not compatible" );
00294 it_assert_debug ( ( R0.cols() ==dimy ) || ( R0.rows() ==dimy ), "Kalman: R is not compatible" );
00295 it_assert_debug ( ( Q0.cols() ==dimx ) || ( Q0.rows() ==dimx ), "Kalman: Q is not compatible" );
00296
00297 A = A0;
00298 B = B0;
00299 C = C0;
00300 D = D0;
00301 R = R0;
00302 Q = Q0;
00303 }
00304
00305 template<class sq_T>
00306 void Kalman<sq_T>::bayes ( const vec &dt ) {
00307 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
00308
00309 sq_T iRy(dimy);
00310 vec u = dt.get ( dimy,dimy+dimu-1 );
00311 vec y = dt.get ( 0,dimy-1 );
00312
00313 _mu = A* _mu + B*u;
00314
00315 _P.mult_sym ( A );
00316 _P +=Q;
00317
00318
00319
00320 _P.mult_sym ( C, _Ry );
00321 _Ry +=R;
00322
00323 mat Pfull = _P.to_mat();
00324
00325 _Ry.inv ( iRy );
00326 _K = Pfull*C.transpose() * ( iRy.to_mat() );
00327
00328 sq_T pom ( ( int ) Pfull.rows() );
00329 iRy.mult_sym_t ( C*Pfull,pom );
00330 (_P ) -= pom;
00331 (_yp ) = C* _mu +D*u;
00332 (_mu ) += _K* ( y- _yp );
00333
00334
00335 if ( evalll==true ) {
00336 ll=fy.evallog ( y );
00337 }
00338
00339
00340
00341 };
00342
00343
00344
00345
00346
00347 template<class sq_T>
00348 EKF<sq_T>::EKF ( RV rvx0, RV rvy0, RV rvu0 ) : Kalman<sq_T> ( rvx0,rvy0,rvu0 ) {}
00349
00350 template<class sq_T>
00351 void EKF<sq_T>::set_parameters ( diffbifn* pfxu0, diffbifn* phxu0,const sq_T Q0,const sq_T R0 ) {
00352 pfxu = pfxu0;
00353 phxu = phxu0;
00354
00355
00356 pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true );
00357
00358 B.clear();
00359 phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true );
00360
00361 D.clear();
00362
00363 R = R0;
00364 Q = Q0;
00365 }
00366
00367 template<class sq_T>
00368 void EKF<sq_T>::bayes ( const vec &dt ) {
00369 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
00370
00371 sq_T iRy(dimy,dimy);
00372 vec u = dt.get ( dimy,dimy+dimu-1 );
00373 vec y = dt.get ( 0,dimy-1 );
00374
00375 _mu = pfxu->eval ( _mu, u );
00376 pfxu->dfdx_cond ( _mu,u,A,false );
00377
00378
00379 _P.mult_sym ( A );
00380 _P +=Q;
00381
00382
00383 phxu->dfdx_cond ( _mu,u,C,false );
00384
00385 _P.mult_sym ( C, _Ry );
00386 ( _Ry ) +=R;
00387
00388 mat Pfull = _P.to_mat();
00389
00390 _Ry.inv ( iRy );
00391 _K = Pfull*C.transpose() * ( iRy.to_mat() );
00392
00393 sq_T pom ( ( int ) Pfull.rows() );
00394 iRy.mult_sym_t ( C*Pfull,pom );
00395 (_P ) -= pom;
00396 _yp = phxu->eval ( _mu,u );
00397 ( _mu ) += _K* ( y-_yp );
00398
00399 if ( evalll==true ) {ll+=fy.evallog ( y );}
00400 };
00401
00402
00403 }
00404 #endif // KF_H
00405
00406