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
RevLine 
[384]1#include "particles.h"
[8]2
[283]3namespace bdm {
[8]4
5using std::endl;
6
[665]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                }
[11]23        }
[638]24}
[11]25
[638]26void PF::bayes_weights(){
27//
28        double mlls=max(lls);
[32]29        // compute weights
[638]30        for (int  i = 0; i < n; i++ ) {
[32]31                _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
[11]32        }
33
[32]34        //renormalize
[638]35        double sw=sum(_w);
[663]36        if (!std::isfinite(sw)) {
[638]37                for (int i=0;i<n;i++){
[663]38                        if (!std::isfinite(_w(i))) {_w(i)=0;}
[638]39                }
40                sw = sum(_w);
[665]41                if (!std::isfinite(sw) || sw==0.0) {
[638]42                        bdm_error("Particle filter is lost; no particle is good enough.");
43                }
44        }
45        _w /= sw;
46}
47
[679]48void PF::bayes( const vec &yt, const vec &cond ) {
49        const vec &ut=cond; //todo
[665]50       
[638]51        int i;
52        // generate samples - time step
[665]53        bayes_gensmp(ut);
[638]54        // weight them - data step
[477]55        for ( i = 0; i < n; i++ ) {
[665]56                lls ( i ) += obs->evallogcond ( yt, _samples ( i ) ); //+= because lls may have something from gensmp!
[638]57        }
[11]58
[638]59        bayes_weights();
[11]60
[638]61        if (do_resampling()) {
62                est.resample ( resmethod);
63        }
[8]64
65}
66
[283]67// void PF::set_est ( const epdf &epdf0 ) {
68//      int i;
[477]69//
[283]70//      for ( i=0;i<n;i++ ) {
71//              _samples ( i ) = epdf0.sample();
72//      }
73// }
[8]74
[679]75void MPF::bayes ( const vec &yt, const vec &cond ) {   
[665]76        // follows PF::bayes in most places!!! 
[638]77        int i;
78        int n=pf->__w().length();
79        vec &lls = pf->_lls();
80       
81        // generate samples - time step
[665]82        pf->bayes_gensmp(vec(0));
[638]83        // weight them - data step
84        #pragma parallel for
85        for ( i = 0; i < n; i++ ) {
[679]86                BMs(i) -> bayes(this2bm.pushdown(yt), this2bm.get_cond(yt,cond));
[638]87                lls ( i ) += BMs(i)->_ll();
88        }
[32]89
[638]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
[254]107}
[32]108//MPF::MPF:{}
Note: See TracBrowser for help on using the browser.