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

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

cleanup of MemDS and its descendants
bdmtoolbox/CMakeLists.txt slightly changed to avoid unnecessary MEX condition
"indeces" replaced by "indices"

  • Property svn:eol-style set to native
RevLine 
[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]20namespace bdm {
[271]21/*!
22* \brief Memory storage of off-line data column-wise
[18]23
[611]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.
[18]25
[271]26The data can be loaded from a file.
27*/
28class MemDS : public DS {
[737]29protected:
30        //! internal matrix of data
31        mat Data;
32        //! active column in the Data matrix
33        int time;
[18]34
[737]35public:
[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
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
[737]58        void write ( const vec &ut ) {
59                bdm_error ( "MemDS::write is not supported" );
60        }
[565]61
[737]62        void write ( const vec &ut, const ivec &indices ) {
63                bdm_error ( "MemDS::write is not supported" );
64        }
[565]65
[737]66        void step();
[896]67
[737]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
[896]86        If \c rowid is given, \c drv will be named after indices in rowids.
[737]87
88        Hence the data provided by method \c getdata() will be full column of matrix Data starting from the first record.
89        */
[896]90        void from_setting ( const Setting &set );
[271]91};
[737]92UIREGISTER ( MemDS );
[263]93
[892]94/*! Pseudovirtual class for reading data from files
95
96*/
97class FileDS: public MemDS {
[896]98protected:
99        string filename;
[892]100public:
[896]101        void from_setting ( const Setting & set );
[892]102};
103
104/*!
105* \brief Read Data Matrix from an IT file
106
107The 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$!
108
109*/
110class ITppFileDS: public FileDS {
111
112public:
113        //! Convenience constructor
114        ITppFileDS ( const string &fname, const string &varname ) : FileDS() {
115                it_file it ( fname );
116                it << Name ( varname );
117                it >> Data;
118                time = 0;
[896]119                //delays are ignored
[892]120        };
121
122        ITppFileDS () : FileDS() {
123        };
124
125        void from_setting ( const Setting &set );
126
127        // TODO dodelat void to_setting( Setting &set ) const;
128
129};
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:
[896]143        void from_setting ( const Setting & set );
[892]144};
145
146
147
148// ARXDs - DELETED
149
[611]150/*!  \brief Simulate data from a static pdf (epdf)
151
[598]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 {
[737]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        }
[598]174
[892]175        //! Accepts action variable and schedule it for application.
[766]176        virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID;
[892]177
[896]178        //! Accepts action variables at specific indices
179        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
[766]180
[737]181        /*!
182        \code
183        class = "EpdfDS";
184        epdf = {class="epdf_offspring", ...}// uncondtitional density to sample from
185        \endcode
[598]186
[737]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        }
[795]196
[737]197        void validate() {
[795]198                DS::validate();
199
[737]200                dt = iepdf->sample();
201        }
[598]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*/
[737]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;
[598]222
[737]223public:
[738]224        void step();
225
226        void getdata ( vec &dt_out ) const;
227
[737]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
[766]235
[896]236        //! Returns data records at indices.
237        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
[766]238
239
[737]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
[895]254                RV Yrv = ipdf->_rv();
[737]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 );
[895]259                set_drv ( concat(Yrv,Urv), Urv );
[737]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 );
[613]274                        }
[737]275                }
[598]276
[737]277                yt = zeros ( ipdf->dimension() );
278                rgr = zeros ( ipdf->dimensionc() );
279                ut = zeros ( Urv._dsize() );
[603]280
[737]281                utsize = ut.length();
[895]282                dtsize = yt.length() + utsize;
[737]283        }
[795]284
[737]285        void validate() {
[795]286                DS::validate();
287
[737]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        }
[598]293};
[693]294UIREGISTER ( PdfDS );
[598]295
[660]296//! State-space data source simulating two densities
[695]297class StateDS : public DS {
[737]298protected:
299        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
300        shared_ptr<pdf> IM;
[527]301
[737]302        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$
303        shared_ptr<pdf> OM;
[527]304
[737]305        //! result storage
306        vec dt;
307        //! state storage
308        vec xt;
309        //! input storage
310        vec ut;
[527]311
[737]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        }
[357]323
[737]324        void getdata ( vec &dt0, const ivec &indices ) {
325                dt0 = dt ( indices );
326        }
[267]327
[738]328        virtual void step();
[267]329
[737]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        }
[357]338
[896]339        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
[766]340
[737]341        /*! UI for stateDS
[357]342
[737]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();
[271]361};
[267]362
[695]363UIREGISTER ( StateDS );
364SHAREDPTR ( StateDS );
[357]365
[254]366}; //namespace
[18]367
368#endif // DS_H
Note: See TracBrowser for help on using the browser.