Changeset 283 for bdm/estim/libPF.h

Show
Ignore:
Timestamp:
02/24/09 14:14:01 (15 years ago)
Author:
smidl
Message:

get rid of BMcond + adaptation in doprava/

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/estim/libPF.h

    r281 r283  
    4040        mpdf *obs; 
    4141 
     42        //! which resampling method will be used 
     43        RESAMPLING_METHOD resmethod; 
     44 
    4245        //! \name Options 
    4346        //!@{ 
    4447 
    45         //! Log resampling times 
    46         bool opt_L_res; 
    4748        //! Log all samples 
    4849        bool opt_L_smp; 
     50        //! Log all samples 
     51        bool opt_L_wei; 
    4952        //!@} 
    5053 
     
    5255        //! \name Constructors 
    5356        //!@{ 
    54         PF ( ) :est(), _w ( est._w() ),_samples ( est._samples() ) {}; 
    55         PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) : 
    56                         est ( ),_w ( est._w() ),_samples ( est._samples() ) 
    57         { set_parameters ( par0,obs0,n0 ); set_statistics ( ones ( n0 ),epdf0 ); }; 
    58         void set_parameters ( mpdf *par0, mpdf *obs0, int n0 ) 
    59         { par = par0; obs=obs0; n=n0; est.set_n ( n );}; 
    60         void set_statistics ( const vec w0, epdf *epdf0 ) {est.set_parameters ( w0,epdf0 );}; 
     57        PF ( ) :est(), _w ( est._w() ),_samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) {LIDs.set_size ( 5 );}; 
     58        /*      PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) : 
     59                                est ( ),_w ( est._w() ),_samples ( est._samples() ),opt_L_smp(false), opt_L_wei(false) 
     60                { set_parameters ( par0,obs0,n0 ); set_statistics ( ones ( n0 ),epdf0 ); };*/ 
     61        void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm=SYSTEMATIC ) 
     62        { par = par0; obs=obs0; n=n0; resmethod= rm;}; 
     63        void set_statistics ( const vec w0, epdf *epdf0 ) {est.set_statistics ( w0,epdf0 );}; 
    6164        //!@} 
    6265        //! Set posterior density by sampling from epdf0 
    63         void set_est ( const epdf &epdf0 ); 
    64         void set_options(const string &opt){ 
    65                 opt_L_res= ( s.find ( "logres" ) !=string::npos ); 
    66                 opt_L_smp= ( s.find ( "logsmp" ) !=string::npos );               
     66//      void set_est ( const epdf &epdf0 ); 
     67        void set_options ( const string &opt ) { 
     68                BM::set_options(opt); 
     69                opt_L_wei= ( opt.find ( "logweights" ) !=string::npos ); 
     70                opt_L_smp= ( opt.find ( "logsamples" ) !=string::npos ); 
    6771        } 
    6872        void bayes ( const vec &dt ); 
     
    7882 
    7983template<class BM_T> 
    80  
    8184class MPF : public PF { 
    82         BM_T* Bms[10000]; 
     85        Array<BM_T*> BMs; 
    8386 
    8487        //! internal class for MPDF providing composition of eEmp with external components 
     
    9497                                Coms ( _w.length() ) { 
    9598                }; 
     99                //! read statistics from MPF 
     100                void read_statistics ( Array<BM_T*> &A ) { 
     101                        dim = E.dimension() +A ( 0 )->posterior().dimension(); 
     102                        for ( int i=0; i<_w.length() ;i++ ) {Coms ( i ) = A ( i )->_e();} 
     103                } 
     104                //! needed in resampling 
    96105                void set_elements ( int &i, double wi, const epdf* ep ) 
    97106                {_w ( i ) =wi; Coms ( i ) =ep;}; 
    98107 
    99                 void set_n ( int n ) {E.set_n ( n ); Coms.set_length ( n );} 
     108                void set_parameters ( int n ) { 
     109                        E.set_parameters ( n, false ); 
     110                        Coms.set_length ( n ); 
     111                } 
    100112                vec mean() const { 
    101113                        // ugly 
     
    114126                        return concat ( E.variance(),pom2-pow ( pom,2 ) ); 
    115127                } 
     128                void qbounds ( vec &lb, vec &ub, double perc=0.95 ) const { 
     129                        //bounds on particles 
     130                        vec lbp; 
     131                        vec ubp; 
     132                        E.qbounds ( lbp,ubp ); 
     133 
     134                        //bounds on Components 
     135                        int dimC=Coms ( 0 )->dimension(); 
     136                        int j; 
     137                        // temporary 
     138                        vec lbc(dimC); 
     139                        vec ubc(dimC); 
     140                        // minima and maxima 
     141                        vec Lbc(dimC); 
     142                        vec Ubc(dimC); 
     143                        Lbc = std::numeric_limits<double>::infinity(); 
     144                        Ubc = -std::numeric_limits<double>::infinity(); 
     145 
     146                        for ( int i=0;i<_w.length();i++ ) { 
     147                                // check Coms 
     148                                Coms ( i )->qbounds ( lbc,ubc ); 
     149                                for ( j=0;j<dimC; j++ ) { 
     150                                        if ( lbc ( j ) <Lbc ( j ) ) {Lbc ( j ) =lbc ( j );} 
     151                                        if ( ubc ( j ) >Ubc ( j ) ) {Ubc ( j ) =ubc ( j );} 
     152                                } 
     153                        } 
     154                        lb=concat(lbp,Lbc); 
     155                        ub=concat(ubp,Ubc); 
     156                } 
    116157 
    117158                vec sample() const {it_error ( "Not implemented" );return 0;} 
     
    125166        //! Log means of BMs 
    126167        bool opt_L_mea; 
    127          
     168 
    128169public: 
    129170        //! Default constructor. 
    130         MPF ( mpdf *par0, mpdf *obs0, int n, const BM_T &BMcond0 ) : PF (), jest ( est ) { 
    131                 PF::set_parameters ( par0,obs0,n ); 
    132                 jest.set_n ( n ); 
    133                 // 
    134                 //TODO test if rv and BMcond.rv are compatible. 
    135 //              rv.add ( rvlin ); 
    136                 // 
    137  
    138                 if ( n>10000 ) {it_error ( "increase 10000 here!" );} 
    139  
    140                 for ( int i=0;i<n;i++ ) { 
    141                         Bms[i] = new BM_T ( BMcond0 ); //copy constructor 
    142                         const epdf& pom=Bms[i]->posterior(); 
    143                         jest.set_elements ( i,1.0/n,&pom ); 
    144                 } 
     171        MPF () : PF (), jest ( est ) {}; 
     172        void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm=SYSTEMATIC ) { 
     173                PF::set_parameters ( par0, obs0, n0, rm ); 
     174                jest.set_parameters ( n0 );//duplication of rm 
     175                BMs.set_length ( n0 ); 
     176        } 
     177        void set_statistics ( epdf *epdf0, const BM_T* BMcond0 ) { 
     178 
     179                PF::set_statistics ( ones ( n ) /n, epdf0 ); 
     180                // copy 
     181                for ( int i=0;i<n;i++ ) { BMs ( i ) = new BM_T ( *BMcond0 ); BMs ( i )->condition ( _samples ( i ) );} 
     182 
     183                jest.read_statistics ( BMs ); 
     184                //options 
    145185        }; 
    146  
    147         ~MPF() { 
    148         } 
    149186 
    150187        void bayes ( const vec &dt ); 
    151188        const epdf& posterior() const {return jest;} 
    152189        const epdf* _e() const {return &jest;} //Fixme: is it useful? 
    153         //! Set postrior of \c rvc to samples from epdf0. Statistics of Bms are not re-computed! Use only for initialization! 
    154         void set_est ( const epdf& epdf0 ) { 
    155                 PF::set_est ( epdf0 );  // sample params in condition 
    156                 // copy conditions to BMs 
    157  
    158                 for ( int i=0;i<n;i++ ) {Bms[i]->condition ( _samples ( i ) );} 
    159         } 
    160         void set_options(const string &opt){ 
    161                 PF:set_options(opt); 
    162                 opt_L_mea = ( s.find ( "logmeans" ) !=string::npos ); 
    163         } 
    164          
     190        //! Set postrior of \c rvc to samples from epdf0. Statistics of BMs are not re-computed! Use only for initialization! 
     191        /*      void set_est ( const epdf& epdf0 ) { 
     192                        PF::set_est ( epdf0 );  // sample params in condition 
     193                        // copy conditions to BMs 
     194 
     195                        for ( int i=0;i<n;i++ ) {BMs(i)->condition ( _samples ( i ) );} 
     196                }*/ 
     197        void set_options ( const string &opt ) { 
     198                PF::set_options ( opt ); 
     199                opt_L_mea = ( opt.find ( "logmeans" ) !=string::npos ); 
     200        } 
     201 
    165202        //!Access function 
    166         BM* _BM ( int i ) {return Bms[i];} 
     203        BM* _BM ( int i ) {return BMs ( i );} 
    167204}; 
    168205 
     
    180217                _samples ( i ) = par->samplecond ( _samples ( i ) ); 
    181218                llsP ( i ) = par->_e()->evallog ( _samples ( i ) ); 
    182                 Bms[i]->condition ( _samples ( i ) ); 
    183                 Bms[i]->bayes ( dt ); 
    184                 lls ( i ) = Bms[i]->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!! 
     219                BMs ( i )->condition ( _samples ( i ) ); 
     220                BMs ( i )->bayes ( dt ); 
     221                lls ( i ) = BMs ( i )->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!! 
    185222                if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability) 
    186223        } 
     
    204241        double eff = 1.0/ ( _w*_w ); 
    205242        if ( eff < ( 0.3*n ) ) { 
    206                 ind = est.resample(); 
     243                ind = est.resample ( resmethod ); 
    207244                // Resample Bms! 
    208245 
     
    215252                                // poor-man's solution: replicate constructor here 
    216253                                // copied from MPF::MPF 
    217                                 delete Bms[i]; 
    218                                 Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor 
    219                                 const epdf& pom=Bms[i]->posterior(); 
     254                                delete BMs ( i ); 
     255                                BMs ( i ) = new BM_T ( *BMs ( ind ( i ) ) ); //copy constructor 
     256                                const epdf& pom=BMs ( i )->posterior(); 
    220257                                jest.set_elements ( i,1.0/n,&pom ); 
    221258                        }