00001
00013 #ifndef PARTICLES_H
00014 #define PARTICLES_H
00015
00016
00017 #include "../stat/exp_family.h"
00018
00019 namespace bdm {
00020
00027 class PF : public BM {
00028 protected:
00030 int n;
00032 eEmp est;
00034 vec &_w;
00036 Array<vec> &_samples;
00038 shared_ptr<mpdf> par;
00040 shared_ptr<mpdf> obs;
00042 vec lls;
00043
00045 RESAMPLING_METHOD resmethod;
00048 double res_threshold;
00049
00052
00054 bool opt_L_smp;
00056 bool opt_L_wei;
00058
00059 public:
00062 PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) {
00063 LIDs.set_size ( 5 );
00064 };
00065
00066 void set_parameters (int n0, double res_th0=0.5, RESAMPLING_METHOD rm = SYSTEMATIC ) {
00067 n = n0;
00068 res_threshold = res_th0;
00069 resmethod = rm;
00070 };
00071 void set_model ( shared_ptr<mpdf> par0, shared_ptr<mpdf> obs0) {
00072 par = par0;
00073 obs = obs0;
00074
00075 est.set_rv(par->_rv());
00076 };
00077 void set_statistics ( const vec w0, const epdf &epdf0 ) {
00078 est.set_statistics ( w0, epdf0 );
00079 };
00080 void set_statistics ( const eEmp &epdf0 ) {
00081 bdm_assert_debug(epdf0._rv().equal(par->_rv()),"Incompatibel input");
00082 est=epdf0;
00083 };
00089 void set_options ( const string &opt ) {
00090 BM::set_options ( opt );
00091 opt_L_wei = ( opt.find ( "logweights" ) != string::npos );
00092 opt_L_smp = ( opt.find ( "logsamples" ) != string::npos );
00093 }
00095 virtual void bayes_gensmp();
00097 virtual void bayes_weights();
00099 virtual bool do_resampling(){
00100 double eff = 1.0 / ( _w * _w );
00101 return eff < ( res_threshold*n );
00102 }
00103 void bayes ( const vec &dt );
00105 vec& __w() { return _w; }
00107 vec& _lls() { return lls; }
00108 RESAMPLING_METHOD _resmethod() const { return resmethod; }
00110 const eEmp& posterior() const {return est;}
00111
00124 void from_setting(const Setting &set){
00125 par = UI::build<mpdf>(set,"parameter_pdf",UI::compulsory);
00126 obs = UI::build<mpdf>(set,"observation_pdf",UI::compulsory);
00127
00128 prior_from_set(set);
00129 resmethod_from_set(set);
00130
00131
00132
00133 RV u = par->_rvc().remove_time().subt( par->_rv() );
00134
00135 RV obs_u = obs->_rvc().remove_time().subt( par->_rv() );
00136
00137 u.add(obs_u);
00138
00139 set_drv(concat(obs->_rv(),u) );
00140 }
00142 void resmethod_from_set(const Setting &set){
00143 string resmeth;
00144 if (UI::get(resmeth,set,"resmethod",UI::optional)){
00145 if (resmeth=="systematic") {
00146 resmethod= SYSTEMATIC;
00147 } else {
00148 if (resmeth=="multinomial"){
00149 resmethod=MULTINOMIAL;
00150 } else {
00151 if (resmeth=="stratified"){
00152 resmethod= STRATIFIED;
00153 } else {
00154 bdm_error("Unknown resampling method");
00155 }
00156 }
00157 }
00158 } else {
00159 resmethod=SYSTEMATIC;
00160 };
00161 if(!UI::get(res_threshold, set, "res_threshold", UI::optional)){
00162 res_threshold=0.5;
00163 }
00164 }
00166 void prior_from_set(const Setting & set){
00167 shared_ptr<epdf> pri = UI::build<epdf>(set,"prior",UI::compulsory);
00168
00169 eEmp *test_emp=dynamic_cast<eEmp*>(&(*pri));
00170 if (test_emp) {
00171 est=*test_emp;
00172 } else {
00173 int n;
00174 if (!UI::get(n,set,"n",UI::optional)){n=10;}
00175
00176 set_statistics(ones(n)/n, *pri);
00177 }
00178
00179 }
00180
00181 void validate(){
00182 n=_w.length();
00183 lls=zeros(n);
00184 if (par->_rv()._dsize()>0) {
00185 bdm_assert(par->_rv()._dsize()==est.dimension(),"Mismatch of RV and dimension of posterior" );
00186 }
00187 }
00189 void resample(ivec &ind){
00190 est.resample(ind,resmethod);
00191 }
00192 };
00193 UIREGISTER(PF);
00194
00202 class MPF : public BM {
00203 shared_ptr<PF> pf;
00204 Array<BM*> BMs;
00205
00207
00208 class mpfepdf : public epdf {
00209 shared_ptr<PF> &pf;
00210 Array<BM*> &BMs;
00211 public:
00212 mpfepdf (shared_ptr<PF> &pf0, Array<BM*> &BMs0): epdf(), pf(pf0), BMs(BMs0) { };
00214 void read_parameters(){
00215 rv = concat(pf->posterior()._rv(), BMs(0)->posterior()._rv());
00216 dim = pf->posterior().dimension() + BMs(0)->posterior().dimension();
00217 bdm_assert_debug(dim == rv._dsize(), "Wrong name ");
00218 }
00219 vec mean() const {
00220 const vec &w = pf->posterior()._w();
00221 vec pom = zeros ( BMs(0)->posterior ().dimension() );
00222
00223 for ( int i = 0; i < w.length(); i++ ) {
00224 pom += BMs ( i )->posterior().mean() * w ( i );
00225 }
00226 return concat ( pf->posterior().mean(), pom );
00227 }
00228 vec variance() const {
00229 const vec &w = pf->posterior()._w();
00230
00231 vec pom = zeros ( BMs(0)->posterior ().dimension() );
00232 vec pom2 = zeros ( BMs(0)->posterior ().dimension() );
00233 vec mea;
00234
00235 for ( int i = 0; i < w.length(); i++ ) {
00236
00237 mea = BMs ( i )->posterior().mean();
00238 pom += mea * w ( i );
00239
00240 pom2 += ( BMs ( i )->posterior().variance() + pow ( mea, 2 ) ) * w ( i );
00241 }
00242 return concat ( pf->posterior().variance(), pom2 - pow ( pom, 2 ) );
00243 }
00244
00245 void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const {
00246
00247 vec lbp;
00248 vec ubp;
00249 pf->posterior().qbounds ( lbp, ubp );
00250
00251
00252 int dimC = BMs ( 0 )->posterior().dimension();
00253 int j;
00254
00255 vec lbc ( dimC );
00256 vec ubc ( dimC );
00257
00258 vec Lbc ( dimC );
00259 vec Ubc ( dimC );
00260 Lbc = std::numeric_limits<double>::infinity();
00261 Ubc = -std::numeric_limits<double>::infinity();
00262
00263 for ( int i = 0; i < BMs.length(); i++ ) {
00264
00265 BMs ( i )->posterior().qbounds ( lbc, ubc );
00266
00267 for ( j = 0; j < dimC; j++ ) {
00268 if ( lbc ( j ) < Lbc ( j ) ) {
00269 Lbc ( j ) = lbc ( j );
00270 }
00271 if ( ubc ( j ) > Ubc ( j ) ) {
00272 Ubc ( j ) = ubc ( j );
00273 }
00274 }
00275 }
00276 lb = concat ( lbp, Lbc );
00277 ub = concat ( ubp, Ubc );
00278 }
00279
00280 vec sample() const {
00281 bdm_error ( "Not implemented" );
00282 return vec();
00283 }
00284
00285 double evallog ( const vec &val ) const {
00286 bdm_error ( "not implemented" );
00287 return 0.0;
00288 }
00289 };
00290
00292 mpfepdf jest;
00293
00295 bool opt_L_mea;
00296
00297 public:
00299 MPF () : jest (pf,BMs) {};
00300 void set_parameters ( shared_ptr<mpdf> par0, shared_ptr<mpdf> obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) {
00301 pf->set_model ( par0, obs0);
00302 pf->set_parameters(n0, rm );
00303 BMs.set_length ( n0 );
00304 }
00305 void set_BM ( const BM &BMcond0 ) {
00306
00307 int n=pf->__w().length();
00308 BMs.set_length(n);
00309
00310
00311 for ( int i = 0; i < n; i++ ) {
00312 BMs ( i ) = BMcond0._copy_();
00313 BMs ( i )->condition ( pf->posterior()._sample ( i ) );
00314 }
00315 };
00316
00317 void bayes ( const vec &dt );
00318 const epdf& posterior() const {
00319 return jest;
00320 }
00323 void set_options ( const string &opt ) {
00324 BM::set_options(opt);
00325 opt_L_mea = ( opt.find ( "logmeans" ) != string::npos );
00326 }
00327
00329 const BM* _BM ( int i ) {
00330 return BMs ( i );
00331 }
00332
00344 void from_setting(const Setting &set){
00345 shared_ptr<mpdf> par = UI::build<mpdf>(set,"parameter_pdf",UI::compulsory);
00346 shared_ptr<mpdf> obs= new mpdf();
00347
00348 pf = new PF;
00349
00350 pf->prior_from_set(set);
00351 pf->resmethod_from_set(set);
00352 pf->set_model(par,obs);
00353
00354 shared_ptr<BM> BM0 =UI::build<BM>(set,"BM",UI::compulsory);
00355 set_BM(*BM0);
00356
00357 string opt;
00358 if (UI::get(opt,set,"options",UI::optional)){
00359 set_options(opt);
00360 }
00361
00362
00363 RV u = par->_rvc().remove_time().subt( par->_rv() );
00364 set_drv(concat(BM0->_drv(),u) );
00365 validate();
00366 }
00367 void validate(){
00368 try{
00369 pf->validate();
00370 } catch (std::exception &e){
00371 throw UIException("Error in PF part of MPF:");
00372 }
00373 jest.read_parameters();
00374 }
00375
00376 };
00377 UIREGISTER(MPF);
00378
00379 }
00380 #endif // KF_H
00381
00382