- 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;}
    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         #pragma omp parallel for
    00150         for ( i=0;i<n;i++ ) {
    00151                 //generate new samples from paramater evolution model;
    00152                 _samples ( i ) = par.samplecond ( _samples ( i ), llsP ( i ) );
    00153                 Bms[i]->condition ( _samples ( i ) );
    00154                 Bms[i]->bayes ( dt );
    00155                 lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!!
    00156                 if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability)
    00157         }
    00158
    00159         if ( false) {
    00160                 #pragma omp parallel for reduction(+:sumLWL,sumL2WL) private(WL)
    00161                 for ( i=0;i<n;i++ ) {
    00162                         WL = _w ( i ) *exp ( llsP ( i ) ); //using old weights!
    00163                         sumLWL += exp ( lls ( i ) ) *WL;
    00164                         sumL2WL += exp ( 2*lls ( i ) ) *WL;
    00165                 }
    00166                 SSAT  = sumL2WL/ ( sumLWL*sumLWL );
    00167         }
    00168
    00169         double sum_w=0.0;
    00170         // compute weights
    00171         #pragma omp parallel for
    00172         for ( i=0;i<n;i++ ) {
    00173                 _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood
    00174                 sum_w+=_w(i);
    00175         }
    00176
    00177         if ( sum_w  >0.0 ) {
    00178                 _w /=sum_w; //?
    00179         } else {
    00180                 cout<<"sum(w)==0"<<endl;
    00181         }
    00182
    00183
    00184         double eff = 1.0/ ( _w*_w );
    00185         if ( eff < ( 0.3*n ) ) {
    00186                 ind = est.resample();
    00187                 // Resample Bms!
    00188
    00189                 #pragma omp parallel for
    00190                 for ( i=0;i<n;i++ ) {
    00191                         if ( ind ( i ) !=i ) {//replace the current Bm by a new one
    00192                                 //fixme this would require new assignment operator
    00193                                 // *Bms[i] = *Bms[ind ( i ) ];
    00194
    00195                                 // poor-man's solution: replicate constructor here
    00196                                 // copied from MPF::MPF
    00197                                 delete Bms[i];
    00198                                 Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor
    00199                                 const epdf& pom=Bms[i]->_epdf();
    00200                                 jest.set_elements ( i,1.0/n,&pom );
    00201                         }
    00202                 };
    00203                 cout << '.';
    00204         }
    00205 }
    00206
    00207 #endif // KF_H
    00208
    00209