Changeset 536
- Timestamp:
- 08/16/09 18:13:31 (15 years ago)
- Location:
- library
- Files:
-
- 17 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r532 r536 29 29 30 30 typedef std::map<string, int> RVmap; 31 //! Internal global variable storing sizes of RVs 31 32 extern ivec RV_SIZES; 33 //! Internal global variable storing names of RVs 32 34 extern Array<string> RV_NAMES; 33 35 … … 160 162 } 161 163 //!@} 162 163 //TODO why not inline and later??164 164 165 165 //! \name Algebra on Random Variables … … 192 192 //!@{ 193 193 194 //! generate \c str from rv, by expanding sizes TODO to_string..194 //! generate \c str from rv, by expanding sizes 195 195 str tostr() const; 196 196 //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. … … 223 223 224 224 // TODO dodelat void to_setting( Setting &set ) const; 225 226 225 //! Invalidate all named RVs. Use before initializing any RV instances, with care... 227 226 static void clear_all(); … … 391 390 SHAREDPTR(epdf); 392 391 393 //! Conditional probability density, e.g. modeling some dependencies. 394 //TODO Samplecond can be generalized 392 //! Conditional probability density, e.g. modeling \f$ f( x | y) \f$, where \f$ x \f$ is random variable, \c rv, and \f$ y \f$ is conditioning variable, \c rvc. 395 393 class mpdf : public root 396 394 { … … 405 403 406 404 protected: 405 //! set internal pointer \c ep to point to given \c iepdf 407 406 void set_ep (epdf &iepdf) { 408 407 ep = &iepdf; 409 408 } 409 //! set internal pointer \c ep to point to given \c iepdf 410 410 void set_ep (epdf *iepdfp) { 411 411 ep = iepdfp; … … 487 487 SHAREDPTR(mpdf); 488 488 489 //! Mpdf with internal epdf that is modified by function \c condition 489 490 template <class EPDF> 490 491 class mpdf_internal: public mpdf 491 492 { 492 493 protected : 494 //! Internal epdf used for sampling 493 495 EPDF iepdf; 494 496 public: … … 555 557 //! Constructor 556 558 datalink() : downsize (0), upsize (0) { } 559 //! Conevnience constructor 557 560 datalink (const RV &rv, const RV &rv_up) { 558 561 set_connection (rv, rv_up); … … 607 610 //! Constructor 608 611 datalink_m2e() : condsize (0) { } 609 612 //! Set connection between vectors 610 613 void set_connection (const RV &rv, const RV &rvc, const RV &rv_up) { 611 614 datalink::set_connection (rv, rv_up); … … 621 624 return tmp; 622 625 } 623 626 //! Copy corresponding values to Up.condition 624 627 void pushup_cond (vec &val_up, const vec &val, const vec &cond) { 625 628 it_assert_debug (downsize == val.length(), "Wrong val"); … … 643 646 //! Constructor 644 647 datalink_m2m() {}; 648 //! Set connection between the vectors 645 649 void set_connection (const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up) { 646 650 datalink_m2e::set_connection (rv, rvc, rv_up); … … 713 717 class mepdf : public mpdf 714 718 { 715 719 //! Internal shared pointer to epdf 716 720 shared_ptr<epdf> iepdf; 717 721 public: 718 722 //!Default constructor 719 723 mepdf() { } 720 724 //! Set internal shared pointer 721 725 mepdf (shared_ptr<epdf> em) { 722 726 iepdf = em; … … 893 897 return NULL; 894 898 }; 895 //!Constructs a conditional density 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t})899 //!Constructs conditional density of 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) \f$ 896 900 virtual mpdf* predictor() const { 897 901 it_error ("Not implemented"); … … 939 943 } 940 944 virtual const epdf& posterior() const = 0; 941 virtual const epdf* _e() const = 0;942 945 //!@} 943 946 -
library/bdm/estim/arx.h
r529 r536 110 110 //!\name Access attributes 111 111 //!@{ 112 const egiw* _e() const {113 return &est ;114 };115 112 const egiw& posterior() const { 116 113 return est; -
library/bdm/estim/kalman.h
r529 r536 125 125 return est; 126 126 } 127 const enorm<sq_T>* _e() const {128 return &est;129 }130 127 //!access function 131 128 mat& __K() { … … 214 211 return E; 215 212 }; 216 const enorm<fsqmat>* _e() const {217 return &E;218 };219 213 const mat _R() { 220 214 return P; … … 277 271 // TODO dodelat void to_setting( Setting &set ) const; 278 272 273 const enorm<chmat>& posterior() {return est;} 279 274 }; 280 275 … … 423 418 424 419 est.set_rv ( RV ( "MM", A ( 0 )->posterior().dimension(), 0 ) ); 425 est.set_parameters ( A ( 0 )-> _e()->mean(), A ( 0 )->_e()->_R() );420 est.set_parameters ( A ( 0 )->posterior().mean(), A ( 0 )->posterior()._R() ); 426 421 } 427 422 void bayes ( const vec &dt ) { … … 439 434 case 1: { 440 435 int mi = max_index ( w ); 441 const enorm<chmat> * st = ( Models ( mi )->_e() );442 est.set_parameters ( st ->mean(), st->_R() );436 const enorm<chmat> &st = Models ( mi )->posterior() ; 437 est.set_parameters ( st.mean(), st._R() ); 443 438 } 444 439 break; … … 451 446 } 452 447 } 453 //all posterior densities are equal => return the first one 454 const enorm<chmat>* _e() const { 455 return &est; 456 } 457 //all posterior densities are equal => return the first one 448 //! posterior density 458 449 const enorm<chmat>& posterior() const { 459 450 return est; -
library/bdm/estim/mixtures.h
r477 r536 21 21 namespace bdm { 22 22 23 //! enum switch for internal approximation used in MixEF 23 24 enum MixEF_METHOD { EM = 0, QB = 1}; 24 25 … … 118 119 return *est; 119 120 } 120 const eprod* _e() const {121 return est;122 }123 121 emix* epredictor() const; 124 122 //! Flatten the density as if it was not estimated from the data -
library/bdm/estim/particles.h
r488 r536 111 111 dim = E.dimension() + A ( 0 )->posterior().dimension(); 112 112 for ( int i = 0; i < _w.length() ; i++ ) { 113 Coms ( i ) = A ( i )->_e();113 Coms ( i ) = &(A ( i )->posterior()); 114 114 } 115 115 } … … 218 218 return jest; 219 219 } 220 const epdf* _e() const {221 return &jest; //Fixme: is it useful?222 }223 220 //! Set postrior of \c rvc to samples from epdf0. Statistics of BMs are not re-computed! Use only for initialization! 224 221 /* void set_est ( const epdf& epdf0 ) { … … 234 231 235 232 //!Access function 236 BM* _BM ( int i ) {233 const BM* _BM ( int i ) { 237 234 return BMs ( i ); 238 235 } -
library/bdm/math/square_mat.cpp
r495 r536 64 64 65 65 ldmat::ldmat ( const mat &V ) : sqmat ( V.cols() ) { 66 //TODO check if correct!! Based on heuristic observation of lu()67 66 68 67 it_assert_debug ( dim == V.rows(), "ldmat::ldmat matrix V is not square!" ); 69 68 70 69 // L and D will be allocated by ldform() 71 72 70 //Chol is unstable 73 71 this->ldform ( chol ( V ), ones ( dim ) ); 74 // this->ldform(ul(V),ones(dim));75 72 } 76 73 -
library/bdm/mex/config2mxstruct.h
r477 r536 8 8 using namespace libconfig; 9 9 10 //! Reimplementation of libconfig's Config class for Matlab mxArray structures 10 11 class UImxConfig : public Config { 11 12 public: 13 //! Matlab structure where the info is stored 12 14 mxArray *mxconfig; 15 //! Load file in libconfig syntax to Matlab arrays 13 16 UImxConfig ( const char * filename ) { 14 17 Config config; … … 16 19 mxconfig = group2mxstruct ( config.getRoot() ); 17 20 } 21 //! Convert existing Setting to Matlab arrays 18 22 UImxConfig ( const Setting &setting ) { 19 23 mxconfig = group2mxstruct ( setting ); … … 21 25 22 26 private: 23 27 //! Convert libconfig's array to Matlab vector 24 28 mxArray* array2mxvector ( const Setting &setting ) { 25 29 if ( !setting.isArray() ) mexErrMsgTxt ( "Given setting is not an array" ); … … 40 44 } 41 45 46 //! Convert libconfig's array to Matlab matrix 42 47 mxArray* list2mxmatrix ( const Setting &setting ) { 43 48 if ( !setting.isList() || ( "matrix" != setting[0] ) ) … … 58 63 return result; 59 64 } 60 65 66 //! Convert libconfig's gourp to Matlab structure 61 67 mxArray* group2mxstruct ( const Setting &setting ) { 62 68 if ( !setting.isGroup() ) mexErrMsgTxt ( "Given setting is not a group." ); … … 102 108 103 109 } 104 110 //! Convert libconfig's list to Matlab cell 105 111 mxArray* list2mxcell ( const Setting &setting ) { 106 112 if ( !setting.isList() ) mexErrMsgTxt ( "Given setting is not a list." ); -
library/bdm/mex/mex_logger.h
r529 r536 37 37 38 38 } 39 40 //! Logger storing results into an mxArray 39 41 class mexlog : public memlog { 40 42 public: -
library/bdm/mex/mex_parser.h
r477 r536 8 8 using namespace libconfig; 9 9 10 //! Class for writing Settings into Matlab's mxArray 10 11 class UImxArray : public Config { 11 12 public: 13 //! Build an instance of Config with fields filled from the given \a mxarray 12 14 UImxArray ( const mxArray *mxarray ) : Config() { 13 15 Setting & setting = this->getRoot(); //setting is a group … … 22 24 setAutoConvert ( true ); 23 25 } 26 //! Add libconfig's \c list to the structure 24 27 void addList ( const mxArray *mxarray, const char* name ) { 25 28 Setting & setting = this->getRoot(); //setting is a group … … 27 30 fillList ( child, mxarray ); 28 31 } 32 //! Add libconfig's \c group to the structure 29 33 void addGroup ( const mxArray *mxarray, const char* name ) { 30 34 Setting & setting = this->getRoot(); //setting is a group … … 32 36 fillGroup ( child, mxarray ); 33 37 } 38 //! Operator for more convenient access to this Confic 34 39 operator Setting&() { 35 40 return getRoot(); -
library/bdm/shared_ptr.h
r529 r536 171 171 }; 172 172 173 //! Compare shared pointers 173 174 template<typename T, typename U> 174 175 bool operator== ( shared_ptr<T> const &a, shared_ptr<U> const &b ) { … … 176 177 } 177 178 179 //! Compare shared pointers 178 180 template<typename T, typename U> 179 181 bool operator!= ( shared_ptr<T> const &a, shared_ptr<U> const &b ) { … … 181 183 } 182 184 185 //! Compare shared pointers 183 186 template<typename T, typename U> 184 187 bool operator< ( shared_ptr<T> const &a, shared_ptr<U> const &b ) { -
library/bdm/stat/emix.h
r529 r536 165 165 166 166 shared_ptr<epdf> marginal ( const RV &rv ) const; 167 //! Update already existing marginal density \c target 167 168 void marginal ( const RV &rv, emix &target ) const; 168 169 shared_ptr<mpdf> condition ( const RV &rv ) const; … … 290 291 set_elements ( mFacs ); 291 292 } 292 293 //! Set internal \c mpdfs from given values 293 294 void set_elements (const Array<shared_ptr<mpdf> > &mFacs ); 294 295 … … 339 340 return smp; 340 341 } 341 mat samplecond ( const vec &cond, int N ) {342 mat Smp ( dimension(), N );343 for ( int i = 0; i < N; i++ ) {344 Smp.set_col ( i, samplecond ( cond ) );345 }346 return Smp;347 }348 342 349 343 //! Load from structure with elements: … … 372 366 Array<datalink*> dls; 373 367 public: 368 //! Default constructor 374 369 eprod () : epdfs ( 0 ), dls ( 0 ) {}; 370 //! Set internal 375 371 void set_parameters ( const Array<const epdf*> &epdfs0, bool named = true ) { 376 372 epdfs = epdfs0;//.set_length ( epdfs0.length() ); … … 470 466 //! Set weights \c w and components \c R 471 467 void set_parameters ( const vec &w0, const Array<shared_ptr<mpdf> > &Coms0 ) { 472 //!\ TODOcheck if all components are OK468 //!\todo check if all components are OK 473 469 Coms = Coms0; 474 470 w=w0; -
library/bdm/stat/exp_family.h
r535 r536 44 44 //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ 45 45 virtual double lognc() const = 0; 46 //!TODO decide if it is really needed47 virtual void dupdate (mat &v) {it_error ("Not implemented");};48 46 //!Evaluate normalized log-probability 49 47 virtual double evallog_nn (const vec &val) const{it_error ("Not implemented");return 0.0;}; … … 220 218 ldmat est_theta_cov() const; 221 219 220 //! expected values of the linear coefficient and the covariance matrix are written to \c M and \c R , respectively 222 221 void mean_mat (mat &M, mat&R) const; 223 222 //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] … … 340 339 if (evalll) {last_lognc = est.lognc();} 341 340 } 342 const epdf& posterior() const {return est;}; 343 const eDirich* _e() const {return &est;}; 341 //! reimplemnetation of BM::posterior() 342 const eDirich& posterior() const {return est;}; 343 //! constructor function 344 344 void set_parameters (const vec &beta0) { 345 345 est.set_parameters (beta0); … … 373 373 374 374 vec sample() const; 375 //! TODO: is it used anywhere?376 // mat sample ( int N ) const;377 375 double evallog (const vec &val) const; 378 376 double lognc () const; 379 //! Returns poi ter to alpha and beta. Potentially dengerous: use with care!377 //! Returns pointer to internal alpha. Potentially dengerous: use with care! 380 378 vec& _alpha() {return alpha;} 379 //! Returns pointer to internal beta. Potentially dengerous: use with care! 381 380 vec& _beta() {return beta;} 382 381 vec mean() const {return elem_div (alpha, beta);} … … 482 481 //!@} 483 482 484 double eval (const vec &val) const {return nk;}485 483 double evallog (const vec &val) const { 486 484 if (any (val < low) && any (val > high)) {return inf;} … … 516 514 \brief Normal distributed linear function with linear function of mean value; 517 515 518 Mean value \f$ mu=A*rvc+mu_0\f$.516 Mean value \f$ \mu=A*\mbox{rvc}+\mu_0 \f$. 519 517 */ 520 518 template < class sq_T, template <typename> class TEpdf = enorm > … … 524 522 //! Internal epdf that arise by conditioning on \c rvc 525 523 mat A; 524 //! Constant additive term 526 525 vec mu_const; 527 526 // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? … … 558 557 mat _R() { return this->iepdf._R().to_mat(); } 559 558 559 //! Debug stream 560 560 template<typename sq_M> 561 561 friend std::ostream &operator<< (std::ostream &os, mlnorm<sq_M, enorm> &ml); … … 649 649 { 650 650 protected: 651 //! Variable \f$ \Lambda \f$ from theory 651 652 ldmat Lambda; 653 //! Reference to variable \f$ R \f$ 652 654 ldmat &_R; 655 //! Variable \f$ R_e \f$ 653 656 ldmat Re; 654 657 public: 655 658 mlstudent () : mlnorm<ldmat, enorm> (), 656 659 Lambda (), _R (iepdf._R()) {} 660 //! constructor function 657 661 void set_parameters (const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) { 658 662 it_assert_debug (A0.rows() == mu0.length(), ""); … … 968 972 double delta; 969 973 public: 974 //! Set internal structures 970 975 void set_parameters (const mat &Y0, const double delta0) {Y = chmat (Y0);delta = delta0; p = Y.rows(); dim = p * p; } 976 //! Sample matrix argument 971 977 mat sample_mat() const { 972 978 mat X = zeros (p, p); … … 998 1004 }; 999 1005 1006 //! Inverse Wishart on Choleski decomposition 1007 /*! Being computed by conversion from `standard' Wishart 1008 */ 1000 1009 class eiWishartCh: public epdf 1001 1010 { 1002 1011 protected: 1012 //! Internal instance of Wishart density 1003 1013 eWishartCh W; 1014 //! size of Ch 1004 1015 int p; 1016 //! parameter delta 1005 1017 double delta; 1006 1018 public: 1019 //! constructor function 1007 1020 void set_parameters (const mat &Y0, const double delta0) { 1008 1021 delta = delta0; … … 1011 1024 } 1012 1025 vec sample() const {mat iCh; iCh = inv (W.sample_mat()); return vec (iCh._data(), dim);} 1026 //! access function 1013 1027 void _setY (const vec &y0) { 1014 1028 mat Ch (p, p); … … 1044 1058 }; 1045 1059 1060 //! Random Walk on inverse Wishart 1046 1061 class rwiWishartCh : public mpdf_internal<eiWishartCh> 1047 1062 { … … 1049 1064 //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions 1050 1065 double sqd; 1051 // reference point for diagonal1066 //!reference point for diagonal 1052 1067 vec refl; 1068 //! power of the reference 1053 1069 double l; 1070 //! dimension 1054 1071 int p; 1055 1072 1056 1073 public: 1057 1074 rwiWishartCh() : sqd (0), l (0), p (0) {} 1058 1075 //! constructor function 1059 1076 void set_parameters (int p0, double k, vec ref0, double l0) { 1060 1077 p = p0; … … 1326 1343 void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);}; 1327 1344 1345 //! \todo unify this stuff with to_string() 1328 1346 template<class sq_T> 1329 1347 std::ostream &operator<< (std::ostream &os, mlnorm<sq_T> &ml) -
library/bdm/stat/merger.cpp
r507 r536 122 122 if ( DBG ) { 123 123 cout << "Resampling =" << 1. / sum_sqr ( w ) << endl; 124 cout << Mix. _e()->mean() << endl;124 cout << Mix.posterior().mean() << endl; 125 125 cout << sum ( Smp_ex, 2 ) / Npoints << endl; 126 126 cout << Smp_ex*Smp_ex.T() / Npoints << endl; -
library/bdm/stat/merger.h
r529 r536 318 318 SHAREDPTR ( merger_base ); 319 319 320 //! Merger using importance sampling with mixture proposal density 320 321 class merger_mix : public merger_base { 321 322 protected: -
library/doc/tutorial/tut_arx.dox
r290 r536 36 36 \begin{array}{c} [y_{t}',\,\psi_{t}']\\ \\\end{array} 37 37 +(1-\phi) V_0 38 \f]</dd> 39 <dt>"Degree of freedom"</dd> <dd>which is an accumulator of number of data records \f[ 38 \f] 39 </dd> 40 <dt>"Degree of freedom"</dt> <dd>which is an accumulator of number of data records \f[ 40 41 \nu_t = \phi \nu_{t-1} + 1 + (1-\phi) \nu_0 41 \f]</dd> 42 \f] 43 </dd> 42 44 </dl> 43 45 where \f$ \phi \f$ is the forgetting factor, typically \f$ \phi \in [0,1]\f$ roughly corresponding to the effective length of the exponential window by relation:\f[ -
library/tests/arx_elem_test.cpp
r477 r536 12 12 mat mu ( 1, 1 ); 13 13 mat R ( 1, 1 ); 14 Ar. _e()->mean_mat ( mu, R );14 Ar.posterior().mean_mat ( mu, R ); 15 15 cout << "Prior moments: mu=" << mu << ", R=" << R << endl; 16 16 … … 24 24 // Ar is now filled with estimates of N(0,1); 25 25 cout << "Empirical moments: mu=" << sum ( smp ) / ndat << ", R=" << sum_sqr ( smp ) / ndat - pow ( sum ( smp ) / ndat, 2 ) << endl; 26 Ar. _e()->mean_mat ( mu, R );26 Ar.posterior().mean_mat ( mu, R ); 27 27 cout << "Posterior moments: mu=" << mu << ", R=" << R << endl; 28 28 -
library/tests/epdf_harness.cpp
r529 r536 210 210 Array<vec> actual(CurrentContext::max_trial_count); 211 211 do { 212 mat smp = mep.samplecond ( vec ( 0 ), nsamples );212 mat smp = mep.samplecond_m ( vec ( 0 ), nsamples ); 213 213 vec emu = sum ( smp, 2 ) / nsamples; 214 214 actual( tc ) = emu; … … 231 231 Array<mat> actual(CurrentContext::max_trial_count); 232 232 do { 233 mat smp = mep.samplecond ( vec ( 0 ), nsamples );233 mat smp = mep.samplecond_m ( vec ( 0 ), nsamples ); 234 234 vec emu = sum ( smp, 2 ) / nsamples; 235 235 mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );