- Main Page
- Classes
- Files
- Related Pages

# 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
    00022 //UGLY HACK
    00023 extern double PF_SSAT;//used for StrSim:06 test... if length>0 the value is written.
    00024
    00031 class PF : public BM {
    00032 protected:
    00034         int n;
    00036         eEmp est;
    00038         vec &_w;
    00040         Array<vec> &_samples;
    00042         mpdf &par;
    00044         mpdf &obs;
    00045 public:
    00047         PF ( const RV &rv0, mpdf &par0,  mpdf &obs0, int n0 ) :BM ( rv0 ),
    00048                         n ( n0 ),est ( rv0,n ),_w ( est._w() ),_samples ( est._samples() ),
    00049                         par ( par0 ), obs ( obs0 ) {};
    00050
    00052         void set_est ( const epdf &epdf0 );
    00053         void bayes ( const vec &dt );
    00054 };
    00055
    00062 template<class BM_T>
    00063
    00064 class MPF : public PF {
    00065         BM_T* Bms[10000];
    00066
    00068
    00069 class mpfepdf : public epdf  {
    00070         protected:
    00071                 eEmp &E;
    00072                 vec &_w;
    00073                 Array<epdf*> Coms;
    00074         public:
    00075                 mpfepdf ( eEmp &E0, const RV &rvc ) :
    00076                                 epdf ( RV( ) ), E ( E0 ),  _w ( E._w() ),
    00077                                 Coms ( _w.length() ) {
    00078                         rv.add ( E._rv() );
    00079                         rv.add ( rvc );
    00080                 };
    00081
    00082                 void set_elements ( int &i, double wi, epdf* ep )
    00083                 {_w ( i ) =wi; Coms ( i ) =ep;};
    00084
    00085                 vec mean() const {
    00086                         // ugly
    00087                         vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() );
    00088
    00089                         for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );}
    00090
    00091                         return concat ( E.mean(),pom );
    00092                 }
    00093
    00094                 vec sample() const {it_error ( "Not implemented" );return 0;}
    00095
    00096                 double evalpdflog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;}
    00097         };
    00098
    00100         mpfepdf jest;
    00101
    00102 public:
    00104         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 ) {
    00105                 //
    00106                 //TODO test if rv and BMcond.rv are compatible.
    00107                 rv.add ( rvlin );
    00108                 //
    00109
    00110                 if ( n>10000 ) it_error ( "increase 10000 here!" );
    00111
    00112                 for ( int i=0;i<n;i++ ) {
    00113                         Bms[i] = new BM_T ( BMcond0 ); //copy constructor
    00114                         epdf& pom=Bms[i]->_epdf();
    00115                         jest.set_elements ( i,1.0/n,&pom );
    00116                 }
    00117         };
    00118
    00119         ~MPF() {
    00120         }
    00121
    00122         void bayes ( const vec &dt );
    00123         epdf& _epdf() {return jest;}
    00125         void set_est ( const epdf& epdf0 ) {
    00126                 PF::set_est ( epdf0 );  // sample params in condition
    00127                 // copy conditions to BMs
    00128
    00129                 for ( int i=0;i<n;i++ ) {Bms[i]->condition ( _samples ( i ) );}
    00130         }
    00131
    00132 //SimStr:
    00133         double SSAT;
    00134 };
    00135
    00136 template<class BM_T>
    00137 void MPF<BM_T>::bayes ( const vec &dt ) {
    00138         int i;
    00139         vec lls ( n );
    00140         vec llsP ( n );
    00141         ivec ind;
    00142         double mlls=-std::numeric_limits<double>::infinity();
    00143
    00144         // StrSim:06
    00145         double sumLWL=0.0;
    00146         double sumL2WL=0.0;
    00147         double WL = 0.0;
    00148
    00149         for ( i=0;i<n;i++ ) {
    00150                 //generate new samples from paramater evolution model;
    00151                 _samples ( i ) = par.samplecond ( _samples ( i ), llsP ( i ) );
    00152                 Bms[i]->condition ( _samples ( i ) );
    00153                 Bms[i]->bayes ( dt );
    00154                 lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!!
    00155
    00156                 if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability)
    00157         }
    00158
    00159         if ( true ) {
    00160                 for ( i=0;i<n;i++ ) {
    00161                         WL = _w ( i ) *exp ( llsP ( i ) ); //using old weights!
    00162                         sumLWL += exp ( lls ( i ) ) *WL;
    00163                         sumL2WL += exp ( 2*lls ( i ) ) *WL;
    00164                 }
    00165                 SSAT  = sumL2WL/(sumLWL*sumLWL);
    00166         }
    00167
    00168         // compute weights
    00169         for ( i=0;i<n;i++ ) {
    00170                 _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
    00171         }
    00172
    00173         if (sum(_w)>0.0){
    00174                 _w /=sum ( _w ); //?
    00175                 }
    00176 else
    00177 {
    00178 cout<<"sum(w)==0"<<endl;
    00179 }
    00180
    00181
    00182         double eff = 1.0/ ( _w*_w );
    00183         if ( eff < ( 0.3*n ) ) {
    00184                 ind = est.resample();
    00185                 // Resample Bms!
    00186
    00187                 for ( i=0;i<n;i++ ) {
    00188                         if ( ind ( i ) !=i ) {//replace the current Bm by a new one
    00189                                 //fixme this would require new assignment operator
    00190                                 // *Bms[i] = *Bms[ind ( i ) ];
    00191
    00192                                 // poor-man's solution: replicate constructor here
    00193                                 // copied from MPF::MPF
    00194                                 delete Bms[i];
    00195                                 Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor
    00196                                 epdf& pom=Bms[i]->_epdf();
    00197                                 jest.set_elements ( i,1.0/n,&pom );
    00198                         }
    00199                 };
    00200                 cout << '.';
    00201         }
    00202 }
    00203
    00204 #endif // KF_H
    00205
    00206