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