root/library/bdm/base/datasources.h @ 1059

Revision 1059, 7.9 kB (checked in by smidl, 14 years ago)

doc

  • Property svn:eol-style set to native
Line 
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
20namespace bdm {
21/*!
22* \brief Memory storage of off-line data column-wise (used mostly in C++)
23
24The data are stored in an internal matrix \c Data .
25Each column of Data corresponds to one discrete time observation \f$t\f$.
26
27*/
28class MemDS : public DS {
29protected:
30        //! internal matrix of data
31        mat Data;
32        //! active column in the Data matrix
33        int time;
34
35public:
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 write ( const vec &ut ) {
52                if (ut.size()>0){
53                        bdm_error ( "MemDS::write is not supported" );
54                }
55        }
56
57        void write ( const vec &ut, const ivec &indices ) {
58                if (ut.size()>0){
59                        bdm_error ( "MemDS::write is not supported" );
60                }
61        }
62
63        void step();
64
65        /*! Create object from the following structure
66        \code
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 ---
72        \endcode
73
74        If the optional fields are not given, they will be filled as follows:
75        \code
76        drv.names=("");
77        drv.sizes=( no_rows_Data );
78        time = 0;
79        \endcode
80        If \c rowid is given, \c drv will be named after indices in rowids.
81
82        */
83        void from_setting ( const Setting &set );
84
85        void validate();
86};
87UIREGISTER ( MemDS );
88
89/*! \brief Pseudovirtual class for reading data from files
90
91Common predecessord to various file formats.
92*/
93class FileDS: public MemDS {
94protected:
95        string filename;
96public:
97        /*! reads what DS::from_setting
98        \code
99        filename = 'data_file.ext';           // name of the file where the data sare stored
100        --- fields from bdm::DS ---
101        \endcode
102        */
103        void from_setting ( const Setting & set );
104};
105
106/*!
107* \brief Read Data Matrix from an IT file
108
109The 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*/
112class ITppFileDS: public FileDS {
113
114public:
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;
121                //delays are ignored
122        };
123
124        ITppFileDS () : FileDS() {
125        };
126
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        */
134        void from_setting ( const Setting &set );
135
136        // TODO dodelat void to_setting( Setting &set ) const;
137};
138UIREGISTER ( ITppFileDS );
139SHAREDPTR ( ITppFileDS );
140
141/*!
142* \brief CSV file data storage
143
144The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types:
1451. \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item.
1462. \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*/
149class CsvFileDS: public FileDS {
150public:
151        void from_setting ( const Setting & set );
152};
153
154
155
156// ARXDs - DELETED
157
158/*!  \brief Simulate data from a static pdf (epdf)
159
160Trivial 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
163class EpdfDS: public DS {
164protected:
165        //! internal pointer to epdf from which we samplecond
166        shared_ptr<epdf> iepdf;
167        //! internal storage of data sample
168        vec dt;
169public:
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        }
182
183        //! Accepts action variable and schedule it for application.
184        virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID;
185
186        //! Accepts action variables at specific indices
187        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
188
189        /*!
190        \code
191        class = "EpdfDS";
192        epdf = {class="epdf_offspring", ...}// uncondtitional density to sample from
193        \endcode
194
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();
201                Drv = iepdf->_rv();
202                utsize = 0;
203        }
204
205        void validate() {
206                DS::validate();
207
208                dt = iepdf->sample();
209        }
210};
211UIREGISTER ( EpdfDS );
212
213/*!  \brief Simulate data from conditional density
214
215Data are simulated by sampling from conditional pdf.
216
217The 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
221Note: 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.
222*/
223class PdfDS : public DS {
224protected:
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;
237
238public:
239        void step();
240
241        void getdata ( vec &dt_out ) const;
242
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
250
251        //! Returns data records at indices.
252        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
253
254
255        /*!
256        \code
257        class = "PdfDS";
258        pdf = bdm::pdf::from_setting;          // pdf to simulate, any offspring of pdf
259        --- optional ---
260        init_rv = bdm::RV::from_setting;       // define what rv to initialize - typically delayed values!
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
269                RV Yrv = ipdf->_rv();
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 );
274                Drv=  concat(Yrv,Urv);
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 );
289                        }
290                }
291
292                yt = zeros ( ipdf->dimension() );
293                rgr = zeros ( ipdf->dimensionc() );
294                ut = zeros ( Urv._dsize() );
295
296                utsize = ut.length();
297                dtsize = yt.length() + utsize;
298        }
299
300        void validate() {
301                DS::validate();
302
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        }
308};
309UIREGISTER ( PdfDS );
310
311}; //namespace
312
313#endif // DS_H
Note: See TracBrowser for help on using the browser.