[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: |
---|
[270] | 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; |
---|
[625] | 50 | //! rv of regressor |
---|
| 51 | RV rgrrv; |
---|
[97] | 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 ν |
---|
[625] | 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; |
---|
[639] | 62 | //! Alternative estimate of parameters, used in stabilized forgetting, see [Kulhavy] |
---|
| 63 | egiw alter_est; |
---|
[97] | 64 | public: |
---|
[270] | 65 | //! \name Constructors |
---|
| 66 | //!@{ |
---|
[639] | 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()); |
---|
[286] | 73 | }; |
---|
[283] | 74 | ARX* _copy_() const; |
---|
[477] | 75 | void set_parameters ( double frg0 ) { |
---|
| 76 | frg = frg0; |
---|
| 77 | } |
---|
[649] | 78 | void set_constant ( bool const0 ) { |
---|
| 79 | have_constant=const0; |
---|
| 80 | } |
---|
[477] | 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 | } |
---|
[270] | 86 | //!@} |
---|
[170] | 87 | |
---|
[145] | 88 | //! Set sufficient statistics |
---|
[170] | 89 | void set_statistics ( const BMEF* BM0 ); |
---|
[625] | 90 | |
---|
[270] | 91 | //!\name Mathematical operations |
---|
| 92 | //!@{ |
---|
| 93 | |
---|
| 94 | //! Weighted Bayes \f$ dt = [y_t psi_t] \f$. |
---|
[170] | 95 | void bayes ( const vec &dt, const double w ); |
---|
[477] | 96 | void bayes ( const vec &dt ) { |
---|
| 97 | bayes ( dt, 1.0 ); |
---|
| 98 | }; |
---|
[170] | 99 | double logpred ( const vec &dt ) const; |
---|
[270] | 100 | void flatten ( const BMEF* B ) { |
---|
[477] | 101 | const ARX* A = dynamic_cast<const ARX*> ( B ); |
---|
[170] | 102 | // nu should be equal to B.nu |
---|
[477] | 103 | est.pow ( A->nu / nu ); |
---|
| 104 | if ( evalll ) { |
---|
| 105 | last_lognc = est.lognc(); |
---|
| 106 | } |
---|
[170] | 107 | } |
---|
[262] | 108 | //! Conditioned version of the predictor |
---|
[270] | 109 | enorm<ldmat>* epredictor ( const vec &rgr ) const; |
---|
| 110 | //! Predictor for empty regressor |
---|
[286] | 111 | enorm<ldmat>* epredictor() const { |
---|
[565] | 112 | bdm_assert_debug ( dimx == V.rows() - 1, "Regressor is not only 1" ); |
---|
[286] | 113 | return epredictor ( vec_1 ( 1.0 ) ); |
---|
| 114 | } |
---|
[262] | 115 | //! conditional version of the predictor |
---|
[270] | 116 | mlnorm<ldmat>* predictor() const; |
---|
| 117 | mlstudent* predictor_student() const; |
---|
[97] | 118 | //! Brute force structure estimation.\return indeces of accepted regressors. |
---|
[170] | 119 | ivec structure_est ( egiw Eg0 ); |
---|
[577] | 120 | //! Smarter structure estimation by Ludvik Tesar.\return indeces of accepted regressors. |
---|
| 121 | ivec structure_est_LT ( egiw Eg0 ); |
---|
[270] | 122 | //!@} |
---|
| 123 | |
---|
| 124 | //!\name Access attributes |
---|
| 125 | //!@{ |
---|
[660] | 126 | //! return correctly typed posterior (covariant return) |
---|
| 127 | const egiw& posterior() const { |
---|
[477] | 128 | return est; |
---|
| 129 | } |
---|
[270] | 130 | //!@} |
---|
| 131 | |
---|
| 132 | //!\name Connection |
---|
| 133 | //!@{ |
---|
[625] | 134 | void set_rv ( const RV &yrv0 , const RV &rgrrv0 ) { |
---|
| 135 | _yrv = yrv0; |
---|
| 136 | rgrrv=rgrrv0; |
---|
| 137 | set_drv(concat(yrv0, rgrrv)); |
---|
[477] | 138 | } |
---|
[565] | 139 | |
---|
[270] | 140 | RV& get_yrv() { |
---|
| 141 | //if yrv is not ready create it |
---|
[477] | 142 | if ( _yrv._dsize() != dimx ) { |
---|
| 143 | int i = 0; |
---|
| 144 | while ( _yrv._dsize() < dimx ) { |
---|
| 145 | _yrv.add ( drv ( vec_1 ( i ) ) ); |
---|
| 146 | i++; |
---|
| 147 | } |
---|
[270] | 148 | } |
---|
| 149 | //yrv should be ready by now |
---|
[565] | 150 | bdm_assert_debug ( _yrv._dsize() == dimx, "incompatible drv" ); |
---|
[270] | 151 | return _yrv; |
---|
| 152 | } |
---|
| 153 | //!@} |
---|
[357] | 154 | |
---|
| 155 | /*! UI for ARX estimator |
---|
| 156 | |
---|
| 157 | \code |
---|
[625] | 158 | class = 'ARX'; |
---|
| 159 | rv = RV({names_of_dt} ) // description of output variables |
---|
| 160 | rgr = RV({names_of_regressors}, [-1,-2]} // description of regressor variables |
---|
[631] | 161 | constant = 1; // 0/1 switch if the constant term is modelled or not |
---|
[357] | 162 | |
---|
[625] | 163 | --- optional --- |
---|
[665] | 164 | prior = {class='egiw',...}; // Prior density, when given default is used instead |
---|
| 165 | alternative = {class='egiw',...}; // Alternative density in stabilized estimation, when not given prior is used |
---|
| 166 | |
---|
[625] | 167 | frg = 1.0; // forgetting, default frg=1.0 |
---|
| 168 | |
---|
| 169 | rv_param = RV({names_of_parameters}} // description of parametetr names |
---|
| 170 | // default: ["theta_i" and "r_i"] |
---|
[357] | 171 | \endcode |
---|
| 172 | */ |
---|
[477] | 173 | void from_setting ( const Setting &set ); |
---|
[357] | 174 | |
---|
[625] | 175 | void validate() { |
---|
| 176 | bdm_assert(dimx == _yrv._dsize(), "RVs of parameters and regressor do not match"); |
---|
| 177 | |
---|
| 178 | } |
---|
[665] | 179 | //! function sets prior and alternative density |
---|
| 180 | void set_prior(const RV &drv, egiw &prior){ |
---|
| 181 | //TODO check ranges in RV and build prior |
---|
| 182 | }; |
---|
| 183 | //! build default prior and alternative when all values are set |
---|
| 184 | void set_prior_default(egiw &prior){ |
---|
| 185 | //assume |
---|
| 186 | vec dV0(prior._V().rows()); |
---|
| 187 | dV0.set_subvector(0,prior._dimx()-1, 1.0); |
---|
| 188 | dV0.set_subvector(prior._dimx(),dV0.length()-1, 1e-5); |
---|
| 189 | |
---|
| 190 | prior.set_parameters(prior._dimx(),ldmat(dV0)); |
---|
| 191 | } |
---|
[97] | 192 | }; |
---|
| 193 | |
---|
[477] | 194 | UIREGISTER ( ARX ); |
---|
[529] | 195 | SHAREDPTR ( ARX ); |
---|
[357] | 196 | |
---|
[639] | 197 | /*! ARX model conditined by knowledge of the forgetting factor |
---|
| 198 | \f[ f(\theta| d_1 \ldots d_t , \phi_t) \f] |
---|
| 199 | */ |
---|
| 200 | class ARXfrg : public ARX{ |
---|
| 201 | public: |
---|
| 202 | ARXfrg():ARX(){}; |
---|
[660] | 203 | //! copy constructor |
---|
[639] | 204 | ARXfrg(const ARXfrg &A0):ARX(A0){}; |
---|
| 205 | ARXfrg* _copy_() const {ARXfrg *A = new ARXfrg(*this); return A;} |
---|
| 206 | void condition(const vec &val){ |
---|
| 207 | frg = val(0); |
---|
| 208 | } |
---|
| 209 | }; |
---|
| 210 | UIREGISTER(ARXfrg); |
---|
| 211 | }; |
---|
[97] | 212 | #endif // AR_H |
---|
| 213 | |
---|