Changeset 665
- Timestamp:
- 10/19/09 22:24:45 (15 years ago)
- Location:
- library/bdm
- Files:
-
- 11 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r660 r665 149 149 friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); 150 150 151 string to_string() {ostringstream o; o <<this; return o.str();}151 string to_string() const {ostringstream o; o << *this; return o.str();} 152 152 153 153 //! total size of a random variable … … 303 303 //! Length of the output vector 304 304 int dimy; 305 //! Length of the input vector 306 int dimc; 305 307 public: 306 308 //!default constructor … … 317 319 int dimension() const { 318 320 return dimy; 321 } 322 //! access function 323 int dimensionc() const { 324 return dimc; 319 325 } 320 326 }; … … 671 677 return downsize; 672 678 } 679 //! for future use 680 virtual ~datalink(){} 673 681 }; 674 682 … … 1219 1227 } 1220 1228 string opt; 1221 if ( !UI::get ( opt, set, "options", UI::optional ) ) {1229 if ( UI::get ( opt, set, "options", UI::optional ) ) { 1222 1230 set_options ( opt ); 1223 1231 } -
library/bdm/base/datasources.cpp
r660 r665 118 118 time = 0; 119 119 //rowid and delays are ignored 120 120 rowid = linspace(0,Data.rows()-1); 121 121 set_drv ( *rvtmp, RV() ); 122 122 } -
library/bdm/base/user_info.h
r659 r665 575 575 //!@} 576 576 577 //! for future use 578 virtual ~UI(){} 577 579 }; 578 580 -
library/bdm/bdmroot.h
r477 r665 33 33 34 34 //! This method returns a basic info about the current instance 35 virtual string to_string() {35 virtual string to_string() const { 36 36 return ""; 37 37 } -
library/bdm/estim/arx.cpp
r639 r665 6 6 7 7 if ( frg < 1.0 ) { 8 est.pow ( frg ); 8 est.pow ( frg ); // multiply V and nu 9 9 10 10 //stabilize 11 ldmat V0(eye(V.rows())); 12 V0*=(1-frg)*1e-3; 11 ldmat V0=alter_est._V(); //$ copy 12 double &nu0=alter_est._nu(); 13 V0*=(1-frg); 13 14 V += V0; //stabilization 14 nu +=(1-frg)* (0.1 + V.rows() + 1* dimx + 2);15 16 // recompute loglikelihood of "prior"15 nu +=(1-frg)*nu0; 16 17 // recompute loglikelihood of new "prior" 17 18 if ( evalll ) { 18 19 last_lognc = est.lognc(); … … 219 220 int rgrlen = rrv->_dsize(); 220 221 222 dimx = ylen; 221 223 set_rv ( *yrv, *rrv ); 222 224 … … 234 236 235 237 //init 236 mat V0; 237 vec dV0; 238 if (!UI::get(V0, set, "V0",UI::optional)){ 239 if ( !UI::get ( dV0, set, "dV0" ) ) 240 dV0 = concat ( 1e-3 * ones ( ylen ), 1e-5 * ones ( rgrlen ) ); 241 V0 = diag ( dV0 ); 242 } 243 double nu0; 244 if ( !UI::get ( nu0, set, "nu0" ) ) 245 nu0 = rgrlen + ylen + 2; 238 shared_ptr<egiw> pri=UI::build<egiw>(set, "prior", UI::optional); 239 if (pri) { 240 bdm_assert(pri->_dimx()==ylen,"prior is not compatible"); 241 bdm_assert(pri->_V().rows()==ylen+rgrlen,"prior is not compatible"); 242 est.set_parameters( pri->_dimx(),pri->_V(), pri->_nu()); 243 }else{ 244 est.set_parameters( ylen, zeros(ylen+rgrlen)); 245 set_prior_default(est); 246 } 247 248 shared_ptr<egiw> alt=UI::build<egiw>(set, "alternative", UI::optional); 249 if (alt) { 250 bdm_assert(alt->_dimx()==ylen,"alternative is not compatible"); 251 bdm_assert(alt->_V().rows()==ylen+rgrlen,"alternative is not compatible"); 252 alter_est.set_parameters( alt->_dimx(),alt->_V(), alt->_nu()); 253 } else { 254 alter_est = est; 255 } 246 256 247 257 double frg; … … 250 260 251 261 set_parameters ( frg ); 252 set_statistics ( ylen, V0, nu0 );253 262 254 263 //name results (for logging) -
library/bdm/estim/arx.h
r660 r665 162 162 163 163 --- optional --- 164 V0 = [1 0;0 1]; // Initial value of information matrix V 165 --- OR --- 166 dV0 = [1e-3, 1e-5, 1e-5, 1e-5]; // Initial value of diagonal of information matrix V 167 // default: 1e-3 for rv, 1e-5 for rgr 168 nu0 = 6; // initial value of nu, default: rgrlen + 2 164 prior = {class='egiw',...}; // Prior density, when given default is used instead 165 alternative = {class='egiw',...}; // Alternative density in stabilized estimation, when not given prior is used 166 169 167 frg = 1.0; // forgetting, default frg=1.0 170 168 … … 178 176 bdm_assert(dimx == _yrv._dsize(), "RVs of parameters and regressor do not match"); 179 177 178 } 179 //! function sets prior and alternative density 180 void set_prior(const RV &drv, egiw &prior){ 181 //TODO check ranges in RV and build prior 182 }; 183 //! build default prior and alternative when all values are set 184 void set_prior_default(egiw &prior){ 185 //assume 186 vec dV0(prior._V().rows()); 187 dV0.set_subvector(0,prior._dimx()-1, 1.0); 188 dV0.set_subvector(prior._dimx(),dV0.length()-1, 1e-5); 189 190 prior.set_parameters(prior._dimx(),ldmat(dV0)); 180 191 } 181 192 }; -
library/bdm/estim/particles.cpp
r663 r665 5 5 using std::endl; 6 6 7 void PF::bayes_gensmp(){ 8 #pragma parallel for 9 for (int i = 0; i < n; i++ ) { 10 _samples ( i ) = par->samplecond ( _samples ( i ) ); 11 lls(i) = 0; 7 void PF::bayes_gensmp(const vec &ut){ 8 if (ut.length()>0){ 9 vec cond(par->dimensionc()); 10 cond.set_subvector(par->dimension(),ut); 11 #pragma parallel for 12 for (int i = 0; i < n; i++ ) { 13 cond.set_subvector(0,_samples ( i )); 14 _samples ( i ) = par->samplecond ( cond ); 15 lls(i) = 0; 16 } 17 } else { 18 #pragma parallel for 19 for (int i = 0; i < n; i++ ) { 20 _samples ( i ) = par->samplecond ( _samples ( i ) ); 21 lls(i) = 0; 22 } 12 23 } 13 24 } … … 28 39 } 29 40 sw = sum(_w); 30 if (!std::isfinite(sw) ) {41 if (!std::isfinite(sw) || sw==0.0) { 31 42 bdm_error("Particle filter is lost; no particle is good enough."); 32 43 } … … 36 47 37 48 void PF::bayes ( const vec &dt ) { 49 vec yt=dt.left(obs->dimension()); 50 vec ut=dt.get(obs->dimension(),dt.length()-1); 51 38 52 int i; 39 53 // generate samples - time step 40 bayes_gensmp( );54 bayes_gensmp(ut); 41 55 // weight them - data step 42 56 for ( i = 0; i < n; i++ ) { 43 lls ( i ) += obs->evallogcond ( dt, _samples ( i ) ); //+= because lls may have something from gensmp!57 lls ( i ) += obs->evallogcond ( yt, _samples ( i ) ); //+= because lls may have something from gensmp! 44 58 } 45 59 … … 61 75 62 76 void MPF::bayes ( const vec &dt ) { 63 // follows PF::bayes in most places!!! 64 77 // follows PF::bayes in most places!!! 65 78 int i; 66 79 int n=pf->__w().length(); … … 68 81 69 82 // generate samples - time step 70 pf->bayes_gensmp( );83 pf->bayes_gensmp(vec(0)); 71 84 // weight them - data step 72 85 #pragma parallel for -
library/bdm/estim/particles.h
r660 r665 93 93 } 94 94 //! bayes I - generate samples and add their weights to lls 95 virtual void bayes_gensmp( );95 virtual void bayes_gensmp(const vec &ut); 96 96 //! bayes II - compute weights of the 97 97 virtual void bayes_weights(); … … 124 124 */ 125 125 void from_setting(const Setting &set){ 126 BM::from_setting(set); 126 127 par = UI::build<mpdf>(set,"parameter_pdf",UI::compulsory); 127 128 obs = UI::build<mpdf>(set,"observation_pdf",UI::compulsory); … … 137 138 138 139 u.add(obs_u); // join both u, and check if they do not overlap 139 140 140 141 set_drv(concat(obs->_rv(),u) ); 141 142 } … … 163 164 res_threshold=0.5; 164 165 } 166 validate(); 165 167 } 166 168 //! load prior information from set and set internal structures accordingly … … 172 174 est=*test_emp; 173 175 } else { 174 int n;176 //int n; 175 177 if (!UI::get(n,set,"n",UI::optional)){n=10;} 176 178 // sample from prior 177 179 set_statistics(ones(n)/n, *pri); 178 180 } 181 n = est._w().length(); 179 182 //validate(); 180 183 } -
library/bdm/math/functions.h
r620 r665 84 84 vec eval ( const vec &cond ) { 85 85 bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." ); 86 return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab) 86 if (dimu>0){ 87 return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab) 88 } else { 89 return eval ( cond ( 0, dimx - 1 ), vec(0) );//-1 = end (in matlab) 90 } 91 87 92 } 88 93 -
library/bdm/stat/exp_family.cpp
r637 r665 82 82 83 83 // bdm_assert_debug ( ( ( -nkG - nkW ) > -Inf ) && ( ( -nkG - nkW ) < Inf ), "ARX improper" ); 84 if (-nkG - nkW==Inf){85 cout << "??" <<endl;86 }84 if (-nkG - nkW==Inf){ 85 cout << "??" <<endl; 86 } 87 87 return -nkG - nkW; 88 88 } -
library/bdm/stat/exp_family.h
r660 r665 244 244 double& _nu() {return nu;} 245 245 const double& _nu() const {return nu;} 246 const int & _dimx() const {return dimx;} 246 247 /*! Create Gauss-inverse-Wishart density 247 248 \f[ f(rv) = GiW(V,\nu) \f] … … 250 251 class = 'egiw'; 251 252 V = []; // square matrix 253 dV = []; // vector of diagonal of V (when V not given) 252 254 nu = []; // scalar \nu ((almost) degrees of freedom) 253 255 // when missing, it will be computed to obtain proper pdf … … 260 262 void from_setting (const Setting &set) { 261 263 epdf::from_setting(set); 262 if (!UI::get (nu, set, "nu", UI::compulsory)) {nu=-1;}263 264 UI::get (dimx, set, "dimx", UI::compulsory); 265 if (!UI::get (nu, set, "nu", UI::optional)) {nu=-1;} 264 266 mat V; 265 UI::get (V, set, "V", UI::compulsory); 266 set_parameters (dimx, V, nu); 267 if (!UI::get (V, set, "V", UI::optional)){ 268 vec dV; 269 UI::get (dV, set, "dV", UI::compulsory); 270 set_parameters (dimx, ldmat(dV), nu); 271 272 } else { 273 set_parameters (dimx, V, nu); 274 } 275 } 276 void validate(){ 277 // check sizes, rvs etc. 267 278 } 268 279 //!@} … … 594 605 595 606 double evallog (const vec &val) const { 596 if (any (val < low) && any (val > high)) {return inf;}607 if (any (val < low) && any (val > high)) {return -inf;} 597 608 else return lnk; 598 609 } … … 632 643 UIREGISTER(euni); 633 644 645 //! Uniform density with conditional mean value 646 class mguni : public mpdf_internal<euni>{ 647 //! function of the mean value 648 shared_ptr<fnc> mean; 649 //! distance from mean to both sides 650 vec delta; 651 public: 652 void condition(const vec &cond){ 653 vec mea=mean->eval(cond); 654 iepdf.set_parameters(mea-delta,mea+delta); 655 } 656 //! load from 657 void from_setting(const Setting &set){ 658 mpdf::from_setting(set); //reads rv and rvc 659 UI::get(delta,set,"delta",UI::compulsory); 660 mean = UI::build<fnc>(set,"mean",UI::compulsory); 661 662 iepdf.set_parameters(-delta,delta); 663 dimc = mean->dimensionc(); 664 validate(); 665 } 666 }; 667 UIREGISTER(mguni); 634 668 /*! 635 669 \brief Normal distributed linear function with linear function of mean value;