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

Revision 625, 4.9 kB (checked in by smidl, 15 years ago)

ARX re-designed

  • 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        //! rv of regressor
51        RV rgrrv;
52        //! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density
53        egiw est;
54        //! cached value of est.V
55        ldmat &V;
56        //! cached value of est.nu
57        double ν
58        //! switch if constant is modelled or not
59        bool have_constant;
60        //! cached value of data vector for have_constant =true
61        vec _dt;
62public:
63        //! \name Constructors
64        //!@{
65        ARX ( const double frg0 = 1.0 ) : BMEF ( frg0 ), est (), V ( est._V() ), nu ( est._nu() ) {};
66        ARX ( const ARX &A0 ) : BMEF (), est (), V ( est._V() ), nu ( est._nu() ) {
67                set_statistics ( A0.dimx, A0.V, A0.nu );
68                set_parameters ( A0.frg );
69        };
70        ARX* _copy_() const;
71        void set_parameters ( double frg0 ) {
72                frg = frg0;
73        }
74        void set_statistics ( int dimx0, const ldmat V0, double nu0 = -1.0 ) {
75                est.set_parameters ( dimx0, V0, nu0 );
76                last_lognc = est.lognc();
77                dimx = dimx0;
78        }
79        //!@}
80
81        //! Set sufficient statistics
82        void set_statistics ( const BMEF* BM0 );
83
84        //!\name Mathematical operations
85        //!@{
86
87        //! Weighted Bayes \f$ dt = [y_t psi_t] \f$.
88        void bayes ( const vec &dt, const double w );
89        void bayes ( const vec &dt ) {
90                bayes ( dt, 1.0 );
91        };
92        double logpred ( const vec &dt ) const;
93        void flatten ( const BMEF* B ) {
94                const ARX* A = dynamic_cast<const ARX*> ( B );
95                // nu should be equal to B.nu
96                est.pow ( A->nu / nu );
97                if ( evalll ) {
98                        last_lognc = est.lognc();
99                }
100        }
101        //! Conditioned version of the predictor
102        enorm<ldmat>* epredictor ( const vec &rgr ) const;
103        //! Predictor for empty regressor
104        enorm<ldmat>* epredictor() const {
105                bdm_assert_debug ( dimx == V.rows() - 1, "Regressor is not only 1" );
106                return epredictor ( vec_1 ( 1.0 ) );
107        }
108        //! conditional version of the predictor
109        mlnorm<ldmat>* predictor() const;
110        mlstudent* predictor_student() const;
111        //! Brute force structure estimation.\return indeces of accepted regressors.
112        ivec structure_est ( egiw Eg0 );
113        //! Smarter structure estimation by Ludvik Tesar.\return indeces of accepted regressors.
114        ivec structure_est_LT ( egiw Eg0 );
115        //!@}
116
117        //!\name Access attributes
118        //!@{
119        const egiw& posterior() const {
120                return est;
121        }
122        //!@}
123
124        //!\name Connection
125        //!@{
126        void set_rv ( const RV &yrv0 , const RV &rgrrv0 ) {
127                _yrv = yrv0;
128                rgrrv=rgrrv0;
129                set_drv(concat(yrv0, rgrrv));
130        }
131
132        RV& get_yrv() {
133                //if yrv is not ready create it
134                if ( _yrv._dsize() != dimx ) {
135                        int i = 0;
136                        while ( _yrv._dsize() < dimx ) {
137                                _yrv.add ( drv ( vec_1 ( i ) ) );
138                                i++;
139                        }
140                }
141                //yrv should be ready by now
142                bdm_assert_debug ( _yrv._dsize() == dimx, "incompatible drv" );
143                return _yrv;
144        }
145        //!@}
146
147        /*! UI for ARX estimator
148
149        \code
150        class = 'ARX';
151        rv    = RV({names_of_dt} )                 // description of output variables
152        rgr   = RV({names_of_regressors}, [-1,-2]} // description of regressor variables
153        constant = true;                           // boolean switch if the constant term is modelled or not
154
155        --- optional ---
156        V0  = [1 0;0 1];                           // Initial value of information matrix V
157          --- OR ---
158        dV0 = [1e-3, 1e-5, 1e-5, 1e-5];            // Initial value of diagonal of information matrix V
159                                                                                           // default: 1e-3 for rv, 1e-5 for rgr
160        nu0 = 6;                                                   // initial value of nu, default: rgrlen + 2
161        frg = 1.0;                                 // forgetting, default frg=1.0
162
163        rv_param   = RV({names_of_parameters}}     // description of parametetr names
164                                                                                           // default: ["theta_i" and "r_i"]
165        \endcode
166        */
167        void from_setting ( const Setting &set );
168
169        void validate() {
170                bdm_assert(dimx == _yrv._dsize(), "RVs of parameters and regressor do not match");
171               
172        }
173};
174
175UIREGISTER ( ARX );
176SHAREDPTR ( ARX );
177
178}
179
180#endif // AR_H
181
Note: See TracBrowser for help on using the browser.