Changeset 477 for library/bdm/estim/particles.h
- Timestamp:
- 08/05/09 14:40:03 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/particles.h
r461 r477 55 55 //! \name Constructors 56 56 //!@{ 57 PF ( ) :est(), _w ( est._w() ),_samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) {LIDs.set_size ( 5 );}; 57 PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) { 58 LIDs.set_size ( 5 ); 59 }; 58 60 /* PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) : 59 61 est ( ),_w ( est._w() ),_samples ( est._samples() ),opt_L_smp(false), opt_L_wei(false) 60 62 { 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 );}; 63 void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { 64 par = par0; 65 obs = obs0; 66 n = n0; 67 resmethod = rm; 68 }; 69 void set_statistics ( const vec w0, epdf *epdf0 ) { 70 est.set_statistics ( w0, epdf0 ); 71 }; 64 72 //!@} 65 73 //! Set posterior density by sampling from epdf0 66 74 // void set_est ( const epdf &epdf0 ); 67 75 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 );76 BM::set_options ( opt ); 77 opt_L_wei = ( opt.find ( "logweights" ) != string::npos ); 78 opt_L_smp = ( opt.find ( "logsamples" ) != string::npos ); 71 79 } 72 80 void bayes ( const vec &dt ); 73 81 //!access function 74 vec* __w() {return &_w;} 82 vec* __w() { 83 return &_w; 84 } 75 85 }; 76 86 … … 87 97 //! internal class for MPDF providing composition of eEmp with external components 88 98 89 class mpfepdf : public epdf {99 class mpfepdf : public epdf { 90 100 protected: 91 101 eEmp &E; … … 99 109 //! read statistics from MPF 100 110 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();} 111 dim = E.dimension() + A ( 0 )->posterior().dimension(); 112 for ( int i = 0; i < _w.length() ; i++ ) { 113 Coms ( i ) = A ( i )->_e(); 114 } 103 115 } 104 116 //! needed in resampling 105 void set_elements ( int &i, double wi, const epdf* ep ) 106 {_w ( i ) =wi; Coms ( i ) =ep;}; 117 void set_elements ( int &i, double wi, const epdf* ep ) { 118 _w ( i ) = wi; 119 Coms ( i ) = ep; 120 }; 107 121 108 122 void set_parameters ( int n ) { … … 112 126 vec mean() const { 113 127 // ugly 114 vec pom=zeros ( Coms ( 0 )->dimension() ); 115 for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );} 116 return concat ( E.mean(),pom ); 128 vec pom = zeros ( Coms ( 0 )->dimension() ); 129 for ( int i = 0; i < _w.length(); i++ ) { 130 pom += Coms ( i )->mean() * _w ( i ); 131 } 132 return concat ( E.mean(), pom ); 117 133 } 118 134 vec variance() const { 119 135 // ugly 120 vec pom =zeros ( Coms ( 0 )->dimension() );121 vec pom2 =zeros ( Coms ( 0 )->dimension() );122 for ( int i =0; i<_w.length(); i++ ) {136 vec pom = zeros ( Coms ( 0 )->dimension() ); 137 vec pom2 = zeros ( Coms ( 0 )->dimension() ); 138 for ( int i = 0; i < _w.length(); i++ ) { 123 139 pom += Coms ( i )->mean() * _w ( i ); 124 pom2 += ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ) * _w ( i );125 } 126 return concat ( E.variance(), pom2-pow ( pom,2 ) );127 } 128 void qbounds ( vec &lb, vec &ub, double perc =0.95 ) const {140 pom2 += ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ) * _w ( i ); 141 } 142 return concat ( E.variance(), pom2 - pow ( pom, 2 ) ); 143 } 144 void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const { 129 145 //bounds on particles 130 146 vec lbp; 131 147 vec ubp; 132 E.qbounds ( lbp, ubp );148 E.qbounds ( lbp, ubp ); 133 149 134 150 //bounds on Components 135 int dimC =Coms ( 0 )->dimension();151 int dimC = Coms ( 0 )->dimension(); 136 152 int j; 137 153 // temporary 138 vec lbc (dimC);139 vec ubc (dimC);154 vec lbc ( dimC ); 155 vec ubc ( dimC ); 140 156 // minima and maxima 141 vec Lbc (dimC);142 vec Ubc (dimC);157 vec Lbc ( dimC ); 158 vec Ubc ( dimC ); 143 159 Lbc = std::numeric_limits<double>::infinity(); 144 160 Ubc = -std::numeric_limits<double>::infinity(); 145 161 146 for ( int i =0;i<_w.length();i++ ) {162 for ( int i = 0; i < _w.length(); i++ ) { 147 163 // 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 );} 164 Coms ( i )->qbounds ( lbc, ubc ); 165 for ( j = 0; j < dimC; j++ ) { 166 if ( lbc ( j ) < Lbc ( j ) ) { 167 Lbc ( j ) = lbc ( j ); 168 } 169 if ( ubc ( j ) > Ubc ( j ) ) { 170 Ubc ( j ) = ubc ( j ); 171 } 152 172 } 153 173 } 154 lb=concat(lbp,Lbc); 155 ub=concat(ubp,Ubc); 156 } 157 158 vec sample() const {it_error ( "Not implemented" );return 0;} 159 160 double evallog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;} 174 lb = concat ( lbp, Lbc ); 175 ub = concat ( ubp, Ubc ); 176 } 177 178 vec sample() const { 179 it_error ( "Not implemented" ); 180 return 0; 181 } 182 183 double evallog ( const vec &val ) const { 184 it_error ( "not implemented" ); 185 return 0.0; 186 } 161 187 }; 162 188 … … 170 196 //! Default constructor. 171 197 MPF () : PF (), jest ( est ) {}; 172 void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm =SYSTEMATIC ) {198 void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { 173 199 PF::set_parameters ( par0, obs0, n0, rm ); 174 200 jest.set_parameters ( n0 );//duplication of rm … … 177 203 void set_statistics ( epdf *epdf0, const BM_T* BMcond0 ) { 178 204 179 PF::set_statistics ( ones ( n ) / n, epdf0 );205 PF::set_statistics ( ones ( n ) / n, epdf0 ); 180 206 // copy 181 for ( int i=0;i<n;i++ ) { BMs ( i ) = new BM_T ( *BMcond0 ); BMs ( i )->condition ( _samples ( i ) );} 207 for ( int i = 0; i < n; i++ ) { 208 BMs ( i ) = new BM_T ( *BMcond0 ); 209 BMs ( i )->condition ( _samples ( i ) ); 210 } 182 211 183 212 jest.read_statistics ( BMs ); … … 186 215 187 216 void bayes ( const vec &dt ); 188 const epdf& posterior() const {return jest;} 189 const epdf* _e() const {return &jest;} //Fixme: is it useful? 217 const epdf& posterior() const { 218 return jest; 219 } 220 const epdf* _e() const { 221 return &jest; //Fixme: is it useful? 222 } 190 223 //! Set postrior of \c rvc to samples from epdf0. Statistics of BMs are not re-computed! Use only for initialization! 191 224 /* void set_est ( const epdf& epdf0 ) { … … 197 230 void set_options ( const string &opt ) { 198 231 PF::set_options ( opt ); 199 opt_L_mea = ( opt.find ( "logmeans" ) != string::npos );232 opt_L_mea = ( opt.find ( "logmeans" ) != string::npos ); 200 233 } 201 234 202 235 //!Access function 203 BM* _BM ( int i ) {return BMs ( i );} 236 BM* _BM ( int i ) { 237 return BMs ( i ); 238 } 204 239 }; 205 240 … … 210 245 vec llsP ( n ); 211 246 ivec ind; 212 double mlls =-std::numeric_limits<double>::infinity();247 double mlls = -std::numeric_limits<double>::infinity(); 213 248 214 249 #pragma omp parallel for 215 for ( i =0;i<n;i++ ) {250 for ( i = 0; i < n; i++ ) { 216 251 //generate new samples from paramater evolution model; 217 252 _samples ( i ) = par->samplecond ( _samples ( i ) ); … … 220 255 BMs ( i )->bayes ( dt ); 221 256 lls ( i ) = BMs ( i )->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!! 222 if ( lls ( i ) > mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability)223 } 224 225 double sum_w =0.0;257 if ( lls ( i ) > mlls ) mlls = lls ( i ); //find maximum likelihood (for numerical stability) 258 } 259 260 double sum_w = 0.0; 226 261 // compute weights 227 262 #pragma omp parallel for 228 for ( i =0;i<n;i++ ) {263 for ( i = 0; i < n; i++ ) { 229 264 _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood 230 sum_w+=_w ( i ); 231 } 232 233 if ( sum_w >0.0 ) { 234 _w /=sum_w; //? 235 } 236 else { 237 cout<<"sum(w)==0"<<endl; 238 } 239 240 241 double eff = 1.0/ ( _w*_w ); 265 sum_w += _w ( i ); 266 } 267 268 if ( sum_w > 0.0 ) { 269 _w /= sum_w; //? 270 } else { 271 cout << "sum(w)==0" << endl; 272 } 273 274 275 double eff = 1.0 / ( _w * _w ); 242 276 if ( eff < ( 0.3*n ) ) { 243 277 ind = est.resample ( resmethod ); … … 245 279 246 280 #pragma omp parallel for 247 for ( i =0;i<n;i++ ) {248 if ( ind ( i ) != i ) {//replace the current Bm by a new one281 for ( i = 0; i < n; i++ ) { 282 if ( ind ( i ) != i ) {//replace the current Bm by a new one 249 283 //fixme this would require new assignment operator 250 284 // *Bms[i] = *Bms[ind ( i ) ]; … … 254 288 delete BMs ( i ); 255 289 BMs ( i ) = new BM_T ( *BMs ( ind ( i ) ) ); //copy constructor 256 const epdf& pom =BMs ( i )->posterior();257 jest.set_elements ( i, 1.0/n,&pom );290 const epdf& pom = BMs ( i )->posterior(); 291 jest.set_elements ( i, 1.0 / n, &pom ); 258 292 } 259 293 };