Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/emix.cpp
r270 r271 60 60 // // If rvaddok==false, mpdfs overlap => assert error. 61 61 // it_assert_debug(rvaddok||overlap,"mprod::mprod() input mpdfs overlap in rv!"); 62 // epdfs ( i ) = & ( mpdfs ( i )-> _epdf() ); // add pointer to epdf62 // epdfs ( i ) = & ( mpdfs ( i )->posterior() ); // add pointer to epdf 63 63 // }; 64 64 // // Create rvc -
bdm/stat/emix.h
r270 r271 163 163 class mprod: public compositepdf, public mpdf { 164 164 protected: 165 //! pointers to epdfs - shortcut to mpdfs(). _epdf()165 //! pointers to epdfs - shortcut to mpdfs().posterior() 166 166 Array<epdf*> epdfs; 167 167 //! Data link for each mpdfs -
bdm/stat/libBM.cpp
r270 r271 5 5 //! Space of basic BDM structures 6 6 namespace bdm { 7 7 8 8 const int RV_BUFFER_STEP=1; 9 9 RVmap RV_MAP; 10 Array<string> RV_NAMES (RV_BUFFER_STEP);11 ivec RV_SIZES (RV_BUFFER_STEP);10 Array<string> RV_NAMES ( RV_BUFFER_STEP ); 11 ivec RV_SIZES ( RV_BUFFER_STEP ); 12 12 13 13 RV RV0=RV(); … … 18 18 RVmap::const_iterator iter = RV_MAP.find ( name ); 19 19 if ( iter == RV_MAP.end() ) { 20 id=RV_MAP.size() +1;20 id=RV_MAP.size() +1; 21 21 RV_MAP.insert ( make_pair ( name,id ) ); //add new rv 22 if ( id>=RV_NAMES.length()){23 RV_NAMES.set_length (id+RV_BUFFER_STEP,true);24 RV_SIZES.set_length (id+RV_BUFFER_STEP,true);25 } 26 RV_NAMES (id)=name;27 RV_SIZES (id)=size;22 if ( id>=RV_NAMES.length() ) { 23 RV_NAMES.set_length ( id+RV_BUFFER_STEP,true ); 24 RV_SIZES.set_length ( id+RV_BUFFER_STEP,true ); 25 } 26 RV_NAMES ( id ) =name; 27 RV_SIZES ( id ) =size; 28 28 } 29 29 else { 30 30 id = iter->second; 31 it_ warning ("RV of name " + name + "already exists");31 it_assert(RV_SIZES(id)==size,"RV "+name+" of different size already exists"); 32 32 } 33 33 return id; 34 34 }; 35 35 36 int RV::countsize() const{ int tmp=0; for(int i=0;i<len;i++){tmp+=RV_SIZES(ids(i));} return tmp;} 36 int RV::countsize() const{ int tmp=0; for ( int i=0;i<len;i++ ) {tmp+=RV_SIZES ( ids ( i ) );} return tmp;} 37 38 ivec RV::cumsizes() const { 39 ivec szs ( len ); 40 int tmp=0; 41 for ( int i=0;i<len;i++ ) { 42 tmp+=RV_SIZES ( ids ( i ) ); 43 szs ( i ) = tmp; 44 } 45 return szs; 46 } 37 47 38 48 void RV::init ( Array<std::string> in_names, ivec in_sizes,ivec in_times ) { … … 40 50 it_assert_debug ( in_names.length() ==in_times.length(), "check \"times\" " ); 41 51 it_assert_debug ( in_names.length() ==in_sizes.length(), "check \"sizes\" " ); 42 52 43 53 times.set_length ( len ); 44 54 ids.set_length ( len ); … … 54 64 RV::RV ( string name, int sz, int tm ) { 55 65 Array<string> A ( 1 ); A ( 0 ) =name; 56 init ( A,vec_1 ( sz ),vec_1 ( tm ) );66 init ( A,vec_1 ( sz ),vec_1 ( tm ) ); 57 67 } 58 68 … … 82 92 RV RV::subselect ( const ivec &ind ) const { 83 93 RV ret; 84 ret.ids = ids (ind);85 ret.times= times (ind);94 ret.ids = ids ( ind ); 95 ret.times= times ( ind ); 86 96 ret.len = ind.length(); 87 97 ret.dsize=ret.countsize(); … … 118 128 int pos = 0; 119 129 for ( i = 0;i < len;i++ ) { 120 idlist.set_subvector ( pos, pos + size ( ids (i) ) - 1, ids ( i ) );121 tmlist.set_subvector ( pos, pos + size ( ids (i) ) - 1, times ( i ) );122 pos += size ( ids (i) );130 idlist.set_subvector ( pos, pos + size ( ids ( i ) ) - 1, ids ( i ) ); 131 tmlist.set_subvector ( pos, pos + size ( ids ( i ) ) - 1, times ( i ) ); 132 pos += size ( ids ( i ) ); 123 133 } 124 134 return str ( idlist, tmlist ); -
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 -
bdm/stat/libDS.cpp
r270 r271 43 43 void ArxDS::step() { 44 44 //shift history 45 H.shift_right ( 0, Drv._dsize() +Urv._dsize());45 H.shift_right ( 0, dt_size ); 46 46 47 H.set_subvector ( Drv._dsize(), U ); // write U after Drv47 H.set_subvector ( dt_size-utsize, U ); // write U after Drv 48 48 49 49 //get regressor 50 rgr = rgrlnk ->pushdown ( H );50 rgr = rgrlnk.pushdown ( H ); 51 51 // Eval Y 52 52 H.set_subvector ( 0, model.samplecond ( rgr ) ); -
bdm/stat/libDS.h
r270 r271 20 20 21 21 namespace bdm { 22 23 22 /*! 23 * \brief Memory storage of off-line data column-wise 24 24 25 25 The data are stored in an internal matrix \c Data . Each column of Data corresponds to one discrete time observation \f$t\f$. Access to this matrix is via indices \c rowid and \c delays. 26 26 27 28 29 30 31 32 33 34 35 36 37 27 The data can be loaded from a file. 28 */ 29 class MemDS : public DS { 30 //! internal matrix of data 31 mat Data; 32 //! active column in the Data matrix 33 int time; 34 //! vector of rows that are presented in Dt 35 ivec rowid; 36 //! vector of delays that are presented in Dt 37 ivec delays; 38 38 39 public: 40 void getdata ( vec &dt ); 41 void getdata ( vec &dt, const ivec &indeces ); 42 void set_rvs ( RV &drv, RV &urv ); 43 void write ( vec &ut ) {it_error ( "MemDS::write is not supported" );} 44 void write ( vec &ut,ivec &indices ) {it_error ( "MemDS::write is not supported" );} 45 void step(); 46 //!Default constructor 47 MemDS ( mat &Dat, ivec &rowid, ivec &delays ); 39 public: 40 void getdata ( vec &dt ); 41 void getdata ( vec &dt, const ivec &indeces ); 42 void set_rvs ( RV &drv, RV &urv ); 43 void write ( vec &ut ) {it_error ( "MemDS::write is not supported" );} 44 void write ( vec &ut,ivec &indices ) {it_error ( "MemDS::write is not supported" );} 45 void step(); 46 //!Default constructor 47 MemDS ( mat &Dat, ivec &rowid, ivec &delays ); 48 }; 49 50 /*! 51 \brief Generator of ARX data 52 53 */ 54 class ArxDS : public DS { 55 protected: 56 //! Rv of the regressor 57 RV Rrv; 58 //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$ 59 vec H; 60 //! (future) input 61 vec U; 62 //! temporary variable for regressor 63 vec rgr; 64 //! data link: H -> rgr 65 datalink rgrlnk; 66 //! model of Y - linear Gaussian 67 mlnorm<chmat> model; 68 //! options 69 bool opt_L_theta; 70 //! loggers 71 int L_theta; 72 int L_R; 73 int dt_size; 74 public: 75 void getdata ( vec &dt ) { 76 //it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" ); 77 dt=H; 78 }; 79 void getdata ( vec &dt, const ivec &indices ) { 80 it_assert_debug ( dt.length() ==indices.length(),"ArxDS" ); 81 dt=H ( indices ); 82 }; 83 void write ( vec &ut ) { 84 //it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" ); 85 U=ut; 86 }; 87 void write ( vec &ut, const ivec &indices ) { 88 it_assert_debug ( ut.length() ==indices.length(),"ArxDS" ); 89 set_subvector ( U, indices,ut ); 90 }; 91 void step(); 92 //!Default constructor 93 ArxDS ( ) {}; 94 //! Set parameters of the internal model, H is maximum time delay 95 void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) 96 { model.set_parameters ( Th0, mu0, sqR0 );}; 97 //! Set 98 void set_drv(RV &yrv, RV &urv, RV &rrv){ 99 Rrv = rrv; 100 Urv = urv; 101 dt_size = yrv._dsize()+urv._dsize(); 102 103 RV drv = concat(yrv,urv); 104 Drv = drv; 105 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 u1 111 } 112 rgrlnk.set_connection(rrv,Drv); 113 114 dtsize = Drv._dsize(); 115 utsize = Urv._dsize(); 116 } 117 //! set options from a string 118 void set_options ( const string &s ) { 119 opt_L_theta= ( s.find ( "L_theta" ) !=string::npos ); 120 }; 121 virtual void log_add ( logger &L ) { 122 //DS::log_add ( L ); too long!! 123 L_dt=L.add ( Drv(0,dt_size),"" ); 124 L_ut=L.add ( Urv,"" ); 125 126 mat &A =model._A(); 127 mat R =model._R(); 128 if ( opt_L_theta ) {L_theta=L.add ( RV ( "{th }", vec_1 ( A.rows() *A.cols() ) ),"t" );} 129 if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );} 130 } 131 virtual void logit ( logger &L ) { 132 //DS::logit ( L ); 133 L.logit( L_dt, H.left(dt_size)); 134 L.logit(L_ut, U); 135 136 mat &A =model._A(); 137 mat R =model._R(); 138 if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );}; 139 if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );}; 140 } 141 142 }; 143 144 class stateDS : public DS { 145 protected: 146 //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 147 mpdf* IM; 148 //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 149 mpdf* OM; 150 //! result storage 151 vec dt; 152 //! state storage 153 vec xt; 154 //! input storage 155 vec ut; 156 //! Logger 157 int L_xt; 158 public: 159 void getdata ( vec &dt0 ) {dt0=dt;} 160 void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );} 161 162 stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ), 163 dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {} 164 ~stateDS() {delete IM; delete OM;} 165 virtual void step() { 166 xt=IM->samplecond ( concat ( xt,ut ) ); 167 dt=OM->samplecond ( concat ( xt,ut ) ); 48 168 }; 49 169 50 /*! 51 \brief Generator of ARX data 170 virtual void log_add ( logger &L ) { 171 DS::log_add ( L ); 172 L_xt=L.add ( IM->_rv(),"true" ); 173 } 174 virtual void logit ( logger &L ) { 175 DS::logit ( L ); 176 L.logit ( L_xt,xt ); 177 } 52 178 53 */ 54 class ArxDS : public DS { 55 protected: 56 //! Rv of the regressor 57 RV Rrv; 58 //! Rv of the history (full regressor) 59 RV Hrv; 60 //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$ 61 vec H; 62 //! (future) input 63 vec U; 64 //! temporary variable for regressor 65 vec rgr; 66 //! data link: H -> rgr 67 datalink *rgrlnk; 68 //! model of Y - linear Gaussian 69 mlnorm<chmat> model; 70 //! options 71 bool opt_L_theta; 72 //! loggers 73 int L_theta; 74 int L_R; 75 public: 76 void getdata ( vec &dt ) { 77 //it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" ); 78 dt=H.left ( Urv._dsize() +Drv._dsize() ); 79 }; 80 void getdata ( vec &dt, const ivec &indices ) { 81 it_assert_debug ( dt.length() ==indices.length(),"ArxDS" ); 82 dt=H ( indices ); 83 }; 84 void write ( vec &ut ) { 85 //it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" ); 86 U=ut; 87 }; 88 void write ( vec &ut, const ivec &indices ) { 89 it_assert_debug ( ut.length() ==indices.length(),"ArxDS" ); 90 set_subvector ( U, indices,ut ); 91 }; 92 void step(); 93 //!Default constructor 94 ArxDS ( ){}; 95 //! Set parameters of the internal model 96 void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) 97 { model.set_parameters ( Th0, mu0, sqR0 ); }; 98 //! set options from a string 99 void set_options ( const string &s ) { 100 opt_L_theta= ( s.find ( "L_theta" ) !=string::npos ); 101 }; 102 virtual void log_add ( logger &L ) { 103 DS::log_add ( L ); 104 mat &A =model._A(); 105 mat R =model._R(); 106 if ( opt_L_theta ) {L_theta=L.add ( RV ( "{theta }", vec_1 ( A.rows() *A.cols() ) ),"t" );} 107 if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );} 108 } 109 virtual void logit ( logger &L ) { 110 DS::logit ( L ); 111 mat &A =model._A(); 112 mat R =model._R(); 113 if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );}; 114 if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );}; 115 } 116 117 }; 118 119 class ARXDS : public ArxDS { 120 public: 121 ARXDS ( ) : ArxDS ( ) {} 122 123 void getdata ( vec &dt ) {dt=H;} 124 void getdata ( vec &dt, const ivec &indeces ) {dt=H ( indeces );} 125 virtual RV _drv() const {return Hrv;} 126 127 }; 128 129 class stateDS : public DS { 130 protected: 131 //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 132 mpdf* IM; 133 //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 134 mpdf* OM; 135 //! result storage 136 vec dt; 137 //! state storage 138 vec xt; 139 //! input storage 140 vec ut; 141 //! Logger 142 int L_xt; 143 public: 144 void getdata ( vec &dt0 ) {dt0=dt;} 145 void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );} 146 147 stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ), 148 dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize) {} 149 ~stateDS() {delete IM; delete OM;} 150 virtual void step() { 151 xt=IM->samplecond(concat ( xt,ut )); 152 dt=OM->samplecond(concat ( xt,ut )); 153 }; 154 155 virtual void log_add ( logger &L ) { 156 DS::log_add ( L ); 157 L_xt=L.add(IM->_rv(),"true"); 158 } 159 virtual void logit ( logger &L ) { 160 DS::logit ( L ); 161 L.logit ( L_xt,xt); 162 } 163 164 }; 179 }; 165 180 166 181 }; //namespace -
bdm/stat/libEF.h
r270 r271 297 297 if ( evalll ) {last_lognc=est.lognc();} 298 298 } 299 const epdf& _epdf() const {return est;};299 const epdf& posterior() const {return est;}; 300 300 const eDirich* _e() const {return &est;}; 301 301 void set_parameters ( const vec &beta0 ) { -
bdm/stat/loggers.h
r270 r271 48 48 void logit ( int id, const vec &v ) { 49 49 it_assert_debug(id<vectors.length(),"Logger was not initialized, run init()."); 50 vectors ( id ).set_row ( ind,v );} 50 if(id>0){ vectors ( id ).set_row ( ind,v );} 51 } 51 52 //! Save values into an itfile named after \c fname. 52 53 void itsave(const char* fname); -
bdm/stat/loggers_ui.h
r263 r271 1 1 2 /*! 2 3 \file … … 19 20 using namespace bdm; 20 21 22 /*! \brief UI for class RV (description of data vectors) 23 24 \code 25 rv = { 26 type = "rv"; //identifier of the description 27 // UNIQUE IDENTIFIER same names = same variable 28 names = ["a", "b", "c", ...]; // which will be used e.g. in loggers 29 30 //optional arguments 31 sizes = [1, 2, 3, ...]; // (optional) default = ones() 32 times = [-1, -2, 0, ...]; // time shifts with respect to current time (optional) default = zeros() 33 } 34 \endcode 35 */ 36 class UIrv: public UIbuilder{ 37 public: 38 UIrv():UIbuilder("rv"){}; 39 bdmroot* build(Setting &S) const{ 40 Array<string> A=get_as(S["names"]); 41 ivec szs; 42 ivec tms; 43 if (S.exists("sizes")){ 44 szs=getivec(S["sizes"]); 45 } else { 46 szs = ones_i(A.length()); 47 } 48 if (S.exists("times")){ 49 tms=getivec(S["times"]); 50 } else { 51 tms = zeros_i(A.length()); 52 } 53 RV *tmp = new RV(A,szs,tms); 54 return tmp; 55 }; 56 }; 57 UIREGISTER(UIrv); 58 59 /*! \brief UI for dirfilelog (Kst file format) 60 \code 61 logger = { 62 type = "dirfilelog"; 63 dirmane = "directory_for_files"; // resulting files will be stored there 64 maxlen = 100; // size of memory buffer, when full results are written to disk 65 } 66 \endcode 67 */ 21 68 class UIdirfilelog : public UIbuilder { 22 69 public: