Changeset 660 for library/bdm/base
- Timestamp:
- 10/15/09 00:04:30 (15 years ago)
- Location:
- library/bdm/base
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r652 r660 238 238 return tmp; 239 239 } 240 //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1}, rv_{240 //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1},\ldots rv_{max_delay}]\f$ 241 241 RV expand_delayes() const { 242 242 RV rvt = this->remove_time(); //rv at t=0 … … 764 764 class datalink_2to1_buffered { 765 765 protected: 766 //! link 1st vector to down 766 767 datalink_buffered dl1; 768 //! link 2nd vector to down 767 769 datalink_buffered dl2; 768 770 public: … … 772 774 dl2.set_connection ( rv, rv_up2 ); 773 775 } 776 //! fill values of down from the values of the two up vectors 774 777 void filldown ( const vec &val1, const vec &val2, vec &val_down ) { 775 778 bdm_assert_debug ( val_down.length() >= dl1._downsize() + dl2._downsize(), "short val_down" ); … … 777 780 dl2.filldown ( val2, val_down ); 778 781 } 782 //! update buffer 779 783 void step ( const vec &dt, const vec &ut ) { 780 784 dl1.step ( dt ); … … 962 966 //!Description of output data 963 967 RV Yrv; // 964 //! Remember its own index in Logger L 965 i nt L_dt, L_ut;968 //! Remember its own index in Logger L, [0=dt, 1=ut] 969 ivec LIDs; 966 970 public: 967 971 //! default constructors 968 DS() : Drv(), Urv(),Yrv() {};972 DS() : Drv(), Urv(),Yrv(), LIDs(2) {}; 969 973 970 974 //! Returns maximum number of provided data, by default it is set to maximum allowed length, shorter DS should overload this method! See, MemDS.max_length(). … … 980 984 981 985 //! Accepts action variable and schedule it for application. 982 virtual void write ( vec &ut ) {986 virtual void write (const vec &ut ) { 983 987 bdm_error ( "abstract class" ); 984 988 } 985 989 986 990 //! Accepts action variables at specific indeces 987 virtual void write ( vec &ut, const ivec &indeces ) {991 virtual void write (const vec &ut, const ivec &indeces ) { 988 992 bdm_error ( "abstract class" ); 989 993 } … … 1000 1004 bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); 1001 1005 1002 L _dt= L.add ( Drv, "" );1003 L _ut= L.add ( Urv, "" );1006 LIDs(0) = L.add ( Drv, "" ); 1007 LIDs(1) = L.add ( Urv, "" ); 1004 1008 } 1005 1009 //! Register DS for logging into logger L … … 1008 1012 getdata ( tmp ); 1009 1013 // d is first in getdata 1010 L.logit ( L _dt, tmp.left ( Drv._dsize() ) );1014 L.logit ( LIDs(0), tmp.left ( Drv._dsize() ) ); 1011 1015 // u follows after d in getdata 1012 L.logit ( L _ut, tmp.mid ( Drv._dsize(), Urv._dsize() ) );1016 L.logit ( LIDs(1), tmp.mid ( Drv._dsize(), Urv._dsize() ) ); 1013 1017 } 1014 1018 //!access function … … 1072 1076 }; 1073 1077 BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 1074 //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually!1078 //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 1075 1079 //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode 1076 1080 virtual BM* _copy_() const { … … 1138 1142 //! \name Access to attributes 1139 1143 //!@{ 1140 1144 //! access function 1141 1145 const RV& _drv() const { 1142 1146 return drv; 1143 1147 } 1148 //! access function 1144 1149 void set_drv ( const RV &rv ) { 1145 1150 drv = rv; 1146 1151 } 1152 //! access to rv of the posterior 1147 1153 void set_rv ( const RV &rv ) { 1148 1154 const_cast<epdf&> ( posterior() ).set_rv ( rv ); 1149 1155 } 1156 //! return internal log-likelihood of the last data vector 1150 1157 double _ll() const { 1151 1158 return ll; 1152 1159 } 1160 //! switch evaluation of log-likelihood on/off 1153 1161 void set_evalll ( bool evl0 ) { 1154 1162 evalll = evl0; 1155 1163 } 1164 //! return posterior density 1156 1165 virtual const epdf& posterior() const = 0; 1157 1166 //!@} … … 1216 1225 1217 1226 }; 1218 1227 //! array of pointers to epdf 1219 1228 typedef Array<shared_ptr<epdf> > epdf_array; 1220 1229 //! array of pointers to mpdf 1221 1230 typedef Array<shared_ptr<mpdf> > mpdf_array; 1222 1231 -
library/bdm/base/datasources.cpp
r609 r660 28 28 } 29 29 30 void MemDS::set_ rvs ( RV &drv,RV &urv ) {30 void MemDS::set_drv (const RV &drv, const RV &urv ) { 31 31 bdm_assert_debug ( drv._dsize() == rowid.length(), "MemDS::set_rvs incompatible drv" ); 32 32 bdm_assert_debug ( urv._dsize() == 0, "MemDS does not support urv." ); 33 33 34 D rv = drv;34 DS::set_drv(drv,urv); 35 35 } 36 36 … … 42 42 } 43 43 44 void ArxDS::step() {45 //shift history46 H.shift_right ( 0, dt_size );47 48 H.set_subvector ( dt_size - utsize, U ); // write U after Drv49 50 //get regressor51 rgr = rgrlnk.pushdown ( H );52 // Eval Y53 H.set_subvector ( 0, model.samplecond ( rgr ) );54 55 }56 57 void ArxDS::from_setting ( const Setting &set ) {58 shared_ptr<RV> yrv = UI::build<RV> ( set, "y" , UI::compulsory );59 shared_ptr<RV> urv = UI::build<RV> ( set, "u" , UI::compulsory );60 shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr" , UI::compulsory );61 62 mat Th;63 UI::get ( Th, set, "theta", UI::compulsory );64 65 vec mu0;66 if ( !UI::get ( mu0, set, "offset" ) )67 mu0 = zeros ( yrv->_dsize() );68 69 mat sqR;70 UI::get ( sqR, set, "r", UI::compulsory );71 set_parameters ( Th, mu0, sqR );72 set_drv ( *yrv, *urv, *rrv );73 74 if ( set.exists ( "opt" ) )75 set_options ( set["opt"] );76 }44 // void ArxDS::step() { 45 // //shift history 46 // H.shift_right ( 0, dt_size ); 47 // 48 // H.set_subvector ( dt_size - utsize, U ); // write U after Drv 49 // 50 // //get regressor 51 // rgr = rgrlnk.pushdown ( H ); 52 // // Eval Y 53 // H.set_subvector ( 0, model.samplecond ( rgr ) ); 54 // 55 // } 56 // 57 // void ArxDS::from_setting ( const Setting &set ) { 58 // shared_ptr<RV> yrv = UI::build<RV> ( set, "y" , UI::compulsory ); 59 // shared_ptr<RV> urv = UI::build<RV> ( set, "u" , UI::compulsory ); 60 // shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr" , UI::compulsory ); 61 // 62 // mat Th; 63 // UI::get ( Th, set, "theta", UI::compulsory ); 64 // 65 // vec mu0; 66 // if ( !UI::get ( mu0, set, "offset" ) ) 67 // mu0 = zeros ( yrv->_dsize() ); 68 // 69 // mat sqR; 70 // UI::get ( sqR, set, "r", UI::compulsory ); 71 // set_parameters ( Th, mu0, sqR ); 72 // set_drv ( *yrv, *urv, *rrv ); 73 // 74 // if ( set.exists ( "opt" ) ) 75 // set_options ( set["opt"] ); 76 // } 77 77 78 78 CsvFileDS::CsvFileDS ( const string& fname, const string& orientation ) : FileDS() { -
library/bdm/base/datasources.h
r618 r660 40 40 void getdata ( vec &dt ); 41 41 void getdata ( vec &dt, const ivec &indeces ); 42 void set_ rvs ( RV &drv,RV &urv );43 44 void write ( vec &ut ) {42 void set_drv (const RV &drv,const RV &urv ); 43 44 void write ( const vec &ut ) { 45 45 bdm_error ( "MemDS::write is not supported" ); 46 46 } 47 47 48 void write ( vec &ut,ivec &indices ) {48 void write ( const vec &ut, const ivec &indices ) { 49 49 bdm_error ( "MemDS::write is not supported" ); 50 50 } … … 53 53 //!Default constructor 54 54 MemDS () {}; 55 //! Convenience constructor 55 56 MemDS ( mat &Dat, ivec &rowid0); 56 57 /*! Create object from the following structure … … 114 115 dt_out = dt ( ids ); 115 116 } 116 const RV& _drv() {117 const RV& _drv() const { 117 118 return iepdf->_rv(); 118 119 } … … 262 263 263 264 public: 265 //! Convenience constructor 264 266 ITppFileDS ( const string &fname, const string &varname ) : FileDS() { 265 267 it_file it ( fname ); … … 298 300 299 301 300 /*! 301 \brief Generator of ARX data 302 303 */ 304 class ArxDS : public DS { 305 protected: 306 //! Rv of the regressor 307 RV Rrv; 308 //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$ 309 vec H; 310 //! (future) input 311 vec U; 312 //! temporary variable for regressor 313 vec rgr; 314 //! data link: H -> rgr 315 datalink rgrlnk; 316 //! model of Y - linear Gaussian 317 mlnorm<chmat> model; 318 //! options 319 bool opt_L_theta; 320 //! loggers 321 int L_theta; 322 int L_R; 323 int dt_size; 324 public: 325 void getdata ( vec &dt ) { 326 dt = H; 327 } 328 329 void getdata ( vec &dt, const ivec &indices ) { 330 dt = H ( indices ); 331 } 332 333 void write ( vec &ut ) { 334 U = ut; 335 } 336 337 void write ( vec &ut, const ivec &indices ) { 338 bdm_assert_debug ( ut.length() == indices.length(), "ArxDS" ); 339 set_subvector ( U, indices, ut ); 340 } 341 342 void step(); 343 344 //!Default constructor 345 ArxDS ( ) {}; 346 //! Set parameters of the internal model, H is maximum time delay 347 void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) { 348 model.set_parameters ( Th0, mu0, sqR0 ); 349 }; 350 //! Set 351 void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) { 352 Rrv = rrv; 353 Urv = urv; 354 dt_size = yrv._dsize() + urv._dsize(); 355 356 RV drv = concat ( yrv, urv ); 357 Drv = drv; 358 int td = rrv.mint(); 359 H.set_size ( drv._dsize() * ( -td + 1 ) ); 360 U.set_size ( Urv._dsize() ); 361 for ( int i = -1; i >= td; i-- ) { 362 drv.t_plus ( -1 ); 363 Drv.add ( drv ); //shift u1 364 } 365 rgrlnk.set_connection ( rrv, Drv ); 366 367 dtsize = Drv._dsize(); 368 utsize = Urv._dsize(); 369 } 370 //! set options from a string 371 void set_options ( const string &s ) { 372 opt_L_theta = ( s.find ( "L_theta" ) != string::npos ); 373 }; 374 virtual void log_add ( logger &L ) { 375 //DS::log_add ( L ); too long!! 376 L_dt = L.add ( Drv ( 0, dt_size ), "" ); 377 L_ut = L.add ( Urv, "" ); 378 379 const mat &A = model._A(); 380 const mat R = model._R(); 381 if ( opt_L_theta ) { 382 L_theta = L.add ( RV ( "{th }", vec_1 ( A.rows() * A.cols() ) ), "t" ); 383 } 384 if ( opt_L_theta ) { 385 L_R = L.add ( RV ( "{R }", vec_1 ( R.rows() * R.cols() ) ), "r" ); 386 } 387 } 388 virtual void logit ( logger &L ) { 389 //DS::logit ( L ); 390 L.logit ( L_dt, H.left ( dt_size ) ); 391 L.logit ( L_ut, U ); 392 393 const mat &A = model._A(); 394 const mat R = model._R(); 395 if ( opt_L_theta ) { 396 L.logit ( L_theta, vec ( A._data(), A.rows() *A.cols() ) ); 397 }; 398 if ( opt_L_theta ) { 399 L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) ); 400 }; 401 } 402 403 // TODO dokumentace - aktualizovat 404 /*! UI for ArxDS using factorized description! 405 406 The ArxDS is constructed from a structure with fields: 407 \code 408 system = { 409 type = "ArxDS"; 410 // description of y variables 411 y = {type="rv"; names=["y", "u"];}; 412 // description of u variable 413 u = {type="rv"; names=[];} 414 // description of regressor 415 rgr = {type="rv"; 416 names = ["y","y","y","u"]; 417 times = [-1, -2, -3, -1]; 418 } 419 420 // theta 421 theta = [0.8, -0.3, 0.4, 1.0, 422 0.0, 0.0, 0.0, 0.0]; 423 // offset (optional) 424 offset = [0.0, 0.0]; 425 //variance 426 r = [0.1, 0.0, 427 0.0, 1.0]; 428 //options: L_theta = log value of theta, 429 opt = "L_theta"; 430 }; 431 \endcode 432 433 Result is ARX data source offering with full history as Drv. 434 */ 435 void from_setting ( const Setting &set ); 436 437 // TODO dodelat void to_setting( Setting &set ) const; 438 }; 439 440 UIREGISTER ( ArxDS ); 441 SHAREDPTR ( ArxDS ); 442 302 // ARXDs - DELETED 303 304 //! State-space data source simulating two densities 443 305 class stateDS : public DS { 444 306 private: … … 467 329 dt0 = dt ( indices ); 468 330 } 469 331 //! convenience constructor 470 332 stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ), 471 333 dt ( OM0->dimension() ), xt ( IM0->dimension() ), -
library/bdm/base/loggers.h
r620 r660 35 35 string itfilename; 36 36 public: 37 //! convenience constructor 37 38 memlog ( int maxlen0, string itf = "" ) : maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 38 39