root/library/bdm/estim/arx.h @ 471

Revision 471, 4.4 kB (checked in by mido, 15 years ago)

1) ad UserInfo?: UI::get a UI::build predelany tak, ze vraci fals / NULL v pripade neexistence pozadovaneho Settingu, pridana enumericky typ UI::SettingPresence?, predelany stavajici UI implementace, dodelana UI dokumentace
2) dokoncena konfigurace ASTYLERU, brzy bude aplikovan
3) doxygen nastaven tak, ze vytvari soubor doxy_warnings.txt

  • Property svn:eol-style set to native
Line 
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 "../math/functions.h"
17#include "../stat/exp_family.h"
18#include "../base/user_info.h"
19
20namespace bdm {
21
22/*!
23* \brief Linear Autoregressive model with Gaussian noise
24
25Regression of the following kind:
26\f[
27y_t = \theta_1 \psi_1 + \theta_2 + \psi_2 +\ldots + \theta_n \psi_n + r e_t
28\f]
29where 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:
30\f[
31e_t \sim \mathcal{N}(0,1).
32\f]
33
34See \ref tut_arx for mathematical treatment.
35
36The easiest way how to use the class is:
37\include arx_simple.cpp
38
39        \todo sort out constant terms - bayes should accept vec without additional 1s
40*/
41class ARX: public BMEF {
42protected:
43        //!size of output variable (needed in regressors)
44        int dimx;
45        //!description of modelled data \f$ y_t \f$ in the likelihood function
46        //! Do NOT access directly, only via \c get_yrv().
47        RV _yrv;
48        //! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density
49        egiw est;
50        //! cached value of est.V
51        ldmat &V;
52        //! cached value of est.nu
53        double ν
54public:
55        //! \name Constructors
56        //!@{
57        ARX ( const double frg0=1.0 ) : BMEF ( frg0 ),est (), V ( est._V() ), nu ( est._nu() ) {};
58        ARX ( const ARX &A0 ) : BMEF (),est (), V ( est._V() ), nu ( est._nu() ) {
59                set_statistics ( A0.dimx,A0.V,A0.nu );
60                set_parameters(A0.frg);
61        };
62        ARX* _copy_() const;
63        void set_parameters ( double frg0 ) {frg=frg0;}
64        void set_statistics ( int dimx0, const ldmat V0, double nu0=-1.0 ) {est.set_parameters ( dimx0,V0,nu0 );last_lognc=est.lognc();dimx=dimx0;}
65        //!@}
66
67//      //! Set parameters given by moments, \c mu (mean of theta), \c R (mean of R) and \c C (variance of theta)
68//      void set_parameters ( const vec &mu, const mat &R, const mat &C, double dfm){};
69        //! Set sufficient statistics
70        void set_statistics ( const BMEF* BM0 );
71//      //! Returns sufficient statistics
72//      void get_parameters ( mat &V0, double &nu0 ) {V0=est._V().to_mat(); nu0=est._nu();}
73        //!\name Mathematical operations
74        //!@{
75
76        //! Weighted Bayes \f$ dt = [y_t psi_t] \f$.
77        void bayes ( const vec &dt, const double w );
78        void bayes ( const vec &dt ) {bayes ( dt,1.0 );};
79        double logpred ( const vec &dt ) const;
80        void flatten ( const BMEF* B ) {
81                const ARX* A=dynamic_cast<const ARX*> ( B );
82                // nu should be equal to B.nu
83                est.pow ( A->nu/nu );
84                if ( evalll ) {last_lognc=est.lognc();}
85        }
86        //! Conditioned version of the predictor
87        enorm<ldmat>* epredictor ( const vec &rgr ) const;
88        //! Predictor for empty regressor
89        enorm<ldmat>* epredictor() const {
90                it_assert_debug ( dimx==V.rows()-1,"Regressor is not only 1" );
91                return epredictor ( vec_1 ( 1.0 ) );
92        }
93        //! conditional version of the predictor
94        mlnorm<ldmat>* predictor() const;
95        mlstudent* predictor_student() const;
96        //! Brute force structure estimation.\return indeces of accepted regressors.
97        ivec structure_est ( egiw Eg0 );
98        //!@}
99
100        //!\name Access attributes
101        //!@{
102        const egiw* _e() const {return &est ;};
103        const egiw& posterior() const {return est;}
104        //!@}
105
106        //!\name Connection
107        //!@{
108        void set_drv ( const RV &drv0 ) {drv=drv0;}
109        RV& get_yrv() {
110                //if yrv is not ready create it
111                if ( _yrv._dsize() !=dimx ) {
112                        int i=0;
113                        while ( _yrv._dsize() <dimx ) {_yrv.add ( drv ( vec_1 ( i ) ) );i++;}
114                }
115                //yrv should be ready by now
116                it_assert_debug ( _yrv._dsize() ==dimx,"incompatible drv" );
117                return _yrv;
118        }
119        //!@}
120
121        // TODO dokumentace - aktualizovat
122        /*! UI for ARX estimator
123
124        The ARX is constructed from a structure with fields:
125        \code
126        estimator = {
127                type = "ARX";
128                y = {type="rv", ...}   // description of output variables
129                rgr = {type="rv", ...} // description of regressor variables
130                constant = true;       // boolean switch if the constant term is modelled or not
131
132                //optional fields
133                dV0 = [1e-3, 1e-5, 1e-5, 1e-5];
134                                                           // default: 1e-3 for y, 1e-5 for rgr
135                nu0 = 6;               // default: rgrlen + 2
136                frg = 1.0;             // forgetting, default frg=1.0
137        };
138        \endcode
139
140        The estimator will assign names of the posterior in the form ["theta_i" and "r_i"]
141        */
142        void from_setting( const Setting &set );
143
144};
145
146UIREGISTER(ARX);
147
148}
149
150#endif // AR_H
151
Note: See TracBrowser for help on using the browser.