root/library/bdm/estim/particles.cpp @ 661

Revision 661, 1.8 kB (checked in by smidl, 15 years ago)

doc

  • Property svn:eol-style set to native
Line 
1#include "particles.h"
2
3namespace bdm {
4
5using std::endl;
6
7void PF::bayes_gensmp(){
8        #pragma parallel for
9        for (int i = 0; i < n; i++ ) {
10                _samples ( i ) = par->samplecond ( _samples ( i ) );
11                lls(i) = 0;
12        }
13}
14
15void PF::bayes_weights(){
16//
17        double mlls=max(lls);
18        // compute weights
19        for (int  i = 0; i < n; i++ ) {
20                _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
21        }
22
23        //renormalize
24        double sw=sum(_w);
25        if (!isfinite(sw)) {
26                for (int i=0;i<n;i++){
27                        if (!isfinite(_w(i))) {_w(i)=0;}
28                }
29                sw = sum(_w);
30                if (!isfinite(sw)) {
31                        bdm_error("Particle filter is lost; no particle is good enough.");
32                }
33        }
34        _w /= sw;
35}
36
37void PF::bayes ( const vec &dt ) {
38        int i;
39        // generate samples - time step
40        bayes_gensmp();
41        // weight them - data step
42        for ( i = 0; i < n; i++ ) {
43                lls ( i ) += obs->evallogcond ( dt, _samples ( i ) ); //+= because lls may have something from gensmp!
44        }
45
46        bayes_weights();
47
48        if (do_resampling()) {
49                est.resample ( resmethod);
50        }
51
52}
53
54// void PF::set_est ( const epdf &epdf0 ) {
55//      int i;
56//
57//      for ( i=0;i<n;i++ ) {
58//              _samples ( i ) = epdf0.sample();
59//      }
60// }
61
62void MPF::bayes ( const vec &dt ) {     
63        // follows PF::bayes in most places!!!
64       
65        int i;
66        int n=pf->__w().length();
67        vec &lls = pf->_lls();
68       
69        // generate samples - time step
70        pf->bayes_gensmp();
71        // weight them - data step
72        #pragma parallel for
73        for ( i = 0; i < n; i++ ) {
74                BMs(i) -> condition(pf->posterior()._sample(i));
75                BMs(i) -> bayes(dt);
76                lls ( i ) += BMs(i)->_ll();
77        }
78
79        pf->bayes_weights();
80       
81        ivec ind;
82        if (pf->do_resampling()) {
83                pf->resample ( ind);
84               
85                #pragma omp parallel for
86                for ( i = 0; i < n; i++ ) {
87                        if ( ind ( i ) != i ) {//replace the current Bm by a new one
88                                delete BMs ( i );
89                                BMs ( i ) = BMs ( ind ( i ) )->_copy_(); //copy constructor
90                        }
91                };
92        }
93};
94
95
96}
97//MPF::MPF:{}
Note: See TracBrowser for help on using the browser.