root/bdm/estim/libPF.h @ 67

Revision 67, 4.8 kB (checked in by smidl, 16 years ago)

opavy

  • 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 PF_H
14#define PF_H
15
16#include <itpp/itbase.h>
17#include "../stat/libEF.h"
18#include "../math/libDC.h"
19
20using namespace itpp;
21
22//UGLY HACK
23extern double PF_SSAT;//used for StrSim:06 test... if length>0 the value is written.
24
25/*!
26* \brief Trivial particle filter with proposal density equal to parameter evolution model.
27
28Posterior density is represented by a weighted empirical density (\c eEmp ).
29*/
30
31class PF : public BM {
32protected:
33        //!number of particles;
34        int n;
35        //!posterior density
36        eEmp est;
37        //! pointer into \c eEmp
38        vec &_w;
39        //! pointer into \c eEmp
40        Array<vec> &_samples;
41        //! Parameter evolution model
42        mpdf &par;
43        //! Observation model
44        mpdf &obs;
45public:
46        //! Default constructor
47        PF ( const RV &rv0, mpdf &par0,  mpdf &obs0, int n0 ) :BM ( rv0 ),
48                        n ( n0 ),est ( rv0,n ),_w ( est._w() ),_samples ( est._samples() ),
49                        par ( par0 ), obs ( obs0 ) {};
50
51        //! Set posterior density by sampling from epdf0
52        void set_est ( const epdf &epdf0 );
53        void bayes ( const vec &dt );
54};
55
56/*!
57\brief Marginalized Particle filter
58
59Trivial version: proposal $=$ parameter evolution, observation model is not used. (it is assumed to be part of BM).
60*/
61
62template<class BM_T>
63
64class MPF : public PF {
65        BM_T* Bms[10000];
66
67        //! internal class for MPDF providing composition of eEmp with external components
68
69class mpfepdf : public epdf  {
70        protected:
71                eEmp &E;
72                vec &_w;
73                Array<epdf*> Coms;
74        public:
75                mpfepdf ( eEmp &E0, const RV &rvc ) :
76                                epdf ( RV( ) ), E ( E0 ),  _w ( E._w() ),
77                                Coms ( _w.length() ) {
78                        rv.add ( E._rv() );
79                        rv.add ( rvc );
80                };
81
82                void set_elements ( int &i, double wi, epdf* ep )
83                {_w ( i ) =wi; Coms ( i ) =ep;};
84
85                vec mean() const {
86                        // ugly
87                        vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() );
88
89                        for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );}
90
91                        return concat ( E.mean(),pom );
92                }
93
94                vec sample() const {it_error ( "Not implemented" );return 0;}
95
96                double evalpdflog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;}
97        };
98
99        //! estimate joining PF.est with conditional
100        mpfepdf jest;
101
102public:
103        //! Default constructor.
104        MPF ( const RV &rvlin, const RV &rvpf, mpdf &par0, mpdf &obs0, int n, const BM_T &BMcond0 ) : PF ( rvpf ,par0,obs0,n ),jest ( est,rvlin ) {
105                //
106                //TODO test if rv and BMcond.rv are compatible.
107                rv.add ( rvlin );
108                //
109
110                if ( n>10000 ) it_error ( "increase 10000 here!" );
111
112                for ( int i=0;i<n;i++ ) {
113                        Bms[i] = new BM_T ( BMcond0 ); //copy constructor
114                        epdf& pom=Bms[i]->_epdf();
115                        jest.set_elements ( i,1.0/n,&pom );
116                }
117        };
118
119        ~MPF() {
120        }
121
122        void bayes ( const vec &dt );
123        epdf& _epdf() {return jest;}
124        //! Set postrior of \c rvc to samples from epdf0. Statistics of Bms are not re-computed! Use only for initialization!
125        void set_est ( const epdf& epdf0 ) {
126                PF::set_est ( epdf0 );  // sample params in condition
127                // copy conditions to BMs
128
129                for ( int i=0;i<n;i++ ) {Bms[i]->condition ( _samples ( i ) );}
130        }
131
132//SimStr:
133        double SSAT;   
134};
135
136template<class BM_T>
137void MPF<BM_T>::bayes ( const vec &dt ) {
138        int i;
139        vec lls ( n );
140        vec llsP ( n );
141        ivec ind;
142        double mlls=-std::numeric_limits<double>::infinity();
143
144        // StrSim:06
145        double sumLWL=0.0;
146        double sumL2WL=0.0;
147        double WL = 0.0;
148
149        for ( i=0;i<n;i++ ) {
150                //generate new samples from paramater evolution model;
151                _samples ( i ) = par.samplecond ( _samples ( i ), llsP ( i ) );
152                Bms[i]->condition ( _samples ( i ) );
153                Bms[i]->bayes ( dt );
154                lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!!
155
156                if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability)
157        }
158       
159        if ( true ) {
160                for ( i=0;i<n;i++ ) {
161                        WL = _w ( i ) *exp ( llsP ( i ) ); //using old weights!
162                        sumLWL += exp ( lls ( i ) ) *WL;
163                        sumL2WL += exp ( 2*lls ( i ) ) *WL;
164                }
165                SSAT  = sumL2WL/(sumLWL*sumLWL);
166        }
167
168        // compute weights
169        for ( i=0;i<n;i++ ) {
170                _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
171        }
172
173        if (sum(_w)>0.0){
174                _w /=sum ( _w ); //?
175                }
176else
177{
178cout<<"sum(w)==0"<<endl;
179}
180
181
182        double eff = 1.0/ ( _w*_w );
183        if ( eff < ( 0.3*n ) ) {
184                ind = est.resample();
185                // Resample Bms!
186
187                for ( i=0;i<n;i++ ) {
188                        if ( ind ( i ) !=i ) {//replace the current Bm by a new one
189                                //fixme this would require new assignment operator
190                                // *Bms[i] = *Bms[ind ( i ) ];
191
192                                // poor-man's solution: replicate constructor here
193                                // copied from MPF::MPF
194                                delete Bms[i];
195                                Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor
196                                epdf& pom=Bms[i]->_epdf();
197                                jest.set_elements ( i,1.0/n,&pom );
198                        }
199                };
200                cout << '.';
201        }
202}
203
204#endif // KF_H
205
Note: See TracBrowser for help on using the browser.