00001
00013 #ifndef KF_H
00014 #define KF_H
00015
00016 #include <itpp/itbase.h>
00017 #include "../stat/libFN.h"
00018 #include "../stat/libEF.h"
00019 #include "../math/chmat.h"
00020
00021 using namespace itpp;
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 ( RV rvx0, RV rvy0, RV rvu0 );
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 &R0,const sq_T &Q0 );
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& _epdf() 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
00133 class KalmanCh : public Kalman<chmat>{
00134 protected:
00136 mat preA;
00138 mat postA;
00139
00140 public:
00142 KalmanCh ( RV rvx0, RV rvy0, RV rvu0 ):Kalman<chmat>(rvx0,rvy0,rvu0),preA(dimy+dimx+dimx,dimy+dimx),postA(dimy+dimx,dimy+dimx){};
00144 void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const chmat &R0,const chmat &Q0 );
00145 void set_est ( const vec &mu0, const chmat &P0 ) {
00146 est.set_parameters ( mu0,P0 );
00147 };
00148
00149
00163 void bayes ( const vec &dt );
00164 };
00165
00171 class EKFfull : public KalmanFull, public BM {
00172
00174 diffbifn* pfxu;
00176 diffbifn* phxu;
00177
00178 enorm<fsqmat> E;
00179 public:
00181 EKFfull ( RV rvx, RV rvy, RV rvu );
00183 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 );
00185 void bayes ( const vec &dt );
00187 void set_est (vec mu0, mat P0){mu=mu0;P=P0;};
00189 const epdf& _epdf()const{return E;};
00190 const enorm<fsqmat>* _e()const{return &E;};
00191 };
00192
00198 template<class sq_T>
00199 class EKF : public Kalman<fsqmat> {
00201 diffbifn* pfxu;
00203 diffbifn* phxu;
00204 public:
00206 EKF ( RV rvx, RV rvy, RV rvu );
00208 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const sq_T Q0, const sq_T R0 );
00210 void bayes ( const vec &dt );
00211 };
00212
00219 class EKFCh : public KalmanCh {
00221 diffbifn* pfxu;
00223 diffbifn* phxu;
00224 public:
00226 EKFCh ( RV rvx, RV rvy, RV rvu );
00228 void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 );
00230 void bayes ( const vec &dt );
00231 };
00232
00237 class KFcondQR : public Kalman<ldmat>, public BMcond {
00238
00239 public:
00241 KFcondQR ( RV rvx, RV rvy, RV rvu, RV rvRQ ) : Kalman<ldmat> ( rvx, rvy,rvu ),BMcond ( rvRQ ) {};
00242
00243 void condition ( const vec &RQ );
00244 };
00245
00250 class KFcondR : public Kalman<ldmat>, public BMcond {
00251
00252 public:
00254 KFcondR ( RV rvx, RV rvy, RV rvu, RV rvR ) : Kalman<ldmat> ( rvx, rvy,rvu ),BMcond ( rvR ) {};
00255
00256 void condition ( const vec &R );
00257 };
00258
00260
00261 template<class sq_T>
00262 Kalman<sq_T>::Kalman ( const Kalman<sq_T> &K0 ) : BM ( K0.rv ),rvy ( K0.rvy ),rvu ( K0.rvu ),
00263 dimx ( rv.count() ), dimy ( rvy.count() ),dimu ( rvu.count() ),
00264 A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ),
00265 Q(dimx), R(dimy),
00266 est ( rv ), fy ( rvy ), _yp(fy._mu()),_Ry(fy._R()), _mu(est._mu()), _P(est._R()) {
00267
00268 this->set_parameters ( K0.A, K0.B, K0.C, K0.D, K0.R, K0.Q );
00269
00270
00271 _mu = K0._mu;
00272 _P = K0._P;
00273 _yp = K0._yp;
00274 _Ry = K0._Ry;
00275
00276 }
00277
00278 template<class sq_T>
00279 Kalman<sq_T>::Kalman ( RV rvx, RV rvy0, RV rvu0 ) : BM ( rvx ),rvy ( rvy0 ),rvu ( rvu0 ),
00280 dimx ( rvx.count() ), dimy ( rvy.count() ),dimu ( rvu.count() ),
00281 A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ),
00282 Q(dimx), R (dimy),
00283 est ( rvx ), fy ( rvy ), _yp(fy._mu()),_Ry(fy._R()),_mu(est._mu()), _P(est._R()) {
00284 };
00285
00286 template<class sq_T>
00287 void Kalman<sq_T>::set_parameters ( const mat &A0,const mat &B0, const mat &C0, const mat &D0, const sq_T &R0, const sq_T &Q0 ) {
00288 it_assert_debug ( A0.cols() ==dimx, "Kalman: A is not square" );
00289 it_assert_debug ( B0.rows() ==dimx, "Kalman: B is not compatible" );
00290 it_assert_debug ( C0.cols() ==dimx, "Kalman: C is not square" );
00291 it_assert_debug ( ( D0.rows() ==dimy ) || ( D0.cols() ==dimu ), "Kalman: D is not compatible" );
00292 it_assert_debug ( ( R0.cols() ==dimy ) || ( R0.rows() ==dimy ), "Kalman: R is not compatible" );
00293 it_assert_debug ( ( Q0.cols() ==dimx ) || ( Q0.rows() ==dimx ), "Kalman: Q is not compatible" );
00294
00295 A = A0;
00296 B = B0;
00297 C = C0;
00298 D = D0;
00299 R = R0;
00300 Q = Q0;
00301 }
00302
00303 template<class sq_T>
00304 void Kalman<sq_T>::bayes ( const vec &dt ) {
00305 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
00306
00307 sq_T iRy(dimy);
00308 vec u = dt.get ( dimy,dimy+dimu-1 );
00309 vec y = dt.get ( 0,dimy-1 );
00310
00311 _mu = A* _mu + B*u;
00312
00313 _P.mult_sym ( A );
00314 _P +=Q;
00315
00316
00317
00318 _P.mult_sym ( C, _Ry );
00319 _Ry +=R;
00320
00321 mat Pfull = _P.to_mat();
00322
00323 _Ry.inv ( iRy );
00324 _K = Pfull*C.transpose() * ( iRy.to_mat() );
00325
00326 sq_T pom ( ( int ) Pfull.rows() );
00327 iRy.mult_sym_t ( C*Pfull,pom );
00328 (_P ) -= pom;
00329 (_yp ) = C* _mu +D*u;
00330 (_mu ) += _K* ( y- _yp );
00331
00332
00333 if ( evalll==true ) {
00334 ll=fy.evalpdflog ( y );
00335 }
00336
00337
00338
00339 };
00340
00341
00342
00343
00344
00345 template<class sq_T>
00346 EKF<sq_T>::EKF ( RV rvx0, RV rvy0, RV rvu0 ) : Kalman<sq_T> ( rvx0,rvy0,rvu0 ) {}
00347
00348 template<class sq_T>
00349 void EKF<sq_T>::set_parameters ( diffbifn* pfxu0, diffbifn* phxu0,const sq_T Q0,const sq_T R0 ) {
00350 pfxu = pfxu0;
00351 phxu = phxu0;
00352
00353
00354 pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true );
00355
00356 B.clear();
00357 phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true );
00358
00359 D.clear();
00360
00361 R = R0;
00362 Q = Q0;
00363 }
00364
00365 template<class sq_T>
00366 void EKF<sq_T>::bayes ( const vec &dt ) {
00367 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
00368
00369 sq_T iRy(dimy,dimy);
00370 vec u = dt.get ( dimy,dimy+dimu-1 );
00371 vec y = dt.get ( 0,dimy-1 );
00372
00373 _mu = pfxu->eval ( _mu, u );
00374 pfxu->dfdx_cond ( _mu,u,A,false );
00375
00376
00377 _P.mult_sym ( A );
00378 _P +=Q;
00379
00380
00381 phxu->dfdx_cond ( _mu,u,C,false );
00382
00383 _P.mult_sym ( C, _Ry );
00384 ( _Ry ) +=R;
00385
00386 mat Pfull = _P.to_mat();
00387
00388 _Ry.inv ( iRy );
00389 _K = Pfull*C.transpose() * ( iRy.to_mat() );
00390
00391 sq_T pom ( ( int ) Pfull.rows() );
00392 iRy.mult_sym_t ( C*Pfull,pom );
00393 (_P ) -= pom;
00394 _yp = phxu->eval ( _mu,u );
00395 ( _mu ) += _K* ( y-_yp );
00396
00397 if ( evalll==true ) {ll+=fy.evalpdflog ( y );}
00398 };
00399
00400
00401 #endif // KF_H
00402
00403