Changeset 283 for bdm/estim/libPF.h
- Timestamp:
- 02/24/09 14:14:01 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/libPF.h
r281 r283 40 40 mpdf *obs; 41 41 42 //! which resampling method will be used 43 RESAMPLING_METHOD resmethod; 44 42 45 //! \name Options 43 46 //!@{ 44 47 45 //! Log resampling times46 bool opt_L_res;47 48 //! Log all samples 48 49 bool opt_L_smp; 50 //! Log all samples 51 bool opt_L_wei; 49 52 //!@} 50 53 … … 52 55 //! \name Constructors 53 56 //!@{ 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 );}; 61 64 //!@} 62 65 //! 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 ); 67 71 } 68 72 void bayes ( const vec &dt ); … … 78 82 79 83 template<class BM_T> 80 81 84 class MPF : public PF { 82 BM_T* Bms[10000];85 Array<BM_T*> BMs; 83 86 84 87 //! internal class for MPDF providing composition of eEmp with external components … … 94 97 Coms ( _w.length() ) { 95 98 }; 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 96 105 void set_elements ( int &i, double wi, const epdf* ep ) 97 106 {_w ( i ) =wi; Coms ( i ) =ep;}; 98 107 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 } 100 112 vec mean() const { 101 113 // ugly … … 114 126 return concat ( E.variance(),pom2-pow ( pom,2 ) ); 115 127 } 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 } 116 157 117 158 vec sample() const {it_error ( "Not implemented" );return 0;} … … 125 166 //! Log means of BMs 126 167 bool opt_L_mea; 127 168 128 169 public: 129 170 //! 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 145 185 }; 146 147 ~MPF() {148 }149 186 150 187 void bayes ( const vec &dt ); 151 188 const epdf& posterior() const {return jest;} 152 189 const epdf* _e() const {return &jest;} //Fixme: is it useful? 153 //! Set postrior of \c rvc to samples from epdf0. Statistics of B ms are not re-computed! Use only for initialization!154 void set_est ( const epdf& epdf0 ) {155 PF::set_est ( epdf0 ); // sample params in condition156 // copy conditions to BMs157 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 165 202 //!Access function 166 BM* _BM ( int i ) {return B ms[i];}203 BM* _BM ( int i ) {return BMs ( i );} 167 204 }; 168 205 … … 180 217 _samples ( i ) = par->samplecond ( _samples ( i ) ); 181 218 llsP ( i ) = par->_e()->evallog ( _samples ( i ) ); 182 B ms[i]->condition ( _samples ( i ) );183 B ms[i]->bayes ( dt );184 lls ( i ) = B ms[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 +=!! 185 222 if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability) 186 223 } … … 204 241 double eff = 1.0/ ( _w*_w ); 205 242 if ( eff < ( 0.3*n ) ) { 206 ind = est.resample ();243 ind = est.resample ( resmethod ); 207 244 // Resample Bms! 208 245 … … 215 252 // poor-man's solution: replicate constructor here 216 253 // copied from MPF::MPF 217 delete B ms[i];218 B ms[i] = new BM_T ( *Bms[ind ( i ) ]); //copy constructor219 const epdf& pom=B ms[i]->posterior();254 delete BMs ( i ); 255 BMs ( i ) = new BM_T ( *BMs ( ind ( i ) ) ); //copy constructor 256 const epdf& pom=BMs ( i )->posterior(); 220 257 jest.set_elements ( i,1.0/n,&pom ); 221 258 }