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

Revision 679, 2.2 kB (checked in by smidl, 15 years ago)

Major changes in BM -- OK is only test suite and tests/tutorial -- the rest is broken!!!

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