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

Revision 723, 6.4 kB (checked in by smidl, 15 years ago)

Big commit of LQG stuff

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