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