[18] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Common DataSources. |
---|
| 4 | \author Vaclav Smidl. |
---|
| 5 | |
---|
| 6 | ----------------------------------- |
---|
| 7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 8 | |
---|
| 9 | Using IT++ for numerical operations |
---|
| 10 | ----------------------------------- |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | #ifndef DS_H |
---|
| 14 | #define DS_H |
---|
| 15 | |
---|
[263] | 16 | |
---|
[19] | 17 | #include "libBM.h" |
---|
[263] | 18 | #include "libEF.h" |
---|
[18] | 19 | |
---|
| 20 | |
---|
[263] | 21 | namespace bdm { |
---|
[271] | 22 | /*! |
---|
| 23 | * \brief Memory storage of off-line data column-wise |
---|
[18] | 24 | |
---|
[271] | 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. |
---|
[18] | 26 | |
---|
[271] | 27 | The data can be loaded from a file. |
---|
| 28 | */ |
---|
| 29 | class MemDS : public DS { |
---|
[283] | 30 | protected: |
---|
[271] | 31 | //! internal matrix of data |
---|
| 32 | mat Data; |
---|
| 33 | //! active column in the Data matrix |
---|
| 34 | int time; |
---|
| 35 | //! vector of rows that are presented in Dt |
---|
| 36 | ivec rowid; |
---|
| 37 | //! vector of delays that are presented in Dt |
---|
| 38 | ivec delays; |
---|
[18] | 39 | |
---|
[271] | 40 | public: |
---|
| 41 | void getdata ( vec &dt ); |
---|
| 42 | void getdata ( vec &dt, const ivec &indeces ); |
---|
| 43 | void set_rvs ( RV &drv, RV &urv ); |
---|
| 44 | void write ( vec &ut ) {it_error ( "MemDS::write is not supported" );} |
---|
| 45 | void write ( vec &ut,ivec &indices ) {it_error ( "MemDS::write is not supported" );} |
---|
| 46 | void step(); |
---|
| 47 | //!Default constructor |
---|
[283] | 48 | MemDS () {}; |
---|
[313] | 49 | MemDS ( mat &Dat, ivec &rowid0, ivec &delays0 ); |
---|
[271] | 50 | }; |
---|
[263] | 51 | |
---|
[308] | 52 | /*! Pseudovirtual class for reading data from files |
---|
[283] | 53 | |
---|
| 54 | */ |
---|
| 55 | class FileDS: public MemDS { |
---|
| 56 | |
---|
| 57 | public: |
---|
| 58 | void getdata ( vec &dt ) { |
---|
| 59 | it_assert_debug ( dt.length() ==Data.rows(),"" ); |
---|
| 60 | dt = Data.get_col(time); |
---|
| 61 | }; |
---|
| 62 | void getdata ( vec &dt, const ivec &indeces ){ |
---|
| 63 | it_assert_debug ( dt.length() ==indeces.length(),"" ); |
---|
| 64 | vec tmp(indeces.length()); |
---|
| 65 | tmp = Data.get_col(time); |
---|
| 66 | dt = tmp(indeces); |
---|
| 67 | }; |
---|
| 68 | //! returns number of data in the file; |
---|
| 69 | int ndat(){return Data.cols();} |
---|
[342] | 70 | //! no sense to log this type |
---|
| 71 | void log_add ( logger &L ) {}; |
---|
| 72 | //! no sense to log this type |
---|
| 73 | void logit ( logger &L ) {}; |
---|
[283] | 74 | }; |
---|
| 75 | |
---|
[308] | 76 | /*! |
---|
| 77 | * \brief Read Data Matrix from an IT file |
---|
| 78 | |
---|
| 79 | The constructor creates an internal matrix \c Data from an IT++ file. The file is binary and can be made using the IT++ library or the Matlab/Octave function itsave. NB: the data are stored columnwise, i.e. each column contains the data for time \f$t\f$! |
---|
| 80 | |
---|
| 81 | */ |
---|
[342] | 82 | class ITppFileDS: public FileDS { |
---|
[308] | 83 | |
---|
| 84 | public: |
---|
[342] | 85 | ITppFileDS ( const string &fname, const string &varname ) :FileDS() { |
---|
[308] | 86 | it_file it ( fname ); |
---|
| 87 | it << Name ( varname ); |
---|
| 88 | it >> Data; |
---|
| 89 | time = 0; |
---|
| 90 | //rowid and delays are ignored |
---|
[342] | 91 | }; |
---|
[308] | 92 | }; |
---|
| 93 | |
---|
[271] | 94 | /*! |
---|
[308] | 95 | * \brief CSV file data storage |
---|
| 96 | The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types: |
---|
| 97 | 1. \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item. |
---|
| 98 | 2. \c BY_ROW if the data are stored the classical CSV style. Then each column stores the values for data item, for ex. \f$[y_{t} y_{t-1} ...]\f$, one row for each discrete time instant. |
---|
| 99 | |
---|
| 100 | */ |
---|
| 101 | class CsvFileDS: public FileDS { |
---|
| 102 | |
---|
| 103 | public: |
---|
| 104 | //! Constructor - create DS from a CSV file. |
---|
| 105 | CsvFileDS ( const string& fname, const string& orientation = "BY_COL" ); |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | /*! |
---|
[271] | 111 | \brief Generator of ARX data |
---|
[263] | 112 | |
---|
[271] | 113 | */ |
---|
| 114 | class ArxDS : public DS { |
---|
| 115 | protected: |
---|
| 116 | //! Rv of the regressor |
---|
| 117 | RV Rrv; |
---|
| 118 | //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$ |
---|
| 119 | vec H; |
---|
| 120 | //! (future) input |
---|
| 121 | vec U; |
---|
| 122 | //! temporary variable for regressor |
---|
| 123 | vec rgr; |
---|
| 124 | //! data link: H -> rgr |
---|
| 125 | datalink rgrlnk; |
---|
| 126 | //! model of Y - linear Gaussian |
---|
| 127 | mlnorm<chmat> model; |
---|
| 128 | //! options |
---|
| 129 | bool opt_L_theta; |
---|
| 130 | //! loggers |
---|
| 131 | int L_theta; |
---|
| 132 | int L_R; |
---|
| 133 | int dt_size; |
---|
| 134 | public: |
---|
| 135 | void getdata ( vec &dt ) { |
---|
| 136 | //it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" ); |
---|
| 137 | dt=H; |
---|
| 138 | }; |
---|
| 139 | void getdata ( vec &dt, const ivec &indices ) { |
---|
| 140 | it_assert_debug ( dt.length() ==indices.length(),"ArxDS" ); |
---|
| 141 | dt=H ( indices ); |
---|
| 142 | }; |
---|
| 143 | void write ( vec &ut ) { |
---|
| 144 | //it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" ); |
---|
| 145 | U=ut; |
---|
| 146 | }; |
---|
| 147 | void write ( vec &ut, const ivec &indices ) { |
---|
| 148 | it_assert_debug ( ut.length() ==indices.length(),"ArxDS" ); |
---|
| 149 | set_subvector ( U, indices,ut ); |
---|
| 150 | }; |
---|
| 151 | void step(); |
---|
| 152 | //!Default constructor |
---|
| 153 | ArxDS ( ) {}; |
---|
| 154 | //! Set parameters of the internal model, H is maximum time delay |
---|
| 155 | void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) |
---|
| 156 | { model.set_parameters ( Th0, mu0, sqR0 );}; |
---|
| 157 | //! Set |
---|
[283] | 158 | void set_drv ( RV &yrv, RV &urv, RV &rrv ) { |
---|
[271] | 159 | Rrv = rrv; |
---|
| 160 | Urv = urv; |
---|
[283] | 161 | dt_size = yrv._dsize() +urv._dsize(); |
---|
| 162 | |
---|
| 163 | RV drv = concat ( yrv,urv ); |
---|
[271] | 164 | Drv = drv; |
---|
| 165 | int td = rrv.mint(); |
---|
[283] | 166 | H.set_size ( drv._dsize() * ( -td+1 ) ); |
---|
| 167 | U.set_size ( Urv._dsize() ); |
---|
| 168 | for ( int i=-1;i>=td;i-- ) { |
---|
| 169 | drv.t ( -1 ); |
---|
| 170 | Drv.add ( drv ); //shift u1 |
---|
[265] | 171 | } |
---|
[283] | 172 | rgrlnk.set_connection ( rrv,Drv ); |
---|
| 173 | |
---|
[271] | 174 | dtsize = Drv._dsize(); |
---|
| 175 | utsize = Urv._dsize(); |
---|
| 176 | } |
---|
| 177 | //! set options from a string |
---|
| 178 | void set_options ( const string &s ) { |
---|
| 179 | opt_L_theta= ( s.find ( "L_theta" ) !=string::npos ); |
---|
[263] | 180 | }; |
---|
[271] | 181 | virtual void log_add ( logger &L ) { |
---|
| 182 | //DS::log_add ( L ); too long!! |
---|
[283] | 183 | L_dt=L.add ( Drv ( 0,dt_size ),"" ); |
---|
[271] | 184 | L_ut=L.add ( Urv,"" ); |
---|
[263] | 185 | |
---|
[271] | 186 | mat &A =model._A(); |
---|
| 187 | mat R =model._R(); |
---|
| 188 | if ( opt_L_theta ) {L_theta=L.add ( RV ( "{th }", vec_1 ( A.rows() *A.cols() ) ),"t" );} |
---|
| 189 | if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );} |
---|
| 190 | } |
---|
| 191 | virtual void logit ( logger &L ) { |
---|
| 192 | //DS::logit ( L ); |
---|
[283] | 193 | L.logit ( L_dt, H.left ( dt_size ) ); |
---|
| 194 | L.logit ( L_ut, U ); |
---|
| 195 | |
---|
[271] | 196 | mat &A =model._A(); |
---|
| 197 | mat R =model._R(); |
---|
| 198 | if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );}; |
---|
| 199 | if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );}; |
---|
| 200 | } |
---|
[265] | 201 | |
---|
[271] | 202 | }; |
---|
[265] | 203 | |
---|
[271] | 204 | class stateDS : public DS { |
---|
| 205 | protected: |
---|
| 206 | //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ |
---|
| 207 | mpdf* IM; |
---|
| 208 | //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ |
---|
| 209 | mpdf* OM; |
---|
| 210 | //! result storage |
---|
| 211 | vec dt; |
---|
| 212 | //! state storage |
---|
| 213 | vec xt; |
---|
| 214 | //! input storage |
---|
| 215 | vec ut; |
---|
| 216 | //! Logger |
---|
| 217 | int L_xt; |
---|
| 218 | public: |
---|
| 219 | void getdata ( vec &dt0 ) {dt0=dt;} |
---|
| 220 | void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );} |
---|
| 221 | |
---|
| 222 | stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ), |
---|
| 223 | dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {} |
---|
| 224 | ~stateDS() {delete IM; delete OM;} |
---|
| 225 | virtual void step() { |
---|
| 226 | xt=IM->samplecond ( concat ( xt,ut ) ); |
---|
| 227 | dt=OM->samplecond ( concat ( xt,ut ) ); |
---|
[265] | 228 | }; |
---|
[267] | 229 | |
---|
[271] | 230 | virtual void log_add ( logger &L ) { |
---|
| 231 | DS::log_add ( L ); |
---|
| 232 | L_xt=L.add ( IM->_rv(),"true" ); |
---|
| 233 | } |
---|
| 234 | virtual void logit ( logger &L ) { |
---|
| 235 | DS::logit ( L ); |
---|
| 236 | L.logit ( L_xt,xt ); |
---|
| 237 | } |
---|
[267] | 238 | |
---|
[271] | 239 | }; |
---|
[267] | 240 | |
---|
[254] | 241 | }; //namespace |
---|
[18] | 242 | |
---|
| 243 | #endif // DS_H |
---|