root/bdm/stat/libDS.h @ 283

Revision 283, 5.1 kB (checked in by smidl, 15 years ago)

get rid of BMcond + adaptation in doprava/

  • 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 &rowid, ivec &delays );
50};
51
52/*! Read Data Matrix from an IT file
53
54*/
55class FileDS: public MemDS {
56
57public:
58        FileDS ( const string &fname, const string &varname ) :MemDS() {
59                it_file it ( fname );
60                it << Name ( varname ); 
61                it >> Data;
62                time =0;
63                //rowid and delays are ignored
64        }
65        void getdata ( vec &dt ) {
66                it_assert_debug ( dt.length() ==Data.rows(),"" );
67                dt = Data.get_col(time);
68        };
69        void getdata ( vec &dt, const ivec &indeces ){
70                it_assert_debug ( dt.length() ==indeces.length(),"" );
71                vec tmp(indeces.length());
72                tmp = Data.get_col(time);
73                dt = tmp(indeces);
74        };
75        //! returns number of data in the file;
76        int ndat(){return Data.cols();}
77};
78
79/*!
80\brief Generator of ARX data
81
82*/
83class ArxDS : public DS {
84protected:
85        //! Rv of the regressor
86        RV Rrv;
87        //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$
88        vec H;
89        //! (future) input
90        vec U;
91        //! temporary variable for regressor
92        vec rgr;
93        //! data link: H -> rgr
94        datalink rgrlnk;
95        //! model of Y - linear Gaussian
96        mlnorm<chmat> model;
97        //! options
98        bool opt_L_theta;
99        //! loggers
100        int L_theta;
101        int L_R;
102        int dt_size;
103public:
104        void getdata ( vec &dt ) {
105                //it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" );
106                dt=H;
107        };
108        void getdata ( vec &dt, const ivec &indices ) {
109                it_assert_debug ( dt.length() ==indices.length(),"ArxDS" );
110                dt=H ( indices );
111        };
112        void write ( vec &ut ) {
113                //it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" );
114                U=ut;
115        };
116        void write ( vec &ut, const ivec &indices ) {
117                it_assert_debug ( ut.length() ==indices.length(),"ArxDS" );
118                set_subvector ( U, indices,ut );
119        };
120        void step();
121        //!Default constructor
122        ArxDS ( ) {};
123        //! Set parameters of the internal model, H is maximum time delay
124        void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 )
125        { model.set_parameters ( Th0, mu0, sqR0 );};
126        //! Set
127        void set_drv ( RV &yrv, RV &urv, RV &rrv ) {
128                Rrv = rrv;
129                Urv = urv;
130                dt_size = yrv._dsize() +urv._dsize();
131
132                RV drv = concat ( yrv,urv );
133                Drv = drv;
134                int td = rrv.mint();
135                H.set_size ( drv._dsize() * ( -td+1 ) );
136                U.set_size ( Urv._dsize() );
137                for ( int i=-1;i>=td;i-- ) {
138                        drv.t ( -1 );
139                        Drv.add ( drv ); //shift u1
140                }
141                rgrlnk.set_connection ( rrv,Drv );
142
143                dtsize = Drv._dsize();
144                utsize = Urv._dsize();
145        }
146        //! set options from a string
147        void set_options ( const string &s ) {
148                opt_L_theta= ( s.find ( "L_theta" ) !=string::npos );
149        };
150        virtual void log_add ( logger &L ) {
151                //DS::log_add ( L ); too long!!
152                L_dt=L.add ( Drv ( 0,dt_size ),"" );
153                L_ut=L.add ( Urv,"" );
154
155                mat &A =model._A();
156                mat R =model._R();
157                if ( opt_L_theta ) {L_theta=L.add ( RV ( "{th }", vec_1 ( A.rows() *A.cols() ) ),"t" );}
158                if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );}
159        }
160        virtual void logit ( logger &L ) {
161                //DS::logit ( L );
162                L.logit ( L_dt, H.left ( dt_size ) );
163                L.logit ( L_ut, U );
164
165                mat &A =model._A();
166                mat R =model._R();
167                if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );};
168                if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );};
169        }
170
171};
172
173class stateDS : public DS {
174protected:
175        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
176        mpdf* IM;
177        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$
178        mpdf* OM;
179        //! result storage
180        vec dt;
181        //! state storage
182        vec xt;
183        //! input storage
184        vec ut;
185        //! Logger
186        int L_xt;
187public:
188        void getdata ( vec &dt0 ) {dt0=dt;}
189        void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );}
190
191        stateDS ( mpdf* IM0, mpdf* OM0, int usize ) :DS ( ),IM ( IM0 ),OM ( OM0 ),
192                        dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {}
193        ~stateDS() {delete IM; delete OM;}
194        virtual void step() {
195                xt=IM->samplecond ( concat ( xt,ut ) );
196                dt=OM->samplecond ( concat ( xt,ut ) );
197        };
198
199        virtual void log_add ( logger &L ) {
200                DS::log_add ( L );
201                L_xt=L.add ( IM->_rv(),"true" );
202        }
203        virtual void logit ( logger &L ) {
204                DS::logit ( L );
205                L.logit ( L_xt,xt );
206        }
207
208};
209
210}; //namespace
211
212#endif // DS_H
Note: See TracBrowser for help on using the browser.