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

Revision 1072, 9.4 kB (checked in by smidl, 14 years ago)

new merger

  • 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 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
74
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
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    /*! 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 );
105};
106
107/*!
108* \brief Read Data Matrix from an IT file
109
110The 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*/
113class ITppFileDS: public FileDS {
114
115public:
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    };
124
125    ITppFileDS () : FileDS() {
126    };
127
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 );
138
139    // TODO dodelat void to_setting( Setting &set ) const;
140};
141UIREGISTER ( ITppFileDS );
142SHAREDPTR ( ITppFileDS );
143
144/*!
145* \brief CSV file data storage
146
147The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types:
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.
150
151*/
152class CsvFileDS: public FileDS {
153public:
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 );
164};
165
166
167
168// ARXDs - DELETED
169
170/*!  \brief Simulate data from a static pdf (epdf)
171
172Trivial 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
175class EpdfDS: public DS {
176protected:
177    //! internal pointer to epdf from which we samplecond
178    shared_ptr<epdf> iepdf;
179    //! internal storage of data sample
180    vec dt;
181public:
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    }
194
195    //! Accepts action variable and schedule it for application.
196    virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID;
197
198    //! Accepts action variables at specific indices
199    virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
200
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    }
216
217    void validate() {
218        DS::validate();
219
220        dt = iepdf->sample();
221    }
222};
223UIREGISTER ( EpdfDS );
224
225/*!  \brief Simulate data from conditional density
226
227Data are simulated by sampling from conditional pdf.
228
229The 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
233Note: 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.
234*/
235class PdfDS : public DS {
236public:
237    //! internal pointer to epdf from which we samplecond
238    shared_ptr<pdf> ipdf;
239protected:
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;
250
251public:
252    void step();
253
254    void getdata ( vec &dt_out ) const;
255
256    void write ( const vec &ut0 ) {
257        ut = ut0;
258    }
259    void write ( const vec &ut0, const ivec &ind ) {
260        set_subvector ( ut, ind, ut0 );
261    }
262
263
264    //! Returns data records at indices.
265    virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
266
267
268    /*! Create object from the following structure
269    \code
270    class = 'PdfDS';
271    pdf = configuration of bdm::pdf;          % any offspring of pdf, bdm::pdf::from_setting
272    --- optional fields ---
273    init_rv = RV({'names',...},[sizes,...],[times,...]);   % define what rv to initialize - typically delayed values, time=-1, etc.!
274    init_values = [...];                      % vector of initial values corresponding to init_rv
275    \endcode
276    Class does not call from bdm::DS::from_setting, names of data are taken from pdf.rv and pdf.rvc.
277
278    If init_rv is not given, init_values are set to zero.
279    */
280    void from_setting ( const Setting &set ) {
281        ipdf = UI::build<pdf> ( set, "pdf", UI::compulsory );
282
283        RV Yrv = ipdf->_rv();
284        // get unique rvs form rvc
285        RV rgrv0 = ipdf->_rvc().remove_time();
286        // input is what in not in Yrv
287        Urv = rgrv0.subt ( Yrv );
288        Drv=  concat(Yrv,Urv);
289        // connect input and output to rvc
290        ut2rgr.set_connection ( ipdf->_rvc(), Urv );
291        yt2rgr.set_connection ( ipdf->_rvc(), Yrv );
292
293        //set history - if given
294        shared_ptr<RV> rv_ini = UI::build<RV> ( set, "init_rv", UI::optional );
295        if ( rv_ini ) { // check if
296            vec val;
297            UI::get ( val, set, "init_values", UI::optional );
298            if ( val.length() != rv_ini->_dsize() ) {
299                bdm_error ( "init_rv and init_values fields have incompatible sizes" );
300            } else {
301                ut2rgr.set_history ( *rv_ini, val );
302                yt2rgr.set_history ( *rv_ini, val );
303            }
304        }
305
306        yt = zeros ( ipdf->dimension() );
307        rgr = zeros ( ipdf->dimensionc() );
308        ut = zeros ( Urv._dsize() );
309
310        utsize = ut.length();
311        dtsize = yt.length() + utsize;
312    }
313
314    void validate() {
315        DS::validate();
316
317        //taken from sample() - shift of history is not done here
318        ut2rgr.filldown ( ut, rgr );
319        yt2rgr.filldown ( yt, rgr );
320        yt = ipdf->samplecond ( rgr );
321    }
322};
323UIREGISTER ( PdfDS );
324
325}; //namespace
326
327#endif // DS_H
Note: See TracBrowser for help on using the browser.