work/mixpp/bdm/estim/libPF.h

Go to the documentation of this file.
00001 
00013 #ifndef PF_H
00014 #define PF_H
00015 
00016 #include <itpp/itbase.h>
00017 #include "../stat/libEF.h"
00018 #include "../math/libDC.h"
00019 
00020 using namespace itpp;
00021 
00028 class PF : public BM {
00029 protected:
00031         int n;
00033         eEmp est;
00035         vec &_w;
00037         Array<vec> &_samples;
00039         mpdf &par;
00041         mpdf &obs;
00042 public:
00044         PF ( const RV &rv0, mpdf &par0,  mpdf &obs0, int n0 ) :BM ( rv0 ),
00045                         n ( n0 ),est ( rv0,n ),_w ( est._w() ),_samples ( est._samples() ),
00046                         par ( par0 ), obs ( obs0 ) {};
00047 
00049         void set_est ( const epdf &epdf0 );
00050         void bayes ( const vec &dt );
00051 };
00052 
00059 template<class BM_T>
00060 
00061 class MPF : public PF {
00062         BM_T* Bms[1000];
00063 
00065 
00066 class mpfepdf : public epdf  {
00067         protected:
00068                 eEmp &E;
00069                 vec &_w;
00070                 Array<epdf*> Coms;
00071         public:
00072                 mpfepdf ( eEmp &E0, const RV &rvc ) :
00073                                 epdf ( RV( ) ), E ( E0 ),  _w ( E._w() ),
00074                                 Coms ( _w.length() ) {
00075                         rv.add ( E._rv() );
00076                         rv.add ( rvc );
00077                 };
00078 
00079                 void set_elements ( int &i, double wi, epdf* ep )
00080                 {_w ( i ) =wi; Coms ( i ) =ep;};
00081 
00082                 vec mean() const {
00083                         // ugly
00084                         vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() );
00085 
00086                         for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );}
00087 
00088                         return concat ( E.mean(),pom );
00089                 }
00090 
00091                 vec sample() const {it_error ( "Not implemented" );return 0;}
00092 
00093                 double evalpdflog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;}
00094         };
00095 
00097         mpfepdf jest;
00098 
00099 public:
00101         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 ) {
00102                 //
00103                 //TODO test if rv and BMcond.rv are compatible.
00104                 rv.add ( rvlin );
00105                 //
00106 
00107                 if ( n>1000 ) it_error ( "increase 1000 here!" );
00108 
00109                 for ( int i=0;i<n;i++ ) {
00110                         Bms[i] = new BM_T ( BMcond0 ); //copy constructor
00111                         epdf& pom=Bms[i]->_epdf();
00112                         jest.set_elements ( i,1.0/n,&pom );
00113                 }
00114         };
00115 
00116         ~MPF() {
00117         }
00118 
00119         void bayes ( const vec &dt );
00120         epdf& _epdf() {return jest;}
00122         void set_est ( const epdf& epdf0 ) {
00123                 PF::set_est ( epdf0 );  // sample params in condition
00124                 // copy conditions to BMs
00125 
00126                 for ( int i=0;i<n;i++ ) {Bms[i]->condition ( _samples ( i ) );}
00127         }
00128 };
00129 
00130 template<class BM_T>
00131 void MPF<BM_T>::bayes ( const vec &dt ) {
00132         int i;
00133         vec lls ( n );
00134         ivec ind;
00135         double mlls=-std::numeric_limits<double>::infinity();
00136 
00137         for ( i=0;i<n;i++ ) {
00138                 //generate new samples from paramater evolution model;
00139                 _samples ( i ) = par.samplecond ( _samples ( i ), lls ( i ) );
00140                 Bms[i]->condition ( _samples ( i ) );
00141                 Bms[i]->bayes ( dt );
00142                 lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!!
00143 
00144                 if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability)
00145         }
00146 
00147         // compute weights
00148         for ( i=0;i<n;i++ ) {
00149                 _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
00150         }
00151 
00152         _w /=sum(_w); //?
00153 
00154         double eff = 1.0/( _w*_w );
00155         if ( eff < ( 0.1*n ) ) {
00156                 ind = est.resample();
00157                 // Resample Bms!
00158 
00159                 for ( i=0;i<n;i++ ) {
00160                         if ( ind ( i ) !=i ) {//replace the current Bm by a new one
00161                                 //fixme this would require new assignment operator
00162                                 // *Bms[i] = *Bms[ind ( i ) ];
00163                                 
00164                                 // poor-man's solution: replicate constructor here
00165                                 // copied from MPF::MPF
00166                                 delete Bms[i];
00167                                 Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor
00168                                 epdf& pom=Bms[i]->_epdf();
00169                                 jest.set_elements ( i,1.0/n,&pom );
00170                         }
00171                 };
00172                 cout << '.'; 
00173         }
00174 }
00175 
00176 #endif // KF_H
00177 
00178 

Generated on Wed Mar 5 15:40:00 2008 for mixpp by  doxygen 1.5.3