root/bdm/stat/libDS.h @ 342

Revision 342, 6.2 kB (checked in by smidl, 15 years ago)

Barcelona

  • 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 DS_H
14#define DS_H
15
16
17#include "libBM.h"
18#include "libEF.h"
19
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 {
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
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 ) {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*/
55class FileDS: public MemDS {
56
57public:
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
79The 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*/
82class ITppFileDS: public FileDS {
83
84public:
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
94/*!
95* \brief CSV file data storage
96The constructor creates \c Data matrix from the records in a CSV file \c fname. The orientation can be of two types:
971. \c BY_COL which is default - the data are stored in columns; one column per time \f$t\f$, one row per data item.
982. \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.
99
100*/
101class CsvFileDS: public FileDS {
102
103public:
104        //! Constructor - create DS from a CSV file.
105        CsvFileDS ( const string& fname, const string& orientation = "BY_COL" );
106};
107
108
109
110/*!
111\brief Generator of ARX data
112
113*/
114class ArxDS : public DS {
115protected:
116        //! Rv of the regressor
117        RV Rrv;
118        //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$
119        vec H;
120        //! (future) input
121        vec U;
122        //! temporary variable for regressor
123        vec rgr;
124        //! data link: H -> rgr
125        datalink rgrlnk;
126        //! model of Y - linear Gaussian
127        mlnorm<chmat> model;
128        //! options
129        bool opt_L_theta;
130        //! loggers
131        int L_theta;
132        int L_R;
133        int dt_size;
134public:
135        void getdata ( vec &dt ) {
136                //it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" );
137                dt=H;
138        };
139        void getdata ( vec &dt, const ivec &indices ) {
140                it_assert_debug ( dt.length() ==indices.length(),"ArxDS" );
141                dt=H ( indices );
142        };
143        void write ( vec &ut ) {
144                //it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" );
145                U=ut;
146        };
147        void write ( vec &ut, const ivec &indices ) {
148                it_assert_debug ( ut.length() ==indices.length(),"ArxDS" );
149                set_subvector ( U, indices,ut );
150        };
151        void step();
152        //!Default constructor
153        ArxDS ( ) {};
154        //! Set parameters of the internal model, H is maximum time delay
155        void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 )
156        { model.set_parameters ( Th0, mu0, sqR0 );};
157        //! Set
158        void set_drv ( RV &yrv, RV &urv, RV &rrv ) {
159                Rrv = rrv;
160                Urv = urv;
161                dt_size = yrv._dsize() +urv._dsize();
162
163                RV drv = concat ( yrv,urv );
164                Drv = drv;
165                int td = rrv.mint();
166                H.set_size ( drv._dsize() * ( -td+1 ) );
167                U.set_size ( Urv._dsize() );
168                for ( int i=-1;i>=td;i-- ) {
169                        drv.t ( -1 );
170                        Drv.add ( drv ); //shift u1
171                }
172                rgrlnk.set_connection ( rrv,Drv );
173
174                dtsize = Drv._dsize();
175                utsize = Urv._dsize();
176        }
177        //! set options from a string
178        void set_options ( const string &s ) {
179                opt_L_theta= ( s.find ( "L_theta" ) !=string::npos );
180        };
181        virtual void log_add ( logger &L ) {
182                //DS::log_add ( L ); too long!!
183                L_dt=L.add ( Drv ( 0,dt_size ),"" );
184                L_ut=L.add ( Urv,"" );
185
186                mat &A =model._A();
187                mat R =model._R();
188                if ( opt_L_theta ) {L_theta=L.add ( RV ( "{th }", vec_1 ( A.rows() *A.cols() ) ),"t" );}
189                if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );}
190        }
191        virtual void logit ( logger &L ) {
192                //DS::logit ( L );
193                L.logit ( L_dt, H.left ( dt_size ) );
194                L.logit ( L_ut, U );
195
196                mat &A =model._A();
197                mat R =model._R();
198                if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );};
199                if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );};
200        }
201
202};
203
204class stateDS : public DS {
205protected:
206        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
207        mpdf* IM;
208        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$
209        mpdf* OM;
210        //! result storage
211        vec dt;
212        //! state storage
213        vec xt;
214        //! input storage
215        vec ut;
216        //! Logger
217        int L_xt;
218public:
219        void getdata ( vec &dt0 ) {dt0=dt;}
220        void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );}
221
222        stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ),
223                        dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {}
224        ~stateDS() {delete IM; delete OM;}
225        virtual void step() {
226                xt=IM->samplecond ( concat ( xt,ut ) );
227                dt=OM->samplecond ( concat ( xt,ut ) );
228        };
229
230        virtual void log_add ( logger &L ) {
231                DS::log_add ( L );
232                L_xt=L.add ( IM->_rv(),"true" );
233        }
234        virtual void logit ( logger &L ) {
235                DS::logit ( L );
236                L.logit ( L_xt,xt );
237        }
238
239};
240
241}; //namespace
242
243#endif // DS_H
Note: See TracBrowser for help on using the browser.