[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; |
---|
[97] | 62 | public: |
---|
[270] | 63 | //! \name Constructors |
---|
| 64 | //!@{ |
---|
[477] | 65 | ARX ( const double frg0 = 1.0 ) : BMEF ( frg0 ), est (), V ( est._V() ), nu ( est._nu() ) {}; |
---|
| 66 | ARX ( const ARX &A0 ) : BMEF (), est (), V ( est._V() ), nu ( est._nu() ) { |
---|
| 67 | set_statistics ( A0.dimx, A0.V, A0.nu ); |
---|
| 68 | set_parameters ( A0.frg ); |
---|
[286] | 69 | }; |
---|
[283] | 70 | ARX* _copy_() const; |
---|
[477] | 71 | void set_parameters ( double frg0 ) { |
---|
| 72 | frg = frg0; |
---|
| 73 | } |
---|
| 74 | void set_statistics ( int dimx0, const ldmat V0, double nu0 = -1.0 ) { |
---|
| 75 | est.set_parameters ( dimx0, V0, nu0 ); |
---|
| 76 | last_lognc = est.lognc(); |
---|
| 77 | dimx = dimx0; |
---|
| 78 | } |
---|
[270] | 79 | //!@} |
---|
[170] | 80 | |
---|
[145] | 81 | //! Set sufficient statistics |
---|
[170] | 82 | void set_statistics ( const BMEF* BM0 ); |
---|
[625] | 83 | |
---|
[270] | 84 | //!\name Mathematical operations |
---|
| 85 | //!@{ |
---|
| 86 | |
---|
| 87 | //! Weighted Bayes \f$ dt = [y_t psi_t] \f$. |
---|
[170] | 88 | void bayes ( const vec &dt, const double w ); |
---|
[477] | 89 | void bayes ( const vec &dt ) { |
---|
| 90 | bayes ( dt, 1.0 ); |
---|
| 91 | }; |
---|
[170] | 92 | double logpred ( const vec &dt ) const; |
---|
[270] | 93 | void flatten ( const BMEF* B ) { |
---|
[477] | 94 | const ARX* A = dynamic_cast<const ARX*> ( B ); |
---|
[170] | 95 | // nu should be equal to B.nu |
---|
[477] | 96 | est.pow ( A->nu / nu ); |
---|
| 97 | if ( evalll ) { |
---|
| 98 | last_lognc = est.lognc(); |
---|
| 99 | } |
---|
[170] | 100 | } |
---|
[262] | 101 | //! Conditioned version of the predictor |
---|
[270] | 102 | enorm<ldmat>* epredictor ( const vec &rgr ) const; |
---|
| 103 | //! Predictor for empty regressor |
---|
[286] | 104 | enorm<ldmat>* epredictor() const { |
---|
[565] | 105 | bdm_assert_debug ( dimx == V.rows() - 1, "Regressor is not only 1" ); |
---|
[286] | 106 | return epredictor ( vec_1 ( 1.0 ) ); |
---|
| 107 | } |
---|
[262] | 108 | //! conditional version of the predictor |
---|
[270] | 109 | mlnorm<ldmat>* predictor() const; |
---|
| 110 | mlstudent* predictor_student() const; |
---|
[97] | 111 | //! Brute force structure estimation.\return indeces of accepted regressors. |
---|
[170] | 112 | ivec structure_est ( egiw Eg0 ); |
---|
[577] | 113 | //! Smarter structure estimation by Ludvik Tesar.\return indeces of accepted regressors. |
---|
| 114 | ivec structure_est_LT ( egiw Eg0 ); |
---|
[270] | 115 | //!@} |
---|
| 116 | |
---|
| 117 | //!\name Access attributes |
---|
| 118 | //!@{ |
---|
[477] | 119 | const egiw& posterior() const { |
---|
| 120 | return est; |
---|
| 121 | } |
---|
[270] | 122 | //!@} |
---|
| 123 | |
---|
| 124 | //!\name Connection |
---|
| 125 | //!@{ |
---|
[625] | 126 | void set_rv ( const RV &yrv0 , const RV &rgrrv0 ) { |
---|
| 127 | _yrv = yrv0; |
---|
| 128 | rgrrv=rgrrv0; |
---|
| 129 | set_drv(concat(yrv0, rgrrv)); |
---|
[477] | 130 | } |
---|
[565] | 131 | |
---|
[270] | 132 | RV& get_yrv() { |
---|
| 133 | //if yrv is not ready create it |
---|
[477] | 134 | if ( _yrv._dsize() != dimx ) { |
---|
| 135 | int i = 0; |
---|
| 136 | while ( _yrv._dsize() < dimx ) { |
---|
| 137 | _yrv.add ( drv ( vec_1 ( i ) ) ); |
---|
| 138 | i++; |
---|
| 139 | } |
---|
[270] | 140 | } |
---|
| 141 | //yrv should be ready by now |
---|
[565] | 142 | bdm_assert_debug ( _yrv._dsize() == dimx, "incompatible drv" ); |
---|
[270] | 143 | return _yrv; |
---|
| 144 | } |
---|
| 145 | //!@} |
---|
[357] | 146 | |
---|
| 147 | /*! UI for ARX estimator |
---|
| 148 | |
---|
| 149 | \code |
---|
[625] | 150 | class = 'ARX'; |
---|
| 151 | rv = RV({names_of_dt} ) // description of output variables |
---|
| 152 | rgr = RV({names_of_regressors}, [-1,-2]} // description of regressor variables |
---|
[631] | 153 | constant = 1; // 0/1 switch if the constant term is modelled or not |
---|
[357] | 154 | |
---|
[625] | 155 | --- optional --- |
---|
| 156 | V0 = [1 0;0 1]; // Initial value of information matrix V |
---|
| 157 | --- OR --- |
---|
| 158 | dV0 = [1e-3, 1e-5, 1e-5, 1e-5]; // Initial value of diagonal of information matrix V |
---|
| 159 | // default: 1e-3 for rv, 1e-5 for rgr |
---|
| 160 | nu0 = 6; // initial value of nu, default: rgrlen + 2 |
---|
| 161 | frg = 1.0; // forgetting, default frg=1.0 |
---|
| 162 | |
---|
| 163 | rv_param = RV({names_of_parameters}} // description of parametetr names |
---|
| 164 | // default: ["theta_i" and "r_i"] |
---|
[357] | 165 | \endcode |
---|
| 166 | */ |
---|
[477] | 167 | void from_setting ( const Setting &set ); |
---|
[357] | 168 | |
---|
[625] | 169 | void validate() { |
---|
| 170 | bdm_assert(dimx == _yrv._dsize(), "RVs of parameters and regressor do not match"); |
---|
| 171 | |
---|
| 172 | } |
---|
[97] | 173 | }; |
---|
| 174 | |
---|
[477] | 175 | UIREGISTER ( ARX ); |
---|
[529] | 176 | SHAREDPTR ( ARX ); |
---|
[357] | 177 | |
---|
[254] | 178 | } |
---|
[97] | 179 | |
---|
| 180 | #endif // AR_H |
---|
| 181 | |
---|