- Main Page
- Related Pages
- Classes
- Files

# work/git/mixpp/bdm/estim/libKF.h

Go to the documentation of this file.

    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         //cache
    00033         mat _Pp, _Ry, _iRy, _K;
    00034 public:
    00035         //posterior
    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         epdf& _epdf() {return est;}
    00125         mat& __K() {return _K;}
    00127         vec _dP() {return _P->getD();}
    00128 };
    00129
    00132 class KalmanCh : public Kalman<chmat>{
    00133 protected:
    00135 mat preA;
    00137 mat postA;
    00138
    00139 public:
    00141         KalmanCh ( RV rvx0, RV rvy0, RV rvu0 ):Kalman<chmat>(rvx0,rvy0,rvu0),preA(dimy+dimx+dimx,dimy+dimx),postA(dimy+dimx,dimy+dimx){};
    00143         void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const chmat &R0,const chmat &Q0 );
    00144         void set_est ( const vec &mu0, const chmat &P0 ) {
    00145                 est.set_parameters ( mu0,P0 );
    00146         };
    00147
    00148
    00162         void bayes ( const vec &dt );
    00163 };
    00164
    00170 class EKFfull : public KalmanFull, public BM {
    00171
    00173         diffbifn* pfxu;
    00175         diffbifn* phxu;
    00176
    00177         enorm<fsqmat> E;
    00178 public:
    00180         EKFfull ( RV rvx, RV rvy, RV rvu );
    00182         void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 );
    00184         void bayes ( const vec &dt );
    00186         void set_est (vec mu0, mat P0){mu=mu0;P=P0;};
    00188         epdf& _epdf(){return E;};
    00189 };
    00190
    00196 template<class sq_T>
    00197 class EKF : public Kalman<fsqmat> {
    00199         diffbifn* pfxu;
    00201         diffbifn* phxu;
    00202 public:
    00204         EKF ( RV rvx, RV rvy, RV rvu );
    00206         void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const sq_T Q0, const sq_T R0 );
    00208         void bayes ( const vec &dt );
    00209 };
    00210
    00217 class EKFCh : public KalmanCh {
    00219         diffbifn* pfxu;
    00221         diffbifn* phxu;
    00222 public:
    00224         EKFCh ( RV rvx, RV rvy, RV rvu );
    00226         void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 );
    00228         void bayes ( const vec &dt );
    00229 };
    00230
    00235 class KFcondQR : public Kalman<ldmat>, public BMcond {
    00236 //protected:
    00237 public:
    00239         KFcondQR ( RV rvx, RV rvy, RV rvu, RV rvRQ ) : Kalman<ldmat> ( rvx, rvy,rvu ),BMcond ( rvRQ ) {};
    00240
    00241         void condition ( const vec &RQ );
    00242 };
    00243
    00248 class KFcondR : public Kalman<ldmat>, public BMcond {
    00249 //protected:
    00250 public:
    00252         KFcondR ( RV rvx, RV rvy, RV rvu, RV rvR ) : Kalman<ldmat> ( rvx, rvy,rvu ),BMcond ( rvR ) {};
    00253
    00254         void condition ( const vec &R );
    00255 };
    00256
    00258
    00259 template<class sq_T>
    00260 Kalman<sq_T>::Kalman ( const Kalman<sq_T> &K0 ) : BM ( K0.rv ),rvy ( K0.rvy ),rvu ( K0.rvu ),
    00261                 dimx ( rv.count() ), dimy ( rvy.count() ),dimu ( rvu.count() ),
    00262                 A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ),
    00263                 Q(dimx), R(dimy),
    00264                 est ( rv ), fy ( rvy ), _yp(fy._mu()),_Ry(fy._R()), _mu(est._mu()), _P(est._R()) {
    00265
    00266         this->set_parameters ( K0.A, K0.B, K0.C, K0.D, K0.R, K0.Q );
    00267
    00268 // copy values in pointers
    00269         _mu = K0._mu;
    00270         _P = K0._P;
    00271         _yp = K0._yp;
    00272         _Ry = K0._Ry;
    00273
    00274 }
    00275
    00276 template<class sq_T>
    00277 Kalman<sq_T>::Kalman ( RV rvx, RV rvy0, RV rvu0 ) : BM ( rvx ),rvy ( rvy0 ),rvu ( rvu0 ),
    00278                 dimx ( rvx.count() ), dimy ( rvy.count() ),dimu ( rvu.count() ),
    00279                 A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ),
    00280                 Q(dimx), R (dimy),
    00281                 est ( rvx ), fy ( rvy ),  _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 &R0, const sq_T &Q0 ) {
    00286         it_assert_debug ( A0.cols() ==dimx, "Kalman: A is not square" );
    00287         it_assert_debug ( B0.rows() ==dimx, "Kalman: B is not compatible" );
    00288         it_assert_debug ( C0.cols() ==dimx, "Kalman: C is not square" );
    00289         it_assert_debug ( ( D0.rows() ==dimy ) || ( D0.cols() ==dimu ), "Kalman: D is not compatible" );
    00290         it_assert_debug ( ( R0.cols() ==dimy ) || ( R0.rows() ==dimy ), "Kalman: R is not compatible" );
    00291         it_assert_debug ( ( Q0.cols() ==dimx ) || ( Q0.rows() ==dimx ), "Kalman: Q is not compatible" );
    00292
    00293         A = A0;
    00294         B = B0;
    00295         C = C0;
    00296         D = D0;
    00297         R = R0;
    00298         Q = Q0;
    00299 }
    00300
    00301 template<class sq_T>
    00302 void Kalman<sq_T>::bayes ( const vec &dt ) {
    00303         it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
    00304
    00305         sq_T iRy(dimy);
    00306         vec u = dt.get ( dimy,dimy+dimu-1 );
    00307         vec y = dt.get ( 0,dimy-1 );
    00308         //Time update
    00309         _mu = A* _mu + B*u;
    00310         //P  = A*P*A.transpose() + Q; in sq_T
    00311         _P.mult_sym ( A );
    00312         _P  +=Q;
    00313
    00314         //Data update
    00315         //_Ry = C*P*C.transpose() + R; in sq_T
    00316         _P.mult_sym ( C, _Ry );
    00317         _Ry  +=R;
    00318
    00319         mat Pfull = _P.to_mat();
    00320
    00321         _Ry.inv ( iRy ); // result is in _iRy;
    00322         _K = Pfull*C.transpose() * ( iRy.to_mat() );
    00323
    00324         sq_T pom ( ( int ) Pfull.rows() );
    00325         iRy.mult_sym_t ( C*Pfull,pom );
    00326         (_P ) -= pom; // P = P -PC'iRy*CP;
    00327         (_yp ) = C* _mu  +D*u; //y prediction
    00328         (_mu ) += _K* ( y- _yp  );
    00329
    00330
    00331         if ( evalll==true ) { //likelihood of observation y
    00332                 ll=fy.evalpdflog ( y );
    00333         }
    00334
    00335 //cout << "y: " << y-(*_yp) <<" R: " << _Ry->to_mat() << " iR: " << _iRy->to_mat() << " ll: " << ll <<endl;
    00336
    00337 };
    00338
    00339
    00340
    00341 //TODO why not const pointer??
    00342
    00343 template<class sq_T>
    00344 EKF<sq_T>::EKF ( RV rvx0, RV rvy0, RV rvu0 ) : Kalman<sq_T> ( rvx0,rvy0,rvu0 ) {}
    00345
    00346 template<class sq_T>
    00347 void EKF<sq_T>::set_parameters ( diffbifn* pfxu0,  diffbifn* phxu0,const sq_T Q0,const sq_T R0 ) {
    00348         pfxu = pfxu0;
    00349         phxu = phxu0;
    00350
    00351         //initialize matrices A C, later, these will be only updated!
    00352         pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true );
    00353 //      pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true );
    00354         B.clear();
    00355         phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true );
    00356 //      phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true );
    00357         D.clear();
    00358
    00359         R = R0;
    00360         Q = Q0;
    00361 }
    00362
    00363 template<class sq_T>
    00364 void EKF<sq_T>::bayes ( const vec &dt ) {
    00365         it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
    00366
    00367         sq_T iRy(dimy,dimy);
    00368         vec u = dt.get ( dimy,dimy+dimu-1 );
    00369         vec y = dt.get ( 0,dimy-1 );
    00370         //Time update
    00371         _mu = pfxu->eval ( _mu, u );
    00372         pfxu->dfdx_cond ( _mu,u,A,false ); //update A by a derivative of fx
    00373
    00374         //P  = A*P*A.transpose() + Q; in sq_T
    00375         _P.mult_sym ( A );
    00376         _P +=Q;
    00377
    00378         //Data update
    00379         phxu->dfdx_cond ( _mu,u,C,false ); //update C by a derivative hx
    00380         //_Ry = C*P*C.transpose() + R; in sq_T
    00381         _P.mult_sym ( C, _Ry );
    00382         ( _Ry ) +=R;
    00383
    00384         mat Pfull = _P.to_mat();
    00385
    00386         _Ry.inv ( iRy ); // result is in _iRy;
    00387         _K = Pfull*C.transpose() * ( iRy.to_mat() );
    00388
    00389         sq_T pom ( ( int ) Pfull.rows() );
    00390         iRy.mult_sym_t ( C*Pfull,pom );
    00391         (_P ) -= pom; // P = P -PC'iRy*CP;
    00392         _yp = phxu->eval ( _mu,u ); //y prediction
    00393         ( _mu ) += _K* ( y-_yp );
    00394
    00395         if ( evalll==true ) {ll+=fy.evalpdflog ( y );}
    00396 };
    00397
    00398
    00399 #endif // KF_H
    00400
    00401