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

Revision 723, 10.1 kB (checked in by smidl, 15 years ago)

Big commit of LQG stuff

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