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