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