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

Revision 649, 5.5 kB (checked in by smidl, 15 years ago)

corrections in arx and ctrl_base

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