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

Revision 1063, 8.6 kB (checked in by mido, 14 years ago)

a small patch of documentation, to be contiuned..

  • 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 {
236protected:
237        //! internal pointer to epdf from which we samplecond
238        shared_ptr<pdf> ipdf;
239        //! internal storage of data sample
240        vec yt;
241        //! input vector
242        vec ut;
243        //! datalink between ut and regressor
244        datalink_buffered ut2rgr;
245        //! datalink between yt and regressor
246        datalink_buffered yt2rgr;
247        //! numeric values of regressor
248        vec rgr;
249
250public:
251        void step();
252
253        void getdata ( vec &dt_out ) const;
254
255        void write ( const vec &ut0 ) {
256                ut = ut0;
257        }
258        void write ( const vec &ut0, const ivec &ind ) {
259                set_subvector ( ut, ind, ut0 );
260        }
261
262
263        //! Returns data records at indices.
264        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
265
266
267        /*! Create object from the following structure
268        \code
269        class = 'PdfDS';
270        pdf = configuration of bdm::pdf;          % any offspring of pdf, bdm::pdf::from_setting
271        --- optional fields ---
272        init_rv = RV({'names',...},[sizes,...],[times,...]);   % define what rv to initialize - typically delayed values, time=-1, etc.!
273        init_values = [...];                      % vector of initial values corresponding to init_rv
274        \endcode
275        Class does not call from bdm::DS::from_setting, names of data are taken from pdf.rv and pdf.rvc.
276       
277        If init_rv is not given, init_values are set to zero.
278        */
279        void from_setting ( const Setting &set ) {
280                ipdf = UI::build<pdf> ( set, "pdf", UI::compulsory );
281
282                RV Yrv = ipdf->_rv();
283                // get unique rvs form rvc
284                RV rgrv0 = ipdf->_rvc().remove_time();
285                // input is what in not in Yrv
286                Urv = rgrv0.subt ( Yrv );
287                Drv=  concat(Yrv,Urv);
288                // connect input and output to rvc
289                ut2rgr.set_connection ( ipdf->_rvc(), Urv );
290                yt2rgr.set_connection ( ipdf->_rvc(), Yrv );
291
292                //set history - if given
293                shared_ptr<RV> rv_ini = UI::build<RV> ( set, "init_rv", UI::optional );
294                if ( rv_ini ) { // check if
295                        vec val;
296                        UI::get ( val, set, "init_values", UI::optional );
297                        if ( val.length() != rv_ini->_dsize() ) {
298                                bdm_error ( "init_rv and init_values fields have incompatible sizes" );
299                        } else {
300                                ut2rgr.set_history ( *rv_ini, val );
301                                yt2rgr.set_history ( *rv_ini, val );
302                        }
303                }
304
305                yt = zeros ( ipdf->dimension() );
306                rgr = zeros ( ipdf->dimensionc() );
307                ut = zeros ( Urv._dsize() );
308
309                utsize = ut.length();
310                dtsize = yt.length() + utsize;
311        }
312
313        void validate() {
314                DS::validate();
315
316                //taken from sample() - shift of history is not done here
317                ut2rgr.filldown ( ut, rgr );
318                yt2rgr.filldown ( yt, rgr );
319                yt = ipdf->samplecond ( rgr );
320        }
321};
322UIREGISTER ( PdfDS );
323
324}; //namespace
325
326#endif // DS_H
Note: See TracBrowser for help on using the browser.