[8] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Bayesian Filtering using stochastic sampling (Particle Filters) |
---|
| 4 | \author Vaclav Smidl. |
---|
| 5 | |
---|
| 6 | ----------------------------------- |
---|
| 7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 8 | |
---|
| 9 | Using IT++ for numerical operations |
---|
| 10 | ----------------------------------- |
---|
| 11 | */ |
---|
| 12 | |
---|
[384] | 13 | #ifndef PARTICLES_H |
---|
| 14 | #define PARTICLES_H |
---|
[8] | 15 | |
---|
[262] | 16 | |
---|
[384] | 17 | #include "../stat/exp_family.h" |
---|
[8] | 18 | |
---|
[270] | 19 | namespace bdm { |
---|
[8] | 20 | |
---|
| 21 | /*! |
---|
[32] | 22 | * \brief Trivial particle filter with proposal density equal to parameter evolution model. |
---|
[8] | 23 | |
---|
[32] | 24 | Posterior density is represented by a weighted empirical density (\c eEmp ). |
---|
[8] | 25 | */ |
---|
[32] | 26 | |
---|
| 27 | class PF : public BM { |
---|
[8] | 28 | protected: |
---|
[32] | 29 | //!number of particles; |
---|
| 30 | int n; |
---|
| 31 | //!posterior density |
---|
| 32 | eEmp est; |
---|
| 33 | //! pointer into \c eEmp |
---|
| 34 | vec &_w; |
---|
| 35 | //! pointer into \c eEmp |
---|
| 36 | Array<vec> &_samples; |
---|
| 37 | //! Parameter evolution model |
---|
[638] | 38 | shared_ptr<mpdf> par; |
---|
[32] | 39 | //! Observation model |
---|
[638] | 40 | shared_ptr<mpdf> obs; |
---|
| 41 | //! internal structure storing loglikelihood of predictions |
---|
| 42 | vec lls; |
---|
| 43 | |
---|
[283] | 44 | //! which resampling method will be used |
---|
| 45 | RESAMPLING_METHOD resmethod; |
---|
[638] | 46 | //! resampling threshold; in this case its meaning is minimum ratio of active particles |
---|
| 47 | //! For example, for 0.5 resampling is performed when the numebr of active aprticles drops belo 50%. |
---|
| 48 | double res_threshold; |
---|
| 49 | |
---|
[281] | 50 | //! \name Options |
---|
| 51 | //!@{ |
---|
| 52 | //!@} |
---|
| 53 | |
---|
[8] | 54 | public: |
---|
[270] | 55 | //! \name Constructors |
---|
| 56 | //!@{ |
---|
[676] | 57 | PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ) { |
---|
[477] | 58 | }; |
---|
[638] | 59 | |
---|
| 60 | void set_parameters (int n0, double res_th0=0.5, RESAMPLING_METHOD rm = SYSTEMATIC ) { |
---|
[477] | 61 | n = n0; |
---|
[638] | 62 | res_threshold = res_th0; |
---|
[477] | 63 | resmethod = rm; |
---|
| 64 | }; |
---|
[638] | 65 | void set_model ( shared_ptr<mpdf> par0, shared_ptr<mpdf> obs0) { |
---|
| 66 | par = par0; |
---|
| 67 | obs = obs0; |
---|
| 68 | // set values for posterior |
---|
| 69 | est.set_rv(par->_rv()); |
---|
| 70 | }; |
---|
[488] | 71 | void set_statistics ( const vec w0, const epdf &epdf0 ) { |
---|
[477] | 72 | est.set_statistics ( w0, epdf0 ); |
---|
| 73 | }; |
---|
[638] | 74 | void set_statistics ( const eEmp &epdf0 ) { |
---|
| 75 | bdm_assert_debug(epdf0._rv().equal(par->_rv()),"Incompatibel input"); |
---|
| 76 | est=epdf0; |
---|
| 77 | }; |
---|
[270] | 78 | //!@} |
---|
[33] | 79 | //! Set posterior density by sampling from epdf0 |
---|
[638] | 80 | //! Extends original BM::set_options by two more options: |
---|
| 81 | //! \li logweights - meaning that all weightes will be logged |
---|
| 82 | //! \li logsamples - all samples will be also logged |
---|
[283] | 83 | void set_options ( const string &opt ) { |
---|
[477] | 84 | BM::set_options ( opt ); |
---|
[281] | 85 | } |
---|
[638] | 86 | //! bayes I - generate samples and add their weights to lls |
---|
[665] | 87 | virtual void bayes_gensmp(const vec &ut); |
---|
[638] | 88 | //! bayes II - compute weights of the |
---|
| 89 | virtual void bayes_weights(); |
---|
| 90 | //! important part of particle filtering - decide if it is time to perform resampling |
---|
| 91 | virtual bool do_resampling(){ |
---|
| 92 | double eff = 1.0 / ( _w * _w ); |
---|
| 93 | return eff < ( res_threshold*n ); |
---|
| 94 | } |
---|
[32] | 95 | void bayes ( const vec &dt ); |
---|
[225] | 96 | //!access function |
---|
[638] | 97 | vec& __w() { return _w; } |
---|
| 98 | //!access function |
---|
| 99 | vec& _lls() { return lls; } |
---|
[660] | 100 | //!access function |
---|
[638] | 101 | RESAMPLING_METHOD _resmethod() const { return resmethod; } |
---|
[660] | 102 | //! return correctly typed posterior (covariant return) |
---|
[638] | 103 | const eEmp& posterior() const {return est;} |
---|
| 104 | |
---|
| 105 | /*! configuration structure for basic PF |
---|
| 106 | \code |
---|
| 107 | parameter_pdf = mpdf_class; // parameter evolution pdf |
---|
| 108 | observation_pdf = mpdf_class; // observation pdf |
---|
| 109 | prior = epdf_class; // prior probability density |
---|
| 110 | --- optional --- |
---|
| 111 | n = 10; // number of particles |
---|
| 112 | resmethod = 'systematic', or 'multinomial', or 'stratified' |
---|
| 113 | // resampling method |
---|
| 114 | res_threshold = 0.5; // resample when active particles drop below 50% |
---|
| 115 | \endcode |
---|
| 116 | */ |
---|
| 117 | void from_setting(const Setting &set){ |
---|
[665] | 118 | BM::from_setting(set); |
---|
[638] | 119 | par = UI::build<mpdf>(set,"parameter_pdf",UI::compulsory); |
---|
| 120 | obs = UI::build<mpdf>(set,"observation_pdf",UI::compulsory); |
---|
| 121 | |
---|
| 122 | prior_from_set(set); |
---|
| 123 | resmethod_from_set(set); |
---|
| 124 | // set resampling method |
---|
| 125 | //set drv |
---|
| 126 | //find potential input - what remains in rvc when we subtract rv |
---|
| 127 | RV u = par->_rvc().remove_time().subt( par->_rv() ); |
---|
| 128 | //find potential input - what remains in rvc when we subtract x_t |
---|
| 129 | RV obs_u = obs->_rvc().remove_time().subt( par->_rv() ); |
---|
| 130 | |
---|
| 131 | u.add(obs_u); // join both u, and check if they do not overlap |
---|
[665] | 132 | |
---|
[638] | 133 | set_drv(concat(obs->_rv(),u) ); |
---|
[477] | 134 | } |
---|
[638] | 135 | //! auxiliary function reading parameter 'resmethod' from configuration file |
---|
| 136 | void resmethod_from_set(const Setting &set){ |
---|
| 137 | string resmeth; |
---|
| 138 | if (UI::get(resmeth,set,"resmethod",UI::optional)){ |
---|
| 139 | if (resmeth=="systematic") { |
---|
| 140 | resmethod= SYSTEMATIC; |
---|
| 141 | } else { |
---|
| 142 | if (resmeth=="multinomial"){ |
---|
| 143 | resmethod=MULTINOMIAL; |
---|
| 144 | } else { |
---|
| 145 | if (resmeth=="stratified"){ |
---|
| 146 | resmethod= STRATIFIED; |
---|
| 147 | } else { |
---|
| 148 | bdm_error("Unknown resampling method"); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } else { |
---|
| 153 | resmethod=SYSTEMATIC; |
---|
| 154 | }; |
---|
| 155 | if(!UI::get(res_threshold, set, "res_threshold", UI::optional)){ |
---|
| 156 | res_threshold=0.5; |
---|
| 157 | } |
---|
[665] | 158 | validate(); |
---|
[638] | 159 | } |
---|
| 160 | //! load prior information from set and set internal structures accordingly |
---|
| 161 | void prior_from_set(const Setting & set){ |
---|
| 162 | shared_ptr<epdf> pri = UI::build<epdf>(set,"prior",UI::compulsory); |
---|
| 163 | |
---|
| 164 | eEmp *test_emp=dynamic_cast<eEmp*>(&(*pri)); |
---|
| 165 | if (test_emp) { // given pdf is sampled |
---|
| 166 | est=*test_emp; |
---|
| 167 | } else { |
---|
[665] | 168 | //int n; |
---|
[638] | 169 | if (!UI::get(n,set,"n",UI::optional)){n=10;} |
---|
| 170 | // sample from prior |
---|
| 171 | set_statistics(ones(n)/n, *pri); |
---|
| 172 | } |
---|
[665] | 173 | n = est._w().length(); |
---|
[638] | 174 | //validate(); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void validate(){ |
---|
| 178 | n=_w.length(); |
---|
| 179 | lls=zeros(n); |
---|
| 180 | if (par->_rv()._dsize()>0) { |
---|
| 181 | bdm_assert(par->_rv()._dsize()==est.dimension(),"Mismatch of RV and dimension of posterior" ); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | //! resample posterior density (from outside - see MPF) |
---|
| 185 | void resample(ivec &ind){ |
---|
| 186 | est.resample(ind,resmethod); |
---|
| 187 | } |
---|
[660] | 188 | //! access function |
---|
[653] | 189 | Array<vec>& __samples(){return _samples;} |
---|
[8] | 190 | }; |
---|
[638] | 191 | UIREGISTER(PF); |
---|
[8] | 192 | |
---|
| 193 | /*! |
---|
[32] | 194 | \brief Marginalized Particle filter |
---|
[8] | 195 | |
---|
[638] | 196 | A composition of particle filter with exact (or almost exact) bayesian models (BMs). |
---|
| 197 | The Bayesian models provide marginalized predictive density. Internaly this is achieved by virtual class MPFmpdf. |
---|
[8] | 198 | */ |
---|
| 199 | |
---|
[638] | 200 | class MPF : public BM { |
---|
[653] | 201 | protected: |
---|
[660] | 202 | //! particle filter on non-linear variable |
---|
[653] | 203 | shared_ptr<PF> pf; |
---|
[660] | 204 | //! Array of Bayesian models |
---|
[638] | 205 | Array<BM*> BMs; |
---|
[32] | 206 | |
---|
| 207 | //! internal class for MPDF providing composition of eEmp with external components |
---|
| 208 | |
---|
[477] | 209 | class mpfepdf : public epdf { |
---|
[660] | 210 | //! pointer to particle filter |
---|
[638] | 211 | shared_ptr<PF> &pf; |
---|
[660] | 212 | //! pointer to Array of BMs |
---|
[638] | 213 | Array<BM*> &BMs; |
---|
[8] | 214 | public: |
---|
[660] | 215 | //! constructor |
---|
[638] | 216 | mpfepdf (shared_ptr<PF> &pf0, Array<BM*> &BMs0): epdf(), pf(pf0), BMs(BMs0) { }; |
---|
| 217 | //! a variant of set parameters - this time, parameters are read from BMs and pf |
---|
| 218 | void read_parameters(){ |
---|
| 219 | rv = concat(pf->posterior()._rv(), BMs(0)->posterior()._rv()); |
---|
| 220 | dim = pf->posterior().dimension() + BMs(0)->posterior().dimension(); |
---|
| 221 | bdm_assert_debug(dim == rv._dsize(), "Wrong name "); |
---|
[283] | 222 | } |
---|
[32] | 223 | vec mean() const { |
---|
[638] | 224 | const vec &w = pf->posterior()._w(); |
---|
| 225 | vec pom = zeros ( BMs(0)->posterior ().dimension() ); |
---|
| 226 | //compute mean of BMs |
---|
| 227 | for ( int i = 0; i < w.length(); i++ ) { |
---|
| 228 | pom += BMs ( i )->posterior().mean() * w ( i ); |
---|
[477] | 229 | } |
---|
[638] | 230 | return concat ( pf->posterior().mean(), pom ); |
---|
[32] | 231 | } |
---|
[229] | 232 | vec variance() const { |
---|
[638] | 233 | const vec &w = pf->posterior()._w(); |
---|
| 234 | |
---|
| 235 | vec pom = zeros ( BMs(0)->posterior ().dimension() ); |
---|
| 236 | vec pom2 = zeros ( BMs(0)->posterior ().dimension() ); |
---|
| 237 | vec mea; |
---|
| 238 | |
---|
| 239 | for ( int i = 0; i < w.length(); i++ ) { |
---|
| 240 | // save current mean |
---|
| 241 | mea = BMs ( i )->posterior().mean(); |
---|
| 242 | pom += mea * w ( i ); |
---|
| 243 | //compute variance |
---|
| 244 | pom2 += ( BMs ( i )->posterior().variance() + pow ( mea, 2 ) ) * w ( i ); |
---|
[270] | 245 | } |
---|
[638] | 246 | return concat ( pf->posterior().variance(), pom2 - pow ( pom, 2 ) ); |
---|
[229] | 247 | } |
---|
[638] | 248 | |
---|
[477] | 249 | void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const { |
---|
[283] | 250 | //bounds on particles |
---|
| 251 | vec lbp; |
---|
| 252 | vec ubp; |
---|
[638] | 253 | pf->posterior().qbounds ( lbp, ubp ); |
---|
[32] | 254 | |
---|
[283] | 255 | //bounds on Components |
---|
[638] | 256 | int dimC = BMs ( 0 )->posterior().dimension(); |
---|
[283] | 257 | int j; |
---|
| 258 | // temporary |
---|
[477] | 259 | vec lbc ( dimC ); |
---|
| 260 | vec ubc ( dimC ); |
---|
[283] | 261 | // minima and maxima |
---|
[477] | 262 | vec Lbc ( dimC ); |
---|
| 263 | vec Ubc ( dimC ); |
---|
[283] | 264 | Lbc = std::numeric_limits<double>::infinity(); |
---|
| 265 | Ubc = -std::numeric_limits<double>::infinity(); |
---|
| 266 | |
---|
[638] | 267 | for ( int i = 0; i < BMs.length(); i++ ) { |
---|
[283] | 268 | // check Coms |
---|
[638] | 269 | BMs ( i )->posterior().qbounds ( lbc, ubc ); |
---|
| 270 | //save either minima or maxima |
---|
[477] | 271 | for ( j = 0; j < dimC; j++ ) { |
---|
| 272 | if ( lbc ( j ) < Lbc ( j ) ) { |
---|
| 273 | Lbc ( j ) = lbc ( j ); |
---|
| 274 | } |
---|
| 275 | if ( ubc ( j ) > Ubc ( j ) ) { |
---|
| 276 | Ubc ( j ) = ubc ( j ); |
---|
| 277 | } |
---|
[283] | 278 | } |
---|
| 279 | } |
---|
[477] | 280 | lb = concat ( lbp, Lbc ); |
---|
| 281 | ub = concat ( ubp, Ubc ); |
---|
[283] | 282 | } |
---|
| 283 | |
---|
[477] | 284 | vec sample() const { |
---|
[565] | 285 | bdm_error ( "Not implemented" ); |
---|
| 286 | return vec(); |
---|
[477] | 287 | } |
---|
[32] | 288 | |
---|
[477] | 289 | double evallog ( const vec &val ) const { |
---|
[565] | 290 | bdm_error ( "not implemented" ); |
---|
[477] | 291 | return 0.0; |
---|
| 292 | } |
---|
[32] | 293 | }; |
---|
| 294 | |
---|
[281] | 295 | //! Density joining PF.est with conditional parts |
---|
[32] | 296 | mpfepdf jest; |
---|
| 297 | |
---|
[281] | 298 | //! Log means of BMs |
---|
| 299 | bool opt_L_mea; |
---|
[283] | 300 | |
---|
[32] | 301 | public: |
---|
| 302 | //! Default constructor. |
---|
[638] | 303 | MPF () : jest (pf,BMs) {}; |
---|
[660] | 304 | //! set all parameters at once |
---|
[638] | 305 | void set_parameters ( shared_ptr<mpdf> par0, shared_ptr<mpdf> obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { |
---|
| 306 | pf->set_model ( par0, obs0); |
---|
| 307 | pf->set_parameters(n0, rm ); |
---|
[283] | 308 | BMs.set_length ( n0 ); |
---|
| 309 | } |
---|
[660] | 310 | //! set a prototype of BM, copy it to as many times as there is particles in pf |
---|
[638] | 311 | void set_BM ( const BM &BMcond0 ) { |
---|
[32] | 312 | |
---|
[638] | 313 | int n=pf->__w().length(); |
---|
| 314 | BMs.set_length(n); |
---|
[283] | 315 | // copy |
---|
[638] | 316 | //BMcond0 .condition ( pf->posterior()._sample ( 0 ) ); |
---|
[477] | 317 | for ( int i = 0; i < n; i++ ) { |
---|
[638] | 318 | BMs ( i ) = BMcond0._copy_(); |
---|
[477] | 319 | } |
---|
[32] | 320 | }; |
---|
| 321 | |
---|
| 322 | void bayes ( const vec &dt ); |
---|
[477] | 323 | const epdf& posterior() const { |
---|
| 324 | return jest; |
---|
| 325 | } |
---|
[638] | 326 | //! Extends options understood by BM::set_options by option |
---|
| 327 | //! \li logmeans - meaning |
---|
[283] | 328 | void set_options ( const string &opt ) { |
---|
[638] | 329 | BM::set_options(opt); |
---|
[477] | 330 | opt_L_mea = ( opt.find ( "logmeans" ) != string::npos ); |
---|
[32] | 331 | } |
---|
[283] | 332 | |
---|
[225] | 333 | //!Access function |
---|
[536] | 334 | const BM* _BM ( int i ) { |
---|
[477] | 335 | return BMs ( i ); |
---|
| 336 | } |
---|
[638] | 337 | |
---|
| 338 | /*! configuration structure for basic PF |
---|
| 339 | \code |
---|
| 340 | BM = BM_class; // Bayesian filtr for analytical part of the model |
---|
| 341 | parameter_pdf = mpdf_class; // transitional pdf for non-parametric part of the model |
---|
| 342 | prior = epdf_class; // prior probability density |
---|
| 343 | --- optional --- |
---|
| 344 | n = 10; // number of particles |
---|
| 345 | resmethod = 'systematic', or 'multinomial', or 'stratified' |
---|
| 346 | // resampling method |
---|
| 347 | \endcode |
---|
| 348 | */ |
---|
| 349 | void from_setting(const Setting &set){ |
---|
| 350 | shared_ptr<mpdf> par = UI::build<mpdf>(set,"parameter_pdf",UI::compulsory); |
---|
| 351 | shared_ptr<mpdf> obs= new mpdf(); // not used!! |
---|
[8] | 352 | |
---|
[638] | 353 | pf = new PF; |
---|
| 354 | // rpior must be set before BM |
---|
| 355 | pf->prior_from_set(set); |
---|
| 356 | pf->resmethod_from_set(set); |
---|
| 357 | pf->set_model(par,obs); |
---|
| 358 | |
---|
| 359 | shared_ptr<BM> BM0 =UI::build<BM>(set,"BM",UI::compulsory); |
---|
| 360 | set_BM(*BM0); |
---|
| 361 | |
---|
| 362 | string opt; |
---|
| 363 | if (UI::get(opt,set,"options",UI::optional)){ |
---|
| 364 | set_options(opt); |
---|
| 365 | } |
---|
| 366 | //set drv |
---|
| 367 | //find potential input - what remains in rvc when we subtract rv |
---|
| 368 | RV u = par->_rvc().remove_time().subt( par->_rv() ); |
---|
| 369 | set_drv(concat(BM0->_drv(),u) ); |
---|
| 370 | validate(); |
---|
[32] | 371 | } |
---|
[638] | 372 | void validate(){ |
---|
| 373 | try{ |
---|
| 374 | pf->validate(); |
---|
| 375 | } catch (std::exception &e){ |
---|
| 376 | throw UIException("Error in PF part of MPF:"); |
---|
| 377 | } |
---|
| 378 | jest.read_parameters(); |
---|
[660] | 379 | for ( int i = 0; i < pf->__w().length(); i++ ) { |
---|
| 380 | BMs ( i )->condition ( pf->posterior()._sample ( i ) ); |
---|
| 381 | } |
---|
[32] | 382 | } |
---|
[675] | 383 | |
---|
[638] | 384 | }; |
---|
| 385 | UIREGISTER(MPF); |
---|
[32] | 386 | |
---|
| 387 | } |
---|
[8] | 388 | #endif // KF_H |
---|
| 389 | |
---|