[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 | |
---|
[384] | 13 | #ifndef DATASOURCE_H |
---|
| 14 | #define DATASOURCE_H |
---|
[18] | 15 | |
---|
[384] | 16 | #include "../base/bdmbase.h" |
---|
| 17 | #include "../stat/exp_family.h" |
---|
| 18 | #include "../base/user_info.h" |
---|
[18] | 19 | |
---|
[263] | 20 | namespace bdm { |
---|
[271] | 21 | /*! |
---|
[1058] | 22 | * \brief Memory storage of off-line data column-wise (used mostly in C++) |
---|
[18] | 23 | |
---|
[1064] | 24 | The data are stored in an internal matrix \c Data . |
---|
| 25 | Each column of Data corresponds to one discrete time observation \f$t\f$. |
---|
[18] | 26 | |
---|
[271] | 27 | */ |
---|
| 28 | class MemDS : public DS { |
---|
[737] | 29 | protected: |
---|
[1064] | 30 | //! internal matrix of data |
---|
| 31 | mat Data; |
---|
| 32 | //! active column in the Data matrix |
---|
| 33 | int time; |
---|
[18] | 34 | |
---|
[737] | 35 | public: |
---|
[1064] | 36 | //!Default constructor |
---|
| 37 | MemDS () {}; |
---|
[896] | 38 | |
---|
[1064] | 39 | //! Convenience constructor |
---|
| 40 | MemDS ( mat &Dat ); |
---|
[896] | 41 | |
---|
[1064] | 42 | //! returns number of data in the file; |
---|
| 43 | int max_length() { |
---|
| 44 | return Data.cols(); |
---|
| 45 | } |
---|
[896] | 46 | |
---|
[1064] | 47 | void getdata ( vec &dt ) const; |
---|
[565] | 48 | |
---|
[1064] | 49 | void getdata ( vec &dt, const ivec &indices ); |
---|
[896] | 50 | |
---|
[1064] | 51 | void write ( const vec &ut ) { |
---|
| 52 | if (ut.size()>0) { |
---|
| 53 | bdm_error ( "MemDS::write is not supported" ); |
---|
| 54 | } |
---|
| 55 | } |
---|
[565] | 56 | |
---|
[1064] | 57 | void write ( const vec &ut, const ivec &indices ) { |
---|
| 58 | if (ut.size()>0) { |
---|
| 59 | bdm_error ( "MemDS::write is not supported" ); |
---|
| 60 | } |
---|
| 61 | } |
---|
[565] | 62 | |
---|
[1064] | 63 | void step(); |
---|
[896] | 64 | |
---|
[1064] | 65 | /*! Create object from the following structure |
---|
| 66 | \code |
---|
| 67 | class = 'MemDS'; |
---|
| 68 | Data = [...]; % Data matrix with records stored in columns |
---|
| 69 | --- optional fields --- |
---|
| 70 | time = 0; % Index of the first column in the matrix Data |
---|
| 71 | --- inherited fields --- |
---|
| 72 | bdm::DS::from_setting |
---|
| 73 | \endcode |
---|
[737] | 74 | |
---|
[1064] | 75 | If the optional fields are not given, they will be filled as follows: |
---|
| 76 | \code |
---|
| 77 | drv.names = { "" }; % empty name |
---|
| 78 | drv.sizes = { no_rows_Data }; % full size of the record |
---|
| 79 | time = 0; |
---|
| 80 | \endcode |
---|
[737] | 81 | |
---|
[1064] | 82 | */ |
---|
| 83 | void from_setting ( const Setting &set ); |
---|
[907] | 84 | |
---|
[1064] | 85 | void validate(); |
---|
[271] | 86 | }; |
---|
[737] | 87 | UIREGISTER ( MemDS ); |
---|
[263] | 88 | |
---|
[1058] | 89 | /*! \brief Pseudovirtual class for reading data from files |
---|
[892] | 90 | |
---|
[1059] | 91 | Common predecessord to various file formats. |
---|
[892] | 92 | */ |
---|
| 93 | class FileDS: public MemDS { |
---|
[896] | 94 | protected: |
---|
[1064] | 95 | string filename; |
---|
[892] | 96 | public: |
---|
[1064] | 97 | /*! Create object from the following structure |
---|
| 98 | \code |
---|
| 99 | filename = 'data_file.ext'; % name of the file where the data sare stored |
---|
| 100 | --- inherited fields --- |
---|
| 101 | bdm::DS::from_setting |
---|
| 102 | \endcode |
---|
| 103 | */ |
---|
| 104 | void from_setting ( const Setting & set ); |
---|
[892] | 105 | }; |
---|
| 106 | |
---|
| 107 | /*! |
---|
| 108 | * \brief Read Data Matrix from an IT file |
---|
| 109 | |
---|
| 110 | 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$! |
---|
| 111 | |
---|
| 112 | */ |
---|
| 113 | class ITppFileDS: public FileDS { |
---|
| 114 | |
---|
| 115 | public: |
---|
[1064] | 116 | //! Convenience constructor |
---|
| 117 | ITppFileDS ( const string &fname, const string &varname ) : FileDS() { |
---|
| 118 | it_file it ( fname ); |
---|
| 119 | it << Name ( varname ); |
---|
| 120 | it >> Data; |
---|
| 121 | time = 0; |
---|
| 122 | //delays are ignored |
---|
| 123 | }; |
---|
[892] | 124 | |
---|
[1064] | 125 | ITppFileDS () : FileDS() { |
---|
| 126 | }; |
---|
[892] | 127 | |
---|
[1064] | 128 | /*! Create object from the following structure |
---|
| 129 | \code |
---|
| 130 | class = 'ITppFileDS'; |
---|
| 131 | filename = 'file_with_data.it'; |
---|
| 132 | varname = 'Data1'; // Name of a variable in which the data are stored |
---|
| 133 | --- inherited fields --- |
---|
| 134 | bdm::DS::from_setting |
---|
| 135 | \endcode |
---|
| 136 | */ |
---|
| 137 | void from_setting ( const Setting &set ); |
---|
[892] | 138 | |
---|
[1064] | 139 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
[892] | 140 | }; |
---|
| 141 | UIREGISTER ( ITppFileDS ); |
---|
| 142 | SHAREDPTR ( ITppFileDS ); |
---|
| 143 | |
---|
| 144 | /*! |
---|
| 145 | * \brief CSV file data storage |
---|
[1058] | 146 | |
---|
[892] | 147 | The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types: |
---|
[1060] | 148 | -# \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item. |
---|
| 149 | -# \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. |
---|
[892] | 150 | |
---|
| 151 | */ |
---|
| 152 | class CsvFileDS: public FileDS { |
---|
| 153 | public: |
---|
[1064] | 154 | /*! Create object from the following structure |
---|
| 155 | \code |
---|
| 156 | class = 'CsvFileDS'; |
---|
| 157 | filename = 'file.csv'; |
---|
| 158 | orientation = 'BY_ROW' or 'BY_COL'; % data records are stored in rows of columns (default). |
---|
| 159 | --- inherited fields --- |
---|
| 160 | bdm::DS::from_setting |
---|
| 161 | \endcode |
---|
| 162 | */ |
---|
| 163 | void from_setting ( const Setting & set ); |
---|
[892] | 164 | }; |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | // ARXDs - DELETED |
---|
| 169 | |
---|
[611] | 170 | /*! \brief Simulate data from a static pdf (epdf) |
---|
| 171 | |
---|
[598] | 172 | 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. |
---|
| 173 | */ |
---|
| 174 | |
---|
| 175 | class EpdfDS: public DS { |
---|
[737] | 176 | protected: |
---|
[1064] | 177 | //! internal pointer to epdf from which we samplecond |
---|
| 178 | shared_ptr<epdf> iepdf; |
---|
| 179 | //! internal storage of data sample |
---|
| 180 | vec dt; |
---|
[737] | 181 | public: |
---|
[1064] | 182 | void step() { |
---|
| 183 | dt = iepdf->sample(); |
---|
| 184 | } |
---|
| 185 | void getdata ( vec &dt_out ) const { |
---|
| 186 | dt_out = dt; |
---|
| 187 | } |
---|
| 188 | void getdata ( vec &dt_out, const ivec &ids ) { |
---|
| 189 | dt_out = dt ( ids ); |
---|
| 190 | } |
---|
| 191 | const RV& _drv() const { |
---|
| 192 | return iepdf->_rv(); |
---|
| 193 | } |
---|
[598] | 194 | |
---|
[1064] | 195 | //! Accepts action variable and schedule it for application. |
---|
| 196 | virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; |
---|
[892] | 197 | |
---|
[1064] | 198 | //! Accepts action variables at specific indices |
---|
| 199 | virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
---|
[766] | 200 | |
---|
[1064] | 201 | /*! Create object from the following structure |
---|
| 202 | \code |
---|
| 203 | class = 'EpdfDS'; |
---|
| 204 | epdf = configuration of bdm::epdf % unconditional density to sample from, bdm::epdf::from_setting |
---|
| 205 | \endcode |
---|
| 206 | Does not inherit from bdm::DS::from_setting, names of data are taken from epdf.rv. |
---|
| 207 | */ |
---|
| 208 | void from_setting ( const Setting &set ) { |
---|
| 209 | iepdf = UI::build<epdf> ( set, "epdf", UI::compulsory ); |
---|
| 210 | bdm_assert ( iepdf->isnamed(), "Input epdf must be named, check if RV is given correctly" ); |
---|
| 211 | dt = zeros ( iepdf->dimension() ); |
---|
| 212 | dtsize = dt.length(); |
---|
| 213 | Drv = iepdf->_rv(); |
---|
| 214 | utsize = 0; |
---|
| 215 | } |
---|
[795] | 216 | |
---|
[1064] | 217 | void validate() { |
---|
| 218 | DS::validate(); |
---|
[795] | 219 | |
---|
[1064] | 220 | dt = iepdf->sample(); |
---|
| 221 | } |
---|
[598] | 222 | }; |
---|
| 223 | UIREGISTER ( EpdfDS ); |
---|
| 224 | |
---|
| 225 | /*! \brief Simulate data from conditional density |
---|
[1059] | 226 | |
---|
| 227 | Data are simulated by sampling from conditional pdf. |
---|
| 228 | |
---|
| 229 | The condition can be of two types: |
---|
| 230 | -# delayed output values: these are cached internally, initiated via \c init_rv and \c init_values. |
---|
| 231 | -# input values: remaining RVs in the rvc of the given pdf are considered to be inputs and are declared as \c urv |
---|
| 232 | |
---|
| 233 | Note: a classical state-space model can be simulated as an instance of this case since it can be seen as a chain rule on state-evolution pdf and observation pdf. See class bdm::mprod and other offsprings of pdf. |
---|
[598] | 234 | */ |
---|
[737] | 235 | class PdfDS : public DS { |
---|
[1072] | 236 | public: |
---|
[1064] | 237 | //! internal pointer to epdf from which we samplecond |
---|
| 238 | shared_ptr<pdf> ipdf; |
---|
[1072] | 239 | protected: |
---|
[1064] | 240 | //! internal storage of data sample |
---|
| 241 | vec yt; |
---|
| 242 | //! input vector |
---|
| 243 | vec ut; |
---|
| 244 | //! datalink between ut and regressor |
---|
| 245 | datalink_buffered ut2rgr; |
---|
| 246 | //! datalink between yt and regressor |
---|
| 247 | datalink_buffered yt2rgr; |
---|
| 248 | //! numeric values of regressor |
---|
| 249 | vec rgr; |
---|
[1080] | 250 | |
---|
| 251 | //! RV for initial conditions |
---|
| 252 | RV init_rv; |
---|
| 253 | //! initial conditions |
---|
| 254 | vec init_values; |
---|
| 255 | |
---|
[737] | 256 | public: |
---|
[1064] | 257 | void step(); |
---|
[738] | 258 | |
---|
[1064] | 259 | void getdata ( vec &dt_out ) const; |
---|
[738] | 260 | |
---|
[1064] | 261 | void write ( const vec &ut0 ) { |
---|
| 262 | ut = ut0; |
---|
| 263 | } |
---|
| 264 | void write ( const vec &ut0, const ivec &ind ) { |
---|
| 265 | set_subvector ( ut, ind, ut0 ); |
---|
| 266 | } |
---|
[737] | 267 | |
---|
[766] | 268 | |
---|
[1064] | 269 | //! Returns data records at indices. |
---|
| 270 | virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
---|
[766] | 271 | |
---|
| 272 | |
---|
[1064] | 273 | /*! Create object from the following structure |
---|
| 274 | \code |
---|
| 275 | class = 'PdfDS'; |
---|
| 276 | pdf = configuration of bdm::pdf; % any offspring of pdf, bdm::pdf::from_setting |
---|
| 277 | --- optional fields --- |
---|
| 278 | init_rv = RV({'names',...},[sizes,...],[times,...]); % define what rv to initialize - typically delayed values, time=-1, etc.! |
---|
| 279 | init_values = [...]; % vector of initial values corresponding to init_rv |
---|
| 280 | \endcode |
---|
| 281 | Class does not call from bdm::DS::from_setting, names of data are taken from pdf.rv and pdf.rvc. |
---|
[737] | 282 | |
---|
[1064] | 283 | If init_rv is not given, init_values are set to zero. |
---|
| 284 | */ |
---|
| 285 | void from_setting ( const Setting &set ) { |
---|
[1080] | 286 | ipdf = UI::build<pdf> ( set, "pdf", UI::compulsory ); |
---|
| 287 | UI::get ( init_rv, set, "init_rv", UI::optional); |
---|
| 288 | UI::get ( init_values, set, "init_values", UI::optional); |
---|
[1064] | 289 | } |
---|
[795] | 290 | |
---|
[1064] | 291 | void validate() { |
---|
[1080] | 292 | RV Yrv = ipdf->_rv(); |
---|
| 293 | // get unique rvs form rvc |
---|
| 294 | RV rgrv0 = ipdf->_rvc().remove_time(); |
---|
| 295 | // input is what in not in Yrv |
---|
| 296 | Urv = rgrv0.subt ( Yrv ); |
---|
| 297 | Drv= concat(Yrv,Urv); |
---|
| 298 | // connect input and output to rvc |
---|
| 299 | ut2rgr.set_connection ( ipdf->_rvc(), Urv ); |
---|
| 300 | yt2rgr.set_connection ( ipdf->_rvc(), Yrv ); |
---|
| 301 | |
---|
| 302 | if ( init_rv._dsize()>0 ) { // check if |
---|
| 303 | if ( init_values.length() != init_rv._dsize() ) { |
---|
| 304 | bdm_error ( "init_rv and init_values fields have incompatible sizes" ); |
---|
| 305 | } else { |
---|
| 306 | ut2rgr.set_history ( init_rv, init_values ); |
---|
| 307 | yt2rgr.set_history ( init_rv, init_values ); |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | yt = zeros ( ipdf->dimension() ); |
---|
| 312 | rgr = zeros ( ipdf->dimensionc() ); |
---|
| 313 | ut = zeros ( Urv._dsize() ); |
---|
| 314 | |
---|
| 315 | utsize = ut.length(); |
---|
| 316 | dtsize = yt.length() + utsize; |
---|
| 317 | |
---|
| 318 | DS::validate(); |
---|
[1064] | 319 | |
---|
| 320 | //taken from sample() - shift of history is not done here |
---|
| 321 | ut2rgr.filldown ( ut, rgr ); |
---|
| 322 | yt2rgr.filldown ( yt, rgr ); |
---|
| 323 | yt = ipdf->samplecond ( rgr ); |
---|
| 324 | } |
---|
[598] | 325 | }; |
---|
[693] | 326 | UIREGISTER ( PdfDS ); |
---|
[598] | 327 | |
---|
[254] | 328 | }; //namespace |
---|
[18] | 329 | |
---|
| 330 | #endif // DS_H |
---|