- 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
    00062 class FileDS: public MemDS {
    00063
    00064 public:
    00065         void getdata ( vec &dt ) {
    00066                 dt = Data.get_col ( time );
    00067         }
    00068
    00069         void getdata ( vec &dt, const ivec &indices ) {
    00070                 vec tmp = Data.get_col ( time );
    00071                 dt = tmp ( indices );
    00072         }
    00073
    00075         int ndat() {
    00076                 return Data.cols();
    00077         }
    00079         void log_add ( logger &L ) {};
    00081         void logit ( logger &L ) {};
    00082 };
    00083
    00090 class ITppFileDS: public FileDS {
    00091
    00092 public:
    00093         ITppFileDS ( const string &fname, const string &varname ) : FileDS() {
    00094                 it_file it ( fname );
    00095                 it << Name ( varname );
    00096                 it >> Data;
    00097                 time = 0;
    00098                 //rowid and delays are ignored
    00099         };
    00100
    00101         ITppFileDS () : FileDS() {
    00102         };
    00103
    00104         void from_setting ( const Setting &set );
    00105
    00106         // TODO dodelat void to_setting( Setting &set ) const;
    00107
    00108 };
    00109
    00110 UIREGISTER ( ITppFileDS );
    00111 SHAREDPTR ( ITppFileDS );
    00112
    00120 class CsvFileDS: public FileDS {
    00121
    00122 public:
    00124         CsvFileDS ( const string& fname, const string& orientation = "BY_COL" );
    00125 };
    00126
    00127
    00128
    00133 class ArxDS : public DS {
    00134 protected:
    00136         RV Rrv;
    00138         vec H;
    00140         vec U;
    00142         vec rgr;
    00144         datalink rgrlnk;
    00146         mlnorm<chmat> model;
    00148         bool opt_L_theta;
    00150         int L_theta;
    00151         int L_R;
    00152         int dt_size;
    00153 public:
    00154         void getdata ( vec &dt ) {
    00155                 dt = H;
    00156         }
    00157
    00158         void getdata ( vec &dt, const ivec &indices ) {
    00159                 dt = H ( indices );
    00160         }
    00161
    00162         void write ( vec &ut ) {
    00163                 U = ut;
    00164         }
    00165
    00166         void write ( vec &ut, const ivec &indices ) {
    00167                 bdm_assert_debug ( ut.length() == indices.length(), "ArxDS" );
    00168                 set_subvector ( U, indices, ut );
    00169         }
    00170
    00171         void step();
    00172
    00174         ArxDS ( ) {};
    00176         void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) {
    00177                 model.set_parameters ( Th0, mu0, sqR0 );
    00178         };
    00180         void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) {
    00181                 Rrv = rrv;
    00182                 Urv = urv;
    00183                 dt_size = yrv._dsize() + urv._dsize();
    00184
    00185                 RV drv = concat ( yrv, urv );
    00186                 Drv = drv;
    00187                 int td = rrv.mint();
    00188                 H.set_size ( drv._dsize() * ( -td + 1 ) );
    00189                 U.set_size ( Urv._dsize() );
    00190                 for ( int i = -1; i >= td; i-- ) {
    00191                         drv.t ( -1 );
    00192                         Drv.add ( drv ); //shift u1
    00193                 }
    00194                 rgrlnk.set_connection ( rrv, Drv );
    00195
    00196                 dtsize = Drv._dsize();
    00197                 utsize = Urv._dsize();
    00198         }
    00200         void set_options ( const string &s ) {
    00201                 opt_L_theta = ( s.find ( "L_theta" ) != string::npos );
    00202         };
    00203         virtual void log_add ( logger &L ) {
    00204                 //DS::log_add ( L ); too long!!
    00205                 L_dt = L.add ( Drv ( 0, dt_size ), "" );
    00206                 L_ut = L.add ( Urv, "" );
    00207
    00208                 mat &A = model._A();
    00209                 mat R = model._R();
    00210                 if ( opt_L_theta ) {
    00211                         L_theta = L.add ( RV ( "{th }", vec_1 ( A.rows() * A.cols() ) ), "t" );
    00212                 }
    00213                 if ( opt_L_theta ) {
    00214                         L_R = L.add ( RV ( "{R }", vec_1 ( R.rows() * R.cols() ) ), "r" );
    00215                 }
    00216         }
    00217         virtual void logit ( logger &L ) {
    00218                 //DS::logit ( L );
    00219                 L.logit ( L_dt, H.left ( dt_size ) );
    00220                 L.logit ( L_ut, U );
    00221
    00222                 mat &A = model._A();
    00223                 mat R = model._R();
    00224                 if ( opt_L_theta ) {
    00225                         L.logit ( L_theta, vec ( A._data(), A.rows() *A.cols() ) );
    00226                 };
    00227                 if ( opt_L_theta ) {
    00228                         L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );
    00229                 };
    00230         }
    00231
    00232         // TODO dokumentace - aktualizovat
    00264         void from_setting ( const Setting &set );
    00265
    00266         // TODO dodelat void to_setting( Setting &set ) const;
    00267 };
    00268
    00269 UIREGISTER ( ArxDS );
    00270 SHAREDPTR ( ArxDS );
    00271
    00272 class stateDS : public DS {
    00273 private:
    00275         shared_ptr<mpdf> IM;
    00276
    00278         shared_ptr<mpdf> OM;
    00279
    00280 protected:
    00282         vec dt;
    00284         vec xt;
    00286         vec ut;
    00288         int L_xt;
    00289
    00290 public:
    00291         void getdata ( vec &dt0 ) {
    00292                 dt0 = dt;
    00293         }
    00294
    00295         void getdata ( vec &dt0, const ivec &indices ) {
    00296                 dt0 = dt ( indices );
    00297         }
    00298
    00299         stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ),
    00300                 dt ( OM0->dimension() ), xt ( IM0->dimension() ),
    00301                 ut ( usize ), L_xt(0) { }
    00302
    00303         stateDS() : L_xt(0) { }
    00304
    00305         virtual void step() {
    00306                 xt = IM->samplecond ( concat ( xt, ut ) );
    00307                 dt = OM->samplecond ( concat ( xt, ut ) );
    00308         }
    00309
    00310         virtual void log_add ( logger &L ) {
    00311                 DS::log_add ( L );
    00312                 L_xt = L.add ( IM->_rv(), "true" );
    00313         }
    00314         virtual void logit ( logger &L ) {
    00315                 DS::logit ( L );
    00316                 L.logit ( L_xt, xt );
    00317         }
    00318
    00348         void from_setting ( const Setting &set );
    00349
    00350         // TODO dodelat void to_setting( Setting &set ) const;
    00351
    00352 };
    00353
    00354 UIREGISTER ( stateDS );
    00355 SHAREDPTR ( stateDS );
    00356
    00357 }; //namespace
    00358
    00359 #endif // DS_H