Changeset 271 for bdm/stat/libBM.h
- Timestamp:
- 02/16/09 10:03:13 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.h
r270 r271 11 11 */ 12 12 13 /*!14 \defgroup core Core BDM classes15 @{16 \defgroup const Constructors17 \defgroup math Mathematical operations18 */19 13 #ifndef BM_H 20 14 #define BM_H … … 106 100 int init ( const string &name, int size ); 107 101 public: 108 //! \name Constructors 109 //!@{ 110 111 //! Full constructor 102 //! \name Constructors 103 //!@{ 104 105 //! Full constructor 112 106 RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) {init ( in_names,in_sizes,in_times );}; 113 //! Constructor with times=0 107 //! Constructor with times=0 114 108 RV ( Array<std::string> in_names, ivec in_sizes ) {init ( in_names,in_sizes,zeros_i ( in_names.length() ) );}; 115 //! Constructor with sizes=1, times=0 109 //! Constructor with sizes=1, times=0 116 110 RV ( Array<std::string> in_names ) {init ( in_names,ones_i ( in_names.length() ),zeros_i ( in_names.length() ) );} 117 //! Constructor of empty RV 111 //! Constructor of empty RV 118 112 RV () :dsize ( 0 ),len ( 0 ),ids ( 0 ),times ( 0 ) {}; 119 113 //! Constructor of a single RV with given id 120 114 RV ( string name, int sz, int tm=0 ); 121 115 //!@} 122 116 123 117 //! \name Access functions 124 118 //!@{ 125 119 126 120 //! Printing output e.g. for debugging. 127 121 friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); … … 129 123 //! Recount size of the corresponding data vector 130 124 int countsize() const; 125 ivec cumsizes() const; 131 126 int length() const {return len;} ; 132 127 int id ( int at ) const{return ids ( at );}; 133 int size ( int at ) const {return RV_SIZES ( at);};128 int size ( int at ) const {return RV_SIZES ( ids(at) );}; 134 129 int time ( int at ) const{return times ( at );}; 135 std::string name ( int at ) const {return RV_NAMES ( at);};130 std::string name ( int at ) const {return RV_NAMES ( ids(at) );}; 136 131 void set_time ( int at, int time0 ) {times ( at ) =time0;}; 137 132 //!@} 138 133 139 134 //TODO why not inline and later?? 140 135 141 136 //! \name Algebra on Random Variables 142 137 //!@{ 143 138 144 139 //! Find indices of self in another rv, \return ivec of the same size as self. 145 140 ivec findself ( const RV &rv2 ) const; … … 154 149 //! Select only variables at indeces ind 155 150 RV operator() ( const ivec &ind ) const {return subselect ( ind );}; 151 //! Select from data vector starting at di1 to di2 152 RV operator() ( int di1, int di2 ) const { 153 ivec sz=cumsizes(); 154 int i1=0; 155 while ( sz ( i1 ) <di1 ) i1++; 156 int i2=i1; 157 while ( sz ( i2 ) <di2 ) i2++; 158 return subselect ( linspace ( i1,i2 ) ); 159 }; 156 160 //! Shift \c time shifted by delta. 157 161 void t ( int delta ); 158 162 //!@} 159 160 //!\name Relation to vectors 161 //!@{ 162 163 164 //!\name Relation to vectors 165 //!@{ 166 163 167 //! generate \c str from rv, by expanding sizes 164 168 str tostr() const; … … 172 176 int mint () const {return min ( times );}; 173 177 //!@} 174 178 175 179 }; 176 180 … … 216 220 public: 217 221 /*! \name Constructors 218 Construction of each epdf should support two types of constructors: 219 \li empty constructor, 222 Construction of each epdf should support two types of constructors: 223 \li empty constructor, 220 224 \li copy constructor, 221 225 222 226 The following constructors should be supported for convenience: 223 \li constructor followed by calling \c set_parameters() 227 \li constructor followed by calling \c set_parameters() 224 228 \li constructor accepting random variables calling \c set_rv() 225 229 226 230 All internal data structures are constructed as empty. Their values (including sizes) will be set by method \c set_parameters(). This way references can be initialized in constructors. 227 231 @{*/ 228 epdf() :dim (0),rv ( ) {};229 epdf (const epdf &e) :dim(e.dim),rv (e.rv) {};230 epdf (const RV &rv0) {set_rv(rv0);};231 void set_parameters (int dim0){dim=dim0;}232 //!@} 233 232 epdf() :dim ( 0 ),rv ( ) {}; 233 epdf ( const epdf &e ) :dim ( e.dim ),rv ( e.rv ) {}; 234 epdf ( const RV &rv0 ) {set_rv ( rv0 );}; 235 void set_parameters ( int dim0 ) {dim=dim0;} 236 //!@} 237 234 238 //! \name Matematical Operations 235 239 //!@{ 236 240 237 241 //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ 238 virtual vec sample () const {it_error ("not implemneted");return vec(0);};242 virtual vec sample () const {it_error ( "not implemneted" );return vec ( 0 );}; 239 243 //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ 240 244 virtual mat sample_m ( int N ) const; 241 245 //! Compute log-probability of argument \c val 242 virtual double evallog ( const vec &val ) const {it_error ("not implemneted");return 0.0;};246 virtual double evallog ( const vec &val ) const {it_error ( "not implemneted" );return 0.0;}; 243 247 //! Compute log-probability of multiple values argument \c val 244 248 virtual vec evallog_m ( const mat &Val ) const { … … 252 256 virtual epdf* marginal ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;} 253 257 //! return expected value 254 virtual vec mean() const {it_error ("not implemneted");return vec(0);};258 virtual vec mean() const {it_error ( "not implemneted" );return vec ( 0 );}; 255 259 //! return expected variance (not covariance!) 256 virtual vec variance() const {it_error ("not implemneted");return vec(0);};257 //!@} 258 260 virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );}; 261 //!@} 262 259 263 //! \name Connection to other classes 260 //! Description of the random quantity via attribute \c rv is optional. 261 //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization 262 //! and \c conditioning \c rv has to be set. NB: 264 //! Description of the random quantity via attribute \c rv is optional. 265 //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization 266 //! and \c conditioning \c rv has to be set. NB: 263 267 //! @{ 264 268 265 269 //!Name its rv 266 270 void set_rv ( const RV &rv0 ) {rv = rv0; }//it_assert_debug(isnamed(),""); }; 267 //! True if rv is assigned 268 bool isnamed() const {bool b= ( dim==rv._dsize() );return b;}271 //! True if rv is assigned 272 bool isnamed() const {bool b= ( dim==rv._dsize() );return b;} 269 273 //! Return name (fails when isnamed is false) 270 274 const RV& _rv() const {it_assert_debug ( isnamed(),"" ); return rv;} 271 275 //!@} 272 276 273 277 //! \name Access to attributes 274 278 //! @{ 275 279 276 280 //! Size of the random variable 277 281 int dimension() const {return dim;} 278 282 //!@} 279 283 280 284 }; 281 285 … … 295 299 //! \name Constructors 296 300 //! @{ 297 298 mpdf ( ) :dimc (0),rvc ( ) {};301 302 mpdf ( ) :dimc ( 0 ),rvc ( ) {}; 299 303 //! copy constructor does not set pointer \c ep - has to be done in offsprings! 300 mpdf ( const mpdf &m ) :dimc(m.dimc),rvc (m.rvc ) {};304 mpdf ( const mpdf &m ) :dimc ( m.dimc ),rvc ( m.rvc ) {}; 301 305 //!@} 302 306 303 307 //! \name Matematical operations 304 308 //!@{ 305 309 306 310 //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 307 311 virtual vec samplecond ( const vec &cond ) { … … 330 334 //! \name Access to attributes 331 335 //! @{ 332 336 333 337 RV _rv() {return ep->_rv();} 334 338 RV _rvc() {it_assert_debug ( isnamed(),"" ); return rvc;} … … 338 342 epdf* _e() {return ep;} 339 343 //!@} 340 344 341 345 //! \name Connection to other objects 342 346 //!@{ 343 347 void set_rvc ( const RV &rvc0 ) {rvc=rvc0;} 344 void set_rv ( const RV &rv0 ) {ep->set_rv (rv0);}345 bool isnamed() {return ( ep->isnamed())&&(dimc==rvc._dsize());}348 void set_rv ( const RV &rv0 ) {ep->set_rv ( rv0 );} 349 bool isnamed() {return ( ep->isnamed() ) && ( dimc==rvc._dsize() );} 346 350 //!@} 347 351 }; … … 382 386 public: 383 387 //! Constructor 384 datalink ( const RV &rv, const RV &rv_up ) : 385 downsize ( rv._dsize() ), upsize ( rv_up._dsize() ), v2v_up ( rv.dataind ( rv_up ) ) { 388 datalink () {}; 389 datalink ( const RV &rv, const RV &rv_up ) {set_connection ( rv,rv_up );}; 390 //! set connection, rv must be fully present in rv_up 391 void set_connection ( const RV &rv, const RV &rv_up ) { 392 downsize = rv._dsize(); 393 upsize = rv_up._dsize(); 394 v2v_up= ( rv.dataind ( rv_up ) ); 395 386 396 it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" ); 387 397 } … … 471 481 logger ( ) : entries ( 0 ),names ( 0 ) {} 472 482 473 //! returns an identifier which will be later needed for calling the log() function 483 //! returns an identifier which will be later needed for calling the \c logit() function 484 //! For empty RV it returns -1, this entry will be ignored by \c logit(). 474 485 virtual int add ( const RV &rv, string name="" ) { 475 int id=entries.length(); 476 names=concat ( names, name ); // diff 477 entries.set_length ( id+1,true ); 478 entries ( id ) = rv; 486 int id; 487 if ( rv._dsize() >0 ) { 488 id=entries.length(); 489 names=concat ( names, name ); // diff 490 entries.set_length ( id+1,true ); 491 entries ( id ) = rv; 492 } 493 else { id =-1;} 479 494 return id; // identifier of the last entry 480 495 } … … 539 554 public: 540 555 //! default constructors 541 DS() :Drv ( 556 DS() :Drv ( ),Urv ( ) {}; 542 557 //! Returns full vector of observed data=[output, input] 543 558 virtual void getdata ( vec &dt ) {it_error ( "abstract class" );}; … … 590 605 //! \name Constructors 591 606 //! @{ 592 593 BM () :ll ( 0),evalll ( false) {};594 BM ( const BM &B ) : drv (B.drv), ll ( B.ll ), evalll ( B.evalll ) {}607 608 BM () :ll ( 0 ),evalll ( false ) {}; 609 BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 595 610 //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 596 611 //! Prototype: \code BM* _copy_(){return new BM(*this);} \endcode … … 600 615 //! \name Mathematical operations 601 616 //!@{ 602 617 603 618 /*! \brief Incremental Bayes rule 604 619 @param dt vector of input data … … 614 629 615 630 //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ 616 virtual epdf* epredictor ( 631 virtual epdf* epredictor ( ) const {it_error ( "Not implemented" );return NULL;}; 617 632 //!Constructs a conditional density 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) 618 633 virtual mpdf* predictor ( ) const {it_error ( "Not implemented" );return NULL;}; 619 634 //!@} 620 635 621 636 //! \name Access to attributes 622 637 //!@{ 623 638 624 639 const RV& _drv() const {return drv;} 625 640 void set_drv ( const RV &rv ) {drv=rv;} 641 void set_rv ( const RV &rv ) {const_cast<epdf&> ( posterior() ).set_rv ( rv );} 626 642 double _ll() const {return ll;} 627 643 void set_evalll ( bool evl0 ) {evalll=evl0;} 628 virtual const epdf& _epdf() const =0;644 virtual const epdf& posterior() const =0; 629 645 virtual const epdf* _e() const =0; 630 646 //!@} … … 657 673 658 674 }; //namespace 659 /*! @} */660 675 #endif // BM_H