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 "libBM.h" |
---|
18 | #include "libDC.h" |
---|
19 | |
---|
20 | |
---|
21 | using namespace itpp; |
---|
22 | |
---|
23 | /*! |
---|
24 | * \brief Basic Kalman filter with full matrices (education purpose only)! |
---|
25 | */ |
---|
26 | class KalmanFull : public BM { |
---|
27 | int dimx, dimy, dimu; |
---|
28 | mat A, B, C, D, R, Q; |
---|
29 | |
---|
30 | //cache |
---|
31 | mat _Pp, _Ry, _iRy, _K; |
---|
32 | public: |
---|
33 | //posterior |
---|
34 | //! Mean value of the posterior density |
---|
35 | vec mu; |
---|
36 | //! Variance of the posterior density |
---|
37 | mat P; |
---|
38 | |
---|
39 | public: |
---|
40 | //! Full constructor |
---|
41 | KalmanFull ( mat A, mat B, mat C, mat D, mat R, mat Q, mat P0, vec mu0); |
---|
42 | //! Here dt = [yt;ut] of appropriate dimensions |
---|
43 | void bayes(const vec &dt, bool evalll=true); |
---|
44 | |
---|
45 | friend std::ostream &operator<< ( std::ostream &os, const KalmanFull &kf ); |
---|
46 | |
---|
47 | }; |
---|
48 | |
---|
49 | |
---|
50 | /*! |
---|
51 | * \brief Kalman filter with covaraince matrices in square root form. |
---|
52 | */ |
---|
53 | template<class sq_T> |
---|
54 | class Kalman : public BM { |
---|
55 | int dimx, dimy, dimu; |
---|
56 | mat A, B, C, D; |
---|
57 | sq_T R, Q; |
---|
58 | |
---|
59 | //cache |
---|
60 | mat _K, _yp; |
---|
61 | sq_T _Ry,_iRy; |
---|
62 | public: |
---|
63 | //posterior |
---|
64 | //! Mean value of the posterior density |
---|
65 | vec mu; |
---|
66 | //! Mean value of the posterior density |
---|
67 | sq_T P; |
---|
68 | |
---|
69 | public: |
---|
70 | //! Full constructor |
---|
71 | Kalman ( mat A0, mat B0, mat C0, mat D0, sq_T R0, sq_T Q0, sq_T P0, vec mu0 ); |
---|
72 | //! Here dt = [yt;ut] of appropriate dimensions |
---|
73 | void bayes(const vec &dt, bool evalll=true); |
---|
74 | |
---|
75 | friend std::ostream &operator<< ( std::ostream &os, const KalmanFull &kf ); |
---|
76 | |
---|
77 | }; |
---|
78 | |
---|
79 | //////// INstance |
---|
80 | |
---|
81 | template<class sq_T> |
---|
82 | Kalman<sq_T>::Kalman( mat A0, mat B0, mat C0, mat D0, sq_T R0, sq_T Q0, sq_T P0, vec mu0 ) { |
---|
83 | dimx = A0.rows(); |
---|
84 | dimu = B0.cols(); |
---|
85 | dimy = C0.rows(); |
---|
86 | |
---|
87 | it_assert_debug( A0.cols()==dimx, "Kalman: A is not square" ); |
---|
88 | it_assert_debug( B0.rows()==dimx, "Kalman: B is not compatible" ); |
---|
89 | it_assert_debug( C0.cols()==dimx, "Kalman: C is not square" ); |
---|
90 | it_assert_debug(( D0.rows()==dimy ) || ( D0.cols()==dimu ), "Kalman: D is not compatible" ); |
---|
91 | it_assert_debug(( R0.cols()==dimy ) || ( R0.rows()==dimy ), "Kalman: R is not compatible" ); |
---|
92 | it_assert_debug(( Q0.cols()==dimx ) || ( Q0.rows()==dimx ), "Kalman: Q is not compatible" ); |
---|
93 | |
---|
94 | A = A0; |
---|
95 | B = B0; |
---|
96 | C = C0; |
---|
97 | D = D0; |
---|
98 | R = R0; |
---|
99 | Q = Q0; |
---|
100 | mu = mu0; |
---|
101 | P = P0; |
---|
102 | |
---|
103 | ll = 0; |
---|
104 | //Fixme should we assign cache?? |
---|
105 | } |
---|
106 | |
---|
107 | template<class sq_T> |
---|
108 | void Kalman<sq_T>::bayes( const vec &dt , bool evalll) { |
---|
109 | it_assert_debug( dt.length()==( dimy+dimu ),"KalmanFull::bayes wrong size of dt" ); |
---|
110 | |
---|
111 | vec u = dt.get( dimy,dimy+dimu-1 ); |
---|
112 | vec y = dt.get( 0,dimy-1 ); |
---|
113 | //Time update |
---|
114 | mu = A*mu + B*u; |
---|
115 | //P = A*P*A.transpose() + Q; in sq_T |
---|
116 | P.mult_qform( A ); |
---|
117 | P+=Q; |
---|
118 | |
---|
119 | //Data update |
---|
120 | //_Ry = C*P*C.transpose() + R; in sq_T |
---|
121 | _Ry.mult_qform( C ); |
---|
122 | _Ry+=R; |
---|
123 | |
---|
124 | mat Pfull = P.to_mat(); |
---|
125 | |
---|
126 | _Ry.inv( _iRy ); // result is in _iRy; |
---|
127 | _K = Pfull*C.transpose()*(_iRy.to_mat()); |
---|
128 | P -= _K*C*Pfull; // P = P -KCP; |
---|
129 | _yp = y-C*mu-D*u; //y prediction |
---|
130 | mu += _K*( _yp ); |
---|
131 | |
---|
132 | if (evalll==true) { |
---|
133 | ll+= -0.5*_Ry.logdet() -0.5*_iRy.qform(_yp); |
---|
134 | } |
---|
135 | }; |
---|
136 | |
---|
137 | //extern template class Kalman<ldmat>; |
---|
138 | |
---|
139 | |
---|
140 | #endif // KF_H |
---|
141 | |
---|