24 | | <a name="l00018"></a>00018 <span class="preprocessor">#include "../math/libDC.h"</span> |
25 | | <a name="l00019"></a>00019 |
26 | | <a name="l00020"></a>00020 <span class="keyword">namespace </span>bdm{ |
27 | | <a name="l00021"></a>00021 |
28 | | <a name="l00028"></a><a class="code" href="classbdm_1_1PF.html">00028</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> : <span class="keyword">public</span> <a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a> { |
29 | | <a name="l00029"></a>00029 <span class="keyword">protected</span>: |
30 | | <a name="l00031"></a><a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe">00031</a> <span class="keywordtype">int</span> <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>; |
31 | | <a name="l00033"></a><a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af">00033</a> <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>; |
32 | | <a name="l00035"></a><a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3">00035</a> vec &<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>; |
33 | | <a name="l00037"></a><a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1">00037</a> Array<vec> &<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a>; |
34 | | <a name="l00039"></a><a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34">00039</a> <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &<a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34" title="Parameter evolution model.">par</a>; |
35 | | <a name="l00041"></a><a class="code" href="classbdm_1_1PF.html#c58b8fa634272c3f48845a9020ba55aa">00041</a> <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &<a class="code" href="classbdm_1_1PF.html#c58b8fa634272c3f48845a9020ba55aa" title="Observation model.">obs</a>; |
36 | | <a name="l00042"></a>00042 <span class="keyword">public</span>: |
37 | | <a name="l00044"></a><a class="code" href="classbdm_1_1PF.html#df9e7dc54dbe1c78a4aa503c42f7f8c4">00044</a> <a class="code" href="classbdm_1_1PF.html#df9e7dc54dbe1c78a4aa503c42f7f8c4" title="Default constructor.">PF</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rv0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &par0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &obs0, <span class="keywordtype">int</span> n0 ) :<a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a> ( rv0 ), |
38 | | <a name="l00045"></a>00045 <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ( n0 ),<a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a> ( rv0,<a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ),<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>.<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>() ),<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>.<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a>() ), |
39 | | <a name="l00046"></a>00046 <a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34" title="Parameter evolution model.">par</a> ( par0 ), <a class="code" href="classbdm_1_1PF.html#c58b8fa634272c3f48845a9020ba55aa" title="Observation model.">obs</a> ( obs0 ) {}; |
40 | | <a name="l00047"></a>00047 |
41 | | <a name="l00049"></a>00049 <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1PF.html#6f1988db4c3f602d187a6c15ec89cb1e" title="Set posterior density by sampling from epdf0.">set_est</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> &epdf0 ); |
42 | | <a name="l00050"></a>00050 <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1PF.html#638946eea22d4964bf9350286bb4efd8" title="Incremental Bayes rule.">bayes</a> ( <span class="keyword">const</span> vec &dt ); |
43 | | <a name="l00052"></a><a class="code" href="classbdm_1_1PF.html#78a9f6809827be1d9bfe215d03b1c6ed">00052</a> vec* <a class="code" href="classbdm_1_1PF.html#78a9f6809827be1d9bfe215d03b1c6ed" title="access function">__w</a>(){<span class="keywordflow">return</span> &<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>;} |
44 | | <a name="l00053"></a>00053 }; |
45 | | <a name="l00054"></a>00054 |
46 | | <a name="l00061"></a>00061 <span class="keyword">template</span><<span class="keyword">class</span> BM_T> |
47 | | <a name="l00062"></a>00062 |
48 | | <a name="l00063"></a><a class="code" href="classbdm_1_1MPF.html">00063</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1MPF.html" title="Marginalized Particle filter.">MPF</a> : <span class="keyword">public</span> <a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> { |
49 | | <a name="l00064"></a>00064 BM_T* Bms[10000]; |
50 | | <a name="l00065"></a>00065 |
51 | | <a name="l00067"></a>00067 |
52 | | <a name="l00068"></a>00068 <span class="keyword">class </span>mpfepdf : <span class="keyword">public</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> { |
53 | | <a name="l00069"></a>00069 <span class="keyword">protected</span>: |
54 | | <a name="l00070"></a>00070 <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> &E; |
55 | | <a name="l00071"></a>00071 vec &<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>; |
56 | | <a name="l00072"></a>00072 Array<const epdf*> Coms; |
57 | | <a name="l00073"></a>00073 <span class="keyword">public</span>: |
58 | | <a name="l00074"></a>00074 mpfepdf ( <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> &E0, <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rvc ) : |
59 | | <a name="l00075"></a>00075 <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> ( <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a>( ) ), E ( E0 ), <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( E._w() ), |
60 | | <a name="l00076"></a>00076 Coms ( <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length() ) { |
61 | | <a name="l00077"></a>00077 <a class="code" href="classbdm_1_1BM.html#18d6db4af8ee42077741d9e3618153ca" title="Random variable of the posterior.">rv</a>.<a class="code" href="classbdm_1_1RV.html#87841b5ee43997b79789c0c22047e224" title="Add (concat) another variable to the current one,.">add</a> ( E._rv() ); |
62 | | <a name="l00078"></a>00078 <a class="code" href="classbdm_1_1BM.html#18d6db4af8ee42077741d9e3618153ca" title="Random variable of the posterior.">rv</a>.<a class="code" href="classbdm_1_1RV.html#87841b5ee43997b79789c0c22047e224" title="Add (concat) another variable to the current one,.">add</a> ( rvc ); |
63 | | <a name="l00079"></a>00079 }; |
64 | | <a name="l00080"></a>00080 |
65 | | <a name="l00081"></a>00081 <span class="keywordtype">void</span> set_elements ( <span class="keywordtype">int</span> &i, <span class="keywordtype">double</span> wi, <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>* ep ) |
66 | | <a name="l00082"></a>00082 {<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ) =wi; Coms ( i ) =ep;}; |
67 | | <a name="l00083"></a>00083 |
68 | | <a name="l00084"></a>00084 vec mean()<span class="keyword"> const </span>{ |
69 | | <a name="l00085"></a>00085 <span class="comment">// ugly</span> |
70 | | <a name="l00086"></a>00086 vec pom=zeros ( ( Coms ( 0 )-><a class="code" href="classbdm_1_1BM.html#40a3c891996391e3135518053a917793" title="access function">_rv</a>() ).count() ); |
71 | | <a name="l00087"></a>00087 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) {pom += Coms ( i )->mean() * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );} |
72 | | <a name="l00088"></a>00088 <span class="keywordflow">return</span> concat ( E.mean(),pom ); |
73 | | <a name="l00089"></a>00089 } |
74 | | <a name="l00090"></a>00090 vec variance()<span class="keyword"> const </span>{ |
75 | | <a name="l00091"></a>00091 <span class="comment">// ugly</span> |
76 | | <a name="l00092"></a>00092 vec pom=zeros ( ( Coms ( 0 )-><a class="code" href="classbdm_1_1BM.html#40a3c891996391e3135518053a917793" title="access function">_rv</a>() ).count() ); |
77 | | <a name="l00093"></a>00093 vec pom2=zeros ( ( Coms ( 0 )-><a class="code" href="classbdm_1_1BM.html#40a3c891996391e3135518053a917793" title="access function">_rv</a>() ).count() ); |
78 | | <a name="l00094"></a>00094 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) { |
79 | | <a name="l00095"></a>00095 pom += Coms ( i )->mean() * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ); |
80 | | <a name="l00096"></a>00096 pom2 += (Coms ( i )->variance() + pow(Coms(i)->mean(),2)) * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );} |
81 | | <a name="l00097"></a>00097 <span class="keywordflow">return</span> concat ( E.variance(),pom2-pow(pom,2) ); |
82 | | <a name="l00098"></a>00098 } |
83 | | <a name="l00099"></a>00099 |
84 | | <a name="l00100"></a>00100 vec sample()<span class="keyword"> const </span>{it_error ( <span class="stringliteral">"Not implemented"</span> );<span class="keywordflow">return</span> 0;} |
85 | | <a name="l00101"></a>00101 |
86 | | <a name="l00102"></a>00102 <span class="keywordtype">double</span> evallog ( <span class="keyword">const</span> vec &val )<span class="keyword"> const </span>{it_error ( <span class="stringliteral">"not implemented"</span> ); <span class="keywordflow">return</span> 0.0;} |
87 | | <a name="l00103"></a>00103 }; |
88 | | <a name="l00104"></a>00104 |
89 | | <a name="l00106"></a>00106 mpfepdf jest; |
90 | | <a name="l00107"></a>00107 |
91 | | <a name="l00108"></a>00108 <span class="keyword">public</span>: |
92 | | <a name="l00110"></a><a class="code" href="classbdm_1_1MPF.html#e2a00c2399599c3613ab632fc36a1f79">00110</a> <a class="code" href="classbdm_1_1MPF.html#e2a00c2399599c3613ab632fc36a1f79" title="Default constructor.">MPF</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rvlin, <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rvpf, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &par0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &obs0, <span class="keywordtype">int</span> <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>, <span class="keyword">const</span> BM_T &BMcond0 ) : <a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> ( rvpf ,par0,obs0,n ),jest ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>,rvlin ) { |
93 | | <a name="l00111"></a>00111 <span class="comment">//</span> |
94 | | <a name="l00112"></a>00112 <span class="comment">//TODO test if rv and BMcond.rv are compatible.</span> |
95 | | <a name="l00113"></a>00113 <a class="code" href="classbdm_1_1BM.html#18d6db4af8ee42077741d9e3618153ca" title="Random variable of the posterior.">rv</a>.<a class="code" href="classbdm_1_1RV.html#87841b5ee43997b79789c0c22047e224" title="Add (concat) another variable to the current one,.">add</a> ( rvlin ); |
96 | | <a name="l00114"></a>00114 <span class="comment">//</span> |
97 | | <a name="l00115"></a>00115 |
98 | | <a name="l00116"></a>00116 <span class="keywordflow">if</span> ( n>10000 ) {it_error ( <span class="stringliteral">"increase 10000 here!"</span> );} |
99 | | <a name="l00117"></a>00117 |
100 | | <a name="l00118"></a>00118 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0;i<n;i++ ) { |
101 | | <a name="l00119"></a>00119 Bms[i] = <span class="keyword">new</span> BM_T ( BMcond0 ); <span class="comment">//copy constructor</span> |
102 | | <a name="l00120"></a>00120 <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>& pom=Bms[i]->_epdf(); |
103 | | <a name="l00121"></a>00121 jest.set_elements ( i,1.0/n,&pom ); |
104 | | <a name="l00122"></a>00122 } |
105 | | <a name="l00123"></a>00123 }; |
106 | | <a name="l00124"></a>00124 |
107 | | <a name="l00125"></a>00125 ~<a class="code" href="classbdm_1_1MPF.html" title="Marginalized Particle filter.">MPF</a>() { |
108 | | <a name="l00126"></a>00126 } |
109 | | <a name="l00127"></a>00127 |
110 | | <a name="l00128"></a>00128 <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html#286d040770d08bd7ff416cea617b1b14" title="Incremental Bayes rule.">bayes</a> ( <span class="keyword">const</span> vec &dt ); |
111 | | <a name="l00129"></a><a class="code" href="classbdm_1_1MPF.html#2da4cbf7645da52ad5630ada411e3f9f">00129</a> <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>& <a class="code" href="classbdm_1_1MPF.html#2da4cbf7645da52ad5630ada411e3f9f" title="Returns a reference to the epdf representing posterior density on parameters.">_epdf</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> jest;} |
112 | | <a name="l00130"></a><a class="code" href="classbdm_1_1MPF.html#1401874aa88a7a3f2593070646779af2">00130</a> <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>* <a class="code" href="classbdm_1_1MPF.html#1401874aa88a7a3f2593070646779af2" title="Returns a pointer to the epdf representing posterior density on parameters. Use with...">_e</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> &jest;} <span class="comment">//Fixme: is it useful?</span> |
113 | | <a name="l00132"></a><a class="code" href="classbdm_1_1MPF.html#dcecdaf2acbbee51acf3018a70989a7e">00132</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html#dcecdaf2acbbee51acf3018a70989a7e" title="Set postrior of rvc to samples from epdf0. Statistics of Bms are not re-computed!...">set_est</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>& epdf0 ) { |
114 | | <a name="l00133"></a>00133 <a class="code" href="classbdm_1_1PF.html#6f1988db4c3f602d187a6c15ec89cb1e" title="Set posterior density by sampling from epdf0.">PF::set_est</a> ( epdf0 ); <span class="comment">// sample params in condition</span> |
115 | | <a name="l00134"></a>00134 <span class="comment">// copy conditions to BMs</span> |
116 | | <a name="l00135"></a>00135 |
117 | | <a name="l00136"></a>00136 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0;i<<a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>;i++ ) {Bms[i]->condition ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) );} |
118 | | <a name="l00137"></a>00137 } |
119 | | <a name="l00138"></a>00138 |
120 | | <a name="l00140"></a><a class="code" href="classbdm_1_1MPF.html#82b5a34d9ed0e78452f98d2ecbf1e93c">00140</a> <a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a>* <a class="code" href="classbdm_1_1MPF.html#82b5a34d9ed0e78452f98d2ecbf1e93c" title="Access function.">_BM</a>(<span class="keywordtype">int</span> i){<span class="keywordflow">return</span> Bms[i];} |
121 | | <a name="l00141"></a>00141 }; |
122 | | <a name="l00142"></a>00142 |
123 | | <a name="l00143"></a>00143 <span class="keyword">template</span><<span class="keyword">class</span> BM_T> |
124 | | <a name="l00144"></a><a class="code" href="classbdm_1_1MPF.html#286d040770d08bd7ff416cea617b1b14">00144</a> <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html" title="Marginalized Particle filter.">MPF<BM_T>::bayes</a> ( <span class="keyword">const</span> vec &dt ) { |
125 | | <a name="l00145"></a>00145 <span class="keywordtype">int</span> i; |
126 | | <a name="l00146"></a>00146 vec lls ( <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ); |
127 | | <a name="l00147"></a>00147 vec llsP ( <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ); |
128 | | <a name="l00148"></a>00148 ivec ind; |
129 | | <a name="l00149"></a>00149 <span class="keywordtype">double</span> mlls=-std::numeric_limits<double>::infinity(); |
130 | | <a name="l00150"></a>00150 |
131 | | <a name="l00151"></a>00151 <span class="preprocessor"> #pragma omp parallel for</span> |
132 | | <a name="l00152"></a>00152 <span class="preprocessor"></span> <span class="keywordflow">for</span> ( i=0;i<<a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>;i++ ) { |
133 | | <a name="l00153"></a>00153 <span class="comment">//generate new samples from paramater evolution model;</span> |
134 | | <a name="l00154"></a>00154 <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) = <a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34" title="Parameter evolution model.">par</a>.<a class="code" href="classbdm_1_1mpdf.html#e4848a428d8ef0549c6e4a9ed386d9f2" title="Returns a sample from the density conditioned on cond, .">samplecond</a> ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ), llsP ( i ) ); |
135 | | <a name="l00155"></a>00155 Bms[i]->condition ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) ); |
136 | | <a name="l00156"></a>00156 Bms[i]->bayes ( dt ); |
137 | | <a name="l00157"></a>00157 lls ( i ) = Bms[i]->_ll(); <span class="comment">// lls above is also in proposal her must be lls(i) =, not +=!!</span> |
138 | | <a name="l00158"></a>00158 <span class="keywordflow">if</span> ( lls ( i ) >mlls ) mlls=lls ( i ); <span class="comment">//find maximum likelihood (for numerical stability)</span> |
139 | | <a name="l00159"></a>00159 } |
140 | | <a name="l00160"></a>00160 |
141 | | <a name="l00161"></a>00161 <span class="keywordtype">double</span> sum_w=0.0; |
142 | | <a name="l00162"></a>00162 <span class="comment">// compute weights</span> |
143 | | <a name="l00163"></a>00163 <span class="preprocessor"> #pragma omp parallel for</span> |
144 | | <a name="l00164"></a>00164 <span class="preprocessor"></span> <span class="keywordflow">for</span> ( i=0;i<n;i++ ) { |
145 | | <a name="l00165"></a>00165 <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ) *= exp ( lls ( i ) - mlls ); <span class="comment">// multiply w by likelihood</span> |
146 | | <a name="l00166"></a>00166 sum_w+=<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>(i); |
147 | | <a name="l00167"></a>00167 } |
148 | | <a name="l00168"></a>00168 |
149 | | <a name="l00169"></a>00169 <span class="keywordflow">if</span> ( sum_w >0.0 ) { |
150 | | <a name="l00170"></a>00170 <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> /=sum_w; <span class="comment">//?</span> |
151 | | <a name="l00171"></a>00171 } <span class="keywordflow">else</span> { |
152 | | <a name="l00172"></a>00172 cout<<<span class="stringliteral">"sum(w)==0"</span><<endl; |
153 | | <a name="l00173"></a>00173 } |
| 25 | <a name="l00018"></a>00018 |
| 26 | <a name="l00019"></a>00019 <span class="keyword">namespace </span>bdm{ |
| 27 | <a name="l00020"></a>00020 |
| 28 | <a name="l00027"></a><a class="code" href="classbdm_1_1PF.html">00027</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> : <span class="keyword">public</span> <a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a> { |
| 29 | <a name="l00028"></a>00028 <span class="keyword">protected</span>: |
| 30 | <a name="l00030"></a><a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe">00030</a> <span class="keywordtype">int</span> <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>; |
| 31 | <a name="l00032"></a><a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af">00032</a> <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>; |
| 32 | <a name="l00034"></a><a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3">00034</a> vec &<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>; |
| 33 | <a name="l00036"></a><a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1">00036</a> Array<vec> &<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a>; |
| 34 | <a name="l00038"></a><a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34">00038</a> <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &<a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34" title="Parameter evolution model.">par</a>; |
| 35 | <a name="l00040"></a><a class="code" href="classbdm_1_1PF.html#c58b8fa634272c3f48845a9020ba55aa">00040</a> <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &<a class="code" href="classbdm_1_1PF.html#c58b8fa634272c3f48845a9020ba55aa" title="Observation model.">obs</a>; |
| 36 | <a name="l00041"></a>00041 <span class="keyword">public</span>: |
| 37 | <a name="l00043"></a><a class="code" href="classbdm_1_1PF.html#df9e7dc54dbe1c78a4aa503c42f7f8c4">00043</a> <a class="code" href="classbdm_1_1PF.html#df9e7dc54dbe1c78a4aa503c42f7f8c4" title="Default constructor.">PF</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rv0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &par0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &obs0, <span class="keywordtype">int</span> n0 ) :<a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a> ( rv0 ), |
| 38 | <a name="l00044"></a>00044 <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ( n0 ),<a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a> ( rv0,<a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ),<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>.<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>() ),<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>.<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a>() ), |
| 39 | <a name="l00045"></a>00045 <a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34" title="Parameter evolution model.">par</a> ( par0 ), <a class="code" href="classbdm_1_1PF.html#c58b8fa634272c3f48845a9020ba55aa" title="Observation model.">obs</a> ( obs0 ) {}; |
| 40 | <a name="l00046"></a>00046 |
| 41 | <a name="l00048"></a>00048 <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1PF.html#6f1988db4c3f602d187a6c15ec89cb1e" title="Set posterior density by sampling from epdf0.">set_est</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> &epdf0 ); |
| 42 | <a name="l00049"></a>00049 <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1PF.html#638946eea22d4964bf9350286bb4efd8" title="Incremental Bayes rule.">bayes</a> ( <span class="keyword">const</span> vec &dt ); |
| 43 | <a name="l00051"></a><a class="code" href="classbdm_1_1PF.html#78a9f6809827be1d9bfe215d03b1c6ed">00051</a> vec* <a class="code" href="classbdm_1_1PF.html#78a9f6809827be1d9bfe215d03b1c6ed" title="access function">__w</a>(){<span class="keywordflow">return</span> &<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>;} |
| 44 | <a name="l00052"></a>00052 }; |
| 45 | <a name="l00053"></a>00053 |
| 46 | <a name="l00060"></a>00060 <span class="keyword">template</span><<span class="keyword">class</span> BM_T> |
| 47 | <a name="l00061"></a>00061 |
| 48 | <a name="l00062"></a><a class="code" href="classbdm_1_1MPF.html">00062</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1MPF.html" title="Marginalized Particle filter.">MPF</a> : <span class="keyword">public</span> <a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> { |
| 49 | <a name="l00063"></a>00063 BM_T* Bms[10000]; |
| 50 | <a name="l00064"></a>00064 |
| 51 | <a name="l00066"></a>00066 |
| 52 | <a name="l00067"></a>00067 <span class="keyword">class </span>mpfepdf : <span class="keyword">public</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> { |
| 53 | <a name="l00068"></a>00068 <span class="keyword">protected</span>: |
| 54 | <a name="l00069"></a>00069 <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> &E; |
| 55 | <a name="l00070"></a>00070 vec &<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>; |
| 56 | <a name="l00071"></a>00071 Array<const epdf*> Coms; |
| 57 | <a name="l00072"></a>00072 <span class="keyword">public</span>: |
| 58 | <a name="l00073"></a>00073 mpfepdf ( <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> &E0, <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rvc ) : |
| 59 | <a name="l00074"></a>00074 <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> ( <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a>( ) ), E ( E0 ), <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( E._w() ), |
| 60 | <a name="l00075"></a>00075 Coms ( <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length() ) { |
| 61 | <a name="l00076"></a>00076 <a class="code" href="classbdm_1_1BM.html#18d6db4af8ee42077741d9e3618153ca" title="Random variable of the posterior.">rv</a>.<a class="code" href="classbdm_1_1RV.html#87841b5ee43997b79789c0c22047e224" title="Add (concat) another variable to the current one,.">add</a> ( E._rv() ); |
| 62 | <a name="l00077"></a>00077 <a class="code" href="classbdm_1_1BM.html#18d6db4af8ee42077741d9e3618153ca" title="Random variable of the posterior.">rv</a>.<a class="code" href="classbdm_1_1RV.html#87841b5ee43997b79789c0c22047e224" title="Add (concat) another variable to the current one,.">add</a> ( rvc ); |
| 63 | <a name="l00078"></a>00078 }; |
| 64 | <a name="l00079"></a>00079 |
| 65 | <a name="l00080"></a>00080 <span class="keywordtype">void</span> set_elements ( <span class="keywordtype">int</span> &i, <span class="keywordtype">double</span> wi, <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>* ep ) |
| 66 | <a name="l00081"></a>00081 {<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ) =wi; Coms ( i ) =ep;}; |
| 67 | <a name="l00082"></a>00082 |
| 68 | <a name="l00083"></a>00083 vec mean()<span class="keyword"> const </span>{ |
| 69 | <a name="l00084"></a>00084 <span class="comment">// ugly</span> |
| 70 | <a name="l00085"></a>00085 vec pom=zeros ( ( Coms ( 0 )-><a class="code" href="classbdm_1_1BM.html#40a3c891996391e3135518053a917793" title="access function">_rv</a>() ).count() ); |
| 71 | <a name="l00086"></a>00086 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) {pom += Coms ( i )->mean() * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );} |
| 72 | <a name="l00087"></a>00087 <span class="keywordflow">return</span> <a class="code" href="namespacebdm.html#b9016687c0e874ca5cdcf75ae28811aa" title="Concat two random variables.">concat</a> ( E.mean(),pom ); |
| 73 | <a name="l00088"></a>00088 } |
| 74 | <a name="l00089"></a>00089 vec variance()<span class="keyword"> const </span>{ |
| 75 | <a name="l00090"></a>00090 <span class="comment">// ugly</span> |
| 76 | <a name="l00091"></a>00091 vec pom=zeros ( ( Coms ( 0 )-><a class="code" href="classbdm_1_1BM.html#40a3c891996391e3135518053a917793" title="access function">_rv</a>() ).count() ); |
| 77 | <a name="l00092"></a>00092 vec pom2=zeros ( ( Coms ( 0 )-><a class="code" href="classbdm_1_1BM.html#40a3c891996391e3135518053a917793" title="access function">_rv</a>() ).count() ); |
| 78 | <a name="l00093"></a>00093 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) { |
| 79 | <a name="l00094"></a>00094 pom += Coms ( i )->mean() * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ); |
| 80 | <a name="l00095"></a>00095 pom2 += (Coms ( i )->variance() + pow(Coms(i)->mean(),2)) * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );} |
| 81 | <a name="l00096"></a>00096 <span class="keywordflow">return</span> <a class="code" href="namespacebdm.html#b9016687c0e874ca5cdcf75ae28811aa" title="Concat two random variables.">concat</a> ( E.variance(),pom2-pow(pom,2) ); |
| 82 | <a name="l00097"></a>00097 } |
| 83 | <a name="l00098"></a>00098 |
| 84 | <a name="l00099"></a>00099 vec sample()<span class="keyword"> const </span>{it_error ( <span class="stringliteral">"Not implemented"</span> );<span class="keywordflow">return</span> 0;} |
| 85 | <a name="l00100"></a>00100 |
| 86 | <a name="l00101"></a>00101 <span class="keywordtype">double</span> evallog ( <span class="keyword">const</span> vec &val )<span class="keyword"> const </span>{it_error ( <span class="stringliteral">"not implemented"</span> ); <span class="keywordflow">return</span> 0.0;} |
| 87 | <a name="l00102"></a>00102 }; |
| 88 | <a name="l00103"></a>00103 |
| 89 | <a name="l00105"></a>00105 mpfepdf jest; |
| 90 | <a name="l00106"></a>00106 |
| 91 | <a name="l00107"></a>00107 <span class="keyword">public</span>: |
| 92 | <a name="l00109"></a><a class="code" href="classbdm_1_1MPF.html#e2a00c2399599c3613ab632fc36a1f79">00109</a> <a class="code" href="classbdm_1_1MPF.html#e2a00c2399599c3613ab632fc36a1f79" title="Default constructor.">MPF</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rvlin, <span class="keyword">const</span> <a class="code" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">RV</a> &rvpf, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &par0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling some dependencies.">mpdf</a> &obs0, <span class="keywordtype">int</span> <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>, <span class="keyword">const</span> BM_T &BMcond0 ) : <a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> ( rvpf ,par0,obs0,n ),jest ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>,rvlin ) { |
| 93 | <a name="l00110"></a>00110 <span class="comment">//</span> |
| 94 | <a name="l00111"></a>00111 <span class="comment">//TODO test if rv and BMcond.rv are compatible.</span> |
| 95 | <a name="l00112"></a>00112 <a class="code" href="classbdm_1_1BM.html#18d6db4af8ee42077741d9e3618153ca" title="Random variable of the posterior.">rv</a>.<a class="code" href="classbdm_1_1RV.html#87841b5ee43997b79789c0c22047e224" title="Add (concat) another variable to the current one,.">add</a> ( rvlin ); |
| 96 | <a name="l00113"></a>00113 <span class="comment">//</span> |
| 97 | <a name="l00114"></a>00114 |
| 98 | <a name="l00115"></a>00115 <span class="keywordflow">if</span> ( n>10000 ) {it_error ( <span class="stringliteral">"increase 10000 here!"</span> );} |
| 99 | <a name="l00116"></a>00116 |
| 100 | <a name="l00117"></a>00117 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0;i<n;i++ ) { |
| 101 | <a name="l00118"></a>00118 Bms[i] = <span class="keyword">new</span> BM_T ( BMcond0 ); <span class="comment">//copy constructor</span> |
| 102 | <a name="l00119"></a>00119 <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>& pom=Bms[i]->_epdf(); |
| 103 | <a name="l00120"></a>00120 jest.set_elements ( i,1.0/n,&pom ); |
| 104 | <a name="l00121"></a>00121 } |
| 105 | <a name="l00122"></a>00122 }; |
| 106 | <a name="l00123"></a>00123 |
| 107 | <a name="l00124"></a>00124 ~<a class="code" href="classbdm_1_1MPF.html" title="Marginalized Particle filter.">MPF</a>() { |
| 108 | <a name="l00125"></a>00125 } |
| 109 | <a name="l00126"></a>00126 |
| 110 | <a name="l00127"></a>00127 <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html#286d040770d08bd7ff416cea617b1b14" title="Incremental Bayes rule.">bayes</a> ( <span class="keyword">const</span> vec &dt ); |
| 111 | <a name="l00128"></a><a class="code" href="classbdm_1_1MPF.html#2da4cbf7645da52ad5630ada411e3f9f">00128</a> <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>& <a class="code" href="classbdm_1_1MPF.html#2da4cbf7645da52ad5630ada411e3f9f" title="Returns a reference to the epdf representing posterior density on parameters.">_epdf</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> jest;} |
| 112 | <a name="l00129"></a><a class="code" href="classbdm_1_1MPF.html#1401874aa88a7a3f2593070646779af2">00129</a> <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>* <a class="code" href="classbdm_1_1MPF.html#1401874aa88a7a3f2593070646779af2" title="Returns a pointer to the epdf representing posterior density on parameters. Use with...">_e</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> &jest;} <span class="comment">//Fixme: is it useful?</span> |
| 113 | <a name="l00131"></a><a class="code" href="classbdm_1_1MPF.html#dcecdaf2acbbee51acf3018a70989a7e">00131</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html#dcecdaf2acbbee51acf3018a70989a7e" title="Set postrior of rvc to samples from epdf0. Statistics of Bms are not re-computed!...">set_est</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a>& epdf0 ) { |
| 114 | <a name="l00132"></a>00132 <a class="code" href="classbdm_1_1PF.html#6f1988db4c3f602d187a6c15ec89cb1e" title="Set posterior density by sampling from epdf0.">PF::set_est</a> ( epdf0 ); <span class="comment">// sample params in condition</span> |
| 115 | <a name="l00133"></a>00133 <span class="comment">// copy conditions to BMs</span> |
| 116 | <a name="l00134"></a>00134 |
| 117 | <a name="l00135"></a>00135 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0;i<<a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>;i++ ) {Bms[i]->condition ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) );} |
| 118 | <a name="l00136"></a>00136 } |
| 119 | <a name="l00137"></a>00137 |
| 120 | <a name="l00139"></a><a class="code" href="classbdm_1_1MPF.html#82b5a34d9ed0e78452f98d2ecbf1e93c">00139</a> <a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.">BM</a>* <a class="code" href="classbdm_1_1MPF.html#82b5a34d9ed0e78452f98d2ecbf1e93c" title="Access function.">_BM</a>(<span class="keywordtype">int</span> i){<span class="keywordflow">return</span> Bms[i];} |
| 121 | <a name="l00140"></a>00140 }; |
| 122 | <a name="l00141"></a>00141 |
| 123 | <a name="l00142"></a>00142 <span class="keyword">template</span><<span class="keyword">class</span> BM_T> |
| 124 | <a name="l00143"></a><a class="code" href="classbdm_1_1MPF.html#286d040770d08bd7ff416cea617b1b14">00143</a> <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html" title="Marginalized Particle filter.">MPF<BM_T>::bayes</a> ( <span class="keyword">const</span> vec &dt ) { |
| 125 | <a name="l00144"></a>00144 <span class="keywordtype">int</span> i; |
| 126 | <a name="l00145"></a>00145 vec lls ( <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ); |
| 127 | <a name="l00146"></a>00146 vec llsP ( <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ); |
| 128 | <a name="l00147"></a>00147 ivec ind; |
| 129 | <a name="l00148"></a>00148 <span class="keywordtype">double</span> mlls=-std::numeric_limits<double>::infinity(); |
| 130 | <a name="l00149"></a>00149 |
| 131 | <a name="l00150"></a>00150 <span class="preprocessor"> #pragma omp parallel for</span> |
| 132 | <a name="l00151"></a>00151 <span class="preprocessor"></span> <span class="keywordflow">for</span> ( i=0;i<<a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>;i++ ) { |
| 133 | <a name="l00152"></a>00152 <span class="comment">//generate new samples from paramater evolution model;</span> |
| 134 | <a name="l00153"></a>00153 <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) = <a class="code" href="classbdm_1_1PF.html#cf3a1b2a407012e47ac878e3aa2fbf34" title="Parameter evolution model.">par</a>.<a class="code" href="classbdm_1_1mpdf.html#e4848a428d8ef0549c6e4a9ed386d9f2" title="Returns a sample from the density conditioned on cond, .">samplecond</a> ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ), llsP ( i ) ); |
| 135 | <a name="l00154"></a>00154 Bms[i]->condition ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) ); |
| 136 | <a name="l00155"></a>00155 Bms[i]->bayes ( dt ); |
| 137 | <a name="l00156"></a>00156 lls ( i ) = Bms[i]->_ll(); <span class="comment">// lls above is also in proposal her must be lls(i) =, not +=!!</span> |
| 138 | <a name="l00157"></a>00157 <span class="keywordflow">if</span> ( lls ( i ) >mlls ) mlls=lls ( i ); <span class="comment">//find maximum likelihood (for numerical stability)</span> |
| 139 | <a name="l00158"></a>00158 } |
| 140 | <a name="l00159"></a>00159 |
| 141 | <a name="l00160"></a>00160 <span class="keywordtype">double</span> sum_w=0.0; |
| 142 | <a name="l00161"></a>00161 <span class="comment">// compute weights</span> |
| 143 | <a name="l00162"></a>00162 <span class="preprocessor"> #pragma omp parallel for</span> |
| 144 | <a name="l00163"></a>00163 <span class="preprocessor"></span> <span class="keywordflow">for</span> ( i=0;i<n;i++ ) { |
| 145 | <a name="l00164"></a>00164 <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ) *= exp ( lls ( i ) - mlls ); <span class="comment">// multiply w by likelihood</span> |
| 146 | <a name="l00165"></a>00165 sum_w+=<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>(i); |
| 147 | <a name="l00166"></a>00166 } |
| 148 | <a name="l00167"></a>00167 |
| 149 | <a name="l00168"></a>00168 <span class="keywordflow">if</span> ( sum_w >0.0 ) { |
| 150 | <a name="l00169"></a>00169 <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> /=sum_w; <span class="comment">//?</span> |
| 151 | <a name="l00170"></a>00170 } <span class="keywordflow">else</span> { |
| 152 | <a name="l00171"></a>00171 cout<<<span class="stringliteral">"sum(w)==0"</span><<endl; |
| 153 | <a name="l00172"></a>00172 } |
| 154 | <a name="l00173"></a>00173 |