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

Revision 565, 8.0 kB (checked in by vbarta, 15 years ago)

using own error macros (basically copied from IT++, but never aborting)

  • 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
17#include "../base/bdmbase.h"
18#include "../stat/exp_family.h"
19#include "../base/user_info.h"
20
21namespace bdm {
22/*!
23* \brief Memory storage of off-line data column-wise
24
25The 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 and \c delays.
26
27The data can be loaded from a file.
28*/
29class MemDS : public DS {
30protected:
31        //! internal matrix of data
32        mat Data;
33        //! active column in the Data matrix
34        int time;
35        //!  vector of rows that are presented in Dt
36        ivec rowid;
37        //! vector of delays that are presented in Dt
38        ivec delays;
39
40public:
41        void getdata ( vec &dt );
42        void getdata ( vec &dt, const ivec &indeces );
43        void set_rvs ( RV &drv, RV &urv );
44
45        void write ( vec &ut ) {
46                bdm_error ( "MemDS::write is not supported" );
47        }
48
49        void write ( vec &ut, ivec &indices ) {
50                bdm_error ( "MemDS::write is not supported" );
51        }
52
53        void step();
54        //!Default constructor
55        MemDS () {};
56        MemDS ( mat &Dat, ivec &rowid0, ivec &delays0 );
57};
58
59/*! Pseudovirtual class for reading data from files
60
61*/
62class FileDS: public MemDS {
63
64public:
65        void getdata ( vec &dt ) {
66                dt = Data.get_col ( time );
67        }
68
69        void getdata ( vec &dt, const ivec &indices ) {
70                vec tmp = Data.get_col ( time );
71                dt = tmp ( indices );
72        }
73
74        //! returns number of data in the file;
75        int ndat() {
76                return Data.cols();
77        }
78        //! no sense to log this type
79        void log_add ( logger &L ) {};
80        //! no sense to log this type
81        void logit ( logger &L ) {};
82};
83
84/*!
85* \brief Read Data Matrix from an IT file
86
87The 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$!
88
89*/
90class ITppFileDS: public FileDS {
91
92public:
93        ITppFileDS ( const string &fname, const string &varname ) : FileDS() {
94                it_file it ( fname );
95                it << Name ( varname );
96                it >> Data;
97                time = 0;
98                //rowid and delays are ignored
99        };
100
101        ITppFileDS () : FileDS() {
102        };
103
104        void from_setting ( const Setting &set );
105
106        // TODO dodelat void to_setting( Setting &set ) const;
107
108};
109
110UIREGISTER ( ITppFileDS );
111SHAREDPTR ( ITppFileDS );
112
113/*!
114* \brief CSV file data storage
115The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types:
1161. \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item.
1172. \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.
118
119*/
120class CsvFileDS: public FileDS {
121
122public:
123        //! Constructor - create DS from a CSV file.
124        CsvFileDS ( const string& fname, const string& orientation = "BY_COL" );
125};
126
127
128
129/*!
130\brief Generator of ARX data
131
132*/
133class ArxDS : public DS {
134protected:
135        //! Rv of the regressor
136        RV Rrv;
137        //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$
138        vec H;
139        //! (future) input
140        vec U;
141        //! temporary variable for regressor
142        vec rgr;
143        //! data link: H -> rgr
144        datalink rgrlnk;
145        //! model of Y - linear Gaussian
146        mlnorm<chmat> model;
147        //! options
148        bool opt_L_theta;
149        //! loggers
150        int L_theta;
151        int L_R;
152        int dt_size;
153public:
154        void getdata ( vec &dt ) {
155                dt = H;
156        }
157
158        void getdata ( vec &dt, const ivec &indices ) {
159                dt = H ( indices );
160        }
161
162        void write ( vec &ut ) {
163                U = ut;
164        }
165
166        void write ( vec &ut, const ivec &indices ) {
167                bdm_assert_debug ( ut.length() == indices.length(), "ArxDS" );
168                set_subvector ( U, indices, ut );
169        }
170
171        void step();
172
173        //!Default constructor
174        ArxDS ( ) {};
175        //! Set parameters of the internal model, H is maximum time delay
176        void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) {
177                model.set_parameters ( Th0, mu0, sqR0 );
178        };
179        //! Set
180        void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) {
181                Rrv = rrv;
182                Urv = urv;
183                dt_size = yrv._dsize() + urv._dsize();
184
185                RV drv = concat ( yrv, urv );
186                Drv = drv;
187                int td = rrv.mint();
188                H.set_size ( drv._dsize() * ( -td + 1 ) );
189                U.set_size ( Urv._dsize() );
190                for ( int i = -1; i >= td; i-- ) {
191                        drv.t ( -1 );
192                        Drv.add ( drv ); //shift u1
193                }
194                rgrlnk.set_connection ( rrv, Drv );
195
196                dtsize = Drv._dsize();
197                utsize = Urv._dsize();
198        }
199        //! set options from a string
200        void set_options ( const string &s ) {
201                opt_L_theta = ( s.find ( "L_theta" ) != string::npos );
202        };
203        virtual void log_add ( logger &L ) {
204                //DS::log_add ( L ); too long!!
205                L_dt = L.add ( Drv ( 0, dt_size ), "" );
206                L_ut = L.add ( Urv, "" );
207
208                mat &A = model._A();
209                mat R = model._R();
210                if ( opt_L_theta ) {
211                        L_theta = L.add ( RV ( "{th }", vec_1 ( A.rows() * A.cols() ) ), "t" );
212                }
213                if ( opt_L_theta ) {
214                        L_R = L.add ( RV ( "{R }", vec_1 ( R.rows() * R.cols() ) ), "r" );
215                }
216        }
217        virtual void logit ( logger &L ) {
218                //DS::logit ( L );
219                L.logit ( L_dt, H.left ( dt_size ) );
220                L.logit ( L_ut, U );
221
222                mat &A = model._A();
223                mat R = model._R();
224                if ( opt_L_theta ) {
225                        L.logit ( L_theta, vec ( A._data(), A.rows() *A.cols() ) );
226                };
227                if ( opt_L_theta ) {
228                        L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );
229                };
230        }
231
232        // TODO dokumentace - aktualizovat
233        /*! UI for ArxDS using factorized description!
234
235        The ArxDS is constructed from a structure with fields:
236        \code
237        system = {
238                type = "ArxDS";
239                // description of y variables
240                y = {type="rv"; names=["y", "u"];};
241                // description of u variable
242                u = {type="rv"; names=[];}
243                // description of regressor
244                rgr = {type="rv";
245                        names = ["y","y","y","u"];
246                        times = [-1, -2, -3, -1];
247                }
248
249                // theta
250                theta = [0.8, -0.3, 0.4, 1.0,
251                                 0.0, 0.0, 0.0, 0.0];
252                // offset (optional)
253                offset = [0.0, 0.0];
254                //variance
255                r = [0.1, 0.0,
256                         0.0, 1.0];
257                //options: L_theta = log value of theta,
258                opt = "L_theta";
259        };
260        \endcode
261
262        Result is ARX data source offering with full history as Drv.
263        */
264        void from_setting ( const Setting &set );
265
266        // TODO dodelat void to_setting( Setting &set ) const;
267};
268
269UIREGISTER ( ArxDS );
270SHAREDPTR ( ArxDS );
271
272class stateDS : public DS {
273private:
274        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
275        shared_ptr<mpdf> IM;
276
277        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$
278        shared_ptr<mpdf> OM;
279
280protected:
281        //! result storage
282        vec dt;
283        //! state storage
284        vec xt;
285        //! input storage
286        vec ut;
287        //! Logger
288        int L_xt;
289
290public:
291        void getdata ( vec &dt0 ) {
292                dt0 = dt;
293        }
294
295        void getdata ( vec &dt0, const ivec &indices ) {
296                dt0 = dt ( indices );
297        }
298
299        stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ),
300                dt ( OM0->dimension() ), xt ( IM0->dimension() ),
301                ut ( usize ), L_xt(0) { }
302
303        stateDS() : L_xt(0) { }
304
305        virtual void step() {
306                xt = IM->samplecond ( concat ( xt, ut ) );
307                dt = OM->samplecond ( concat ( xt, ut ) );
308        }
309
310        virtual void log_add ( logger &L ) {
311                DS::log_add ( L );
312                L_xt = L.add ( IM->_rv(), "true" );
313        }
314        virtual void logit ( logger &L ) {
315                DS::logit ( L );
316                L.logit ( L_xt, xt );
317        }
318
319        /*! UI for stateDS
320
321        The DS is constructed from a structure with fields:
322        \code
323        system = {
324                type = "stateDS";
325                //Internal model
326                IM = { type = "mpdf"; //<-- valid offspring! e.g. "mlnorm"
327                        rv = { //description of x_t
328                                names=["name1",...];
329                                sizes=[2,1]; // optional default=[1,1...];
330                                times=[0,0]; // optional default=[0,0...];
331                                }
332                        rvu= { //description of  u_t
333                                //optional default=empty
334                                }
335
336                        // remaining fields depending on the chosen type
337                        };
338                //Observation model
339                OM = { type = "mpdf-offspring";
340                        rv = {}; //description of d_t
341                        rvu = {type="internal", path="system.IM.rvu"}; //description of u_t
342
343                        //remaining fields
344                }
345        };
346        \endcode
347        */
348        void from_setting ( const Setting &set );
349
350        // TODO dodelat void to_setting( Setting &set ) const;
351
352};
353
354UIREGISTER ( stateDS );
355SHAREDPTR ( stateDS );
356
357}; //namespace
358
359#endif // DS_H
Note: See TracBrowser for help on using the browser.