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

Revision 878, 6.5 kB (checked in by sarka, 14 years ago)

dim ze set_parameters do validate

  • Property svn:eol-style set to native
RevLine 
[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]22namespace bdm {
[97]23
24/*!
25* \brief Linear Autoregressive model with Gaussian noise
26
27Regression of the following kind:
28\f[
29y_t = \theta_1 \psi_1 + \theta_2 + \psi_2 +\ldots + \theta_n \psi_n + r e_t
30\f]
31where 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[
33e_t \sim \mathcal{N}(0,1).
34\f]
35
[271]36See \ref tut_arx for mathematical treatment.
37
38The 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]43class ARX: public BMEF {
[97]44protected:
[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]53public:
[270]54        //! \name Constructors
55        //!@{
[737]56        ARX ( const double frg0 = 1.0 ) : BMEF ( frg0 ),  have_constant ( true ), dyad(), est(), alter_est() {};
57        ARX ( const ARX &A0 ) : BMEF ( A0 ),  have_constant ( A0.have_constant ), dyad ( A0.dyad ), est ( A0.est ), alter_est ( A0.alter_est ) { };
[766]58
59        ARX* _copy() const;
60
[796]61        void set_frg ( double frg0 ) {
[477]62                frg = frg0;
63        }
[649]64        void set_constant ( bool const0 ) {
[737]65                have_constant = const0;
[649]66        }
[679]67        void set_statistics ( int dimy0, const ldmat V0, double nu0 = -1.0 ) {
68                est.set_parameters ( dimy0, V0, nu0 );
[878]69                est.validate();
[477]70                last_lognc = est.lognc();
[679]71                dimy = dimy0;
[477]72        }
[270]73        //!@}
[170]74
[145]75        //! Set sufficient statistics
[170]76        void set_statistics ( const BMEF* BM0 );
[625]77
[270]78        //!\name Mathematical operations
79        //!@{
80
81        //! Weighted Bayes \f$ dt = [y_t psi_t] \f$.
[737]82        void bayes_weighted ( const vec &yt, const vec &cond = empty_vec, const double w = 1.0 );
83        void bayes ( const vec &yt, const vec &cond = empty_vec ) {
84                bayes_weighted ( yt, cond, 1.0 );
[477]85        };
[679]86        double logpred ( const vec &yt ) const;
[738]87        void flatten ( const BMEF* B );
[262]88        //! Conditioned version of the predictor
[270]89        enorm<ldmat>* epredictor ( const vec &rgr ) const;
90        //! Predictor for empty regressor
[738]91        enorm<ldmat>* epredictor() const;
[262]92        //! conditional version of the predictor
[723]93        template<class sq_T>
94        shared_ptr<mlnorm<sq_T> > ml_predictor() const;
[737]95        //! fast version of predicto
[723]96        template<class sq_T>
[737]97        void ml_predictor_update ( mlnorm<sq_T> &pred ) const;
[270]98        mlstudent* predictor_student() const;
[97]99        //! Brute force structure estimation.\return indeces of accepted regressors.
[170]100        ivec structure_est ( egiw Eg0 );
[577]101        //! Smarter structure estimation by Ludvik Tesar.\return indeces of accepted regressors.
102        ivec structure_est_LT ( egiw Eg0 );
[270]103        //!@}
104
105        //!\name Access attributes
106        //!@{
[737]107        //! return correctly typed posterior (covariant return)
108        const egiw& posterior() const {
[477]109                return est;
110        }
[270]111        //!@}
112
[357]113        /*! UI for ARX estimator
114
115        \code
[625]116        class = 'ARX';
117        rv    = RV({names_of_dt} )                 // description of output variables
118        rgr   = RV({names_of_regressors}, [-1,-2]} // description of regressor variables
[631]119        constant = 1;                              // 0/1 switch if the constant term is modelled or not
[357]120
[625]121        --- optional ---
[665]122        prior = {class='egiw',...};                // Prior density, when given default is used instead
123        alternative = {class='egiw',...};          // Alternative density in stabilized estimation, when not given prior is used
[737]124
[625]125        frg = 1.0;                                 // forgetting, default frg=1.0
126
[737]127        rv_param   = RV({names_of_parameters}}     // description of parametetr names
[625]128                                                                                           // default: ["theta_i" and "r_i"]
[357]129        \endcode
130        */
[477]131        void from_setting ( const Setting &set );
[357]132
[625]133        void validate() {
[850]134                BMEF::validate();       
135
[679]136                //if dimc not set set it from V
[737]137                if ( dimc == 0 ) {
138                        dimc = posterior()._V().rows() - dimy - int ( have_constant == true );
[679]139                }
[737]140
141                if ( have_constant ) {
142                        dyad = ones ( dimy + dimc + 1 );
143                } else {
144                        dyad = zeros ( dimy + dimc );
[679]145                }
[737]146
[625]147        }
[737]148        //! function sets prior and alternative density
149        void set_prior ( const RV &drv, egiw &prior ) {
[665]150                //TODO check ranges in RV and build prior
151        };
152        //! build default prior and alternative when all values are set
[737]153        void set_prior_default ( egiw &prior ) {
154                //assume
155                vec dV0 ( prior._V().rows() );
156                dV0.set_subvector ( 0, prior._dimx() - 1, 1.0 );
[810]157                if (dV0.length()>prior._dimx())
158                        dV0.set_subvector ( prior._dimx(), dV0.length() - 1, 1e-5 );
[878]159               
[737]160                prior.set_parameters ( prior._dimx(), ldmat ( dV0 ) );
[878]161                prior.validate();
[665]162        }
[746]163
164        void to_setting ( Setting &set ) const
165        {                       
[796]166                BMEF::to_setting( set ); // takes care of rv, yrv, rvc
167                int constant = have_constant ? 1 : 0;
168                UI::save(constant, set, "constant");
169                UI::save(&est, set, "prior");
170                UI::save(&alter_est, set, "alternative");
171               
172               
[746]173        } 
[97]174};
[477]175UIREGISTER ( ARX );
[529]176SHAREDPTR ( ARX );
[357]177
[639]178/*! ARX model conditined by knowledge of the forgetting factor
179\f[ f(\theta| d_1 \ldots d_t , \phi_t) \f]
[700]180
181The symbol \f$ \phi \f$ is assumed to be the last of the conditioning variables.
[639]182*/
[737]183class ARXfrg : public ARX {
184public:
185        ARXfrg() : ARX() {};
186        //! copy constructor
187        ARXfrg ( const ARXfrg &A0 ) : ARX ( A0 ) {};
[766]188        virtual ARXfrg* _copy() const {
[737]189                ARXfrg *A = new ARXfrg ( *this );
190                return A;
191        }
[700]192
[737]193        void bayes ( const vec &val, const vec &cond ) {
194                frg = cond ( dimc - 1 ); //  last in cond is phi
195                ARX::bayes ( val, cond );
[639]196        }
[700]197        void validate() {
198                ARX::validate();
[737]199                rvc.add ( RV ( "{phi }", vec_1 ( 1 ) ) );
200                dimc += 1;
[700]201        }
[639]202};
[737]203UIREGISTER ( ARXfrg );
[723]204
205
206////////////////////
207template<class sq_T>
208shared_ptr< mlnorm<sq_T> > ARX::ml_predictor ( ) const {
209        shared_ptr< mlnorm<sq_T> > tmp = new mlnorm<sq_T> ( );
[737]210        tmp->set_rv ( yrv );
211        tmp->set_rvc ( _rvc() );
212
213        ml_predictor_update ( *tmp );
[723]214        tmp->validate();
215        return tmp;
216}
217
218template<class sq_T>
[737]219void ARX::ml_predictor_update ( mlnorm<sq_T> &pred ) const {
[723]220        mat mu ( dimy, posterior()._V().rows() - dimy );
221        mat R ( dimy, dimy );
[737]222
[723]223        est.mean_mat ( mu, R ); //mu =
224        mu = mu.T();
225        //correction for student-t  -- TODO check if correct!!
[737]226
227        if ( have_constant ) { // constant term
[723]228                //Assume the constant term is the last one:
229                pred.set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), sq_T ( R ) );
230        } else {
[737]231                pred.set_parameters ( mu, zeros ( dimy ), sq_T ( R ) );
[723]232        }
233}
234
[639]235};
[97]236#endif // AR_H
237
Note: See TracBrowser for help on using the browser.