Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.h
r281 r283 126 126 int length() const {return len;} ; 127 127 int id ( int at ) const{return ids ( at );}; 128 int size ( int at ) const {return RV_SIZES ( ids (at) );};128 int size ( int at ) const {return RV_SIZES ( ids ( at ) );}; 129 129 int time ( int at ) const{return times ( at );}; 130 std::string name ( int at ) const {return RV_NAMES ( ids (at) );};130 std::string name ( int at ) const {return RV_NAMES ( ids ( at ) );}; 131 131 void set_time ( int at, int time0 ) {times ( at ) =time0;}; 132 132 //!@} … … 204 204 205 205 //! access function 206 int _dimy() const{return dimy;}206 int dimension() const{return dimy;} 207 207 }; 208 208 … … 259 259 //! return expected variance (not covariance!) 260 260 virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );}; 261 //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default 262 virtual void qbounds ( vec &lb, vec &ub, double percentage=0.95 ) const { 263 vec mea=mean(); vec std=sqrt(variance()); 264 lb = mea-2*std; ub=mea+2*std; 265 }; 261 266 //!@} 262 267 … … 592 597 /*! \brief Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities. 593 598 599 This object represents exact or approximate evaluation of the Bayes rule: 600 \f[ 601 f(\theta_t | d_1,\ldots,d_t) = \frac{f(y_t|\theta_t,\cdot) f(\theta_t|d_1,\ldots,d_{t-1})}{f(y_t|d_1,\ldots,d_{t-1})} 602 \f] 603 604 Access to the resulting posterior density is via function \c posterior(). 605 606 As a "side-effect" it also evaluates log-likelihood of the data, which can be accessed via function _ll(). 607 It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor(). 608 609 Alternatively, it can evaluate posterior density conditioned by a known constant, \f$ c_t \f$: 610 \f[ 611 f(\theta_t | c_t, d_1,\ldots,d_t) \propto f(y_t,\theta_t|c_t,\cdot, d_1,\ldots,d_{t-1}) 612 \f] 613 614 The value of \f$ c_t \f$ is set by function condition(). 615 594 616 */ 595 617 … … 606 628 //! @{ 607 629 608 BM () :ll ( 0 ),evalll ( true ) {};630 BM () :ll ( 0 ),evalll ( true ), LIDs ( 3 ), opt_L_bounds ( false ) {}; 609 631 BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 610 632 //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 611 //! Prototype: \code BM* _copy_() {return new BM(*this);} \endcode612 virtual BM* _copy_ () {return NULL;};633 //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode 634 virtual BM* _copy_ () const {return NULL;}; 613 635 //!@} 614 636 … … 634 656 //!@} 635 657 658 //! \name Extension to conditional BM 659 //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). 660 //! Alternatively, it can be used for automated connection to DS when the condition is observed 661 //!@{ 662 663 //! Name of extension variable 664 RV rvc; 665 //! access function 666 const RV& _rvc() const {return rvc;} 667 668 //! Substitute \c val for \c rvc. 669 virtual void condition ( const vec &val ) {it_error ( "Not implemented!" );}; 670 671 //!@} 672 673 636 674 //! \name Access to attributes 637 675 //!@{ … … 646 684 //!@} 647 685 648 }; 649 650 /*! 651 \brief Conditional Bayesian Filter 652 653 Evaluates conditional filtering density \f$f(rv|rvc,data)\f$ for a given \c rvc which is specified in each step by calling function \c condition. 654 655 This is an interface class used to assure that certain BM has operation \c condition . 656 657 */ 658 659 class BMcond :public bdmroot { 660 protected: 661 //!dimension of the conditioning variable 662 int dimc; 663 //! Identificator of the conditioning variable 664 RV rvc; 665 public: 666 //! Substitute \c val for \c rvc. 667 virtual void condition ( const vec &val ) =0; 668 //! Default constructor 669 BMcond ( ) :rvc ( ) {}; 670 //! Destructor for future use 671 virtual ~BMcond() {}; 672 //! access function 673 const RV& _rvc() const {return rvc;} 686 //! \name Logging of results 687 //!@{ 688 689 //! Set boolean options from a string 690 void set_options ( const string &opt ) { 691 opt_L_bounds= ( opt.find ( "logbounds" ) !=string::npos ); 692 } 693 //! IDs of storages in loggers 694 ivec LIDs; 695 696 //! Option for logging bounds 697 bool opt_L_bounds; 698 //! Add all logged variables to a logger 699 void log_add ( logger *L, const string &name="" ) { 700 // internal 701 RV r; 702 if ( posterior().isnamed() ) {r=posterior()._rv();} 703 else{r=RV ( "est", posterior().dimension() );}; 704 705 // Add mean value 706 LIDs ( 0 ) =L->add ( r,name ); 707 if ( opt_L_bounds ) { 708 LIDs ( 1 ) =L->add ( r,name+"_lb" ); 709 LIDs ( 2 ) =L->add ( r,name+"_ub" ); 710 } 711 } 712 void logit ( logger *L ) { 713 L->logit ( LIDs ( 0 ), posterior().mean() ); 714 if ( opt_L_bounds ) { 715 vec ub,lb; 716 posterior().qbounds(lb,ub); 717 L->logit ( LIDs ( 1 ), lb ); 718 L->logit ( LIDs ( 2 ), ub ); 719 } 720 } 721 //!@} 674 722 }; 675 723 -
bdm/stat/libDS.h
r271 r283 28 28 */ 29 29 class MemDS : public DS { 30 protected: 30 31 //! internal matrix of data 31 32 mat Data; … … 45 46 void step(); 46 47 //!Default constructor 48 MemDS () {}; 47 49 MemDS ( mat &Dat, ivec &rowid, ivec &delays ); 50 }; 51 52 /*! Read Data Matrix from an IT file 53 54 */ 55 class FileDS: public MemDS { 56 57 public: 58 FileDS ( const string &fname, const string &varname ) :MemDS() { 59 it_file it ( fname ); 60 it << Name ( varname ); 61 it >> Data; 62 time =0; 63 //rowid and delays are ignored 64 } 65 void getdata ( vec &dt ) { 66 it_assert_debug ( dt.length() ==Data.rows(),"" ); 67 dt = Data.get_col(time); 68 }; 69 void getdata ( vec &dt, const ivec &indeces ){ 70 it_assert_debug ( dt.length() ==indeces.length(),"" ); 71 vec tmp(indeces.length()); 72 tmp = Data.get_col(time); 73 dt = tmp(indeces); 74 }; 75 //! returns number of data in the file; 76 int ndat(){return Data.cols();} 48 77 }; 49 78 … … 96 125 { model.set_parameters ( Th0, mu0, sqR0 );}; 97 126 //! Set 98 void set_drv (RV &yrv, RV &urv, RV &rrv){127 void set_drv ( RV &yrv, RV &urv, RV &rrv ) { 99 128 Rrv = rrv; 100 129 Urv = urv; 101 dt_size = yrv._dsize() +urv._dsize();102 103 RV drv = concat (yrv,urv);130 dt_size = yrv._dsize() +urv._dsize(); 131 132 RV drv = concat ( yrv,urv ); 104 133 Drv = drv; 105 134 int td = rrv.mint(); 106 H.set_size (drv._dsize()*(-td+1));107 U.set_size (Urv._dsize());108 for ( int i=-1;i>=td;i--){109 drv.t (-1);110 Drv.add (drv); //shift u1135 H.set_size ( drv._dsize() * ( -td+1 ) ); 136 U.set_size ( Urv._dsize() ); 137 for ( int i=-1;i>=td;i-- ) { 138 drv.t ( -1 ); 139 Drv.add ( drv ); //shift u1 111 140 } 112 rgrlnk.set_connection (rrv,Drv);113 141 rgrlnk.set_connection ( rrv,Drv ); 142 114 143 dtsize = Drv._dsize(); 115 144 utsize = Urv._dsize(); … … 121 150 virtual void log_add ( logger &L ) { 122 151 //DS::log_add ( L ); too long!! 123 L_dt=L.add ( Drv (0,dt_size),"" );152 L_dt=L.add ( Drv ( 0,dt_size ),"" ); 124 153 L_ut=L.add ( Urv,"" ); 125 154 … … 131 160 virtual void logit ( logger &L ) { 132 161 //DS::logit ( L ); 133 L.logit ( L_dt, H.left(dt_size));134 L.logit (L_ut, U);135 162 L.logit ( L_dt, H.left ( dt_size ) ); 163 L.logit ( L_ut, U ); 164 136 165 mat &A =model._A(); 137 166 mat R =model._R(); -
bdm/stat/libEF.cpp
r281 r283 183 183 }; 184 184 185 ivec eEmp::resample ( RESAMPLING_METHOD method) {185 ivec eEmp::resample (RESAMPLING_METHOD method) { 186 186 ivec ind=zeros_i ( n ); 187 187 ivec N_babies = zeros_i ( n ); … … 268 268 } 269 269 270 void eEmp::set_ parameters ( const vec &w0, const epdf* epdf0 ) {270 void eEmp::set_statistics ( const vec &w0, const epdf* epdf0 ) { 271 271 //it_assert_debug(rv==epdf0->rv(),"Wrong epdf0"); 272 272 dim = epdf0->dimension(); -
bdm/stat/libEF.h
r281 r283 94 94 // virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );} 95 95 96 BMEF* _copy_ ( bool changerv=false ) {it_error ( "function _copy_ not implemented for this BM" ); return NULL;};96 BMEF* _copy_ ( bool changerv=false ) const {it_error ( "function _copy_ not implemented for this BM" ); return NULL;}; 97 97 }; 98 98 … … 348 348 \f] 349 349 350 Vector \f$\beta\f$ has different meaning (in fact it is 1/beta as used in definition of iG) 351 350 352 Inverse Gamma can be converted to Gamma using \f[ 351 353 x\sim iG(a,b) => 1/x\sim G(a,1/b) … … 354 356 */ 355 357 356 class eigamma : public eEF { 357 protected: 358 //!internal egamma 359 egamma eg; 360 //! Vector \f$\alpha\f$ 361 vec α 362 //! Vector \f$\beta\f$ (in fact it is 1/beta as used in definition of iG) 363 vec β 358 class eigamma : public egamma { 359 protected: 364 360 public : 365 361 //! \name Constructors 366 //!@{ 367 eigamma ( ) :eEF ( ), eg(),alpha ( eg._alpha() ), beta ( eg._beta() ) {}; 368 eigamma ( const vec &a, const vec &b ) :eEF ( ), eg(),alpha ( eg._alpha() ), beta ( eg._beta() ) {eg.set_parameters ( a,b );}; 369 void set_parameters ( const vec &a, const vec &b ) {eg.set_parameters ( a,b );}; 370 //!@} 371 372 vec sample() const {return 1.0/eg.sample();}; 373 //! TODO: is it used anywhere? 374 // mat sample ( int N ) const; 375 double evallog ( const vec &val ) const {return eg.evallog ( val );}; 376 double lognc () const {return eg.lognc();}; 362 //! All constructors are inherited 363 //!@{ 364 //!@} 365 366 vec sample() const {return 1.0/egamma::sample();}; 377 367 //! Returns poiter to alpha and beta. Potentially dangerous: use with care! 378 368 vec mean() const {return elem_div ( beta,alpha-1 );} 379 369 vec variance() const {vec mea=mean(); return elem_div ( elem_mult ( mea,mea ),alpha-2 );} 380 vec& _alpha() {return alpha;}381 vec& _beta() {return beta;}382 370 }; 383 371 /* … … 490 478 mgnorm() :mu ( epdf._mu() ) {ep=&epdf;} 491 479 //!set mean function 492 void set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; epdf.set_parameters ( zeros ( g-> _dimy() ), R0 );}480 void set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; epdf.set_parameters ( zeros ( g->dimension() ), R0 );} 493 481 void condition ( const vec &cond ) {mu=g->eval ( cond );}; 494 482 }; … … 686 674 687 675 //! Set samples and weights 688 void set_parameters ( const vec &w0, const epdf* pdf0 ); 676 void set_statistics ( const vec &w0, const epdf* pdf0 ); 677 //! Set samples and weights 678 void set_statistics ( const epdf* pdf0 , int n ) {set_statistics ( ones ( n ) /n,pdf0 );}; 689 679 //! Set sample 690 680 void set_samples ( const epdf* pdf0 ); 691 681 //! Set sample 692 void set_ n( int n0, bool copy=true ) {n=n0; w.set_size ( n0,copy );samples.set_size ( n0,copy );};682 void set_parameters ( int n0, bool copy=true ) {n=n0; w.set_size ( n0,copy );samples.set_size ( n0,copy );}; 693 683 //! Potentially dangerous, use with care. 694 684 vec& _w() {return w;}; … … 700 690 const Array<vec>& _samples() const {return samples;}; 701 691 //! Function performs resampling, i.e. removal of low-weight samples and duplication of high-weight samples such that the new samples represent the same density. 702 ivec resample ( RESAMPLING_METHOD method =SYSTEMATIC );692 ivec resample ( RESAMPLING_METHOD method=SYSTEMATIC ); 703 693 //! inherited operation : NOT implemneted 704 694 vec sample() const {it_error ( "Not implemented" );return 0;} … … 714 704 for ( int i=0;i<n;i++ ) {pom+=pow ( samples ( i ),2 ) *w ( i );} 715 705 return pom-pow ( mean(),2 ); 706 } 707 //! For this class, qbounds are minimum and maximum value of the population! 708 void qbounds ( vec &lb, vec &ub, double perc=0.95 ) const { 709 // lb in inf so than it will be pushed below; 710 lb.set_size(dim); 711 ub.set_size(dim); 712 lb = std::numeric_limits<double>::infinity(); 713 ub = -std::numeric_limits<double>::infinity(); 714 int j; 715 for ( int i=0;i<n;i++ ) { 716 for ( j=0;j<dim; j++ ) { 717 if ( samples ( i ) ( j ) <lb ( j ) ) {lb ( j ) =samples ( i ) ( j );} 718 if ( samples ( i ) ( j ) >ub ( j ) ) {ub ( j ) =samples ( i ) ( j );} 719 } 720 } 716 721 } 717 722 };