root/library/bdm/estim/arx.h @ 620

Revision 585, 4.7 kB (checked in by smidl, 15 years ago)

ticket #12 preparations

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Bayesian Filtering for generalized autoregressive (ARX) model
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 AR_H
14#define AR_H
15
16#include "../math/functions.h"
17#include "../stat/exp_family.h"
18#include "../base/user_info.h"
19//#include "../estim/kalman.h"
20#include "arx_straux.h"
21
22namespace bdm {
23
24/*!
25* \brief Linear Autoregressive model with Gaussian noise
26
27Regression of the following kind:
28\f[
29y_t = \theta_1 \psi_1 + \theta_2 + \psi_2 +\ldots + \theta_n \psi_n + r e_t
30\f]
31where unknown parameters \c rv are \f$[\theta r]\f$, regression vector \f$\psi=\psi(y_{1:t},u_{1:t})\f$ is a known function of past outputs and exogeneous variables \f$u_t\f$. Distrubances \f$e_t\f$ are supposed to be normally distributed:
32\f[
33e_t \sim \mathcal{N}(0,1).
34\f]
35
36See \ref tut_arx for mathematical treatment.
37
38The easiest way how to use the class is:
39\include arx_simple.cpp
40
41        \todo sort out constant terms - bayes should accept vec without additional 1s
42*/
43class ARX: public BMEF {
44protected:
45        //!size of output variable (needed in regressors)
46        int dimx;
47        //!description of modelled data \f$ y_t \f$ in the likelihood function
48        //! Do NOT access directly, only via \c get_yrv().
49        RV _yrv;
50        //! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density
51        egiw est;
52        //! cached value of est.V
53        ldmat &V;
54        //! cached value of est.nu
55        double ν
56public:
57        //! \name Constructors
58        //!@{
59        ARX ( const double frg0 = 1.0 ) : BMEF ( frg0 ), est (), V ( est._V() ), nu ( est._nu() ) {};
60        ARX ( const ARX &A0 ) : BMEF (), est (), V ( est._V() ), nu ( est._nu() ) {
61                set_statistics ( A0.dimx, A0.V, A0.nu );
62                set_parameters ( A0.frg );
63        };
64        ARX* _copy_() const;
65        void set_parameters ( double frg0 ) {
66                frg = frg0;
67        }
68        void set_statistics ( int dimx0, const ldmat V0, double nu0 = -1.0 ) {
69                est.set_parameters ( dimx0, V0, nu0 );
70                last_lognc = est.lognc();
71                dimx = dimx0;
72        }
73        //!@}
74
75//      //! Set parameters given by moments, \c mu (mean of theta), \c R (mean of R) and \c C (variance of theta)
76//      void set_parameters ( const vec &mu, const mat &R, const mat &C, double dfm){};
77        //! Set sufficient statistics
78        void set_statistics ( const BMEF* BM0 );
79//      //! Returns sufficient statistics
80//      void get_parameters ( mat &V0, double &nu0 ) {V0=est._V().to_mat(); nu0=est._nu();}
81        //!\name Mathematical operations
82        //!@{
83
84        //! Weighted Bayes \f$ dt = [y_t psi_t] \f$.
85        void bayes ( const vec &dt, const double w );
86        void bayes ( const vec &dt ) {
87                bayes ( dt, 1.0 );
88        };
89        double logpred ( const vec &dt ) const;
90        void flatten ( const BMEF* B ) {
91                const ARX* A = dynamic_cast<const ARX*> ( B );
92                // nu should be equal to B.nu
93                est.pow ( A->nu / nu );
94                if ( evalll ) {
95                        last_lognc = est.lognc();
96                }
97        }
98        //! Conditioned version of the predictor
99        enorm<ldmat>* epredictor ( const vec &rgr ) const;
100        //! Predictor for empty regressor
101        enorm<ldmat>* epredictor() const {
102                bdm_assert_debug ( dimx == V.rows() - 1, "Regressor is not only 1" );
103                return epredictor ( vec_1 ( 1.0 ) );
104        }
105        //! conditional version of the predictor
106        mlnorm<ldmat>* predictor() const;
107        mlstudent* predictor_student() const;
108        //! Brute force structure estimation.\return indeces of accepted regressors.
109        ivec structure_est ( egiw Eg0 );
110        //! Smarter structure estimation by Ludvik Tesar.\return indeces of accepted regressors.
111        ivec structure_est_LT ( egiw Eg0 );
112        //!@}
113
114        //!\name Access attributes
115        //!@{
116        const egiw& posterior() const {
117                return est;
118        }
119        //!@}
120
121        //!\name Connection
122        //!@{
123        void set_drv ( const RV &drv0 ) {
124                drv = drv0;
125        }
126
127        RV& get_yrv() {
128                //if yrv is not ready create it
129                if ( _yrv._dsize() != dimx ) {
130                        int i = 0;
131                        while ( _yrv._dsize() < dimx ) {
132                                _yrv.add ( drv ( vec_1 ( i ) ) );
133                                i++;
134                        }
135                }
136                //yrv should be ready by now
137                bdm_assert_debug ( _yrv._dsize() == dimx, "incompatible drv" );
138                return _yrv;
139        }
140        //!@}
141
142        // TODO dokumentace - aktualizovat
143        /*! UI for ARX estimator
144
145        The ARX is constructed from a structure with fields:
146        \code
147        estimator = {
148                class = "ARX";
149                y = {type="rv", ...}   // description of output variables
150                rgr = {type="rv", ...} // description of regressor variables
151                constant = true;       // boolean switch if the constant term is modelled or not
152
153                //optional fields
154                dV0 = [1e-3, 1e-5, 1e-5, 1e-5];
155                                                           // default: 1e-3 for y, 1e-5 for rgr
156                nu0 = 6;               // default: rgrlen + 2
157                frg = 1.0;             // forgetting, default frg=1.0
158        };
159        \endcode
160
161        The estimator will assign names of the posterior in the form ["theta_i" and "r_i"]
162        */
163        void from_setting ( const Setting &set );
164
165};
166
167UIREGISTER ( ARX );
168SHAREDPTR ( ARX );
169
170}
171
172#endif // AR_H
173
Note: See TracBrowser for help on using the browser.