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

Revision 910, 8.9 kB (checked in by mido, 14 years ago)

patch of LOG LEVELS

  • 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
23
24The data are stored in an internal matrix \c Data . Each column of Data corresponds to one discrete time observation \f$t\f$. Access to this matrix is via indices \c rowid.
25
26The data can be loaded from a file.
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 set_drv ( const RV &drv );
52
53        void set_drv ( const RV &drv, const RV &urv )
54        {
55                bdm_error ( "MemDS::urv is not supported" );
56        }
57
58        void write ( const vec &ut ) {
59                bdm_error ( "MemDS::write is not supported" );
60        }
61
62        void write ( const vec &ut, const ivec &indices ) {
63                bdm_error ( "MemDS::write is not supported" );
64        }
65
66        void step();
67
68        /*! Create object from the following structure
69        \code
70        { class = "MemDS";
71           Data = (...);            // Data matrix or data vector
72           --- optional ---
73           drv = {class="RV"; ...} // Identification how rows of the matrix Data will be known to others
74           time = 0;               // Index of the first column to user_info,
75        }
76        \endcode
77
78        If the optional fields are not given, they will be filled as follows:
79        \code
80        drv = {names=("ch0", "ch1", "ch2", ..."number_of_rows_of_Data");
81              sizes=( 1    1    1 ...);
82                  times=( 0    0    0 ...);
83                  };
84        time = 0;
85        \endcode
86        If \c rowid is given, \c drv will be named after indices in rowids.
87
88        Hence the data provided by method \c getdata() will be full column of matrix Data starting from the first record.
89        */
90        void from_setting ( const Setting &set );
91
92        void validate();
93};
94UIREGISTER ( MemDS );
95
96/*! Pseudovirtual class for reading data from files
97
98*/
99class FileDS: public MemDS {
100protected:
101        string filename;
102public:
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        void from_setting ( const Setting &set );
128
129        // TODO dodelat void to_setting( Setting &set ) const;
130};
131UIREGISTER ( ITppFileDS );
132SHAREDPTR ( ITppFileDS );
133
134/*!
135* \brief CSV file data storage
136The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types:
1371. \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item.
1382. \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.
139
140*/
141class CsvFileDS: public FileDS {
142public:
143        void from_setting ( const Setting & set );
144};
145
146
147
148// ARXDs - DELETED
149
150/*!  \brief Simulate data from a static pdf (epdf)
151
152Trivial 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.
153*/
154
155class EpdfDS: public DS {
156protected:
157        //! internal pointer to epdf from which we samplecond
158        shared_ptr<epdf> iepdf;
159        //! internal storage of data sample
160        vec dt;
161public:
162        void step() {
163                dt = iepdf->sample();
164        }
165        void getdata ( vec &dt_out ) const {
166                dt_out = dt;
167        }
168        void getdata ( vec &dt_out, const ivec &ids ) {
169                dt_out = dt ( ids );
170        }
171        const RV& _drv() const {
172                return iepdf->_rv();
173        }
174
175        //! Accepts action variable and schedule it for application.
176        virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID;
177
178        //! Accepts action variables at specific indices
179        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
180
181        /*!
182        \code
183        class = "EpdfDS";
184        epdf = {class="epdf_offspring", ...}// uncondtitional density to sample from
185        \endcode
186
187        */
188        void from_setting ( const Setting &set ) {
189                iepdf = UI::build<epdf> ( set, "epdf", UI::compulsory );
190                bdm_assert ( iepdf->isnamed(), "Input epdf must be named, check if RV is given correctly" );
191                dt =  zeros ( iepdf->dimension() );
192                dtsize = dt.length();
193                set_drv ( iepdf->_rv(), RV() );
194                utsize = 0;
195        }
196
197        void validate() {
198                DS::validate();
199
200                dt = iepdf->sample();
201        }
202};
203UIREGISTER ( EpdfDS );
204
205/*!  \brief Simulate data from conditional density
206Still having only one density but allowing conditioning on either input or delayed values.
207*/
208class PdfDS : public DS {
209protected:
210        //! internal pointer to epdf from which we samplecond
211        shared_ptr<pdf> ipdf;
212        //! internal storage of data sample
213        vec yt;
214        //! input vector
215        vec ut;
216        //! datalink between ut and regressor
217        datalink_buffered ut2rgr;
218        //! datalink between yt and regressor
219        datalink_buffered yt2rgr;
220        //! numeric values of regressor
221        vec rgr;
222
223public:
224        void step();
225
226        void getdata ( vec &dt_out ) const;
227
228        void write ( const vec &ut0 ) {
229                ut = ut0;
230        }
231        void write ( const vec &ut0, const ivec &ind ) {
232                set_subvector ( ut, ind, ut0 );
233        }
234
235
236        //! Returns data records at indices.
237        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
238
239
240        /*!
241        \code
242        class = "PdfDS";
243        pdf = {class="pdf_offspring", ...};  // pdf to simulate
244        --- optional ---
245        init_rv = {class="RV",names=...};      // define what rv to initialize - typically delayed values!
246        init_values = [...];                   // vector of initial values corresponding to init_rv
247        \endcode
248
249        If init_rv is not given, init_values are set to zero.
250        */
251        void from_setting ( const Setting &set ) {
252                ipdf = UI::build<pdf> ( set, "pdf", UI::compulsory );
253
254                RV Yrv = ipdf->_rv();
255                // get unique rvs form rvc
256                RV rgrv0 = ipdf->_rvc().remove_time();
257                // input is what in not in Yrv
258                Urv = rgrv0.subt ( Yrv );
259                set_drv ( concat(Yrv,Urv), Urv );
260                // connect input and output to rvc
261                ut2rgr.set_connection ( ipdf->_rvc(), Urv );
262                yt2rgr.set_connection ( ipdf->_rvc(), Yrv );
263
264                //set history - if given
265                shared_ptr<RV> rv_ini = UI::build<RV> ( set, "init_rv", UI::optional );
266                if ( rv_ini ) { // check if
267                        vec val;
268                        UI::get ( val, set, "init_values", UI::optional );
269                        if ( val.length() != rv_ini->_dsize() ) {
270                                bdm_error ( "init_rv and init_values fields have incompatible sizes" );
271                        } else {
272                                ut2rgr.set_history ( *rv_ini, val );
273                                yt2rgr.set_history ( *rv_ini, val );
274                        }
275                }
276
277                yt = zeros ( ipdf->dimension() );
278                rgr = zeros ( ipdf->dimensionc() );
279                ut = zeros ( Urv._dsize() );
280
281                utsize = ut.length();
282                dtsize = yt.length() + utsize;
283        }
284
285        void validate() {
286                DS::validate();
287
288                //taken from sample() - shift of history is not done here
289                ut2rgr.filldown ( ut, rgr );
290                yt2rgr.filldown ( yt, rgr );
291                yt = ipdf->samplecond ( rgr );
292        }
293};
294UIREGISTER ( PdfDS );
295
296//! State-space data source simulating two densities
297class StateDS : public DS {
298protected:
299        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
300        shared_ptr<pdf> IM;
301
302        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$
303        shared_ptr<pdf> OM;
304
305        //! result storage
306        vec dt;
307        //! state storage
308        vec xt;
309        //! input storage
310        vec ut;
311
312        //! datalink from ut to IM.rvc
313        datalink_part u2imc;
314        //! datalink from ut to OM.rvc
315        datalink_part u2omc;
316public:
317        void getdata ( vec &dt0 ) const {
318                dt0 = dt;
319        }
320        void write ( const vec &ut0 ) {
321                ut = ut0;
322        }
323
324        void getdata ( vec &dt0, const ivec &indices ) {
325                dt0 = dt ( indices );
326        }
327
328        virtual void step();
329
330        //! set parameters
331        void set_parameters ( shared_ptr<pdf> IM0, shared_ptr<pdf> OM0 ) {
332                IM = IM0;
333                OM = OM0;
334        }
335        void set_initx ( const vec &x0 ) {
336                xt = x0;
337        }
338
339        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
340
341        /*! UI for stateDS
342
343        The DS is constructed from a structure with fields:
344        \code
345        class = "stateDS";
346        //Internal model
347        IM = { type = "pdf-offspring"; };
348        //Observation model
349        OM = { type = "pdf-offspring"; }
350        //initial state
351        x0 = [...]; //vector of data
352        \endcode
353        Both models must have defined \c rv. and \c rvc
354        Random variables not found in any rv are considered to be inputs.
355        */
356        void from_setting ( const Setting &set );
357
358        // TODO dodelat void to_setting( Setting &set ) const;
359
360        void validate();
361};
362
363UIREGISTER ( StateDS );
364SHAREDPTR ( StateDS );
365
366}; //namespace
367
368#endif // DS_H
Note: See TracBrowser for help on using the browser.