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 "../stat/libFN.h" |
---|
17 | #include "../stat/libEF.h" |
---|
18 | |
---|
19 | namespace bdm { |
---|
20 | |
---|
21 | /*! |
---|
22 | * \brief Linear Autoregressive model with Gaussian noise |
---|
23 | |
---|
24 | Regression of the following kind: |
---|
25 | \f[ |
---|
26 | y_t = \theta_1 \psi_1 + \theta_2 + \psi_2 +\ldots + \theta_n \psi_n + r e_t |
---|
27 | \f] |
---|
28 | 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: |
---|
29 | \f[ |
---|
30 | e_t \sim \mathcal{N}(0,1). |
---|
31 | \f] |
---|
32 | |
---|
33 | See \ref tut_arx for mathematical treatment. |
---|
34 | |
---|
35 | The easiest way how to use the class is: |
---|
36 | \include arx_simple.cpp |
---|
37 | |
---|
38 | */ |
---|
39 | class ARX: public BMEF { |
---|
40 | protected: |
---|
41 | //!size of output variable (needed in regressors) |
---|
42 | int dimx; |
---|
43 | //!description of modelled data \f$ y_t \f$ in the likelihood function |
---|
44 | //! Do NOT access directly, only via \c get_yrv(). |
---|
45 | RV _yrv; |
---|
46 | //! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density |
---|
47 | egiw est; |
---|
48 | //! cached value of est.V |
---|
49 | ldmat &V; |
---|
50 | //! cached value of est.nu |
---|
51 | double ν |
---|
52 | public: |
---|
53 | //! \name Constructors |
---|
54 | //!@{ |
---|
55 | ARX ( const double frg0=1.0 ) : BMEF ( frg0 ),est (), V ( est._V() ), nu ( est._nu() ) {}; |
---|
56 | ARX ( const ARX &A0 ) : BMEF (),est ( A0.est ), V ( est._V() ), nu ( est._nu() ) {}; |
---|
57 | ARX* _copy_(); |
---|
58 | void set_parameters ( double frg0 ) {frg=frg0;} |
---|
59 | void set_statistics ( int dimx0, const ldmat V0, double nu0=-1.0 ) {est.set_parameters ( dimx0,V0,nu0 );last_lognc=est.lognc();dimx=dimx0;} |
---|
60 | //!@} |
---|
61 | |
---|
62 | // //! Set parameters given by moments, \c mu (mean of theta), \c R (mean of R) and \c C (variance of theta) |
---|
63 | // void set_parameters ( const vec &mu, const mat &R, const mat &C, double dfm){}; |
---|
64 | //! Set sufficient statistics |
---|
65 | void set_statistics ( const BMEF* BM0 ); |
---|
66 | // //! Returns sufficient statistics |
---|
67 | // void get_parameters ( mat &V0, double &nu0 ) {V0=est._V().to_mat(); nu0=est._nu();} |
---|
68 | //!\name Mathematical operations |
---|
69 | //!@{ |
---|
70 | |
---|
71 | //! Weighted Bayes \f$ dt = [y_t psi_t] \f$. |
---|
72 | void bayes ( const vec &dt, const double w ); |
---|
73 | void bayes ( const vec &dt ) {bayes ( dt,1.0 );}; |
---|
74 | double logpred ( const vec &dt ) const; |
---|
75 | void flatten ( const BMEF* B ) { |
---|
76 | const ARX* A=dynamic_cast<const ARX*> ( B ); |
---|
77 | // nu should be equal to B.nu |
---|
78 | est.pow ( A->nu/nu ); |
---|
79 | if ( evalll ) {last_lognc=est.lognc();} |
---|
80 | } |
---|
81 | //! Conditioned version of the predictor |
---|
82 | enorm<ldmat>* epredictor ( const vec &rgr ) const; |
---|
83 | //! Predictor for empty regressor |
---|
84 | enorm<ldmat>* epredictor() const {it_assert_debug ( est.dimension() ==V.rows()-1,"Regressor is not only 1" );return epredictor ( vec_1 ( 1.0 ) );} |
---|
85 | //! conditional version of the predictor |
---|
86 | mlnorm<ldmat>* predictor() const; |
---|
87 | mlstudent* predictor_student() const; |
---|
88 | //! Brute force structure estimation.\return indeces of accepted regressors. |
---|
89 | ivec structure_est ( egiw Eg0 ); |
---|
90 | //!@} |
---|
91 | |
---|
92 | //!\name Access attributes |
---|
93 | //!@{ |
---|
94 | const egiw* _e() const {return &est ;}; |
---|
95 | const egiw& posterior() const {return est;} |
---|
96 | //!@} |
---|
97 | |
---|
98 | //!\name Connection |
---|
99 | //!@{ |
---|
100 | void set_drv ( const RV &drv0 ) {drv=drv0;} |
---|
101 | RV& get_yrv() { |
---|
102 | //if yrv is not ready create it |
---|
103 | if ( _yrv._dsize() !=dimx ) { |
---|
104 | int i=0; |
---|
105 | while ( _yrv._dsize() <dimx ) {_yrv.add ( drv ( vec_1(i) ) );i++;} |
---|
106 | } |
---|
107 | //yrv should be ready by now |
---|
108 | it_assert_debug ( _yrv._dsize() ==dimx,"incompatible drv" ); |
---|
109 | return _yrv; |
---|
110 | } |
---|
111 | //!@} |
---|
112 | }; |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | #endif // AR_H |
---|
117 | |
---|