| 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 DATASOURCE_H |
|---|
| 14 | #define DATASOURCE_H |
|---|
| 15 | |
|---|
| 16 | #include "../base/bdmbase.h" |
|---|
| 17 | #include "../stat/exp_family.h" |
|---|
| 18 | #include "../base/user_info.h" |
|---|
| 19 | |
|---|
| 20 | namespace bdm { |
|---|
| 21 | /*! |
|---|
| 22 | * \brief Memory storage of off-line data column-wise |
|---|
| 23 | |
|---|
| 24 | 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. |
|---|
| 25 | |
|---|
| 26 | The data can be loaded from a file. |
|---|
| 27 | */ |
|---|
| 28 | class MemDS : public DS { |
|---|
| 29 | protected: |
|---|
| 30 | //! internal matrix of data |
|---|
| 31 | mat Data; |
|---|
| 32 | //! active column in the Data matrix |
|---|
| 33 | int time; |
|---|
| 34 | |
|---|
| 35 | public: |
|---|
| 36 | //!Default constructor |
|---|
| 37 | MemDS () {}; |
|---|
| 38 | |
|---|
| 39 | //! Convenience constructor |
|---|
| 40 | MemDS ( mat &Dat ); |
|---|
| 41 | |
|---|
| 42 | //! returns number of data in the file; |
|---|
| 43 | int max_length() { |
|---|
| 44 | return Data.cols(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | void getdata ( vec &dt ) const; |
|---|
| 48 | |
|---|
| 49 | void getdata ( vec &dt, const ivec &indices ); |
|---|
| 50 | |
|---|
| 51 | void set_drv ( const RV &drv ); |
|---|
| 52 | |
|---|
| 53 | void set_drv ( const RV &drv, const RV &urv ) |
|---|
| 54 | { |
|---|
| 55 | if (urv._dsize()>0){ |
|---|
| 56 | bdm_error ( "MemDS::urv is not supported" ); |
|---|
| 57 | } else { |
|---|
| 58 | DS::set_drv(drv,urv); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void write ( const vec &ut ) { |
|---|
| 64 | if (ut.size()>0){ |
|---|
| 65 | bdm_error ( "MemDS::write is not supported" ); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void write ( const vec &ut, const ivec &indices ) { |
|---|
| 70 | if (ut.size()>0){ |
|---|
| 71 | bdm_error ( "MemDS::write is not supported" ); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void step(); |
|---|
| 76 | |
|---|
| 77 | /*! Create object from the following structure |
|---|
| 78 | \code |
|---|
| 79 | DS.class = "MemDS"; |
|---|
| 80 | DS.Data = (...); % Data matrix or data vector |
|---|
| 81 | --- optional --- |
|---|
| 82 | DS.drv = {class="RV"; ...} % Identification how rows of the matrix Data will be known to others |
|---|
| 83 | DS.time = 0; % Index of the first column to user_info, |
|---|
| 84 | DS.log_level = "logdt,logut"; % switches to log (or not log) dt or ut |
|---|
| 85 | } |
|---|
| 86 | \endcode |
|---|
| 87 | |
|---|
| 88 | If the optional fields are not given, they will be filled as follows: |
|---|
| 89 | \code |
|---|
| 90 | drv = {names=("ch0", "ch1", "ch2", ..."number_of_rows_of_Data"); |
|---|
| 91 | sizes=( 1 1 1 ...); |
|---|
| 92 | times=( 0 0 0 ...); |
|---|
| 93 | }; |
|---|
| 94 | time = 0; |
|---|
| 95 | \endcode |
|---|
| 96 | If \c rowid is given, \c drv will be named after indices in rowids. |
|---|
| 97 | |
|---|
| 98 | Hence the data provided by method \c getdata() will be full column of matrix Data starting from the first record. |
|---|
| 99 | */ |
|---|
| 100 | void from_setting ( const Setting &set ); |
|---|
| 101 | |
|---|
| 102 | void validate(); |
|---|
| 103 | }; |
|---|
| 104 | UIREGISTER ( MemDS ); |
|---|
| 105 | |
|---|
| 106 | /*! Pseudovirtual class for reading data from files |
|---|
| 107 | |
|---|
| 108 | */ |
|---|
| 109 | class FileDS: public MemDS { |
|---|
| 110 | protected: |
|---|
| 111 | string filename; |
|---|
| 112 | public: |
|---|
| 113 | void from_setting ( const Setting & set ); |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | /*! |
|---|
| 117 | * \brief Read Data Matrix from an IT file |
|---|
| 118 | |
|---|
| 119 | 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$! |
|---|
| 120 | |
|---|
| 121 | */ |
|---|
| 122 | class ITppFileDS: public FileDS { |
|---|
| 123 | |
|---|
| 124 | public: |
|---|
| 125 | //! Convenience constructor |
|---|
| 126 | ITppFileDS ( const string &fname, const string &varname ) : FileDS() { |
|---|
| 127 | it_file it ( fname ); |
|---|
| 128 | it << Name ( varname ); |
|---|
| 129 | it >> Data; |
|---|
| 130 | time = 0; |
|---|
| 131 | //delays are ignored |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | ITppFileDS () : FileDS() { |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | void from_setting ( const Setting &set ); |
|---|
| 138 | |
|---|
| 139 | // TODO dodelat void to_setting( Setting &set ) const; |
|---|
| 140 | }; |
|---|
| 141 | UIREGISTER ( ITppFileDS ); |
|---|
| 142 | SHAREDPTR ( ITppFileDS ); |
|---|
| 143 | |
|---|
| 144 | /*! |
|---|
| 145 | * \brief CSV file data storage |
|---|
| 146 | The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types: |
|---|
| 147 | 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. |
|---|
| 148 | 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. |
|---|
| 149 | |
|---|
| 150 | */ |
|---|
| 151 | class CsvFileDS: public FileDS { |
|---|
| 152 | public: |
|---|
| 153 | void from_setting ( const Setting & set ); |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | // ARXDs - DELETED |
|---|
| 159 | |
|---|
| 160 | /*! \brief Simulate data from a static pdf (epdf) |
|---|
| 161 | |
|---|
| 162 | Trivial example of a data source, could be used for tests of some estimation algorithms. For example, simulating data from a mixture model and feeding them to mixture model estimators. |
|---|
| 163 | */ |
|---|
| 164 | |
|---|
| 165 | class EpdfDS: public DS { |
|---|
| 166 | protected: |
|---|
| 167 | //! internal pointer to epdf from which we samplecond |
|---|
| 168 | shared_ptr<epdf> iepdf; |
|---|
| 169 | //! internal storage of data sample |
|---|
| 170 | vec dt; |
|---|
| 171 | public: |
|---|
| 172 | void step() { |
|---|
| 173 | dt = iepdf->sample(); |
|---|
| 174 | } |
|---|
| 175 | void getdata ( vec &dt_out ) const { |
|---|
| 176 | dt_out = dt; |
|---|
| 177 | } |
|---|
| 178 | void getdata ( vec &dt_out, const ivec &ids ) { |
|---|
| 179 | dt_out = dt ( ids ); |
|---|
| 180 | } |
|---|
| 181 | const RV& _drv() const { |
|---|
| 182 | return iepdf->_rv(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | //! Accepts action variable and schedule it for application. |
|---|
| 186 | virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; |
|---|
| 187 | |
|---|
| 188 | //! Accepts action variables at specific indices |
|---|
| 189 | virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
|---|
| 190 | |
|---|
| 191 | /*! |
|---|
| 192 | \code |
|---|
| 193 | class = "EpdfDS"; |
|---|
| 194 | epdf = {class="epdf_offspring", ...}// uncondtitional density to sample from |
|---|
| 195 | \endcode |
|---|
| 196 | |
|---|
| 197 | */ |
|---|
| 198 | void from_setting ( const Setting &set ) { |
|---|
| 199 | iepdf = UI::build<epdf> ( set, "epdf", UI::compulsory ); |
|---|
| 200 | bdm_assert ( iepdf->isnamed(), "Input epdf must be named, check if RV is given correctly" ); |
|---|
| 201 | dt = zeros ( iepdf->dimension() ); |
|---|
| 202 | dtsize = dt.length(); |
|---|
| 203 | set_drv ( iepdf->_rv(), RV() ); |
|---|
| 204 | utsize = 0; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | void validate() { |
|---|
| 208 | DS::validate(); |
|---|
| 209 | |
|---|
| 210 | dt = iepdf->sample(); |
|---|
| 211 | } |
|---|
| 212 | }; |
|---|
| 213 | UIREGISTER ( EpdfDS ); |
|---|
| 214 | |
|---|
| 215 | /*! \brief Simulate data from conditional density |
|---|
| 216 | Still having only one density but allowing conditioning on either input or delayed values. |
|---|
| 217 | */ |
|---|
| 218 | class PdfDS : public DS { |
|---|
| 219 | protected: |
|---|
| 220 | //! internal pointer to epdf from which we samplecond |
|---|
| 221 | shared_ptr<pdf> ipdf; |
|---|
| 222 | //! internal storage of data sample |
|---|
| 223 | vec yt; |
|---|
| 224 | //! input vector |
|---|
| 225 | vec ut; |
|---|
| 226 | //! datalink between ut and regressor |
|---|
| 227 | datalink_buffered ut2rgr; |
|---|
| 228 | //! datalink between yt and regressor |
|---|
| 229 | datalink_buffered yt2rgr; |
|---|
| 230 | //! numeric values of regressor |
|---|
| 231 | vec rgr; |
|---|
| 232 | |
|---|
| 233 | public: |
|---|
| 234 | void step(); |
|---|
| 235 | |
|---|
| 236 | void getdata ( vec &dt_out ) const; |
|---|
| 237 | |
|---|
| 238 | void write ( const vec &ut0 ) { |
|---|
| 239 | ut = ut0; |
|---|
| 240 | } |
|---|
| 241 | void write ( const vec &ut0, const ivec &ind ) { |
|---|
| 242 | set_subvector ( ut, ind, ut0 ); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | //! Returns data records at indices. |
|---|
| 247 | virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | /*! |
|---|
| 251 | \code |
|---|
| 252 | class = "PdfDS"; |
|---|
| 253 | pdf = {class="pdf_offspring", ...}; // pdf to simulate |
|---|
| 254 | --- optional --- |
|---|
| 255 | init_rv = {class="RV",names=...}; // define what rv to initialize - typically delayed values! |
|---|
| 256 | init_values = [...]; // vector of initial values corresponding to init_rv |
|---|
| 257 | \endcode |
|---|
| 258 | |
|---|
| 259 | If init_rv is not given, init_values are set to zero. |
|---|
| 260 | */ |
|---|
| 261 | void from_setting ( const Setting &set ) { |
|---|
| 262 | ipdf = UI::build<pdf> ( set, "pdf", UI::compulsory ); |
|---|
| 263 | |
|---|
| 264 | RV Yrv = ipdf->_rv(); |
|---|
| 265 | // get unique rvs form rvc |
|---|
| 266 | RV rgrv0 = ipdf->_rvc().remove_time(); |
|---|
| 267 | // input is what in not in Yrv |
|---|
| 268 | Urv = rgrv0.subt ( Yrv ); |
|---|
| 269 | set_drv ( concat(Yrv,Urv), Urv ); |
|---|
| 270 | // connect input and output to rvc |
|---|
| 271 | ut2rgr.set_connection ( ipdf->_rvc(), Urv ); |
|---|
| 272 | yt2rgr.set_connection ( ipdf->_rvc(), Yrv ); |
|---|
| 273 | |
|---|
| 274 | //set history - if given |
|---|
| 275 | shared_ptr<RV> rv_ini = UI::build<RV> ( set, "init_rv", UI::optional ); |
|---|
| 276 | if ( rv_ini ) { // check if |
|---|
| 277 | vec val; |
|---|
| 278 | UI::get ( val, set, "init_values", UI::optional ); |
|---|
| 279 | if ( val.length() != rv_ini->_dsize() ) { |
|---|
| 280 | bdm_error ( "init_rv and init_values fields have incompatible sizes" ); |
|---|
| 281 | } else { |
|---|
| 282 | ut2rgr.set_history ( *rv_ini, val ); |
|---|
| 283 | yt2rgr.set_history ( *rv_ini, val ); |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | yt = zeros ( ipdf->dimension() ); |
|---|
| 288 | rgr = zeros ( ipdf->dimensionc() ); |
|---|
| 289 | ut = zeros ( Urv._dsize() ); |
|---|
| 290 | |
|---|
| 291 | utsize = ut.length(); |
|---|
| 292 | dtsize = yt.length() + utsize; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | void validate() { |
|---|
| 296 | DS::validate(); |
|---|
| 297 | |
|---|
| 298 | //taken from sample() - shift of history is not done here |
|---|
| 299 | ut2rgr.filldown ( ut, rgr ); |
|---|
| 300 | yt2rgr.filldown ( yt, rgr ); |
|---|
| 301 | yt = ipdf->samplecond ( rgr ); |
|---|
| 302 | } |
|---|
| 303 | }; |
|---|
| 304 | UIREGISTER ( PdfDS ); |
|---|
| 305 | |
|---|
| 306 | //! State-space data source simulating two densities |
|---|
| 307 | class StateDS : public DS { |
|---|
| 308 | protected: |
|---|
| 309 | //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ |
|---|
| 310 | shared_ptr<pdf> IM; |
|---|
| 311 | |
|---|
| 312 | //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ |
|---|
| 313 | shared_ptr<pdf> OM; |
|---|
| 314 | |
|---|
| 315 | //! result storage |
|---|
| 316 | vec dt; |
|---|
| 317 | //! state storage |
|---|
| 318 | vec xt; |
|---|
| 319 | //! input storage |
|---|
| 320 | vec ut; |
|---|
| 321 | |
|---|
| 322 | //! datalink from ut to IM.rvc |
|---|
| 323 | datalink_part u2imc; |
|---|
| 324 | //! datalink from ut to OM.rvc |
|---|
| 325 | datalink_part u2omc; |
|---|
| 326 | public: |
|---|
| 327 | void getdata ( vec &dt0 ) const { |
|---|
| 328 | dt0 = dt; |
|---|
| 329 | } |
|---|
| 330 | void write ( const vec &ut0 ) { |
|---|
| 331 | ut = ut0; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | void getdata ( vec &dt0, const ivec &indices ) { |
|---|
| 335 | dt0 = dt ( indices ); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | virtual void step(); |
|---|
| 339 | |
|---|
| 340 | //! set parameters |
|---|
| 341 | void set_parameters ( shared_ptr<pdf> IM0, shared_ptr<pdf> OM0 ) { |
|---|
| 342 | IM = IM0; |
|---|
| 343 | OM = OM0; |
|---|
| 344 | } |
|---|
| 345 | void set_initx ( const vec &x0 ) { |
|---|
| 346 | xt = x0; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
|---|
| 350 | |
|---|
| 351 | /*! UI for stateDS |
|---|
| 352 | |
|---|
| 353 | The DS is constructed from a structure with fields: |
|---|
| 354 | \code |
|---|
| 355 | class = "stateDS"; |
|---|
| 356 | //Internal model |
|---|
| 357 | IM = { type = "pdf-offspring"; }; |
|---|
| 358 | //Observation model |
|---|
| 359 | OM = { type = "pdf-offspring"; } |
|---|
| 360 | //initial state |
|---|
| 361 | x0 = [...]; //vector of data |
|---|
| 362 | \endcode |
|---|
| 363 | Both models must have defined \c rv. and \c rvc |
|---|
| 364 | Random variables not found in any rv are considered to be inputs. |
|---|
| 365 | */ |
|---|
| 366 | void from_setting ( const Setting &set ); |
|---|
| 367 | |
|---|
| 368 | // TODO dodelat void to_setting( Setting &set ) const; |
|---|
| 369 | |
|---|
| 370 | void validate(); |
|---|
| 371 | }; |
|---|
| 372 | |
|---|
| 373 | UIREGISTER ( StateDS ); |
|---|
| 374 | SHAREDPTR ( StateDS ); |
|---|
| 375 | |
|---|
| 376 | }; //namespace |
|---|
| 377 | |
|---|
| 378 | #endif // DS_H |
|---|