root/bdm/estim/libKF.h @ 271

Revision 271, 10.0 kB (checked in by smidl, 15 years ago)

Next major revision

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Bayesian Filtering for linear Gaussian models (Kalman Filter) and extensions
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef KF_H
14#define KF_H
15
16
17#include "../stat/libFN.h"
18#include "../stat/libEF.h"
19#include "../math/chmat.h"
20
21namespace bdm{
22
23/*!
24* \brief Basic Kalman filter with full matrices (education purpose only)! Will be deleted soon!
25*/
26
27class KalmanFull {
28protected:
29        int dimx, dimy, dimu;
30        mat A, B, C, D, R, Q;
31
32        //cache
33        mat _Pp, _Ry, _iRy, _K;
34public:
35        //posterior
36        //! Mean value of the posterior density
37        vec mu;
38        //! Variance of the posterior density
39        mat P;
40
41        bool evalll;
42        double ll;
43public:
44        //! Full constructor
45        KalmanFull ( mat A, mat B, mat C, mat D, mat R, mat Q, mat P0, vec mu0 );
46        //! Here dt = [yt;ut] of appropriate dimensions
47        void bayes ( const vec &dt );
48        //! print elements of KF
49        friend std::ostream &operator<< ( std::ostream &os, const KalmanFull &kf );
50        //! For EKFfull;
51        KalmanFull(){};
52};
53
54
55/*!
56* \brief Kalman filter with covariance matrices in square root form.
57
58Parameter evolution model:\f[ x_t = A x_{t-1} + B u_t + Q^{1/2} e_t \f]
59Observation model: \f[ y_t = C x_{t-1} + C u_t + Q^{1/2} w_t. \f]
60Where $e_t$ and $w_t$ are independent vectors Normal(0,1)-distributed disturbances.
61*/
62template<class sq_T>
63
64class Kalman : public BM {
65protected:
66        //! Indetifier of output rv
67        RV rvy;
68        //! Indetifier of exogeneous rv
69        RV rvu;
70        //! cache of rv.count()
71        int dimx;
72        //! cache of rvy.count()
73        int dimy;
74        //! cache of rvu.count()
75        int dimu;
76        //! Matrix A
77        mat A;
78        //! Matrix B
79        mat B; 
80        //! Matrix C
81        mat C;
82        //! Matrix D
83        mat D;
84        //! Matrix Q in square-root form
85        sq_T Q;
86        //! Matrix R in square-root form
87        sq_T R;
88
89        //!posterior density on $x_t$
90        enorm<sq_T> est;
91        //!preditive density on $y_t$
92        enorm<sq_T> fy;
93
94        //! placeholder for Kalman gain
95        mat _K;
96        //! cache of fy.mu
97        vec& _yp;
98        //! cache of fy.R
99        sq_T& _Ry;
100        //!cache of est.mu
101        vec& _mu;
102        //!cache of est.R
103        sq_T& _P;
104
105public:
106        //! Default constructor
107        Kalman ( );
108        //! Copy constructor
109        Kalman ( const Kalman<sq_T> &K0 );
110        //! Set parameters with check of relevance
111        void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const sq_T &Q0,const sq_T &R0 );
112        //! Set estimate values, used e.g. in initialization.
113        void set_est ( const vec &mu0, const sq_T &P0 ) {
114                sq_T pom(dimy);
115                est.set_parameters ( mu0,P0 );
116                P0.mult_sym(C,pom);
117                fy.set_parameters ( C*mu0, pom );
118        };
119
120        //! Here dt = [yt;ut] of appropriate dimensions
121        void bayes ( const vec &dt );
122        //!access function
123        const epdf& posterior() const {return est;}
124        const enorm<sq_T>* _e() const {return &est;}
125        //!access function
126        mat& __K() {return _K;}
127        //!access function
128        vec _dP() {return _P->getD();}
129};
130
131/*! \brief Kalman filter in square root form
132
133Trivial example:
134\include kalman_simple.cpp
135 
136*/
137class KalmanCh : public Kalman<chmat>{
138protected:
139//! pre array (triangular matrix)
140mat preA;
141//! post array (triangular matrix)
142mat postA;
143
144public:
145        //! Default constructor
146        KalmanCh ():Kalman<chmat>(),preA(),postA(){};
147        //! Set parameters with check of relevance
148        void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const chmat &Q0,const chmat &R0 );
149        void set_statistics ( const vec &mu0, const chmat &P0 ) {
150                est.set_parameters ( mu0,P0 );
151        };
152       
153       
154        /*!\brief  Here dt = [yt;ut] of appropriate dimensions
155       
156        The following equality hold::\f[
157\left[\begin{array}{cc}
158R^{0.5}\\
159P_{t|t-1}^{0.5}C' & P_{t|t-1}^{0.5}CA'\\
160 & Q^{0.5}\end{array}\right]<\mathrm{orth.oper.}>=\left[\begin{array}{cc}
161R_{y}^{0.5} & KA'\\
162 & P_{t+1|t}^{0.5}\\
163\\\end{array}\right]\f]
164
165Thus this object evaluates only predictors! Not filtering densities.
166        */
167        void bayes ( const vec &dt );
168};
169
170/*!
171\brief Extended Kalman Filter in full matrices
172
173An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
174*/
175class EKFfull : public KalmanFull, public BM {
176
177        //! Internal Model f(x,u)
178        diffbifn* pfxu;
179        //! Observation Model h(x,u)
180        diffbifn* phxu;
181       
182        enorm<fsqmat> E; 
183public:
184        //! Default constructor
185        EKFfull ( );
186        //! Set nonlinear functions for mean values and covariance matrices.
187        void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 );
188        //! Here dt = [yt;ut] of appropriate dimensions
189        void bayes ( const vec &dt );
190        //! set estimates
191        void set_est (vec mu0, mat P0){mu=mu0;P=P0;};
192        //!dummy!
193        const epdf& posterior()const{return E;};
194        const enorm<fsqmat>* _e()const{return &E;};
195        const mat _R(){return P;}
196};
197
198/*!
199\brief Extended Kalman Filter
200
201An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
202*/
203template<class sq_T>
204class EKF : public Kalman<fsqmat> {
205        //! Internal Model f(x,u)
206        diffbifn* pfxu;
207        //! Observation Model h(x,u)
208        diffbifn* phxu;
209public:
210        //! Default constructor
211        EKF ( RV rvx, RV rvy, RV rvu );
212        //! Set nonlinear functions for mean values and covariance matrices.
213        void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const sq_T Q0, const sq_T R0 );
214        //! Here dt = [yt;ut] of appropriate dimensions
215        void bayes ( const vec &dt );
216};
217
218/*!
219\brief Extended Kalman Filter in Square root
220
221An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
222*/
223
224class EKFCh : public KalmanCh {
225        protected:
226        //! Internal Model f(x,u)
227        diffbifn* pfxu;
228        //! Observation Model h(x,u)
229        diffbifn* phxu;
230public:
231        //! Default constructor
232        EKFCh ();
233        //! Set nonlinear functions for mean values and covariance matrices.
234        void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 );
235        //! Here dt = [yt;ut] of appropriate dimensions
236        void bayes ( const vec &dt );
237};
238
239/*!
240\brief Kalman Filter with conditional diagonal matrices R and Q.
241*/
242
243class KFcondQR : public Kalman<ldmat>, public BMcond {
244//protected:
245public:
246        //!Default constructor
247        KFcondQR ( ) : Kalman<ldmat> ( ),BMcond ( ) {};
248
249        void condition ( const vec &RQ );
250};
251
252/*!
253\brief Kalman Filter with conditional diagonal matrices R and Q.
254*/
255
256class KFcondR : public Kalman<ldmat>, public BMcond {
257//protected:
258public:
259        //!Default constructor
260        KFcondR ( ) : Kalman<ldmat> ( ),BMcond ( ) {};
261
262        void condition ( const vec &R );
263};
264
265//////// INstance
266
267template<class sq_T>
268Kalman<sq_T>::Kalman ( const Kalman<sq_T> &K0 ) : BM ( ),rvy ( K0.rvy ),rvu ( K0.rvu ),
269                dimx ( K0.dimx ), dimy ( K0.dimy ),dimu ( K0.dimu ),
270                A ( K0.A ), B ( K0.B ), C ( K0.C ), D ( K0.D ),
271                Q(K0.Q), R(K0.R),
272                est ( K0.est ), fy ( K0.fy ), _yp(fy._mu()),_Ry(fy._R()), _mu(est._mu()), _P(est._R()) {
273
274// copy values in pointers
275//      _mu = K0._mu;
276//      _P = K0._P;
277//      _yp = K0._yp;
278//      _Ry = K0._Ry;
279
280}
281
282template<class sq_T>
283Kalman<sq_T>::Kalman ( ) : BM (), est ( ), fy (),  _yp(fy._mu()), _Ry(fy._R()), _mu(est._mu()), _P(est._R()) {
284};
285
286template<class sq_T>
287void 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 ) {
288        dimx = A0.rows();
289        dimy = C0.rows();
290        dimu = B0.cols();
291       
292        it_assert_debug ( A0.cols() ==dimx, "Kalman: A is not square" );
293        it_assert_debug ( B0.rows() ==dimx, "Kalman: B is not compatible" );
294        it_assert_debug ( C0.cols() ==dimx, "Kalman: C is not square" );
295        it_assert_debug ( ( D0.rows() ==dimy ) || ( D0.cols() ==dimu ), "Kalman: D is not compatible" );
296        it_assert_debug ( ( R0.cols() ==dimy ) || ( R0.rows() ==dimy ), "Kalman: R is not compatible" );
297        it_assert_debug ( ( Q0.cols() ==dimx ) || ( Q0.rows() ==dimx ), "Kalman: Q is not compatible" );
298
299        A = A0;
300        B = B0;
301        C = C0;
302        D = D0;
303        R = R0;
304        Q = Q0;
305}
306
307template<class sq_T>
308void Kalman<sq_T>::bayes ( const vec &dt ) {
309        it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
310
311        sq_T iRy(dimy);
312        vec u = dt.get ( dimy,dimy+dimu-1 );
313        vec y = dt.get ( 0,dimy-1 );
314        //Time update
315        _mu = A* _mu + B*u;
316        //P  = A*P*A.transpose() + Q; in sq_T
317        _P.mult_sym ( A );
318        _P  +=Q;
319
320        //Data update
321        //_Ry = C*P*C.transpose() + R; in sq_T
322        _P.mult_sym ( C, _Ry );
323        _Ry  +=R;
324
325        mat Pfull = _P.to_mat();
326
327        _Ry.inv ( iRy ); // result is in _iRy;
328        _K = Pfull*C.transpose() * ( iRy.to_mat() );
329
330        sq_T pom ( ( int ) Pfull.rows() );
331        iRy.mult_sym_t ( C*Pfull,pom );
332        (_P ) -= pom; // P = P -PC'iRy*CP;
333        (_yp ) = C* _mu  +D*u; //y prediction
334        (_mu ) += _K* ( y- _yp  );
335
336
337        if ( evalll==true ) { //likelihood of observation y
338                ll=fy.evallog ( y );
339        }
340
341//cout << "y: " << y-(*_yp) <<" R: " << _Ry->to_mat() << " iR: " << _iRy->to_mat() << " ll: " << ll <<endl;
342
343};
344 
345
346
347//TODO why not const pointer??
348
349template<class sq_T>
350EKF<sq_T>::EKF ( RV rvx0, RV rvy0, RV rvu0 ) : Kalman<sq_T> ( rvx0,rvy0,rvu0 ) {}
351
352template<class sq_T>
353void EKF<sq_T>::set_parameters ( diffbifn* pfxu0,  diffbifn* phxu0,const sq_T Q0,const sq_T R0 ) {
354        pfxu = pfxu0;
355        phxu = phxu0;
356
357        //initialize matrices A C, later, these will be only updated!
358        pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true );
359//      pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true );
360        B.clear();
361        phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true );
362//      phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true );
363        D.clear();
364
365        R = R0;
366        Q = Q0;
367}
368
369template<class sq_T>
370void EKF<sq_T>::bayes ( const vec &dt ) {
371        it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" );
372
373        sq_T iRy(dimy,dimy);
374        vec u = dt.get ( dimy,dimy+dimu-1 );
375        vec y = dt.get ( 0,dimy-1 );
376        //Time update
377        _mu = pfxu->eval ( _mu, u );
378        pfxu->dfdx_cond ( _mu,u,A,false ); //update A by a derivative of fx
379
380        //P  = A*P*A.transpose() + Q; in sq_T
381        _P.mult_sym ( A );
382        _P +=Q;
383
384        //Data update
385        phxu->dfdx_cond ( _mu,u,C,false ); //update C by a derivative hx
386        //_Ry = C*P*C.transpose() + R; in sq_T
387        _P.mult_sym ( C, _Ry );
388        ( _Ry ) +=R;
389
390        mat Pfull = _P.to_mat();
391
392        _Ry.inv ( iRy ); // result is in _iRy;
393        _K = Pfull*C.transpose() * ( iRy.to_mat() );
394
395        sq_T pom ( ( int ) Pfull.rows() );
396        iRy.mult_sym_t ( C*Pfull,pom );
397        (_P ) -= pom; // P = P -PC'iRy*CP;
398        _yp = phxu->eval ( _mu,u ); //y prediction
399        ( _mu ) += _K* ( y-_yp );
400
401        if ( evalll==true ) {ll+=fy.evallog ( y );}
402};
403
404
405}
406#endif // KF_H
407
Note: See TracBrowser for help on using the browser.