1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
---|
3 | <title>mixpp: work/mixpp/libKF.h Source File</title> |
---|
4 | <link href="doxygen.css" rel="stylesheet" type="text/css"> |
---|
5 | <link href="tabs.css" rel="stylesheet" type="text/css"> |
---|
6 | </head><body> |
---|
7 | <!-- Generated by Doxygen 1.5.3 --> |
---|
8 | <div class="tabs"> |
---|
9 | <ul> |
---|
10 | <li><a href="index.html"><span>Main Page</span></a></li> |
---|
11 | <li><a href="annotated.html"><span>Classes</span></a></li> |
---|
12 | <li class="current"><a href="files.html"><span>Files</span></a></li> |
---|
13 | </ul> |
---|
14 | </div> |
---|
15 | <h1>work/mixpp/libKF.h</h1><a href="libKF_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 |
---|
16 | <a name="l00013"></a>00013 <span class="preprocessor">#ifndef KF_H</span> |
---|
17 | <a name="l00014"></a>00014 <span class="preprocessor"></span><span class="preprocessor">#define KF_H</span> |
---|
18 | <a name="l00015"></a>00015 <span class="preprocessor"></span> |
---|
19 | <a name="l00016"></a>00016 <span class="preprocessor">#include <itpp/itbase.h></span> |
---|
20 | <a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="libBM_8h.html" title="Bayesian Models (bm) that use Bayes rule to learn from observations.">libBM.h</a>"</span> |
---|
21 | <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="libDC_8h.html" title="Matrices in decomposed forms (LDL&#39;, LU, UDU&#39;, etc).">libDC.h</a>"</span> |
---|
22 | <a name="l00019"></a>00019 |
---|
23 | <a name="l00020"></a>00020 |
---|
24 | <a name="l00021"></a>00021 <span class="keyword">using namespace </span>itpp; |
---|
25 | <a name="l00022"></a>00022 |
---|
26 | <a name="l00026"></a><a class="code" href="classKalmanFull.html">00026</a> <span class="keyword">class </span><a class="code" href="classKalmanFull.html" title="Basic Kalman filter with full matrices (education purpose only)! Will be deleted...">KalmanFull</a> : <span class="keyword">public</span> <a class="code" href="classBM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a> { |
---|
27 | <a name="l00027"></a>00027 <span class="keywordtype">int</span> dimx, dimy, dimu; |
---|
28 | <a name="l00028"></a>00028 mat A, B, C, D, R, Q; |
---|
29 | <a name="l00029"></a>00029 |
---|
30 | <a name="l00030"></a>00030 <span class="comment">//cache </span> |
---|
31 | <a name="l00031"></a>00031 mat _Pp, _Ry, _iRy, _K; |
---|
32 | <a name="l00032"></a>00032 <span class="keyword">public</span>: |
---|
33 | <a name="l00033"></a>00033 <span class="comment">//posterior </span> |
---|
34 | <a name="l00035"></a><a class="code" href="classKalmanFull.html#fb5aec635e2720cc5ac31bc01c18a68a">00035</a> <span class="comment"></span> vec <a class="code" href="classKalmanFull.html#fb5aec635e2720cc5ac31bc01c18a68a" title="Mean value of the posterior density.">mu</a>; |
---|
35 | <a name="l00037"></a><a class="code" href="classKalmanFull.html#b75dc059e84fa8ffc076203b30f926cc">00037</a> mat <a class="code" href="classKalmanFull.html#b75dc059e84fa8ffc076203b30f926cc" title="Variance of the posterior density.">P</a>; |
---|
36 | <a name="l00038"></a>00038 |
---|
37 | <a name="l00039"></a>00039 <span class="keyword">public</span>: |
---|
38 | <a name="l00041"></a>00041 <a class="code" href="classKalmanFull.html#7197ab6e7380790006394eabd3b97043" title="Full constructor.">KalmanFull</a> ( mat A, mat B, mat C, mat D, mat R, mat Q, mat P0, vec mu0); |
---|
39 | <a name="l00043"></a>00043 <span class="keywordtype">void</span> <a class="code" href="classKalmanFull.html#048b13739b94c331cda08249b278552b" title="Here dt = [yt;ut] of appropriate dimensions.">bayes</a>(<span class="keyword">const</span> vec &dt, <span class="keywordtype">bool</span> evalll=<span class="keyword">true</span>); |
---|
40 | <a name="l00044"></a>00044 |
---|
41 | <a name="l00045"></a>00045 <span class="keyword">friend</span> std::ostream &operator<< ( std::ostream &os, <span class="keyword">const</span> <a class="code" href="classKalmanFull.html" title="Basic Kalman filter with full matrices (education purpose only)! Will be deleted...">KalmanFull</a> &kf ); |
---|
42 | <a name="l00046"></a>00046 |
---|
43 | <a name="l00047"></a>00047 }; |
---|
44 | <a name="l00048"></a>00048 |
---|
45 | <a name="l00049"></a>00049 |
---|
46 | <a name="l00053"></a>00053 <span class="keyword">template</span><<span class="keyword">class</span> sq_T> |
---|
47 | <a name="l00054"></a><a class="code" href="classKalman.html">00054</a> <span class="keyword">class </span><a class="code" href="classKalman.html" title="Kalman filter with covaraince matrices in square root form.">Kalman</a> : <span class="keyword">public</span> <a class="code" href="classBM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a> { |
---|
48 | <a name="l00055"></a>00055 <span class="keywordtype">int</span> dimx, dimy, dimu; |
---|
49 | <a name="l00056"></a>00056 mat A, B, C, D; |
---|
50 | <a name="l00057"></a>00057 sq_T R, Q; |
---|
51 | <a name="l00058"></a>00058 |
---|
52 | <a name="l00059"></a>00059 <span class="comment">//cache</span> |
---|
53 | <a name="l00060"></a>00060 mat _K; |
---|
54 | <a name="l00061"></a>00061 vec _yp; |
---|
55 | <a name="l00062"></a>00062 sq_T _Ry,_iRy; |
---|
56 | <a name="l00063"></a>00063 <span class="keyword">public</span>: |
---|
57 | <a name="l00064"></a>00064 <span class="comment">//posterior </span> |
---|
58 | <a name="l00066"></a><a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed">00066</a> <span class="comment"></span> vec <a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed" title="Mean value of the posterior density.">mu</a>; |
---|
59 | <a name="l00068"></a><a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3">00068</a> sq_T <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a>; |
---|
60 | <a name="l00069"></a>00069 |
---|
61 | <a name="l00070"></a>00070 <span class="keyword">public</span>: |
---|
62 | <a name="l00072"></a>00072 <a class="code" href="classKalman.html#83118f4bd2ecbc70b03cfd573088ed6f" title="Full constructor.">Kalman</a> ( mat A0, mat B0, mat C0, mat D0, sq_T R0, sq_T Q0, sq_T P0, vec mu0 ); |
---|
63 | <a name="l00074"></a>00074 <span class="keywordtype">void</span> <a class="code" href="classKalman.html#e945d9205ca14acbd83ba80ea6f72b8e" title="Here dt = [yt;ut] of appropriate dimensions.">bayes</a>(<span class="keyword">const</span> vec &dt, <span class="keywordtype">bool</span> evalll=<span class="keyword">true</span>); |
---|
64 | <a name="l00075"></a>00075 |
---|
65 | <a name="l00076"></a>00076 <span class="keyword">friend</span> std::ostream &operator<< ( std::ostream &os, <span class="keyword">const</span> <a class="code" href="classKalmanFull.html" title="Basic Kalman filter with full matrices (education purpose only)! Will be deleted...">KalmanFull</a> &kf ); |
---|
66 | <a name="l00077"></a>00077 |
---|
67 | <a name="l00078"></a>00078 }; |
---|
68 | <a name="l00079"></a>00079 |
---|
69 | <a name="l00081"></a>00081 |
---|
70 | <a name="l00082"></a>00082 <span class="keyword">template</span><<span class="keyword">class</span> sq_T> |
---|
71 | <a name="l00083"></a><a class="code" href="classKalman.html#83118f4bd2ecbc70b03cfd573088ed6f">00083</a> <a class="code" href="classKalman.html#83118f4bd2ecbc70b03cfd573088ed6f" title="Full constructor.">Kalman<sq_T>::Kalman</a>( mat A0, mat B0, mat C0, mat D0, sq_T R0, sq_T Q0, sq_T P0, vec mu0 ) { |
---|
72 | <a name="l00084"></a>00084 dimx = A0.rows(); |
---|
73 | <a name="l00085"></a>00085 dimu = B0.cols(); |
---|
74 | <a name="l00086"></a>00086 dimy = C0.rows(); |
---|
75 | <a name="l00087"></a>00087 |
---|
76 | <a name="l00088"></a>00088 it_assert_debug( A0.cols()==dimx, <span class="stringliteral">"Kalman: A is not square"</span> ); |
---|
77 | <a name="l00089"></a>00089 it_assert_debug( B0.rows()==dimx, <span class="stringliteral">"Kalman: B is not compatible"</span> ); |
---|
78 | <a name="l00090"></a>00090 it_assert_debug( C0.cols()==dimx, <span class="stringliteral">"Kalman: C is not square"</span> ); |
---|
79 | <a name="l00091"></a>00091 it_assert_debug(( D0.rows()==dimy ) || ( D0.cols()==dimu ), <span class="stringliteral">"Kalman: D is not compatible"</span> ); |
---|
80 | <a name="l00092"></a>00092 it_assert_debug(( R0.cols()==dimy ) || ( R0.rows()==dimy ), <span class="stringliteral">"Kalman: R is not compatible"</span> ); |
---|
81 | <a name="l00093"></a>00093 it_assert_debug(( Q0.cols()==dimx ) || ( Q0.rows()==dimx ), <span class="stringliteral">"Kalman: Q is not compatible"</span> ); |
---|
82 | <a name="l00094"></a>00094 |
---|
83 | <a name="l00095"></a>00095 A = A0; |
---|
84 | <a name="l00096"></a>00096 B = B0; |
---|
85 | <a name="l00097"></a>00097 C = C0; |
---|
86 | <a name="l00098"></a>00098 D = D0; |
---|
87 | <a name="l00099"></a>00099 R = R0; |
---|
88 | <a name="l00100"></a>00100 Q = Q0; |
---|
89 | <a name="l00101"></a>00101 <a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed" title="Mean value of the posterior density.">mu</a> = mu0; |
---|
90 | <a name="l00102"></a>00102 <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a> = P0; |
---|
91 | <a name="l00103"></a>00103 |
---|
92 | <a name="l00104"></a>00104 <a class="code" href="classBM.html#5623fef6572a08c2b53b8c87b82dc979" title="Logarithm of marginalized data likelihood.">ll</a> = 0; |
---|
93 | <a name="l00105"></a>00105 <span class="comment">//Fixme should we assign cache??</span> |
---|
94 | <a name="l00106"></a>00106 _iRy = eye(dimy); <span class="comment">// needed in inv(_iRy)</span> |
---|
95 | <a name="l00107"></a>00107 } |
---|
96 | <a name="l00108"></a>00108 |
---|
97 | <a name="l00109"></a>00109 <span class="keyword">template</span><<span class="keyword">class</span> sq_T> |
---|
98 | <a name="l00110"></a><a class="code" href="classKalman.html#e945d9205ca14acbd83ba80ea6f72b8e">00110</a> <span class="keywordtype">void</span> <a class="code" href="classKalman.html#e945d9205ca14acbd83ba80ea6f72b8e" title="Here dt = [yt;ut] of appropriate dimensions.">Kalman<sq_T>::bayes</a>( <span class="keyword">const</span> vec &dt , <span class="keywordtype">bool</span> evalll) { |
---|
99 | <a name="l00111"></a>00111 it_assert_debug( dt.length()==( dimy+dimu ),<span class="stringliteral">"KalmanFull::bayes wrong size of dt"</span> ); |
---|
100 | <a name="l00112"></a>00112 |
---|
101 | <a name="l00113"></a>00113 vec u = dt.get( dimy,dimy+dimu-1 ); |
---|
102 | <a name="l00114"></a>00114 vec y = dt.get( 0,dimy-1 ); |
---|
103 | <a name="l00115"></a>00115 <span class="comment">//Time update</span> |
---|
104 | <a name="l00116"></a>00116 <a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed" title="Mean value of the posterior density.">mu</a> = A*<a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed" title="Mean value of the posterior density.">mu</a> + B*u; |
---|
105 | <a name="l00117"></a>00117 <span class="comment">//P = A*P*A.transpose() + Q; in sq_T</span> |
---|
106 | <a name="l00118"></a>00118 <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a>.mult_sym( A ); |
---|
107 | <a name="l00119"></a>00119 <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a>+=Q; |
---|
108 | <a name="l00120"></a>00120 |
---|
109 | <a name="l00121"></a>00121 <span class="comment">//Data update</span> |
---|
110 | <a name="l00122"></a>00122 <span class="comment">//_Ry = C*P*C.transpose() + R; in sq_T</span> |
---|
111 | <a name="l00123"></a>00123 _Ry.mult_sym( C, <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a>); |
---|
112 | <a name="l00124"></a>00124 _Ry+=R; |
---|
113 | <a name="l00125"></a>00125 |
---|
114 | <a name="l00126"></a>00126 mat Pfull = <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a>.to_mat(); |
---|
115 | <a name="l00127"></a>00127 |
---|
116 | <a name="l00128"></a>00128 _Ry.inv( _iRy ); <span class="comment">// result is in _iRy;</span> |
---|
117 | <a name="l00129"></a>00129 _K = Pfull*C.transpose()*(_iRy.to_mat()); |
---|
118 | <a name="l00130"></a>00130 <a class="code" href="classKalman.html#188cd5ac1c9e496b1a371eb7c57c97d3" title="Mean value of the posterior density.">P</a> -= _K*C*Pfull; <span class="comment">// P = P -KCP;</span> |
---|
119 | <a name="l00131"></a>00131 _yp = y-C*<a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed" title="Mean value of the posterior density.">mu</a>-D*u; <span class="comment">//y prediction</span> |
---|
120 | <a name="l00132"></a>00132 <a class="code" href="classKalman.html#3063a3f58a74cea672ae889971012eed" title="Mean value of the posterior density.">mu</a> += _K*( _yp ); |
---|
121 | <a name="l00133"></a>00133 |
---|
122 | <a name="l00134"></a>00134 <span class="keywordflow">if</span> (evalll==<span class="keyword">true</span>) { |
---|
123 | <a name="l00135"></a>00135 <a class="code" href="classBM.html#5623fef6572a08c2b53b8c87b82dc979" title="Logarithm of marginalized data likelihood.">ll</a>+= -0.5*(_Ry.cols()*0.79817986835811504957 \ |
---|
124 | <a name="l00136"></a>00136 +_Ry.logdet() +_iRy.qform(_yp)); |
---|
125 | <a name="l00137"></a>00137 } |
---|
126 | <a name="l00138"></a>00138 }; |
---|
127 | <a name="l00139"></a>00139 |
---|
128 | <a name="l00140"></a>00140 <span class="comment">//extern template class Kalman<ldmat>; </span> |
---|
129 | <a name="l00141"></a>00141 |
---|
130 | <a name="l00142"></a>00142 |
---|
131 | <a name="l00143"></a>00143 <span class="preprocessor">#endif // KF_H</span> |
---|
132 | <a name="l00144"></a>00144 <span class="preprocessor"></span> |
---|
133 | <a name="l00145"></a>00145 |
---|
134 | </pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Wed Jan 23 11:30:10 2008 for mixpp by |
---|
135 | <a href="http://www.doxygen.org/index.html"> |
---|
136 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address> |
---|
137 | </body> |
---|
138 | </html> |
---|