Show
Ignore:
Timestamp:
11/15/09 23:02:02 (15 years ago)
Author:
smidl
Message:

Big commit of LQG stuff

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/estim/kalman.h

    r703 r723  
    2727 * \brief Basic elements of linear state-space model 
    2828 
    29 Parameter evolution model:\f[ x_t = A x_{t-1} + B u_t + Q^{1/2} e_t \f] 
    30 Observation model: \f[ y_t = C x_{t-1} + C u_t + Q^{1/2} w_t. \f] 
    31 Where $e_t$ and $w_t$ are independent vectors Normal(0,1)-distributed disturbances. 
     29Parameter evolution model:\f[ x_{t+1} = A x_{t} + B u_t + Q^{1/2} e_t \f] 
     30Observation model: \f[ y_t = C x_{t} + C u_t + R^{1/2} w_t. \f] 
     31Where $e_t$ and $w_t$ are mutually independent vectors of Normal(0,1)-distributed disturbances. 
    3232 */ 
    3333template<class sq_T> 
     
    6161                        UI::get (C, set, "C", UI::compulsory); 
    6262                        UI::get (D, set, "D", UI::compulsory); 
    63                         mat Qtm, Rtm; 
     63                        mat Qtm, Rtm; // full matrices 
    6464                        if(!UI::get(Qtm, set, "Q", UI::optional)){ 
    6565                                vec dq; 
     
    7272                                Rtm=diag(dr); 
    7373                        } 
    74                         R=Rtm; // automatic conversion 
     74                        R=Rtm; // automatic conversion to square-root form 
    7575                        Q=Qtm;  
    7676                         
     
    519519 
    520520*/ 
    521 class StateFromARX: public StateSpace<fsqmat>{ 
     521class StateFromARX: public StateSpace<chmat>{ 
    522522        protected: 
    523523                //! remember connection from theta ->A 
    524524                datalink_part th2A; 
    525                 //! xrv 
    526                 RV xrv; 
     525                //! remember connection from theta ->B 
     526                datalink_part th2B; 
    527527                //!function adds n diagonal elements from given starting point r,c 
    528528                void diagonal_part(mat &A, int r, int c, int n){ 
    529529                        for(int i=0; i<n; i++){A(r,c)=1.0; r++;c++;} 
    530530                }; 
     531                //! similar to ARX.have_constant 
     532                bool have_constant; 
    531533        public: 
    532534                //! set up this object to match given mlnorm 
    533                 void connect_mlnorm(const mlnorm<fsqmat> &ml){ 
     535                //! Note that state-space and common mpdf use different meaning of \f$ _t \f$ in \f$ u_t \f$.  
     536                //!While mlnorm typically assumes that \f$ u_t \rightarrow y_t \f$ in state space it is \f$ u_{t-1} \rightarrow y_t \f$ 
     537                //! For consequences in notation of internal variable xt see arx2statespace_notes.lyx. 
     538                void connect_mlnorm(const mlnorm<chmat> &ml, RV &xrv, RV &urv){ 
     539 
    534540                        //get ids of yrv                                 
    535541                        const RV &yrv = ml._rv(); 
     
    537543                        const RV &rgr = ml._rvc(); 
    538544                        RV rgr0 = rgr.remove_time(); 
    539                         RV urv = rgr0.subt(yrv);  
     545                        urv = rgr0.subt(yrv);  
    540546                                                 
    541547                        // create names for state variables 
    542548                        xrv=yrv;  
    543549                         
    544                         int y_multiplicity = rgr.mint(yrv); 
    545                         int y_block_size = yrv.length()*(y_multiplicity+1); // current yt + all delayed yts 
    546                         for (int m=0;m<y_multiplicity;m++){  
     550                        int y_multiplicity = -rgr.mint(yrv); 
     551                        int y_block_size = yrv.length()*(y_multiplicity); // current yt + all delayed yts 
     552                        for (int m=0;m<y_multiplicity - 1;m++){ // ========= -1 is important see arx2statespace_notes 
    547553                                xrv.add(yrv.copy_t(-m-1)); //add delayed yt 
    548554                        } 
     555                        //! temporary RV for connection to ml.rvc, since notation of xrv and ml.rvc does not match 
     556                        RV xrv_ml = xrv.copy_t(-1); 
     557                         
    549558                        // add regressors 
    550559                        ivec u_block_sizes(urv.length()); // no_blocks = yt + unique rgr 
     
    552561                                RV R=urv.subselect(vec_1(r)); //non-delayed element of rgr 
    553562                                int r_size=urv.size(r); 
    554                                 int r_multiplicity=rgr.mint(R); 
     563                                int r_multiplicity=-rgr.mint(R); 
    555564                                u_block_sizes(r)= r_size*r_multiplicity ; 
    556565                                for(int m=0;m<r_multiplicity;m++){  
    557566                                        xrv.add(R.copy_t(-m-1)); //add delayed yt 
     567                                        xrv_ml.add(R.copy_t(-m-1)); //add delayed yt 
    558568                                } 
    559569                        } 
    560                          
     570                        // add constant 
     571                        if (any(ml._mu_const()!=0.0)){ 
     572                                have_constant=true; 
     573                                xrv.add(RV("bdm_reserved_constant_one",1)); 
     574                        } else {have_constant=false;} 
     575                                 
     576                 
    561577                        // get mapp 
    562                         th2A.set_connection(xrv, ml._rvc()); 
     578                        th2A.set_connection(xrv_ml, ml._rvc()); 
     579                        th2B.set_connection(urv, ml._rvc()); 
    563580                         
    564581                        //set matrix sizes 
     
    577594                        this->C=zeros(yrv._dsize(), xrv._dsize()); 
    578595                        this->C.set_submatrix(0,0,eye(yrv._dsize())); 
    579                                 this->D=zeros(yrv._dsize(),urv._dsize()); 
    580                                 this->R = zeros(yrv._dsize(),yrv._dsize()); 
    581                                 this->Q = zeros(xrv._dsize(),xrv._dsize()); 
    582                                 // Q is set by update 
    583                                                                  
    584                                 update_from(ml); 
    585                                 validate(); 
     596                        this->D=zeros(yrv._dsize(),urv._dsize()); 
     597                        this->R.setCh(zeros(yrv._dsize(),yrv._dsize())); 
     598                        this->Q.setCh(zeros(xrv._dsize(),xrv._dsize())); 
     599                        // Q is set by update 
     600                                                         
     601                        update_from(ml); 
     602                        validate(); 
    586603                }; 
    587604                //! fast function to update parameters from ml - not checked for compatibility!! 
    588                 void update_from(const mlnorm<fsqmat> &ml){ 
    589                          
    590                         vec theta = ml._A().get_row(0); // this  
    591                         this->Q._M().set_submatrix(0,0,ml._R()); 
    592                          
    593                 } 
     605                void update_from(const mlnorm<chmat> &ml){ 
     606 
     607                        vec Arow=zeros(A.cols()); 
     608                        vec Brow=zeros(B.cols()); 
     609                        //  ROW- WISE EVALUATION ===== 
     610                        for(int i=0;i<ml._rv()._dsize(); i++){  
     611                                 
     612                                vec theta = ml._A().get_row(i);  
     613                                 
     614                                th2A.filldown(theta,Arow); 
     615                                if (have_constant){ 
     616                                        // constant is always at the end no need for datalink 
     617                                        Arow(A.cols()-1) = ml._mu_const()(i); 
     618                                } 
     619                                this->A.set_row(i,Arow); 
     620                                 
     621                                th2B.filldown(theta,Brow); 
     622                                this->B.set_row(i,Brow); 
     623                        } 
     624                        this->Q._Ch().set_submatrix(0,0,ml.__R()._Ch()); 
     625                         
     626                }; 
     627                //! access function 
     628                bool _have_constant() const {return have_constant;} 
    594629}; 
    595630