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 DS_H |
---|
14 | #define DS_H |
---|
15 | |
---|
16 | |
---|
17 | #include "libBM.h" |
---|
18 | #include "libEF.h" |
---|
19 | |
---|
20 | |
---|
21 | namespace bdm { |
---|
22 | /*! |
---|
23 | * \brief Memory storage of off-line data column-wise |
---|
24 | |
---|
25 | The 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 | |
---|
27 | The data can be loaded from a file. |
---|
28 | */ |
---|
29 | class 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 | //! vector of delays that are presented in Dt |
---|
38 | ivec delays; |
---|
39 | |
---|
40 | public: |
---|
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 ) {it_error ( "MemDS::write is not supported" );} |
---|
45 | void write ( vec &ut,ivec &indices ) {it_error ( "MemDS::write is not supported" );} |
---|
46 | void step(); |
---|
47 | //!Default constructor |
---|
48 | MemDS () {}; |
---|
49 | MemDS ( mat &Dat, ivec &rowid0, ivec &delays0 ); |
---|
50 | }; |
---|
51 | |
---|
52 | /*! Pseudovirtual class for reading data from files |
---|
53 | |
---|
54 | */ |
---|
55 | class FileDS: public MemDS { |
---|
56 | |
---|
57 | public: |
---|
58 | void getdata ( vec &dt ) { |
---|
59 | it_assert_debug ( dt.length() ==Data.rows(),"" ); |
---|
60 | dt = Data.get_col(time); |
---|
61 | }; |
---|
62 | void getdata ( vec &dt, const ivec &indeces ){ |
---|
63 | it_assert_debug ( dt.length() ==indeces.length(),"" ); |
---|
64 | vec tmp(indeces.length()); |
---|
65 | tmp = Data.get_col(time); |
---|
66 | dt = tmp(indeces); |
---|
67 | }; |
---|
68 | //! returns number of data in the file; |
---|
69 | int ndat(){return Data.cols();} |
---|
70 | }; |
---|
71 | |
---|
72 | /*! |
---|
73 | * \brief Read Data Matrix from an IT file |
---|
74 | |
---|
75 | The 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$! |
---|
76 | |
---|
77 | */ |
---|
78 | class ItppFileDS: public FileDS { |
---|
79 | |
---|
80 | public: |
---|
81 | ItppFileDS ( const string &fname, const string &varname ) :FileDS() { |
---|
82 | it_file it ( fname ); |
---|
83 | it << Name ( varname ); |
---|
84 | it >> Data; |
---|
85 | time = 0; |
---|
86 | //rowid and delays are ignored |
---|
87 | } |
---|
88 | }; |
---|
89 | |
---|
90 | /*! |
---|
91 | * \brief CSV file data storage |
---|
92 | The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types: |
---|
93 | 1. \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item. |
---|
94 | 2. \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. |
---|
95 | |
---|
96 | */ |
---|
97 | class CsvFileDS: public FileDS { |
---|
98 | |
---|
99 | public: |
---|
100 | //! Constructor - create DS from a CSV file. |
---|
101 | CsvFileDS ( const string& fname, const string& orientation = "BY_COL" ); |
---|
102 | }; |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | /*! |
---|
107 | \brief Generator of ARX data |
---|
108 | |
---|
109 | */ |
---|
110 | class ArxDS : public DS { |
---|
111 | protected: |
---|
112 | //! Rv of the regressor |
---|
113 | RV Rrv; |
---|
114 | //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$ |
---|
115 | vec H; |
---|
116 | //! (future) input |
---|
117 | vec U; |
---|
118 | //! temporary variable for regressor |
---|
119 | vec rgr; |
---|
120 | //! data link: H -> rgr |
---|
121 | datalink rgrlnk; |
---|
122 | //! model of Y - linear Gaussian |
---|
123 | mlnorm<chmat> model; |
---|
124 | //! options |
---|
125 | bool opt_L_theta; |
---|
126 | //! loggers |
---|
127 | int L_theta; |
---|
128 | int L_R; |
---|
129 | int dt_size; |
---|
130 | public: |
---|
131 | void getdata ( vec &dt ) { |
---|
132 | //it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" ); |
---|
133 | dt=H; |
---|
134 | }; |
---|
135 | void getdata ( vec &dt, const ivec &indices ) { |
---|
136 | it_assert_debug ( dt.length() ==indices.length(),"ArxDS" ); |
---|
137 | dt=H ( indices ); |
---|
138 | }; |
---|
139 | void write ( vec &ut ) { |
---|
140 | //it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" ); |
---|
141 | U=ut; |
---|
142 | }; |
---|
143 | void write ( vec &ut, const ivec &indices ) { |
---|
144 | it_assert_debug ( ut.length() ==indices.length(),"ArxDS" ); |
---|
145 | set_subvector ( U, indices,ut ); |
---|
146 | }; |
---|
147 | void step(); |
---|
148 | //!Default constructor |
---|
149 | ArxDS ( ) {}; |
---|
150 | //! Set parameters of the internal model, H is maximum time delay |
---|
151 | void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 ) |
---|
152 | { model.set_parameters ( Th0, mu0, sqR0 );}; |
---|
153 | //! Set |
---|
154 | void set_drv ( RV &yrv, RV &urv, RV &rrv ) { |
---|
155 | Rrv = rrv; |
---|
156 | Urv = urv; |
---|
157 | dt_size = yrv._dsize() +urv._dsize(); |
---|
158 | |
---|
159 | RV drv = concat ( yrv,urv ); |
---|
160 | Drv = drv; |
---|
161 | int td = rrv.mint(); |
---|
162 | H.set_size ( drv._dsize() * ( -td+1 ) ); |
---|
163 | U.set_size ( Urv._dsize() ); |
---|
164 | for ( int i=-1;i>=td;i-- ) { |
---|
165 | drv.t ( -1 ); |
---|
166 | Drv.add ( drv ); //shift u1 |
---|
167 | } |
---|
168 | rgrlnk.set_connection ( rrv,Drv ); |
---|
169 | |
---|
170 | dtsize = Drv._dsize(); |
---|
171 | utsize = Urv._dsize(); |
---|
172 | } |
---|
173 | //! set options from a string |
---|
174 | void set_options ( const string &s ) { |
---|
175 | opt_L_theta= ( s.find ( "L_theta" ) !=string::npos ); |
---|
176 | }; |
---|
177 | virtual void log_add ( logger &L ) { |
---|
178 | //DS::log_add ( L ); too long!! |
---|
179 | L_dt=L.add ( Drv ( 0,dt_size ),"" ); |
---|
180 | L_ut=L.add ( Urv,"" ); |
---|
181 | |
---|
182 | mat &A =model._A(); |
---|
183 | mat R =model._R(); |
---|
184 | if ( opt_L_theta ) {L_theta=L.add ( RV ( "{th }", vec_1 ( A.rows() *A.cols() ) ),"t" );} |
---|
185 | if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );} |
---|
186 | } |
---|
187 | virtual void logit ( logger &L ) { |
---|
188 | //DS::logit ( L ); |
---|
189 | L.logit ( L_dt, H.left ( dt_size ) ); |
---|
190 | L.logit ( L_ut, U ); |
---|
191 | |
---|
192 | mat &A =model._A(); |
---|
193 | mat R =model._R(); |
---|
194 | if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );}; |
---|
195 | if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );}; |
---|
196 | } |
---|
197 | |
---|
198 | }; |
---|
199 | |
---|
200 | class stateDS : public DS { |
---|
201 | protected: |
---|
202 | //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ |
---|
203 | mpdf* IM; |
---|
204 | //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ |
---|
205 | mpdf* OM; |
---|
206 | //! result storage |
---|
207 | vec dt; |
---|
208 | //! state storage |
---|
209 | vec xt; |
---|
210 | //! input storage |
---|
211 | vec ut; |
---|
212 | //! Logger |
---|
213 | int L_xt; |
---|
214 | public: |
---|
215 | void getdata ( vec &dt0 ) {dt0=dt;} |
---|
216 | void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );} |
---|
217 | |
---|
218 | stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ), |
---|
219 | dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {} |
---|
220 | ~stateDS() {delete IM; delete OM;} |
---|
221 | virtual void step() { |
---|
222 | xt=IM->samplecond ( concat ( xt,ut ) ); |
---|
223 | dt=OM->samplecond ( concat ( xt,ut ) ); |
---|
224 | }; |
---|
225 | |
---|
226 | virtual void log_add ( logger &L ) { |
---|
227 | DS::log_add ( L ); |
---|
228 | L_xt=L.add ( IM->_rv(),"true" ); |
---|
229 | } |
---|
230 | virtual void logit ( logger &L ) { |
---|
231 | DS::logit ( L ); |
---|
232 | L.logit ( L_xt,xt ); |
---|
233 | } |
---|
234 | |
---|
235 | }; |
---|
236 | |
---|
237 | }; //namespace |
---|
238 | |
---|
239 | #endif // DS_H |
---|