[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; |
---|
| 49 | //! posterior density |
---|
| 50 | egiw est; |
---|
[639] | 51 | //! Alternative estimate of parameters, used in stabilized forgetting, see [Kulhavy] |
---|
| 52 | egiw alter_est; |
---|
[97] | 53 | public: |
---|
[270] | 54 | //! \name Constructors |
---|
| 55 | //!@{ |
---|
[679] | 56 | ARX ( const double frg0 = 1.0 ) : BMEF ( frg0 ), have_constant(true), dyad(), est() {}; |
---|
| 57 | ARX ( const ARX &A0 ) : BMEF (A0.frg), have_constant(A0.have_constant), dyad(A0.dyad),est(est) { }; |
---|
[283] | 58 | ARX* _copy_() const; |
---|
[477] | 59 | void set_parameters ( double frg0 ) { |
---|
| 60 | frg = frg0; |
---|
| 61 | } |
---|
[649] | 62 | void set_constant ( bool const0 ) { |
---|
| 63 | have_constant=const0; |
---|
| 64 | } |
---|
[679] | 65 | void set_statistics ( int dimy0, const ldmat V0, double nu0 = -1.0 ) { |
---|
| 66 | est.set_parameters ( dimy0, V0, nu0 ); |
---|
[477] | 67 | last_lognc = est.lognc(); |
---|
[679] | 68 | dimy = dimy0; |
---|
[477] | 69 | } |
---|
[270] | 70 | //!@} |
---|
[170] | 71 | |
---|
[145] | 72 | //! Set sufficient statistics |
---|
[170] | 73 | void set_statistics ( const BMEF* BM0 ); |
---|
[625] | 74 | |
---|
[270] | 75 | //!\name Mathematical operations |
---|
| 76 | //!@{ |
---|
| 77 | |
---|
| 78 | //! Weighted Bayes \f$ dt = [y_t psi_t] \f$. |
---|
[679] | 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 ); |
---|
[477] | 82 | }; |
---|
[679] | 83 | double logpred ( const vec &yt ) const; |
---|
[270] | 84 | void flatten ( const BMEF* B ) { |
---|
[477] | 85 | const ARX* A = dynamic_cast<const ARX*> ( B ); |
---|
[170] | 86 | // nu should be equal to B.nu |
---|
[679] | 87 | est.pow ( A->posterior()._nu() / posterior()._nu() ); |
---|
[477] | 88 | if ( evalll ) { |
---|
| 89 | last_lognc = est.lognc(); |
---|
| 90 | } |
---|
[170] | 91 | } |
---|
[262] | 92 | //! Conditioned version of the predictor |
---|
[270] | 93 | enorm<ldmat>* epredictor ( const vec &rgr ) const; |
---|
| 94 | //! Predictor for empty regressor |
---|
[286] | 95 | enorm<ldmat>* epredictor() const { |
---|
[679] | 96 | bdm_assert_debug ( dimy == posterior()._V().rows() - 1, "Regressor is not only 1" ); |
---|
[286] | 97 | return epredictor ( vec_1 ( 1.0 ) ); |
---|
| 98 | } |
---|
[262] | 99 | //! conditional version of the predictor |
---|
[270] | 100 | mlnorm<ldmat>* predictor() const; |
---|
| 101 | mlstudent* predictor_student() const; |
---|
[97] | 102 | //! Brute force structure estimation.\return indeces of accepted regressors. |
---|
[170] | 103 | ivec structure_est ( egiw Eg0 ); |
---|
[577] | 104 | //! Smarter structure estimation by Ludvik Tesar.\return indeces of accepted regressors. |
---|
| 105 | ivec structure_est_LT ( egiw Eg0 ); |
---|
[270] | 106 | //!@} |
---|
| 107 | |
---|
| 108 | //!\name Access attributes |
---|
| 109 | //!@{ |
---|
[660] | 110 | //! return correctly typed posterior (covariant return) |
---|
| 111 | const egiw& posterior() const { |
---|
[477] | 112 | return est; |
---|
| 113 | } |
---|
[270] | 114 | //!@} |
---|
| 115 | |
---|
[357] | 116 | /*! UI for ARX estimator |
---|
| 117 | |
---|
| 118 | \code |
---|
[625] | 119 | class = 'ARX'; |
---|
| 120 | rv = RV({names_of_dt} ) // description of output variables |
---|
| 121 | rgr = RV({names_of_regressors}, [-1,-2]} // description of regressor variables |
---|
[631] | 122 | constant = 1; // 0/1 switch if the constant term is modelled or not |
---|
[357] | 123 | |
---|
[625] | 124 | --- optional --- |
---|
[665] | 125 | prior = {class='egiw',...}; // Prior density, when given default is used instead |
---|
| 126 | alternative = {class='egiw',...}; // Alternative density in stabilized estimation, when not given prior is used |
---|
| 127 | |
---|
[625] | 128 | frg = 1.0; // forgetting, default frg=1.0 |
---|
| 129 | |
---|
| 130 | rv_param = RV({names_of_parameters}} // description of parametetr names |
---|
| 131 | // default: ["theta_i" and "r_i"] |
---|
[357] | 132 | \endcode |
---|
| 133 | */ |
---|
[477] | 134 | void from_setting ( const Setting &set ); |
---|
[357] | 135 | |
---|
[625] | 136 | void validate() { |
---|
[679] | 137 | //if dimc not set set it from V |
---|
| 138 | if (dimc==0){ |
---|
| 139 | dimc = posterior()._V().rows()-dimy-int(have_constant==true); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | if (have_constant) { |
---|
| 143 | dyad = ones(dimy+dimc+1); |
---|
| 144 | } else { |
---|
| 145 | dyad = zeros(dimy+dimc); |
---|
| 146 | } |
---|
| 147 | |
---|
[625] | 148 | } |
---|
[665] | 149 | //! function sets prior and alternative density |
---|
| 150 | void set_prior(const RV &drv, egiw &prior){ |
---|
| 151 | //TODO check ranges in RV and build prior |
---|
| 152 | }; |
---|
| 153 | //! build default prior and alternative when all values are set |
---|
| 154 | void set_prior_default(egiw &prior){ |
---|
| 155 | //assume |
---|
| 156 | vec dV0(prior._V().rows()); |
---|
| 157 | dV0.set_subvector(0,prior._dimx()-1, 1.0); |
---|
| 158 | dV0.set_subvector(prior._dimx(),dV0.length()-1, 1e-5); |
---|
| 159 | |
---|
| 160 | prior.set_parameters(prior._dimx(),ldmat(dV0)); |
---|
| 161 | } |
---|
[97] | 162 | }; |
---|
| 163 | |
---|
[477] | 164 | UIREGISTER ( ARX ); |
---|
[529] | 165 | SHAREDPTR ( ARX ); |
---|
[357] | 166 | |
---|
[639] | 167 | /*! ARX model conditined by knowledge of the forgetting factor |
---|
| 168 | \f[ f(\theta| d_1 \ldots d_t , \phi_t) \f] |
---|
| 169 | */ |
---|
| 170 | class ARXfrg : public ARX{ |
---|
| 171 | public: |
---|
| 172 | ARXfrg():ARX(){}; |
---|
[660] | 173 | //! copy constructor |
---|
[639] | 174 | ARXfrg(const ARXfrg &A0):ARX(A0){}; |
---|
| 175 | ARXfrg* _copy_() const {ARXfrg *A = new ARXfrg(*this); return A;} |
---|
| 176 | void condition(const vec &val){ |
---|
| 177 | frg = val(0); |
---|
| 178 | } |
---|
| 179 | }; |
---|
| 180 | UIREGISTER(ARXfrg); |
---|
| 181 | }; |
---|
[97] | 182 | #endif // AR_H |
---|
| 183 | |
---|