root/library/doc/html/particles_8h-source.html @ 590

Revision 590, 34.9 kB (checked in by smidl, 15 years ago)

doc

Line 
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: particles.h Source File</title>
4<link href="tabs.css" rel="stylesheet" type="text/css">
5<link href="doxygen.css" rel="stylesheet" type="text/css">
6</head><body>
7<!-- Generated by Doxygen 1.5.8 -->
8<script type="text/javascript">
9<!--
10function changeDisplayState (e){
11  var num=this.id.replace(/[^[0-9]/g,'');
12  var button=this.firstChild;
13  var sectionDiv=document.getElementById('dynsection'+num);
14  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
15    sectionDiv.style.display='block';
16    button.src='open.gif';
17  }else{
18    sectionDiv.style.display='none';
19    button.src='closed.gif';
20  }
21}
22function initDynSections(){
23  var divs=document.getElementsByTagName('div');
24  var sectionCounter=1;
25  for(var i=0;i<divs.length-1;i++){
26    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
27      var header=divs[i];
28      var section=divs[i+1];
29      var button=header.firstChild;
30      if (button!='IMG'){
31        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
32        button=document.createElement('img');
33        divs[i].insertBefore(button,divs[i].firstChild);
34      }
35      header.style.cursor='pointer';
36      header.onclick=changeDisplayState;
37      header.id='dynheader'+sectionCounter;
38      button.src='closed.gif';
39      section.id='dynsection'+sectionCounter;
40      section.style.display='none';
41      section.style.marginLeft='14px';
42      sectionCounter++;
43    }
44  }
45}
46window.onload = initDynSections;
47-->
48</script>
49<div class="navigation" id="top">
50  <div class="tabs">
51    <ul>
52      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
53      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
54      <li><a href="annotated.html"><span>Classes</span></a></li>
55      <li class="current"><a href="files.html"><span>Files</span></a></li>
56    </ul>
57  </div>
58  <div class="tabs">
59    <ul>
60      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
61      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
62    </ul>
63  </div>
64<h1>particles.h</h1><a href="particles_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001
65<a name="l00013"></a>00013 <span class="preprocessor">#ifndef PARTICLES_H</span>
66<a name="l00014"></a>00014 <span class="preprocessor"></span><span class="preprocessor">#define PARTICLES_H</span>
67<a name="l00015"></a>00015 <span class="preprocessor"></span>
68<a name="l00016"></a>00016
69<a name="l00017"></a>00017 <span class="preprocessor">#include "../stat/exp_family.h"</span>
70<a name="l00018"></a>00018
71<a name="l00019"></a>00019 <span class="keyword">namespace </span>bdm {
72<a name="l00020"></a>00020
73<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 a system, i.e. all uncertainty is modeled by probabilities.">BM</a> {
74<a name="l00028"></a>00028 <span class="keyword">protected</span>:
75<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>;
76<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>;
77<a name="l00034"></a><a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3">00034</a>         vec &amp;<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>;
78<a name="l00036"></a><a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1">00036</a>         Array&lt;vec&gt; &amp;<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a>;
79<a name="l00038"></a><a class="code" href="classbdm_1_1PF.html#521e9621d3b5e1274275f323691afdaf">00038</a>         <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">mpdf</a> *<a class="code" href="classbdm_1_1PF.html#521e9621d3b5e1274275f323691afdaf" title="Parameter evolution model.">par</a>;
80<a name="l00040"></a><a class="code" href="classbdm_1_1PF.html#d6e7a62fba1e0a0d73c9b87f4fb683ec">00040</a>         <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">mpdf</a> *<a class="code" href="classbdm_1_1PF.html#d6e7a62fba1e0a0d73c9b87f4fb683ec" title="Observation model.">obs</a>;
81<a name="l00041"></a>00041
82<a name="l00043"></a><a class="code" href="classbdm_1_1PF.html#9932c7c5865954ef9a438afcbe944e52">00043</a>         RESAMPLING_METHOD <a class="code" href="classbdm_1_1PF.html#9932c7c5865954ef9a438afcbe944e52" title="which resampling method will be used">resmethod</a>;
83<a name="l00044"></a>00044
84<a name="l00047"></a>00047
85<a name="l00049"></a><a class="code" href="classbdm_1_1PF.html#98ef9ff80c394fafd28680b7a3f831b1">00049</a>         <span class="keywordtype">bool</span> <a class="code" href="classbdm_1_1PF.html#98ef9ff80c394fafd28680b7a3f831b1" title="Log all samples.">opt_L_smp</a>;
86<a name="l00051"></a><a class="code" href="classbdm_1_1PF.html#5a49463a88ee80771a464861df845ff6">00051</a>         <span class="keywordtype">bool</span> <a class="code" href="classbdm_1_1PF.html#5a49463a88ee80771a464861df845ff6" title="Log all samples.">opt_L_wei</a>;
87<a name="l00053"></a>00053
88<a name="l00054"></a>00054 <span class="keyword">public</span>:
89<a name="l00057"></a>00057         <a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</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#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>() ), <a class="code" href="classbdm_1_1PF.html#98ef9ff80c394fafd28680b7a3f831b1" title="Log all samples.">opt_L_smp</a> ( false ), <a class="code" href="classbdm_1_1PF.html#5a49463a88ee80771a464861df845ff6" title="Log all samples.">opt_L_wei</a> ( false ) {
90<a name="l00058"></a>00058                 <a class="code" href="classbdm_1_1BM.html#109c1a626a69031658e3a44e9e500cca" title="IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll].">LIDs</a>.set_size ( 5 );
91<a name="l00059"></a>00059         };
92<a name="l00060"></a>00060         <span class="comment">/*      PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) :</span>
93<a name="l00061"></a>00061 <span class="comment">                                est ( ),_w ( est._w() ),_samples ( est._samples() ),opt_L_smp(false), opt_L_wei(false)</span>
94<a name="l00062"></a>00062 <span class="comment">                { set_parameters ( par0,obs0,n0 ); set_statistics ( ones ( n0 ),epdf0 ); };*/</span>
95<a name="l00063"></a>00063         <span class="keywordtype">void</span> set_parameters ( <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">mpdf</a> *par0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">mpdf</a> *obs0, <span class="keywordtype">int</span> n0, RESAMPLING_METHOD rm = SYSTEMATIC ) {
96<a name="l00064"></a>00064                 <a class="code" href="classbdm_1_1PF.html#521e9621d3b5e1274275f323691afdaf" title="Parameter evolution model.">par</a> = par0;
97<a name="l00065"></a>00065                 <a class="code" href="classbdm_1_1PF.html#d6e7a62fba1e0a0d73c9b87f4fb683ec" title="Observation model.">obs</a> = obs0;
98<a name="l00066"></a>00066                 <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> = n0;
99<a name="l00067"></a>00067                 <a class="code" href="classbdm_1_1PF.html#9932c7c5865954ef9a438afcbe944e52" title="which resampling method will be used">resmethod</a> = rm;
100<a name="l00068"></a>00068         };
101<a name="l00069"></a>00069         <span class="keywordtype">void</span> set_statistics ( <span class="keyword">const</span> vec w0, <span class="keyword">const</span> epdf &amp;epdf0 ) {
102<a name="l00070"></a>00070                 <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>.set_statistics ( w0, epdf0 );
103<a name="l00071"></a>00071         };
104<a name="l00074"></a>00074 <span class="comment">//      void set_est ( const epdf &amp;epdf0 );</span>
105<a name="l00075"></a><a class="code" href="classbdm_1_1PF.html#bf104b869b5df8dd4a14bbe430d40488">00075</a>         <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1PF.html#bf104b869b5df8dd4a14bbe430d40488">set_options</a> ( <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;opt ) {
106<a name="l00076"></a>00076                 <a class="code" href="classbdm_1_1PF.html#bf104b869b5df8dd4a14bbe430d40488">BM::set_options</a> ( opt );
107<a name="l00077"></a>00077                 <a class="code" href="classbdm_1_1PF.html#5a49463a88ee80771a464861df845ff6" title="Log all samples.">opt_L_wei</a> = ( opt.find ( <span class="stringliteral">"logweights"</span> ) != string::npos );
108<a name="l00078"></a>00078                 <a class="code" href="classbdm_1_1PF.html#98ef9ff80c394fafd28680b7a3f831b1" title="Log all samples.">opt_L_smp</a> = ( opt.find ( <span class="stringliteral">"logsamples"</span> ) != string::npos );
109<a name="l00079"></a>00079         }
110<a name="l00080"></a>00080         <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 &amp;dt );
111<a name="l00082"></a><a class="code" href="classbdm_1_1PF.html#78a9f6809827be1d9bfe215d03b1c6ed">00082</a>         vec* <a class="code" href="classbdm_1_1PF.html#78a9f6809827be1d9bfe215d03b1c6ed" title="access function">__w</a>() {
112<a name="l00083"></a>00083                 <span class="keywordflow">return</span> &amp;<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>;
113<a name="l00084"></a>00084         }
114<a name="l00085"></a>00085 };
115<a name="l00086"></a>00086
116<a name="l00093"></a>00093 <span class="keyword">template</span>&lt;<span class="keyword">class</span> BM_T&gt;
117<a name="l00094"></a><a class="code" href="classbdm_1_1MPF.html">00094</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> {
118<a name="l00095"></a>00095         Array&lt;BM_T*&gt; BMs;
119<a name="l00096"></a>00096
120<a name="l00098"></a>00098
121<a name="l00099"></a>00099         <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>  {
122<a name="l00100"></a>00100         <span class="keyword">protected</span>:
123<a name="l00101"></a>00101                 <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> &amp;E;
124<a name="l00102"></a>00102                 vec &amp;<a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>;
125<a name="l00103"></a>00103                 Array&lt;const epdf*&gt; Coms;
126<a name="l00104"></a>00104         <span class="keyword">public</span>:
127<a name="l00105"></a>00105                 mpfepdf ( <a class="code" href="classbdm_1_1eEmp.html" title="Weighted empirical density.">eEmp</a> &amp;E0 ) :
128<a name="l00106"></a>00106                                 <a class="code" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">epdf</a> ( ), E ( E0 ),  <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( E._w() ),
129<a name="l00107"></a>00107                                 Coms ( <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length() ) {
130<a name="l00108"></a>00108                 };
131<a name="l00110"></a>00110                 <span class="keywordtype">void</span> read_statistics ( Array&lt;BM_T*&gt; &amp;A ) {
132<a name="l00111"></a>00111                         dim = E.dimension() + A ( 0 )-&gt;posterior().dimension();
133<a name="l00112"></a>00112                         <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length() ; i++ ) {
134<a name="l00113"></a>00113                                 Coms ( i ) = &amp;(A ( i )-&gt;posterior());
135<a name="l00114"></a>00114                         }
136<a name="l00115"></a>00115                 }
137<a name="l00117"></a>00117                 <span class="keywordtype">void</span> set_elements ( <span class="keywordtype">int</span> &amp;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 ) {
138<a name="l00118"></a>00118                         <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i ) = wi;
139<a name="l00119"></a>00119                         Coms ( i ) = ep;
140<a name="l00120"></a>00120                 };
141<a name="l00121"></a>00121
142<a name="l00122"></a>00122                 <span class="keywordtype">void</span> set_parameters ( <span class="keywordtype">int</span> <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ) {
143<a name="l00123"></a>00123                         E.<a class="code" href="classbdm_1_1epdf.html#840de94aa33cf4f2ebd2427f45a165d8">set_parameters</a> ( n, <span class="keyword">false</span> );
144<a name="l00124"></a>00124                         Coms.set_length ( n );
145<a name="l00125"></a>00125                 }
146<a name="l00126"></a>00126                 vec mean()<span class="keyword"> const </span>{
147<a name="l00127"></a>00127                         <span class="comment">// ugly</span>
148<a name="l00128"></a>00128                         vec pom = zeros ( Coms ( 0 )-&gt;dimension() );
149<a name="l00129"></a>00129                         <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) {
150<a name="l00130"></a>00130                                 pom += Coms ( i )-&gt;mean() * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );
151<a name="l00131"></a>00131                         }
152<a name="l00132"></a>00132                         <span class="keywordflow">return</span> concat ( E.mean(), pom );
153<a name="l00133"></a>00133                 }
154<a name="l00134"></a>00134                 vec variance()<span class="keyword"> const </span>{
155<a name="l00135"></a>00135                         <span class="comment">// ugly</span>
156<a name="l00136"></a>00136                         vec pom = zeros ( Coms ( 0 )-&gt;dimension() );
157<a name="l00137"></a>00137                         vec pom2 = zeros ( Coms ( 0 )-&gt;dimension() );
158<a name="l00138"></a>00138                         <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) {
159<a name="l00139"></a>00139                                 pom += Coms ( i )-&gt;mean() * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );
160<a name="l00140"></a>00140                                 pom2 += ( Coms ( i )-&gt;variance() + pow ( Coms ( i )-&gt;mean(), 2 ) ) * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );
161<a name="l00141"></a>00141                         }
162<a name="l00142"></a>00142                         <span class="keywordflow">return</span> concat ( E.variance(), pom2 - pow ( pom, 2 ) );
163<a name="l00143"></a>00143                 }
164<a name="l00144"></a>00144                 <span class="keywordtype">void</span> qbounds ( vec &amp;lb, vec &amp;ub, <span class="keywordtype">double</span> perc = 0.95 )<span class="keyword"> const </span>{
165<a name="l00145"></a>00145                         <span class="comment">//bounds on particles</span>
166<a name="l00146"></a>00146                         vec lbp;
167<a name="l00147"></a>00147                         vec ubp;
168<a name="l00148"></a>00148                         E.qbounds ( lbp, ubp );
169<a name="l00149"></a>00149
170<a name="l00150"></a>00150                         <span class="comment">//bounds on Components</span>
171<a name="l00151"></a>00151                         <span class="keywordtype">int</span> dimC = Coms ( 0 )-&gt;dimension();
172<a name="l00152"></a>00152                         <span class="keywordtype">int</span> j;
173<a name="l00153"></a>00153                         <span class="comment">// temporary</span>
174<a name="l00154"></a>00154                         vec lbc ( dimC );
175<a name="l00155"></a>00155                         vec ubc ( dimC );
176<a name="l00156"></a>00156                         <span class="comment">// minima and maxima</span>
177<a name="l00157"></a>00157                         vec Lbc ( dimC );
178<a name="l00158"></a>00158                         vec Ubc ( dimC );
179<a name="l00159"></a>00159                         Lbc = std::numeric_limits&lt;double&gt;::infinity();
180<a name="l00160"></a>00160                         Ubc = -std::numeric_limits&lt;double&gt;::infinity();
181<a name="l00161"></a>00161
182<a name="l00162"></a>00162                         <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a>.length(); i++ ) {
183<a name="l00163"></a>00163                                 <span class="comment">// check Coms</span>
184<a name="l00164"></a>00164                                 Coms ( i )-&gt;qbounds ( lbc, ubc );
185<a name="l00165"></a>00165                                 <span class="keywordflow">for</span> ( j = 0; j &lt; dimC; j++ ) {
186<a name="l00166"></a>00166                                         <span class="keywordflow">if</span> ( lbc ( j ) &lt; Lbc ( j ) ) {
187<a name="l00167"></a>00167                                                 Lbc ( j ) = lbc ( j );
188<a name="l00168"></a>00168                                         }
189<a name="l00169"></a>00169                                         <span class="keywordflow">if</span> ( ubc ( j ) &gt; Ubc ( j ) ) {
190<a name="l00170"></a>00170                                                 Ubc ( j ) = ubc ( j );
191<a name="l00171"></a>00171                                         }
192<a name="l00172"></a>00172                                 }
193<a name="l00173"></a>00173                         }
194<a name="l00174"></a>00174                         lb = concat ( lbp, Lbc );
195<a name="l00175"></a>00175                         ub = concat ( ubp, Ubc );
196<a name="l00176"></a>00176                 }
197<a name="l00177"></a>00177
198<a name="l00178"></a>00178                 vec sample()<span class="keyword"> const </span>{
199<a name="l00179"></a>00179                         <a class="code" href="bdmerror_8h.html#7c43f3a72afe68ab0c85663a1bb3521a" title="Unconditionally throw std::runtime_error.">bdm_error</a> ( <span class="stringliteral">"Not implemented"</span> );
200<a name="l00180"></a>00180                         <span class="keywordflow">return</span> vec();
201<a name="l00181"></a>00181                 }
202<a name="l00182"></a>00182
203<a name="l00183"></a>00183                 <span class="keywordtype">double</span> evallog ( <span class="keyword">const</span> vec &amp;val )<span class="keyword"> const </span>{
204<a name="l00184"></a>00184                         <a class="code" href="bdmerror_8h.html#7c43f3a72afe68ab0c85663a1bb3521a" title="Unconditionally throw std::runtime_error.">bdm_error</a> ( <span class="stringliteral">"not implemented"</span> );
205<a name="l00185"></a>00185                         <span class="keywordflow">return</span> 0.0;
206<a name="l00186"></a>00186                 }
207<a name="l00187"></a>00187         };
208<a name="l00188"></a>00188
209<a name="l00190"></a>00190         mpfepdf jest;
210<a name="l00191"></a>00191
211<a name="l00193"></a>00193         <span class="keywordtype">bool</span> opt_L_mea;
212<a name="l00194"></a>00194
213<a name="l00195"></a>00195 <span class="keyword">public</span>:
214<a name="l00197"></a><a class="code" href="classbdm_1_1MPF.html#0068e7ca53d90fa5911eb31a0d657f26">00197</a>         <a class="code" href="classbdm_1_1MPF.html#0068e7ca53d90fa5911eb31a0d657f26" title="Default constructor.">MPF</a> () : <a class="code" href="classbdm_1_1PF.html" title="Trivial particle filter with proposal density equal to parameter evolution model...">PF</a> (), jest ( <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a> ) {};
215<a name="l00198"></a>00198         <span class="keywordtype">void</span> set_parameters ( <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">mpdf</a> *par0, <a class="code" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">mpdf</a> *obs0, <span class="keywordtype">int</span> n0, RESAMPLING_METHOD rm = SYSTEMATIC ) {
216<a name="l00199"></a>00199                 PF::set_parameters ( par0, obs0, n0, rm );
217<a name="l00200"></a>00200                 jest.set_parameters ( n0 );<span class="comment">//duplication of rm</span>
218<a name="l00201"></a>00201                 BMs.set_length ( n0 );
219<a name="l00202"></a>00202         }
220<a name="l00203"></a>00203         <span class="keywordtype">void</span> set_statistics ( <span class="keyword">const</span> epdf &amp;epdf0, <span class="keyword">const</span> BM_T* BMcond0 ) {
221<a name="l00204"></a>00204
222<a name="l00205"></a>00205                 PF::set_statistics ( ones ( <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a> ) / <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>, epdf0 );
223<a name="l00206"></a>00206                 <span class="comment">// copy</span>
224<a name="l00207"></a>00207                 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="classbdm_1_1PF.html#eeafaf9b8ad75fe62ee9fd6369e3f7fe" title="number of particles;">n</a>; i++ ) {
225<a name="l00208"></a>00208                         BMs ( i ) = <span class="keyword">new</span> BM_T ( *BMcond0 );
226<a name="l00209"></a>00209                         BMs ( i )-&gt;condition ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) );
227<a name="l00210"></a>00210                 }
228<a name="l00211"></a>00211
229<a name="l00212"></a>00212                 jest.read_statistics ( BMs );
230<a name="l00213"></a>00213                 <span class="comment">//options</span>
231<a name="l00214"></a>00214         };
232<a name="l00215"></a>00215
233<a name="l00216"></a>00216         <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 &amp;dt );
234<a name="l00217"></a>00217         <span class="keyword">const</span> epdf&amp; posterior()<span class="keyword"> const </span>{
235<a name="l00218"></a>00218                 <span class="keywordflow">return</span> jest;
236<a name="l00219"></a>00219         }
237<a name="l00221"></a>00221         <span class="comment">/*      void set_est ( const epdf&amp; epdf0 ) {</span>
238<a name="l00222"></a>00222 <span class="comment">                        PF::set_est ( epdf0 );  // sample params in condition</span>
239<a name="l00223"></a>00223 <span class="comment">                        // copy conditions to BMs</span>
240<a name="l00224"></a>00224 <span class="comment"></span>
241<a name="l00225"></a>00225 <span class="comment">                        for ( int i=0;i&lt;n;i++ ) {BMs(i)-&gt;condition ( _samples ( i ) );}</span>
242<a name="l00226"></a>00226 <span class="comment">                }*/</span>
243<a name="l00227"></a><a class="code" href="classbdm_1_1MPF.html#2e95498dec734088ab9f4878ff404144">00227</a>         <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html#2e95498dec734088ab9f4878ff404144" title="Set postrior of rvc to samples from epdf0. Statistics of BMs are not re-computed!...">set_options</a> ( <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;opt ) {
244<a name="l00228"></a>00228                 <a class="code" href="classbdm_1_1MPF.html#2e95498dec734088ab9f4878ff404144" title="Set postrior of rvc to samples from epdf0. Statistics of BMs are not re-computed!...">PF::set_options</a> ( opt );
245<a name="l00229"></a>00229                 opt_L_mea = ( opt.find ( <span class="stringliteral">"logmeans"</span> ) != string::npos );
246<a name="l00230"></a>00230         }
247<a name="l00231"></a>00231
248<a name="l00233"></a><a class="code" href="classbdm_1_1MPF.html#b6e7b094cbe32944b8ccde5df73cd839">00233</a>         <span class="keyword">const</span> <a class="code" href="classbdm_1_1BM.html" title="Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities.">BM</a>* <a class="code" href="classbdm_1_1MPF.html#b6e7b094cbe32944b8ccde5df73cd839" title="Access function.">_BM</a> ( <span class="keywordtype">int</span> i ) {
249<a name="l00234"></a>00234                 <span class="keywordflow">return</span> BMs ( i );
250<a name="l00235"></a>00235         }
251<a name="l00236"></a>00236 };
252<a name="l00237"></a>00237
253<a name="l00238"></a>00238 <span class="keyword">template</span>&lt;<span class="keyword">class</span> BM_T&gt;
254<a name="l00239"></a><a class="code" href="classbdm_1_1MPF.html#286d040770d08bd7ff416cea617b1b14">00239</a> <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1MPF.html#286d040770d08bd7ff416cea617b1b14" title="Incremental Bayes rule.">MPF&lt;BM_T&gt;::bayes</a> ( <span class="keyword">const</span> vec &amp;dt ) {
255<a name="l00240"></a>00240         <span class="keywordtype">int</span> i;
256<a name="l00241"></a>00241         vec lls ( n );
257<a name="l00242"></a>00242         vec llsP ( n );
258<a name="l00243"></a>00243         ivec ind;
259<a name="l00244"></a>00244         <span class="keywordtype">double</span> mlls = -std::numeric_limits&lt;double&gt;::infinity();
260<a name="l00245"></a>00245
261<a name="l00246"></a>00246 <span class="preprocessor">#pragma omp parallel for</span>
262<a name="l00247"></a>00247 <span class="preprocessor"></span>        <span class="keywordflow">for</span> ( i = 0; i &lt; n; i++ ) {
263<a name="l00248"></a>00248                 <span class="comment">//generate new samples from paramater evolution model;</span>
264<a name="l00249"></a>00249                 vec old_smp=<a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i );
265<a name="l00250"></a>00250                 <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) = <a class="code" href="classbdm_1_1PF.html#521e9621d3b5e1274275f323691afdaf" title="Parameter evolution model.">par</a>-&gt;<a class="code" href="classbdm_1_1mpdf.html#f0c1db6fcbb3aae2dd6123884457a367" title="Returns a sample from the density conditioned on cond, .">samplecond</a> ( old_smp );
266<a name="l00251"></a>00251                 llsP ( i ) = <a class="code" href="classbdm_1_1PF.html#521e9621d3b5e1274275f323691afdaf" title="Parameter evolution model.">par</a>-&gt;<a class="code" href="classbdm_1_1mpdf.html#6336a8a72462e2a56a3989a220f18b1b" title="Shortcut for conditioning and evaluation of the internal epdf. In some cases, this...">evallogcond</a> ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ), old_smp );
267<a name="l00252"></a>00252                 BMs ( i )-&gt;condition ( <a class="code" href="classbdm_1_1PF.html#914bd66025692c4018dbd482cb3c47c1" title="pointer into eEmp ">_samples</a> ( i ) );
268<a name="l00253"></a>00253                 BMs ( i )-&gt;bayes ( dt );
269<a name="l00254"></a>00254                 lls ( i ) = BMs ( i )-&gt;_ll(); <span class="comment">// lls above is also in proposal her must be lls(i) =, not +=!!</span>
270<a name="l00255"></a>00255                 <span class="keywordflow">if</span> ( lls ( i ) &gt; mlls ) mlls = lls ( i ); <span class="comment">//find maximum likelihood (for numerical stability)</span>
271<a name="l00256"></a>00256         }
272<a name="l00257"></a>00257
273<a name="l00258"></a>00258         <span class="keywordtype">double</span> sum_w = 0.0;
274<a name="l00259"></a>00259         <span class="comment">// compute weights</span>
275<a name="l00260"></a>00260 <span class="preprocessor">#pragma omp parallel for</span>
276<a name="l00261"></a>00261 <span class="preprocessor"></span>        <span class="keywordflow">for</span> ( i = 0; i &lt; n; i++ ) {
277<a name="l00262"></a>00262                 <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>
278<a name="l00263"></a>00263                 sum_w += <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> ( i );
279<a name="l00264"></a>00264         }
280<a name="l00265"></a>00265
281<a name="l00266"></a>00266         <span class="keywordflow">if</span> ( sum_w  &gt; 0.0 ) {
282<a name="l00267"></a>00267                 <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> /= sum_w; <span class="comment">//?</span>
283<a name="l00268"></a>00268         } <span class="keywordflow">else</span> {
284<a name="l00269"></a>00269                 cout &lt;&lt; <span class="stringliteral">"sum(w)==0"</span> &lt;&lt; endl;
285<a name="l00270"></a>00270         }
286<a name="l00271"></a>00271
287<a name="l00272"></a>00272
288<a name="l00273"></a>00273         <span class="keywordtype">double</span> eff = 1.0 / ( <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> * <a class="code" href="classbdm_1_1PF.html#f5149d5522d1095d39240c4c607f61a3" title="pointer into eEmp ">_w</a> );
289<a name="l00274"></a>00274         <span class="keywordflow">if</span> ( eff &lt; ( 0.3*n ) ) {
290<a name="l00275"></a>00275                 ind = <a class="code" href="classbdm_1_1PF.html#dc049265b9086cad7071f98d00a2b9af" title="posterior density">est</a>.<a class="code" href="classbdm_1_1eEmp.html#f06ce255de5dbb2313f52ee51f82ba3d" title="Function performs resampling, i.e. removal of low-weight samples and duplication...">resample</a> ( <a class="code" href="classbdm_1_1PF.html#9932c7c5865954ef9a438afcbe944e52" title="which resampling method will be used">resmethod</a> );
291<a name="l00276"></a>00276                 <span class="comment">// Resample Bms!</span>
292<a name="l00277"></a>00277
293<a name="l00278"></a>00278 <span class="preprocessor">#pragma omp parallel for</span>
294<a name="l00279"></a>00279 <span class="preprocessor"></span>                <span class="keywordflow">for</span> ( i = 0; i &lt; n; i++ ) {
295<a name="l00280"></a>00280                         <span class="keywordflow">if</span> ( ind ( i ) != i ) {<span class="comment">//replace the current Bm by a new one</span>
296<a name="l00281"></a>00281                                 <span class="comment">//fixme this would require new assignment operator</span>
297<a name="l00282"></a>00282                                 <span class="comment">// *Bms[i] = *Bms[ind ( i ) ];</span>
298<a name="l00283"></a>00283
299<a name="l00284"></a>00284                                 <span class="comment">// poor-man's solution: replicate constructor here</span>
300<a name="l00285"></a>00285                                 <span class="comment">// copied from MPF::MPF</span>
301<a name="l00286"></a>00286                                 <span class="keyword">delete</span> BMs ( i );
302<a name="l00287"></a>00287                                 BMs ( i ) = <span class="keyword">new</span> BM_T ( *BMs ( ind ( i ) ) ); <span class="comment">//copy constructor</span>
303<a name="l00288"></a>00288                                 <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>&amp; pom = BMs ( i )-&gt;posterior();
304<a name="l00289"></a>00289                                 jest.set_elements ( i, 1.0 / n, &amp;pom );
305<a name="l00290"></a>00290                         }
306<a name="l00291"></a>00291                 };
307<a name="l00292"></a>00292                 cout &lt;&lt; <span class="charliteral">'.'</span>;
308<a name="l00293"></a>00293         }
309<a name="l00294"></a>00294 }
310<a name="l00295"></a>00295
311<a name="l00296"></a>00296 }
312<a name="l00297"></a>00297 <span class="preprocessor">#endif // KF_H</span>
313<a name="l00298"></a>00298 <span class="preprocessor"></span>
314<a name="l00299"></a>00299
315</pre></div></div>
316<hr size="1"><address style="text-align: right;"><small>Generated on Sat Aug 29 20:49:42 2009 for mixpp by&nbsp;
317<a href="http://www.doxygen.org/index.html">
318<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address>
319</body>
320</html>
Note: See TracBrowser for help on using the browser.