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

Revision 529, 8.3 kB (checked in by vbarta, 15 years ago)

defined *_ptr wrappers of shared pointers

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