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

Revision 739, 4.0 kB (checked in by mido, 14 years ago)

the rest of h to cpp movements, with exception of from_setting and validate to avoid conflicts with Sarka

  • 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 ) ) ) {
39                                _w ( i ) = 0;
40                        }
41                }
42                sw = sum ( _w );
43                if ( !std::isfinite ( sw ) || sw == 0.0 ) {
44                        bdm_error ( "Particle filter is lost; no particle is good enough." );
45                }
46        }
47        _w /= sw;
48}
49
50void PF::bayes ( const vec &yt, const vec &cond ) {
51        const vec &ut = cond; //todo
52
53        int i;
54        // generate samples - time step
55        bayes_gensmp ( ut );
56        // weight them - data step
57        for ( i = 0; i < n; i++ ) {
58                lls ( i ) += obs->evallogcond ( yt, _samples ( i ) ); //+= because lls may have something from gensmp!
59        }
60
61        bayes_weights();
62
63        if ( do_resampling() ) {
64                est.resample ( resmethod );
65        }
66
67}
68
69// void PF::set_est ( const epdf &epdf0 ) {
70//      int i;
71//
72//      for ( i=0;i<n;i++ ) {
73//              _samples ( i ) = epdf0.sample();
74//      }
75// }
76
77vec MPF::mpfepdf::mean() const {
78        const vec &w = pf->posterior()._w();
79        vec pom = zeros ( BMs ( 0 )->posterior ().dimension() );
80        //compute mean of BMs
81        for ( int i = 0; i < w.length(); i++ ) {
82                pom += BMs ( i )->posterior().mean() * w ( i );
83        }
84        return concat ( pf->posterior().mean(), pom );
85}
86
87vec MPF::mpfepdf::variance() const {
88        const vec &w = pf->posterior()._w();
89
90        vec pom = zeros ( BMs ( 0 )->posterior ().dimension() );
91        vec pom2 = zeros ( BMs ( 0 )->posterior ().dimension() );
92        vec mea;
93
94        for ( int i = 0; i < w.length(); i++ ) {
95                // save current mean
96                mea = BMs ( i )->posterior().mean();
97                pom += mea * w ( i );
98                //compute variance
99                pom2 += ( BMs ( i )->posterior().variance() + pow ( mea, 2 ) ) * w ( i );
100        }
101        return concat ( pf->posterior().variance(), pom2 - pow ( pom, 2 ) );
102}
103
104void MPF::mpfepdf::qbounds ( vec &lb, vec &ub, double perc ) const {
105        //bounds on particles
106        vec lbp;
107        vec ubp;
108        pf->posterior().qbounds ( lbp, ubp );
109
110        //bounds on Components
111        int dimC = BMs ( 0 )->posterior().dimension();
112        int j;
113        // temporary
114        vec lbc ( dimC );
115        vec ubc ( dimC );
116        // minima and maxima
117        vec Lbc ( dimC );
118        vec Ubc ( dimC );
119        Lbc = std::numeric_limits<double>::infinity();
120        Ubc = -std::numeric_limits<double>::infinity();
121
122        for ( int i = 0; i < BMs.length(); i++ ) {
123                // check Coms
124                BMs ( i )->posterior().qbounds ( lbc, ubc );
125                //save either minima or maxima
126                for ( j = 0; j < dimC; j++ ) {
127                        if ( lbc ( j ) < Lbc ( j ) ) {
128                                Lbc ( j ) = lbc ( j );
129                        }
130                        if ( ubc ( j ) > Ubc ( j ) ) {
131                                Ubc ( j ) = ubc ( j );
132                        }
133                }
134        }
135        lb = concat ( lbp, Lbc );
136        ub = concat ( ubp, Ubc );
137}
138
139
140
141void MPF::bayes ( const vec &yt, const vec &cond ) {
142        // follows PF::bayes in most places!!!
143        int i;
144        int n = pf->__w().length();
145        vec &lls = pf->_lls();
146        Array<vec> &samples = pf->__samples();
147
148        // generate samples - time step
149        pf->bayes_gensmp ( vec ( 0 ) );
150        // weight them - data step
151#pragma parallel for
152        for ( i = 0; i < n; i++ ) {
153                vec bm_cond ( BMs ( i )->dimensionc() );
154                this2bm.fill_cond ( yt, cond, bm_cond );
155                pf2bm.filldown ( samples ( i ), bm_cond );
156                BMs ( i ) -> bayes ( this2bm.pushdown ( yt ), bm_cond );
157                lls ( i ) += BMs ( i )->_ll();
158        }
159
160        pf->bayes_weights();
161
162        ivec ind;
163        if ( pf->do_resampling() ) {
164                pf->resample ( ind );
165
166#pragma omp parallel for
167                for ( i = 0; i < n; i++ ) {
168                        if ( ind ( i ) != i ) {//replace the current Bm by a new one
169                                delete BMs ( i );
170                                BMs ( i ) = BMs ( ind ( i ) )->_copy_(); //copy constructor
171                        }
172                };
173        }
174};
175
176
177}
178//MPF::MPF:{}
Note: See TracBrowser for help on using the browser.