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 | #include <itpp/itbase.h> |
---|
17 | #include "../stat/libFN.h" |
---|
18 | #include "../stat/libEF.h" |
---|
19 | #include "../math/chmat.h" |
---|
20 | |
---|
21 | using namespace itpp; |
---|
22 | |
---|
23 | /*! |
---|
24 | * \brief Basic Kalman filter with full matrices (education purpose only)! Will be deleted soon! |
---|
25 | */ |
---|
26 | |
---|
27 | class KalmanFull { |
---|
28 | protected: |
---|
29 | int dimx, dimy, dimu; |
---|
30 | mat A, B, C, D, R, Q; |
---|
31 | |
---|
32 | //cache |
---|
33 | mat _Pp, _Ry, _iRy, _K; |
---|
34 | public: |
---|
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; |
---|
43 | public: |
---|
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 | |
---|
58 | Parameter evolution model:\f[ x_t = A x_{t-1} + B u_t + Q^{1/2} e_t \f] |
---|
59 | Observation model: \f[ y_t = C x_{t-1} + C u_t + Q^{1/2} w_t. \f] |
---|
60 | Where $e_t$ and $w_t$ are independent vectors Normal(0,1)-distributed disturbances. |
---|
61 | */ |
---|
62 | template<class sq_T> |
---|
63 | |
---|
64 | class Kalman : public BM { |
---|
65 | protected: |
---|
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 | |
---|
105 | public: |
---|
106 | //! Default constructor |
---|
107 | Kalman ( RV rvx0, RV rvy0, RV rvu0 ); |
---|
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 &R0,const sq_T &Q0 ); |
---|
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 | epdf& _epdf() {return est;} |
---|
124 | //!access function |
---|
125 | mat& __K() {return _K;} |
---|
126 | //!access function |
---|
127 | vec _dP() {return _P->getD();} |
---|
128 | }; |
---|
129 | |
---|
130 | /*! \brief Kalman filter in square root form |
---|
131 | */ |
---|
132 | class KalmanCh : public Kalman<chmat>{ |
---|
133 | protected: |
---|
134 | //! pre array (triangular matrix) |
---|
135 | mat preA; |
---|
136 | //! post array (triangular matrix) |
---|
137 | mat postA; |
---|
138 | |
---|
139 | public: |
---|
140 | //! Default constructor |
---|
141 | KalmanCh ( RV rvx0, RV rvy0, RV rvu0 ):Kalman<chmat>(rvx0,rvy0,rvu0),preA(dimy+dimx+dimx,dimy+dimx),postA(dimy+dimx,dimy+dimx){}; |
---|
142 | //! Set parameters with check of relevance |
---|
143 | void set_parameters ( const mat &A0,const mat &B0,const mat &C0,const mat &D0,const chmat &R0,const chmat &Q0 ); |
---|
144 | void set_est ( const vec &mu0, const chmat &P0 ) { |
---|
145 | est.set_parameters ( mu0,P0 ); |
---|
146 | }; |
---|
147 | |
---|
148 | |
---|
149 | /*!\brief Here dt = [yt;ut] of appropriate dimensions |
---|
150 | |
---|
151 | The following equality hold::\f[ |
---|
152 | \left[\begin{array}{cc} |
---|
153 | R^{0.5}\\ |
---|
154 | P_{t|t-1}^{0.5}C' & P_{t|t-1}^{0.5}CA'\\ |
---|
155 | & Q^{0.5}\end{array}\right]<\mathrm{orth.oper.}>=\left[\begin{array}{cc} |
---|
156 | R_{y}^{0.5} & KA'\\ |
---|
157 | & P_{t+1|t}^{0.5}\\ |
---|
158 | \\\end{array}\right]\f] |
---|
159 | |
---|
160 | Thus this object evaluates only predictors! Not filtering densities. |
---|
161 | */ |
---|
162 | void bayes ( const vec &dt ); |
---|
163 | }; |
---|
164 | |
---|
165 | /*! |
---|
166 | \brief Extended Kalman Filter in full matrices |
---|
167 | |
---|
168 | An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean. |
---|
169 | */ |
---|
170 | class EKFfull : public KalmanFull, public BM { |
---|
171 | |
---|
172 | //! Internal Model f(x,u) |
---|
173 | diffbifn* pfxu; |
---|
174 | //! Observation Model h(x,u) |
---|
175 | diffbifn* phxu; |
---|
176 | |
---|
177 | enorm<fsqmat> E; |
---|
178 | public: |
---|
179 | //! Default constructor |
---|
180 | EKFfull ( RV rvx, RV rvy, RV rvu ); |
---|
181 | //! Set nonlinear functions for mean values and covariance matrices. |
---|
182 | void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const mat Q0, const mat R0 ); |
---|
183 | //! Here dt = [yt;ut] of appropriate dimensions |
---|
184 | void bayes ( const vec &dt ); |
---|
185 | //! set estimates |
---|
186 | void set_est (vec mu0, mat P0){mu=mu0;P=P0;}; |
---|
187 | //!dummy! |
---|
188 | epdf& _epdf(){return E;}; |
---|
189 | }; |
---|
190 | |
---|
191 | /*! |
---|
192 | \brief Extended Kalman Filter |
---|
193 | |
---|
194 | An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean. |
---|
195 | */ |
---|
196 | template<class sq_T> |
---|
197 | class EKF : public Kalman<fsqmat> { |
---|
198 | //! Internal Model f(x,u) |
---|
199 | diffbifn* pfxu; |
---|
200 | //! Observation Model h(x,u) |
---|
201 | diffbifn* phxu; |
---|
202 | public: |
---|
203 | //! Default constructor |
---|
204 | EKF ( RV rvx, RV rvy, RV rvu ); |
---|
205 | //! Set nonlinear functions for mean values and covariance matrices. |
---|
206 | void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const sq_T Q0, const sq_T R0 ); |
---|
207 | //! Here dt = [yt;ut] of appropriate dimensions |
---|
208 | void bayes ( const vec &dt ); |
---|
209 | }; |
---|
210 | |
---|
211 | /*! |
---|
212 | \brief Extended Kalman Filter in Square root |
---|
213 | |
---|
214 | An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean. |
---|
215 | */ |
---|
216 | |
---|
217 | class EKFCh : public KalmanCh { |
---|
218 | //! Internal Model f(x,u) |
---|
219 | diffbifn* pfxu; |
---|
220 | //! Observation Model h(x,u) |
---|
221 | diffbifn* phxu; |
---|
222 | public: |
---|
223 | //! Default constructor |
---|
224 | EKFCh ( RV rvx, RV rvy, RV rvu ); |
---|
225 | //! Set nonlinear functions for mean values and covariance matrices. |
---|
226 | void set_parameters ( diffbifn* pfxu, diffbifn* phxu, const chmat Q0, const chmat R0 ); |
---|
227 | //! Here dt = [yt;ut] of appropriate dimensions |
---|
228 | void bayes ( const vec &dt ); |
---|
229 | }; |
---|
230 | |
---|
231 | /*! |
---|
232 | \brief Kalman Filter with conditional diagonal matrices R and Q. |
---|
233 | */ |
---|
234 | |
---|
235 | class KFcondQR : public Kalman<ldmat>, public BMcond { |
---|
236 | //protected: |
---|
237 | public: |
---|
238 | //!Default constructor |
---|
239 | KFcondQR ( RV rvx, RV rvy, RV rvu, RV rvRQ ) : Kalman<ldmat> ( rvx, rvy,rvu ),BMcond ( rvRQ ) {}; |
---|
240 | |
---|
241 | void condition ( const vec &RQ ); |
---|
242 | }; |
---|
243 | |
---|
244 | /*! |
---|
245 | \brief Kalman Filter with conditional diagonal matrices R and Q. |
---|
246 | */ |
---|
247 | |
---|
248 | class KFcondR : public Kalman<ldmat>, public BMcond { |
---|
249 | //protected: |
---|
250 | public: |
---|
251 | //!Default constructor |
---|
252 | KFcondR ( RV rvx, RV rvy, RV rvu, RV rvR ) : Kalman<ldmat> ( rvx, rvy,rvu ),BMcond ( rvR ) {}; |
---|
253 | |
---|
254 | void condition ( const vec &R ); |
---|
255 | }; |
---|
256 | |
---|
257 | //////// INstance |
---|
258 | |
---|
259 | template<class sq_T> |
---|
260 | Kalman<sq_T>::Kalman ( const Kalman<sq_T> &K0 ) : BM ( K0.rv ),rvy ( K0.rvy ),rvu ( K0.rvu ), |
---|
261 | dimx ( rv.count() ), dimy ( rvy.count() ),dimu ( rvu.count() ), |
---|
262 | A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ), |
---|
263 | Q(dimx), R(dimy), |
---|
264 | est ( rv ), fy ( rvy ), _yp(fy._mu()),_Ry(fy._R()), _mu(est._mu()), _P(est._R()) { |
---|
265 | |
---|
266 | this->set_parameters ( K0.A, K0.B, K0.C, K0.D, K0.R, K0.Q ); |
---|
267 | |
---|
268 | // copy values in pointers |
---|
269 | _mu = K0._mu; |
---|
270 | _P = K0._P; |
---|
271 | _yp = K0._yp; |
---|
272 | _Ry = K0._Ry; |
---|
273 | |
---|
274 | } |
---|
275 | |
---|
276 | template<class sq_T> |
---|
277 | Kalman<sq_T>::Kalman ( RV rvx, RV rvy0, RV rvu0 ) : BM ( rvx ),rvy ( rvy0 ),rvu ( rvu0 ), |
---|
278 | dimx ( rvx.count() ), dimy ( rvy.count() ),dimu ( rvu.count() ), |
---|
279 | A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ), |
---|
280 | Q(dimx), R (dimy), |
---|
281 | est ( rvx ), fy ( rvy ), _yp(fy._mu()),_Ry(fy._R()),_mu(est._mu()), _P(est._R()) { |
---|
282 | }; |
---|
283 | |
---|
284 | template<class sq_T> |
---|
285 | 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 ) { |
---|
286 | it_assert_debug ( A0.cols() ==dimx, "Kalman: A is not square" ); |
---|
287 | it_assert_debug ( B0.rows() ==dimx, "Kalman: B is not compatible" ); |
---|
288 | it_assert_debug ( C0.cols() ==dimx, "Kalman: C is not square" ); |
---|
289 | it_assert_debug ( ( D0.rows() ==dimy ) || ( D0.cols() ==dimu ), "Kalman: D is not compatible" ); |
---|
290 | it_assert_debug ( ( R0.cols() ==dimy ) || ( R0.rows() ==dimy ), "Kalman: R is not compatible" ); |
---|
291 | it_assert_debug ( ( Q0.cols() ==dimx ) || ( Q0.rows() ==dimx ), "Kalman: Q is not compatible" ); |
---|
292 | |
---|
293 | A = A0; |
---|
294 | B = B0; |
---|
295 | C = C0; |
---|
296 | D = D0; |
---|
297 | R = R0; |
---|
298 | Q = Q0; |
---|
299 | } |
---|
300 | |
---|
301 | template<class sq_T> |
---|
302 | void Kalman<sq_T>::bayes ( const vec &dt ) { |
---|
303 | it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" ); |
---|
304 | |
---|
305 | sq_T iRy(dimy); |
---|
306 | vec u = dt.get ( dimy,dimy+dimu-1 ); |
---|
307 | vec y = dt.get ( 0,dimy-1 ); |
---|
308 | //Time update |
---|
309 | _mu = A* _mu + B*u; |
---|
310 | //P = A*P*A.transpose() + Q; in sq_T |
---|
311 | _P.mult_sym ( A ); |
---|
312 | _P +=Q; |
---|
313 | |
---|
314 | //Data update |
---|
315 | //_Ry = C*P*C.transpose() + R; in sq_T |
---|
316 | _P.mult_sym ( C, _Ry ); |
---|
317 | _Ry +=R; |
---|
318 | |
---|
319 | mat Pfull = _P.to_mat(); |
---|
320 | |
---|
321 | _Ry.inv ( iRy ); // result is in _iRy; |
---|
322 | _K = Pfull*C.transpose() * ( iRy.to_mat() ); |
---|
323 | |
---|
324 | sq_T pom ( ( int ) Pfull.rows() ); |
---|
325 | iRy.mult_sym_t ( C*Pfull,pom ); |
---|
326 | (_P ) -= pom; // P = P -PC'iRy*CP; |
---|
327 | (_yp ) = C* _mu +D*u; //y prediction |
---|
328 | (_mu ) += _K* ( y- _yp ); |
---|
329 | |
---|
330 | |
---|
331 | if ( evalll==true ) { //likelihood of observation y |
---|
332 | ll=fy.evalpdflog ( y ); |
---|
333 | } |
---|
334 | |
---|
335 | //cout << "y: " << y-(*_yp) <<" R: " << _Ry->to_mat() << " iR: " << _iRy->to_mat() << " ll: " << ll <<endl; |
---|
336 | |
---|
337 | }; |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | //TODO why not const pointer?? |
---|
342 | |
---|
343 | template<class sq_T> |
---|
344 | EKF<sq_T>::EKF ( RV rvx0, RV rvy0, RV rvu0 ) : Kalman<sq_T> ( rvx0,rvy0,rvu0 ) {} |
---|
345 | |
---|
346 | template<class sq_T> |
---|
347 | void EKF<sq_T>::set_parameters ( diffbifn* pfxu0, diffbifn* phxu0,const sq_T Q0,const sq_T R0 ) { |
---|
348 | pfxu = pfxu0; |
---|
349 | phxu = phxu0; |
---|
350 | |
---|
351 | //initialize matrices A C, later, these will be only updated! |
---|
352 | pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true ); |
---|
353 | // pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); |
---|
354 | B.clear(); |
---|
355 | phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true ); |
---|
356 | // phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); |
---|
357 | D.clear(); |
---|
358 | |
---|
359 | R = R0; |
---|
360 | Q = Q0; |
---|
361 | } |
---|
362 | |
---|
363 | template<class sq_T> |
---|
364 | void EKF<sq_T>::bayes ( const vec &dt ) { |
---|
365 | it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" ); |
---|
366 | |
---|
367 | sq_T iRy(dimy,dimy); |
---|
368 | vec u = dt.get ( dimy,dimy+dimu-1 ); |
---|
369 | vec y = dt.get ( 0,dimy-1 ); |
---|
370 | //Time update |
---|
371 | _mu = pfxu->eval ( _mu, u ); |
---|
372 | pfxu->dfdx_cond ( _mu,u,A,false ); //update A by a derivative of fx |
---|
373 | |
---|
374 | //P = A*P*A.transpose() + Q; in sq_T |
---|
375 | _P.mult_sym ( A ); |
---|
376 | _P +=Q; |
---|
377 | |
---|
378 | //Data update |
---|
379 | phxu->dfdx_cond ( _mu,u,C,false ); //update C by a derivative hx |
---|
380 | //_Ry = C*P*C.transpose() + R; in sq_T |
---|
381 | _P.mult_sym ( C, _Ry ); |
---|
382 | ( _Ry ) +=R; |
---|
383 | |
---|
384 | mat Pfull = _P.to_mat(); |
---|
385 | |
---|
386 | _Ry.inv ( iRy ); // result is in _iRy; |
---|
387 | _K = Pfull*C.transpose() * ( iRy.to_mat() ); |
---|
388 | |
---|
389 | sq_T pom ( ( int ) Pfull.rows() ); |
---|
390 | iRy.mult_sym_t ( C*Pfull,pom ); |
---|
391 | (_P ) -= pom; // P = P -PC'iRy*CP; |
---|
392 | _yp = phxu->eval ( _mu,u ); //y prediction |
---|
393 | ( _mu ) += _K* ( y-_yp ); |
---|
394 | |
---|
395 | if ( evalll==true ) {ll+=fy.evalpdflog ( y );} |
---|
396 | }; |
---|
397 | |
---|
398 | |
---|
399 | #endif // KF_H |
---|
400 | |
---|