/*! \file \brief Bayesian Filtering using stochastic sampling (Particle Filters) \author Vaclav Smidl. ----------------------------------- BDM++ - C++ library for Bayesian Decision Making under Uncertainty Using IT++ for numerical operations ----------------------------------- */ #ifndef PF_H #define PF_H #include #include "../stat/libEF.h" #include "../math/libDC.h" using namespace itpp; //UGLY HACK extern double PF_SSAT;//used for StrSim:06 test... if length>0 the value is written. /*! * \brief Trivial particle filter with proposal density equal to parameter evolution model. Posterior density is represented by a weighted empirical density (\c eEmp ). */ class PF : public BM { protected: //!number of particles; int n; //!posterior density eEmp est; //! pointer into \c eEmp vec &_w; //! pointer into \c eEmp Array &_samples; //! Parameter evolution model mpdf ∥ //! Observation model mpdf &obs; public: //! Default constructor PF ( const RV &rv0, mpdf &par0, mpdf &obs0, int n0 ) :BM ( rv0 ), n ( n0 ),est ( rv0,n ),_w ( est._w() ),_samples ( est._samples() ), par ( par0 ), obs ( obs0 ) {}; //! Set posterior density by sampling from epdf0 void set_est ( const epdf &epdf0 ); void bayes ( const vec &dt ); }; /*! \brief Marginalized Particle filter Trivial version: proposal $=$ parameter evolution, observation model is not used. (it is assumed to be part of BM). */ template class MPF : public PF { BM_T* Bms[10000]; //! internal class for MPDF providing composition of eEmp with external components class mpfepdf : public epdf { protected: eEmp &E; vec &_w; Array Coms; public: mpfepdf ( eEmp &E0, const RV &rvc ) : epdf ( RV( ) ), E ( E0 ), _w ( E._w() ), Coms ( _w.length() ) { rv.add ( E._rv() ); rv.add ( rvc ); }; void set_elements ( int &i, double wi, epdf* ep ) {_w ( i ) =wi; Coms ( i ) =ep;}; vec mean() const { // ugly vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() ); for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );} return concat ( E.mean(),pom ); } vec sample() const {it_error ( "Not implemented" );return 0;} double evalpdflog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;} }; //! estimate joining PF.est with conditional mpfepdf jest; public: //! Default constructor. 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 ) { // //TODO test if rv and BMcond.rv are compatible. rv.add ( rvlin ); // if ( n>10000 ) it_error ( "increase 10000 here!" ); for ( int i=0;i_epdf(); jest.set_elements ( i,1.0/n,&pom ); } }; ~MPF() { } void bayes ( const vec &dt ); epdf& _epdf() {return jest;} //! Set postrior of \c rvc to samples from epdf0. Statistics of Bms are not re-computed! Use only for initialization! void set_est ( const epdf& epdf0 ) { PF::set_est ( epdf0 ); // sample params in condition // copy conditions to BMs for ( int i=0;icondition ( _samples ( i ) );} } //SimStr: double SSAT; }; template void MPF::bayes ( const vec &dt ) { int i; vec lls ( n ); vec llsP ( n ); ivec ind; double mlls=-std::numeric_limits::infinity(); // StrSim:06 double sumLWL=0.0; double sumL2WL=0.0; double WL = 0.0; for ( i=0;icondition ( _samples ( i ) ); Bms[i]->bayes ( dt ); lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!! if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability) } if ( true ) { for ( i=0;i0.0){ _w /=sum ( _w ); //? } else { cout<<"sum(w)==0"<_epdf(); jest.set_elements ( i,1.0/n,&pom ); } }; cout << '.'; } } #endif // KF_H