- Main Page
- Related Pages
- Classes
- Files

# work/git/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<const 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, const 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                         const 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         const epdf& _epdf() const {return jest;}
    00124         const epdf* _e() const {return &jest;} //Fixme: is it useful?
    00126         void set_est ( const epdf& epdf0 ) {
    00127                 PF::set_est ( epdf0 );  // sample params in condition
    00128                 // copy conditions to BMs
    00129
    00130                 for ( int i=0;i<n;i++ ) {Bms[i]->condition ( _samples ( i ) );}
    00131         }
    00132
    00133 //SimStr:
    00134         double SSAT;
    00135 };
    00136
    00137 template<class BM_T>
    00138 void MPF<BM_T>::bayes ( const vec &dt ) {
    00139         int i;
    00140         vec lls ( n );
    00141         vec llsP ( n );
    00142         ivec ind;
    00143         double mlls=-std::numeric_limits<double>::infinity();
    00144
    00145         // StrSim:06
    00146         double sumLWL=0.0;
    00147         double sumL2WL=0.0;
    00148         double WL = 0.0;
    00149
    00150         #pragma omp parallel for
    00151         for ( i=0;i<n;i++ ) {
    00152                 //generate new samples from paramater evolution model;
    00153                 _samples ( i ) = par.samplecond ( _samples ( i ), llsP ( i ) );
    00154                 Bms[i]->condition ( _samples ( i ) );
    00155                 Bms[i]->bayes ( dt );
    00156                 lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!!
    00157                 if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability)
    00158         }
    00159
    00160         if ( false) {
    00161                 #pragma omp parallel for reduction(+:sumLWL,sumL2WL) private(WL)
    00162                 for ( i=0;i<n;i++ ) {
    00163                         WL = _w ( i ) *exp ( llsP ( i ) ); //using old weights!
    00164                         sumLWL += exp ( lls ( i ) ) *WL;
    00165                         sumL2WL += exp ( 2*lls ( i ) ) *WL;
    00166                 }
    00167                 SSAT  = sumL2WL/ ( sumLWL*sumLWL );
    00168         }
    00169
    00170         double sum_w=0.0;
    00171         // compute weights
    00172         #pragma omp parallel for
    00173         for ( i=0;i<n;i++ ) {
    00174                 _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
    00175                 sum_w+=_w(i);
    00176         }
    00177
    00178         if ( sum_w  >0.0 ) {
    00179                 _w /=sum_w; //?
    00180         } else {
    00181                 cout<<"sum(w)==0"<<endl;
    00182         }
    00183
    00184
    00185         double eff = 1.0/ ( _w*_w );
    00186         if ( eff < ( 0.3*n ) ) {
    00187                 ind = est.resample();
    00188                 // Resample Bms!
    00189
    00190                 #pragma omp parallel for
    00191                 for ( i=0;i<n;i++ ) {
    00192                         if ( ind ( i ) !=i ) {//replace the current Bm by a new one
    00193                                 //fixme this would require new assignment operator
    00194                                 // *Bms[i] = *Bms[ind ( i ) ];
    00195
    00196                                 // poor-man's solution: replicate constructor here
    00197                                 // copied from MPF::MPF
    00198                                 delete Bms[i];
    00199                                 Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor
    00200                                 const epdf& pom=Bms[i]->_epdf();
    00201                                 jest.set_elements ( i,1.0/n,&pom );
    00202                         }
    00203                 };
    00204                 cout << '.';
    00205         }
    00206 }
    00207
    00208 #endif // KF_H
    00209
    00210