root/bdm/stat/libDS.h @ 267

Revision 267, 4.4 kB (checked in by smidl, 15 years ago)

samplecond modification

  • 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
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 indexes \c rowid and \c delays.
26
27        The data can be loaded from a file.
28        */
29        class MemDS : public DS {
30                //! internal matrix of data
31                mat Data;
32                //! active column in the Data matrix
33                int time;
34                //!  vector of rows that are presented in Dt
35                ivec rowid;
36                //! vector of delays that are presented in Dt
37                ivec delays;
38
39        public:
40                void getdata ( vec &dt );
41                void getdata ( vec &dt, const ivec &indeces );
42                void linkrvs ( RV &drv, RV &urv );
43                void write ( vec &ut ) {it_error ( "MemDS::write is not supported" );}
44                void write ( vec &ut,ivec &indexes ) {it_error ( "MemDS::write is not supported" );}
45                void step();
46                //!Default constructor
47                MemDS ( mat &Dat, ivec &rowid, ivec &delays );
48        };
49
50        /*!
51        \brief Generator of ARX data
52
53        */
54        class ArxDS : public DS {
55        protected:
56                //! Rv of the regressor
57                RV Rrv;
58                //! Rv of the history (full regressor)
59                RV Hrv;
60                //! History, ordered as \f$[y_t, u_t, y_{t-1 }, u_{t-1}, \ldots]\f$
61                vec H;
62                //! (future) input
63                vec U;
64                //! temporary variable for regressor
65                vec rgr;
66                //! data link: H -> rgr
67                datalink_e2e rgrlnk;
68                //! model of Y - linear Gaussian
69                mlnorm<chmat> model;
70                //! options
71                bool opt_L_theta;
72                //! loggers
73                int L_theta;
74                int L_R;
75        public:
76                void getdata ( vec &dt ) {
77                        it_assert_debug ( dt.length() ==Drv.count(),"ArxDS" );
78                        dt=H.left ( Urv.count() +Drv.count() );
79                };
80                void getdata ( vec &dt, const ivec &indexes ) {
81                        it_assert_debug ( dt.length() ==indeces.length(),"ArxDS" );
82                        dt=H ( indexes );
83                };
84                void write ( vec &ut ) {
85                        it_assert_debug ( ut.length() ==Urv.count(),"ArxDS" );
86                        U=ut;
87                };
88                void write ( vec &ut, const ivec &indexes ) {
89                        it_assert_debug ( ut.length() ==indeces.length(),"ArxDS" );
90                        set_subvector ( U, indexes,ut );
91                };
92                void step();
93                //!Default constructor
94                ArxDS ( RV &drv, RV &urv, RV &rrv );
95                //! Set parameters of the internal model
96                void set_parameters ( const mat &Th0, const vec mu0, const chmat &sqR0 )
97                { model.set_parameters ( Th0, mu0, sqR0 ); };
98                //! set options from a string
99                void set_options ( const string &s ) {
100                        opt_L_theta= ( s.find ( "L_theta" ) !=string::npos );
101                };
102                virtual void log_add ( logger &L ) {
103                        DS::log_add ( L );
104                        mat &A =model._A();
105                        mat R =model._R();
106                        if ( opt_L_theta ) {L_theta=L.add ( RV ( "{theta }", vec_1 ( A.rows() *A.cols() ) ),"t" );}
107                        if ( opt_L_theta ) {L_R=L.add ( RV ( "{R }", vec_1 ( R.rows() *R.cols() ) ),"r" );}
108                }
109                virtual void logit ( logger &L ) {
110                        DS::logit ( L );
111                        mat &A =model._A();
112                        mat R =model._R();
113                        if ( opt_L_theta ) {L.logit ( L_theta,vec ( A._data(), A.rows() *A.cols() ) );};
114                        if ( opt_L_theta ) {L.logit ( L_R, vec ( R._data(), R.rows() *R.rows() ) );};
115                }
116
117        };
118
119        class ARXDS : public ArxDS {
120        public:
121                ARXDS ( RV &drv, RV &urv, RV &rrv ) : ArxDS ( drv,urv,rrv ) {}
122
123                void getdata ( vec &dt ) {dt=H;}
124                void getdata ( vec &dt, const ivec &indeces ) {dt=H ( indeces );}
125                virtual RV _drv() const {return Hrv;}
126
127        };
128
129        class stateDS : public DS {
130        protected:
131                //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
132                mpdf* IM;
133                //!conditional pdf of the observations \f$ f(d_t|x_t) \f$
134                mpdf* OM;
135                //! result storage
136                vec dt;
137                //! state storage
138                vec xt;
139                //! input storage
140                vec ut;
141                //! Logger
142                int L_xt;
143        public:
144                void getdata ( vec &dt0 ) {dt0=dt;}
145                void getdata ( vec &dt0, const ivec &indeces ) {dt0=dt ( indeces );}
146
147                stateDS ( mpdf* IM0, mpdf* OM0, RV &Urv0 ) :DS ( OM0->_rv(),Urv0 ),IM ( IM0 ),OM ( OM0 ),
148                                dt ( OM0->_rv().count() ), xt ( IM0->_rv().count() ), ut ( Urv0.count() ) {}
149                ~stateDS() {delete IM; delete OM;}
150                virtual void step() {
151                        double tmp;
152                        xt=IM->samplecond(concat ( xt,ut ));
153                        dt=OM->samplecond(concat ( xt,ut ));
154                };
155               
156                virtual void log_add ( logger &L ) {
157                        DS::log_add ( L );
158                        L_xt=L.add(IM->_rv(),"true");
159                }
160                virtual void logit ( logger &L ) {
161                        DS::logit ( L );
162                        L.logit ( L_xt,xt);
163                }
164
165        };
166
167}; //namespace
168
169#endif // DS_H
Note: See TracBrowser for help on using the browser.