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

Revision 979, 6.8 kB (checked in by smidl, 14 years ago)

estimator returns the array of posterior estimators as second argument

  • 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        //! switch if constant is modelled or not
46        bool have_constant;
47        //! vector of dyadic update
48        vec dyad;
49        //! RV of regressor
50        RV rgr;
51        //! length of the regressor
52        int rgrlen;
53        //! posterior density
54        egiw est;
55        //! Alternative estimate of parameters, used in stabilized forgetting, see [Kulhavy]
56        egiw alter_est;
57public:
58        //! \name Constructors
59        //!@{
60        ARX ( const double frg0 = 1.0 ) : BMEF ( frg0 ),  have_constant ( true ), dyad(), rgrlen(),est(), alter_est() {};
61        ARX ( const ARX &A0 ) : BMEF ( A0 ),  have_constant ( A0.have_constant ), dyad ( A0.dyad ),rgrlen(A0.rgrlen), est ( A0.est ), alter_est ( A0.alter_est ) { };
62
63        ARX* _copy() const;
64
65        void set_frg ( double frg0 ) {
66                frg = frg0;
67        }
68        void set_constant ( bool const0 ) {
69                have_constant = const0;
70        }
71        void set_statistics ( int dimy0, const ldmat V0, double nu0 = -1.0 ) {
72                est.set_parameters ( dimy0, V0, nu0 );
73                est.validate();
74                last_lognc = est.lognc();
75                dimy = dimy0;
76        }
77        //!@}
78
79        //! Set sufficient statistics
80        void set_statistics ( const BMEF* BM0 );
81
82        //!\name Mathematical operations
83        //!@{
84
85        //! Weighted Bayes \f$ dt = [y_t psi_t] \f$.
86        void bayes_weighted ( const vec &yt, const vec &cond = empty_vec, const double w = 1.0 );
87        void bayes ( const vec &yt, const vec &cond = empty_vec ) {
88                bayes_weighted ( yt, cond, 1.0 );
89        };
90        double logpred ( const vec &yt ) const;
91        void flatten ( const BMEF* B );
92        //! Conditioned version of the predictor
93        enorm<ldmat>* epredictor ( const vec &rgr ) const;
94        //! Predictor for empty regressor
95        enorm<ldmat>* epredictor() const;
96        //! conditional version of the predictor
97        template<class sq_T>
98        shared_ptr<mlnorm<sq_T> > ml_predictor() const;
99        //! fast version of predicto
100        template<class sq_T>
101        void ml_predictor_update ( mlnorm<sq_T> &pred ) const;
102        mlstudent* predictor_student() const;
103        //! Brute force structure estimation.\return indices of accepted regressors.
104        ivec structure_est ( egiw Eg0 );
105        //! Smarter structure estimation by Ludvik Tesar.\return indices of accepted regressors.
106        ivec structure_est_LT ( egiw Eg0 );
107        //!@}
108
109        //!\name Access attributes
110        //!@{
111        //! return correctly typed posterior (covariant return)
112        const egiw& posterior() const {
113                return est;
114        }
115        //!@}
116
117        /*! UI for ARX estimator
118
119        \code
120        class = 'ARX';
121        yrv   = RV({names_of_dt} )                 // description of output variables
122        rgr   = RV({names_of_regressors}, [-1,-2]} // description of regressor variables
123        constant = 1;                              // 0/1 switch if the constant term is modelled or not
124
125        --- optional ---
126        prior = {class='egiw',...};                // Prior density, when given default is used instead
127        alternative = {class='egiw',...};          // Alternative density in stabilized estimation, when not given prior is used
128
129        frg = 1.0;                                 // forgetting, default frg=1.0
130
131        rv  = RV({names_of_parameters}}            // description of parametetr names
132                                                                                           // default: [""]
133        \endcode
134        */
135        void from_setting ( const Setting &set );
136
137        void validate() {
138                BMEF::validate();       
139                est.validate();
140                //if dimc not set set it from V
141                if ( dimc == 0 ) {
142                        dimc = posterior()._V().rows() - dimy - int ( have_constant == true );
143                }
144
145                if ( have_constant ) {
146                        dyad = ones ( dimy + dimc + 1 );
147                } else {
148                        dyad = zeros ( dimy + dimc );
149                }
150
151        }
152        //! function sets prior and alternative density
153        void set_prior ( const epdf *prior );
154        //! build default prior and alternative when all values are set
155        void set_prior_default ( egiw &prior ) {
156                //assume
157                vec dV0 ( prior._V().rows() );
158                dV0.set_subvector ( 0, prior._dimx() - 1, 1.0 );
159                if (dV0.length()>prior._dimx())
160                        dV0.set_subvector ( prior._dimx(), dV0.length() - 1, 1e-5 );
161               
162                prior.set_parameters ( prior._dimx(), ldmat ( dV0 ) );
163                prior.validate();
164        }
165
166        void to_setting ( Setting &set ) const
167        {                       
168                BMEF::to_setting( set ); // takes care of rv, yrv, rvc
169                UI::save(rgr, set, "rgr");
170                int constant = have_constant ? 1 : 0;
171                UI::save(constant, set, "constant");
172                UI::save(&alter_est, set, "alternative");
173                UI::save(&posterior(), set, "posterior");
174               
175        } 
176};
177UIREGISTER ( ARX );
178SHAREDPTR ( ARX );
179
180/*! ARX model conditined by knowledge of the forgetting factor
181\f[ f(\theta| d_1 \ldots d_t , \phi_t) \f]
182
183The symbol \f$ \phi \f$ is assumed to be the last of the conditioning variables.
184*/
185class ARXfrg : public ARX {
186public:
187        ARXfrg() : ARX() {};
188        //! copy constructor
189        ARXfrg ( const ARXfrg &A0 ) : ARX ( A0 ) {};
190        virtual ARXfrg* _copy() const {
191                ARXfrg *A = new ARXfrg ( *this );
192                return A;
193        }
194
195        void bayes ( const vec &val, const vec &cond ) {
196                int arx_cond_size=rgrlen -int(have_constant==true);
197                bdm_assert_debug(cond.size()>arx_cond_size, "ARXfrg: Insufficient conditioning, frg not given.");
198                frg = cond ( arx_cond_size ); // the first part after rgrlen
199                ARX::bayes ( val, cond.left(arx_cond_size) );
200        }
201        void validate() {
202                ARX::validate();
203                rvc.add ( RV ( "{phi }", vec_1 ( 1 ) ) );
204                dimc += 1;
205        }
206};
207UIREGISTER ( ARXfrg );
208
209
210////////////////////
211template<class sq_T>
212shared_ptr< mlnorm<sq_T> > ARX::ml_predictor ( ) const {
213        shared_ptr< mlnorm<sq_T> > tmp = new mlnorm<sq_T> ( );
214        tmp->set_rv ( yrv );
215        tmp->set_rvc ( _rvc() );
216
217        ml_predictor_update ( *tmp );
218        tmp->validate();
219        return tmp;
220}
221
222template<class sq_T>
223void ARX::ml_predictor_update ( mlnorm<sq_T> &pred ) const {
224        mat mu ( dimy, posterior()._V().rows() - dimy );
225        mat R ( dimy, dimy );
226
227        est.mean_mat ( mu, R ); //mu =
228        mu = mu.T();
229        //correction for student-t  -- TODO check if correct!!
230
231        if ( have_constant ) { // constant term
232                //Assume the constant term is the last one:
233                pred.set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), sq_T ( R ) );
234        } else {
235                pred.set_parameters ( mu, zeros ( dimy ), sq_T ( R ) );
236        }
237}
238
239};
240#endif // AR_H
241
Note: See TracBrowser for help on using the browser.