root/library/bdm/estim/particles.h @ 487

Revision 487, 7.3 kB (checked in by smidl, 15 years ago)

1st step of mpdf redesign - BROKEN compile

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Bayesian Filtering using stochastic sampling (Particle Filters)
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef PARTICLES_H
14#define PARTICLES_H
15
16
17#include "../stat/exp_family.h"
18
19namespace bdm {
20
21/*!
22* \brief Trivial particle filter with proposal density equal to parameter evolution model.
23
24Posterior density is represented by a weighted empirical density (\c eEmp ).
25*/
26
27class PF : public BM {
28protected:
29        //!number of particles;
30        int n;
31        //!posterior density
32        eEmp est;
33        //! pointer into \c eEmp
34        vec &_w;
35        //! pointer into \c eEmp
36        Array<vec> &_samples;
37        //! Parameter evolution model
38        mpdf *par;
39        //! Observation model
40        mpdf *obs;
41
42        //! which resampling method will be used
43        RESAMPLING_METHOD resmethod;
44
45        //! \name Options
46        //!@{
47
48        //! Log all samples
49        bool opt_L_smp;
50        //! Log all samples
51        bool opt_L_wei;
52        //!@}
53
54public:
55        //! \name Constructors
56        //!@{
57        PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) {
58                LIDs.set_size ( 5 );
59        };
60        /*      PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) :
61                                est ( ),_w ( est._w() ),_samples ( est._samples() ),opt_L_smp(false), opt_L_wei(false)
62                { set_parameters ( par0,obs0,n0 ); set_statistics ( ones ( n0 ),epdf0 ); };*/
63        void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) {
64                par = par0;
65                obs = obs0;
66                n = n0;
67                resmethod = rm;
68        };
69        void set_statistics ( const vec w0, epdf *epdf0 ) {
70                est.set_statistics ( w0, epdf0 );
71        };
72        //!@}
73        //! Set posterior density by sampling from epdf0
74//      void set_est ( const epdf &epdf0 );
75        void set_options ( const string &opt ) {
76                BM::set_options ( opt );
77                opt_L_wei = ( opt.find ( "logweights" ) != string::npos );
78                opt_L_smp = ( opt.find ( "logsamples" ) != string::npos );
79        }
80        void bayes ( const vec &dt );
81        //!access function
82        vec* __w() {
83                return &_w;
84        }
85};
86
87/*!
88\brief Marginalized Particle filter
89
90Trivial version: proposal = parameter evolution, observation model is not used. (it is assumed to be part of BM).
91*/
92
93template<class BM_T>
94class MPF : public PF {
95        Array<BM_T*> BMs;
96
97        //! internal class for MPDF providing composition of eEmp with external components
98
99        class mpfepdf : public epdf  {
100        protected:
101                eEmp &E;
102                vec &_w;
103                Array<const epdf*> Coms;
104        public:
105                mpfepdf ( eEmp &E0 ) :
106                                epdf ( ), E ( E0 ),  _w ( E._w() ),
107                                Coms ( _w.length() ) {
108                };
109                //! read statistics from MPF
110                void read_statistics ( Array<BM_T*> &A ) {
111                        dim = E.dimension() + A ( 0 )->posterior().dimension();
112                        for ( int i = 0; i < _w.length() ; i++ ) {
113                                Coms ( i ) = A ( i )->_e();
114                        }
115                }
116                //! needed in resampling
117                void set_elements ( int &i, double wi, const epdf* ep ) {
118                        _w ( i ) = wi;
119                        Coms ( i ) = ep;
120                };
121
122                void set_parameters ( int n ) {
123                        E.set_parameters ( n, false );
124                        Coms.set_length ( n );
125                }
126                vec mean() const {
127                        // ugly
128                        vec pom = zeros ( Coms ( 0 )->dimension() );
129                        for ( int i = 0; i < _w.length(); i++ ) {
130                                pom += Coms ( i )->mean() * _w ( i );
131                        }
132                        return concat ( E.mean(), pom );
133                }
134                vec variance() const {
135                        // ugly
136                        vec pom = zeros ( Coms ( 0 )->dimension() );
137                        vec pom2 = zeros ( Coms ( 0 )->dimension() );
138                        for ( int i = 0; i < _w.length(); i++ ) {
139                                pom += Coms ( i )->mean() * _w ( i );
140                                pom2 += ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ) * _w ( i );
141                        }
142                        return concat ( E.variance(), pom2 - pow ( pom, 2 ) );
143                }
144                void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const {
145                        //bounds on particles
146                        vec lbp;
147                        vec ubp;
148                        E.qbounds ( lbp, ubp );
149
150                        //bounds on Components
151                        int dimC = Coms ( 0 )->dimension();
152                        int j;
153                        // temporary
154                        vec lbc ( dimC );
155                        vec ubc ( dimC );
156                        // minima and maxima
157                        vec Lbc ( dimC );
158                        vec Ubc ( dimC );
159                        Lbc = std::numeric_limits<double>::infinity();
160                        Ubc = -std::numeric_limits<double>::infinity();
161
162                        for ( int i = 0; i < _w.length(); i++ ) {
163                                // check Coms
164                                Coms ( i )->qbounds ( lbc, ubc );
165                                for ( j = 0; j < dimC; j++ ) {
166                                        if ( lbc ( j ) < Lbc ( j ) ) {
167                                                Lbc ( j ) = lbc ( j );
168                                        }
169                                        if ( ubc ( j ) > Ubc ( j ) ) {
170                                                Ubc ( j ) = ubc ( j );
171                                        }
172                                }
173                        }
174                        lb = concat ( lbp, Lbc );
175                        ub = concat ( ubp, Ubc );
176                }
177
178                vec sample() const {
179                        it_error ( "Not implemented" );
180                        return 0;
181                }
182
183                double evallog ( const vec &val ) const {
184                        it_error ( "not implemented" );
185                        return 0.0;
186                }
187        };
188
189        //! Density joining PF.est with conditional parts
190        mpfepdf jest;
191
192        //! Log means of BMs
193        bool opt_L_mea;
194
195public:
196        //! Default constructor.
197        MPF () : PF (), jest ( est ) {};
198        void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) {
199                PF::set_parameters ( par0, obs0, n0, rm );
200                jest.set_parameters ( n0 );//duplication of rm
201                BMs.set_length ( n0 );
202        }
203        void set_statistics ( epdf *epdf0, const BM_T* BMcond0 ) {
204
205                PF::set_statistics ( ones ( n ) / n, epdf0 );
206                // copy
207                for ( int i = 0; i < n; i++ ) {
208                        BMs ( i ) = new BM_T ( *BMcond0 );
209                        BMs ( i )->condition ( _samples ( i ) );
210                }
211
212                jest.read_statistics ( BMs );
213                //options
214        };
215
216        void bayes ( const vec &dt );
217        const epdf& posterior() const {
218                return jest;
219        }
220        const epdf* _e() const {
221                return &jest;    //Fixme: is it useful?
222        }
223        //! Set postrior of \c rvc to samples from epdf0. Statistics of BMs are not re-computed! Use only for initialization!
224        /*      void set_est ( const epdf& epdf0 ) {
225                        PF::set_est ( epdf0 );  // sample params in condition
226                        // copy conditions to BMs
227
228                        for ( int i=0;i<n;i++ ) {BMs(i)->condition ( _samples ( i ) );}
229                }*/
230        void set_options ( const string &opt ) {
231                PF::set_options ( opt );
232                opt_L_mea = ( opt.find ( "logmeans" ) != string::npos );
233        }
234
235        //!Access function
236        BM* _BM ( int i ) {
237                return BMs ( i );
238        }
239};
240
241template<class BM_T>
242void MPF<BM_T>::bayes ( const vec &dt ) {
243        int i;
244        vec lls ( n );
245        vec llsP ( n );
246        ivec ind;
247        double mlls = -std::numeric_limits<double>::infinity();
248
249#pragma omp parallel for
250        for ( i = 0; i < n; i++ ) {
251                //generate new samples from paramater evolution model;
252                vec old_smp=_samples ( i );
253                _samples ( i ) = par->samplecond ( old_smp );
254                llsP ( i ) = par->evallogcond ( _samples ( i ), old_smp );
255                BMs ( i )->condition ( _samples ( i ) );
256                BMs ( i )->bayes ( dt );
257                lls ( i ) = BMs ( i )->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!!
258                if ( lls ( i ) > mlls ) mlls = lls ( i ); //find maximum likelihood (for numerical stability)
259        }
260
261        double sum_w = 0.0;
262        // compute weights
263#pragma omp parallel for
264        for ( i = 0; i < n; i++ ) {
265                _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
266                sum_w += _w ( i );
267        }
268
269        if ( sum_w  > 0.0 ) {
270                _w /= sum_w; //?
271        } else {
272                cout << "sum(w)==0" << endl;
273        }
274
275
276        double eff = 1.0 / ( _w * _w );
277        if ( eff < ( 0.3*n ) ) {
278                ind = est.resample ( resmethod );
279                // Resample Bms!
280
281#pragma omp parallel for
282                for ( i = 0; i < n; i++ ) {
283                        if ( ind ( i ) != i ) {//replace the current Bm by a new one
284                                //fixme this would require new assignment operator
285                                // *Bms[i] = *Bms[ind ( i ) ];
286
287                                // poor-man's solution: replicate constructor here
288                                // copied from MPF::MPF
289                                delete BMs ( i );
290                                BMs ( i ) = new BM_T ( *BMs ( ind ( i ) ) ); //copy constructor
291                                const epdf& pom = BMs ( i )->posterior();
292                                jest.set_elements ( i, 1.0 / n, &pom );
293                        }
294                };
295                cout << '.';
296        }
297}
298
299}
300#endif // KF_H
301
Note: See TracBrowser for help on using the browser.