00001
00013 #ifndef DS_H
00014 #define DS_H
00015
00016
00017 #include "libBM.h"
00018 #include "libEF.h"
00019
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 void write ( vec &ut ) {it_error ( "MemDS::write is not supported" );}
00045 void write ( vec &ut,ivec &indices ) {it_error ( "MemDS::write is not supported" );}
00046 void step();
00048 MemDS () {};
00049 MemDS ( mat &Dat, ivec &rowid0, ivec &delays0 );
00050 };
00051
00055 class FileDS: public MemDS {
00056
00057 public:
00058 void getdata ( vec &dt ) {
00059 it_assert_debug ( dt.length() ==Data.rows(),"" );
00060 dt = Data.get_col(time);
00061 };
00062 void getdata ( vec &dt, const ivec &indeces ){
00063 it_assert_debug ( dt.length() ==indeces.length(),"" );
00064 vec tmp(indeces.length());
00065 tmp = Data.get_col(time);
00066 dt = tmp(indeces);
00067 };
00069 int ndat(){return Data.cols();}
00071 void log_add ( logger &L ) {};
00073 void logit ( logger &L ) {};
00074 };
00075
00082 class ITppFileDS: public FileDS {
00083
00084 public:
00085 ITppFileDS ( const string &fname, const string &varname ) :FileDS() {
00086 it_file it ( fname );
00087 it << Name ( varname );
00088 it >> Data;
00089 time = 0;
00090
00091 };
00092 };
00093
00101 class CsvFileDS: public FileDS {
00102
00103 public:
00105 CsvFileDS ( const string& fname, const string& orientation = "BY_COL" );
00106 };
00107
00108
00109
00114 class ArxDS : public DS {
00115 protected:
00117 RV Rrv;
00119 vec H;
00121 vec U;
00123 vec rgr;
00125 datalink rgrlnk;
00127 mlnorm<chmat> model;
00129 bool opt_L_theta;
00131 int L_theta;
00132 int L_R;
00133 int dt_size;
00134 public:
00135 void getdata ( vec &dt ) {
00136
00137 dt=H;
00138 };
00139 void getdata ( vec &dt, const ivec &indices ) {
00140 it_assert_debug ( dt.length() ==indices.length(),"ArxDS" );
00141 dt=H ( indices );
00142 };
00143 void write ( vec &ut ) {
00144
00145 U=ut;
00146 };
00147 void write ( vec &ut, const ivec &indices ) {
00148 it_assert_debug ( ut.length() ==indices.length(),"ArxDS" );
00149 set_subvector ( U, indices,ut );
00150 };
00151 void step();
00153 ArxDS ( ) {};
00155 void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 )
00156 { model.set_parameters ( Th0, mu0, sqR0 );};
00158 void set_drv ( RV &yrv, RV &urv, RV &rrv ) {
00159 Rrv = rrv;
00160 Urv = urv;
00161 dt_size = yrv._dsize() +urv._dsize();
00162
00163 RV drv = concat ( yrv,urv );
00164 Drv = drv;
00165 int td = rrv.mint();
00166 H.set_size ( drv._dsize() * ( -td+1 ) );
00167 U.set_size ( Urv._dsize() );
00168 for ( int i=-1;i>=td;i-- ) {
00169 drv.t ( -1 );
00170 Drv.add ( drv );
00171 }
00172 rgrlnk.set_connection ( rrv,Drv );
00173
00174 dtsize = Drv._dsize();
00175 utsize = Urv._dsize();
00176 }
00178 void set_options ( const string &s ) {
00179 opt_L_theta= ( s.find ( "L_theta" ) !=string::npos );
00180 };
00181 virtual void log_add ( logger &L ) {
00182
00183 L_dt=L.add ( Drv ( 0,dt_size ),"" );
00184 L_ut=L.add ( Urv,"" );
00185
00186 mat &A =model._A();
00187 mat R =model._R();
00188 if ( opt_L_theta ) {L_theta=L.add ( RV ( "{th }", vec_1 ( A.rows() *A.cols() ) ),"t" );}
00189 if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );}
00190 }
00191 virtual void logit ( logger &L ) {
00192
00193 L.logit ( L_dt, H.left ( dt_size ) );
00194 L.logit ( L_ut, U );
00195
00196 mat &A =model._A();
00197 mat R =model._R();
00198 if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );};
00199 if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );};
00200 }
00201
00202 };
00203
00204 class stateDS : public DS {
00205 protected:
00207 mpdf* IM;
00209 mpdf* OM;
00211 vec dt;
00213 vec xt;
00215 vec ut;
00217 int L_xt;
00218 public:
00219 void getdata ( vec &dt0 ) {dt0=dt;}
00220 void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );}
00221
00222 stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ),
00223 dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {}
00224 ~stateDS() {delete IM; delete OM;}
00225 virtual void step() {
00226 xt=IM->samplecond ( concat ( xt,ut ) );
00227 dt=OM->samplecond ( concat ( xt,ut ) );
00228 };
00229
00230 virtual void log_add ( logger &L ) {
00231 DS::log_add ( L );
00232 L_xt=L.add ( IM->_rv(),"true" );
00233 }
00234 virtual void logit ( logger &L ) {
00235 DS::logit ( L );
00236 L.logit ( L_xt,xt );
00237 }
00238
00239 };
00240
00241 };
00242
00243 #endif // DS_H