- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# datasources.h

Go to the documentation of this file.

    00001
    00013 #ifndef DATASOURCE_H
    00014 #define DATASOURCE_H
    00015
    00016
    00017 #include "../base/bdmbase.h"
    00018 #include "../stat/exp_family.h"
    00019 #include "../base/user_info.h"
    00020
    00021 namespace bdm {
    00029 class MemDS : public DS {
    00030         protected:
    00032                 mat Data;
    00034                 int time;
    00036                 ivec rowid;
    00038                 ivec delays;
    00039
    00040         public:
    00041                 void getdata ( vec &dt );
    00042                 void getdata ( vec &dt, const ivec &indeces );
    00043                 void set_rvs ( RV &drv, RV &urv );
    00044
    00045                 void write ( vec &ut ) {
    00046                         bdm_error ( "MemDS::write is not supported" );
    00047                 }
    00048
    00049                 void write ( vec &ut, ivec &indices ) {
    00050                         bdm_error ( "MemDS::write is not supported" );
    00051                 }
    00052
    00053                 void step();
    00055                 MemDS () {};
    00056                 MemDS ( mat &Dat, ivec &rowid0, ivec &delays0 );
    00057 };
    00058
    00063 class EpdfDS: public DS {
    00064         protected:
    00066                 shared_ptr<epdf> iepdf;
    00068                 vec dt;
    00069         public:
    00070                 void step() {
    00071                         dt=iepdf->sample();
    00072                 }
    00073                 void getdata ( vec &dt_out ) {
    00074                         dt_out = dt;
    00075                 }
    00076                 void getdata ( vec &dt_out, const ivec &ids ) {
    00077                         dt_out = dt ( ids );
    00078                 }
    00079                 const RV& _drv() {
    00080                         return iepdf->_rv();
    00081                 }
    00082
    00090                 void from_setting ( const Setting &set ) {
    00091                         iepdf=UI::build<epdf> ( set,"epdf",UI::compulsory );
    00092                         dt = zeros ( iepdf->dimension() );
    00093                 }
    00094 };
    00095 UIREGISTER ( EpdfDS );
    00096
    00100 class MpdfDS :public DS {
    00101         protected:
    00103                 shared_ptr<mpdf> impdf;
    00105                 vec yt;
    00107                 vec ut;
    00109                 datalink_buffered ut2rgr;
    00111                 datalink_buffered yt2rgr;
    00113                 vec rgr;
    00114
    00115         public:
    00116                 void step() {
    00117                         yt2rgr.step(yt); // y is now history
    00118                         ut2rgr.filldown ( ut,rgr );
    00119                         yt2rgr.filldown ( yt,rgr );
    00120                         yt=impdf->samplecond ( rgr );
    00121                         ut2rgr.step(ut); //u is now history
    00122                 }
    00123                 void getdata ( vec &dt_out ) {
    00124                         bdm_assert_debug(dt_out.length()>=utsize+ytsize,"Short output vector");
    00125                         dt_out.set_subvector(0, yt);
    00126                         dt_out.set_subvector(ytsize, ut);
    00127                 }
    00128                 void write(const vec &ut0){ut=ut0;}
    00129
    00137                 void from_setting ( const Setting &set ) {
    00138                         impdf=UI::build<mpdf> ( set,"mpdf",UI::compulsory );
    00139
    00140                         Yrv = impdf->_rv();
    00141                         // get unique rvs form rvc
    00142                         RV rgrv0=impdf->_rvc().remove_time();
    00143                         // input is what in not in Yrv
    00144                         Urv=rgrv0.subt(Yrv);
    00145                         set_drv(Yrv, Urv);
    00146                         // connect input and output to rvc
    00147                         ut2rgr.set_connection(impdf->_rvc(), Urv);
    00148                         yt2rgr.set_connection(impdf->_rvc(), Yrv);
    00149
    00150                         yt = zeros ( impdf->dimension() );
    00151                         rgr = zeros ( impdf->dimensionc() );
    00152                         ut = zeros(Urv._dsize());
    00153
    00154                         ytsize=yt.length();
    00155                         utsize=ut.length();
    00156                         dtsize = ytsize+utsize;
    00157                 }
    00158 };
    00159 UIREGISTER ( MpdfDS );
    00160
    00164 class FileDS: public MemDS {
    00165
    00166         public:
    00167                 void getdata ( vec &dt ) {
    00168                         dt = Data.get_col ( time );
    00169                 }
    00170
    00171                 void getdata ( vec &dt, const ivec &indices ) {
    00172                         vec tmp = Data.get_col ( time );
    00173                         dt = tmp ( indices );
    00174                 }
    00175
    00177                 int ndat() {
    00178                         return Data.cols();
    00179                 }
    00181                 void log_add ( logger &L ) {};
    00183                 void logit ( logger &L ) {};
    00184 };
    00185
    00192 class ITppFileDS: public FileDS {
    00193
    00194         public:
    00195                 ITppFileDS ( const string &fname, const string &varname ) : FileDS() {
    00196                         it_file it ( fname );
    00197                         it << Name ( varname );
    00198                         it >> Data;
    00199                         time = 0;
    00200                         //rowid and delays are ignored
    00201                 };
    00202
    00203                 ITppFileDS () : FileDS() {
    00204                 };
    00205
    00206                 void from_setting ( const Setting &set );
    00207
    00208                 // TODO dodelat void to_setting( Setting &set ) const;
    00209
    00210 };
    00211
    00212 UIREGISTER ( ITppFileDS );
    00213 SHAREDPTR ( ITppFileDS );
    00214
    00222 class CsvFileDS: public FileDS {
    00223
    00224         public:
    00226                 CsvFileDS ( const string& fname, const string& orientation = "BY_COL" );
    00227 };
    00228
    00229
    00230
    00235 class ArxDS : public DS {
    00236         protected:
    00238                 RV Rrv;
    00240                 vec H;
    00242                 vec U;
    00244                 vec rgr;
    00246                 datalink rgrlnk;
    00248                 mlnorm<chmat> model;
    00250                 bool opt_L_theta;
    00252                 int L_theta;
    00253                 int L_R;
    00254                 int dt_size;
    00255         public:
    00256                 void getdata ( vec &dt ) {
    00257                         dt = H;
    00258                 }
    00259
    00260                 void getdata ( vec &dt, const ivec &indices ) {
    00261                         dt = H ( indices );
    00262                 }
    00263
    00264                 void write ( vec &ut ) {
    00265                         U = ut;
    00266                 }
    00267
    00268                 void write ( vec &ut, const ivec &indices ) {
    00269                         bdm_assert_debug ( ut.length() == indices.length(), "ArxDS" );
    00270                         set_subvector ( U, indices, ut );
    00271                 }
    00272
    00273                 void step();
    00274
    00276                 ArxDS ( ) {};
    00278                 void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) {
    00279                         model.set_parameters ( Th0, mu0, sqR0 );
    00280                 };
    00282                 void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) {
    00283                         Rrv = rrv;
    00284                         Urv = urv;
    00285                         dt_size = yrv._dsize() + urv._dsize();
    00286
    00287                         RV drv = concat ( yrv, urv );
    00288                         Drv = drv;
    00289                         int td = rrv.mint();
    00290                         H.set_size ( drv._dsize() * ( -td + 1 ) );
    00291                         U.set_size ( Urv._dsize() );
    00292                         for ( int i = -1; i >= td; i-- ) {
    00293                                 drv.t_plus ( -1 );
    00294                                 Drv.add ( drv ); //shift u1
    00295                         }
    00296                         rgrlnk.set_connection ( rrv, Drv );
    00297
    00298                         dtsize = Drv._dsize();
    00299                         utsize = Urv._dsize();
    00300                 }
    00302                 void set_options ( const string &s ) {
    00303                         opt_L_theta = ( s.find ( "L_theta" ) != string::npos );
    00304                 };
    00305                 virtual void log_add ( logger &L ) {
    00306                         //DS::log_add ( L ); too long!!
    00307                         L_dt = L.add ( Drv ( 0, dt_size ), "" );
    00308                         L_ut = L.add ( Urv, "" );
    00309
    00310                         const mat &A = model._A();
    00311                         const mat R = model._R();
    00312                         if ( opt_L_theta ) {
    00313                                 L_theta = L.add ( RV ( "{th }", vec_1 ( A.rows() * A.cols() ) ), "t" );
    00314                         }
    00315                         if ( opt_L_theta ) {
    00316                                 L_R = L.add ( RV ( "{R }", vec_1 ( R.rows() * R.cols() ) ), "r" );
    00317                         }
    00318                 }
    00319                 virtual void logit ( logger &L ) {
    00320                         //DS::logit ( L );
    00321                         L.logit ( L_dt, H.left ( dt_size ) );
    00322                         L.logit ( L_ut, U );
    00323
    00324                         const mat &A = model._A();
    00325                         const mat R = model._R();
    00326                         if ( opt_L_theta ) {
    00327                                 L.logit ( L_theta, vec ( A._data(), A.rows() *A.cols() ) );
    00328                         };
    00329                         if ( opt_L_theta ) {
    00330                                 L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );
    00331                         };
    00332                 }
    00333
    00334                 // TODO dokumentace - aktualizovat
    00366                 void from_setting ( const Setting &set );
    00367
    00368                 // TODO dodelat void to_setting( Setting &set ) const;
    00369 };
    00370
    00371 UIREGISTER ( ArxDS );
    00372 SHAREDPTR ( ArxDS );
    00373
    00374 class stateDS : public DS {
    00375         private:
    00377                 shared_ptr<mpdf> IM;
    00378
    00380                 shared_ptr<mpdf> OM;
    00381
    00382         protected:
    00384                 vec dt;
    00386                 vec xt;
    00388                 vec ut;
    00390                 int L_xt;
    00391
    00392         public:
    00393                 void getdata ( vec &dt0 ) {
    00394                         dt0 = dt;
    00395                 }
    00396
    00397                 void getdata ( vec &dt0, const ivec &indices ) {
    00398                         dt0 = dt ( indices );
    00399                 }
    00400
    00401                 stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ),
    00402                                 dt ( OM0->dimension() ), xt ( IM0->dimension() ),
    00403                                 ut ( usize ), L_xt ( 0 ) { }
    00404
    00405                 stateDS() : L_xt ( 0 ) { }
    00406
    00407                 virtual void step() {
    00408                         xt = IM->samplecond ( concat ( xt, ut ) );
    00409                         dt = OM->samplecond ( concat ( xt, ut ) );
    00410                 }
    00411
    00412                 virtual void log_add ( logger &L ) {
    00413                         DS::log_add ( L );
    00414                         L_xt = L.add ( IM->_rv(), "true" );
    00415                 }
    00416                 virtual void logit ( logger &L ) {
    00417                         DS::logit ( L );
    00418                         L.logit ( L_xt, xt );
    00419                 }
    00420
    00450                 void from_setting ( const Setting &set );
    00451
    00452                 // TODO dodelat void to_setting( Setting &set ) const;
    00453
    00454 };
    00455
    00456 UIREGISTER ( stateDS );
    00457 SHAREDPTR ( stateDS );
    00458
    00459 }; //namespace
    00460
    00461 #endif // DS_H