Show
Ignore:
Timestamp:
11/25/09 18:02:21 (14 years ago)
Author:
mido
Message:

the rest of h to cpp movements, with exception of from_setting and validate to avoid conflicts with Sarka

Files:
1 modified

Legend:

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

    r737 r739  
    457457public: 
    458458        //! set up this object to match given mlnorm 
    459         void connect_mlnorm ( const mlnorm<fsqmat> &ml ) { 
    460                 //get ids of yrv 
    461                 const RV &yrv = ml._rv(); 
    462                 //need to determine u_t - it is all in _rvc that is not in ml._rv() 
    463                 RV rgr0 = ml._rvc().remove_time(); 
    464                 RV urv = rgr0.subt ( yrv ); 
    465  
    466                 //We can do only 1d now... :( 
    467                 bdm_assert ( yrv._dsize() == 1, "Only for SISO so far..." ); 
    468  
    469                 // create names for 
    470                 RV xrv; //empty 
    471                 RV Crv; //empty 
    472                 int td = ml._rvc().mint(); 
    473                 // assuming strictly proper function!!! 
    474                 for ( int t = -1; t >= td; t-- ) { 
    475                         xrv.add ( yrv.copy_t ( t ) ); 
    476                         Crv.add ( urv.copy_t ( t ) ); 
    477                 } 
    478  
    479                 // get mapp 
    480                 th2A.set_connection ( xrv, ml._rvc() ); 
    481                 th2C.set_connection ( Crv, ml._rvc() ); 
    482                 th2D.set_connection ( urv, ml._rvc() ); 
    483  
    484                 //set matrix sizes 
    485                 this->A = zeros ( xrv._dsize(), xrv._dsize() ); 
    486                 for ( int j = 1; j < xrv._dsize(); j++ ) { 
    487                         A ( j, j - 1 ) = 1.0;    // off diagonal 
    488                 } 
    489                 this->B = zeros ( xrv._dsize(), 1 ); 
    490                 this->B ( 0 ) = 1.0; 
    491                 this->C = zeros ( 1, xrv._dsize() ); 
    492                 this->D = zeros ( 1, urv._dsize() ); 
    493                 this->Q = zeros ( xrv._dsize(), xrv._dsize() ); 
    494                 // R is set by update 
    495  
    496                 //set cache 
    497                 this->A1row = zeros ( xrv._dsize() ); 
    498                 this->C1row = zeros ( xrv._dsize() ); 
    499                 this->D1row = zeros ( urv._dsize() ); 
    500  
    501                 update_from ( ml ); 
    502                 validate(); 
    503         }; 
     459        void connect_mlnorm ( const mlnorm<fsqmat> &ml ); 
     460 
    504461        //! fast function to update parameters from ml - not checked for compatibility!! 
    505         void update_from ( const mlnorm<fsqmat> &ml ) { 
    506  
    507                 vec theta = ml._A().get_row ( 0 ); // this 
    508  
    509                 th2A.filldown ( theta, A1row ); 
    510                 th2C.filldown ( theta, C1row ); 
    511                 th2D.filldown ( theta, D1row ); 
    512  
    513                 R = ml._R(); 
    514  
    515                 A.set_row ( 0, A1row ); 
    516                 C.set_row ( 0, C1row + D1row ( 0 ) *A1row ); 
    517                 D.set_row ( 0, D1row ); 
    518  
    519         } 
     462        void update_from ( const mlnorm<fsqmat> &ml ); 
    520463}; 
    521464/*! 
     
    553496        //!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$ 
    554497        //! For consequences in notation of internal variable xt see arx2statespace_notes.lyx. 
    555         void connect_mlnorm ( const mlnorm<chmat> &ml, RV &xrv, RV &urv ) { 
    556  
    557                 //get ids of yrv 
    558                 const RV &yrv = ml._rv(); 
    559                 //need to determine u_t - it is all in _rvc that is not in ml._rv() 
    560                 const RV &rgr = ml._rvc(); 
    561                 RV rgr0 = rgr.remove_time(); 
    562                 urv = rgr0.subt ( yrv ); 
    563  
    564                 // create names for state variables 
    565                 xrv = yrv; 
    566  
    567                 int y_multiplicity = -rgr.mint ( yrv ); 
    568                 int y_block_size = yrv.length() * ( y_multiplicity ); // current yt + all delayed yts 
    569                 for ( int m = 0; m < y_multiplicity - 1; m++ ) { // ========= -1 is important see arx2statespace_notes 
    570                         xrv.add ( yrv.copy_t ( -m - 1 ) ); //add delayed yt 
    571                 } 
    572                 //! temporary RV for connection to ml.rvc, since notation of xrv and ml.rvc does not match 
    573                 RV xrv_ml = xrv.copy_t ( -1 ); 
    574  
    575                 // add regressors 
    576                 ivec u_block_sizes ( urv.length() ); // no_blocks = yt + unique rgr 
    577                 for ( int r = 0; r < urv.length(); r++ ) { 
    578                         RV R = urv.subselect ( vec_1 ( r ) ); //non-delayed element of rgr 
    579                         int r_size = urv.size ( r ); 
    580                         int r_multiplicity = -rgr.mint ( R ); 
    581                         u_block_sizes ( r ) = r_size * r_multiplicity ; 
    582                         for ( int m = 0; m < r_multiplicity; m++ ) { 
    583                                 xrv.add ( R.copy_t ( -m - 1 ) ); //add delayed yt 
    584                                 xrv_ml.add ( R.copy_t ( -m - 1 ) ); //add delayed yt 
    585                         } 
    586                 } 
    587                 // add constant 
    588                 if ( any ( ml._mu_const() != 0.0 ) ) { 
    589                         have_constant = true; 
    590                         xrv.add ( RV ( "bdm_reserved_constant_one", 1 ) ); 
    591                 } else { 
    592                         have_constant = false; 
    593                 } 
    594  
    595  
    596                 // get mapp 
    597                 th2A.set_connection ( xrv_ml, ml._rvc() ); 
    598                 th2B.set_connection ( urv, ml._rvc() ); 
    599  
    600                 //set matrix sizes 
    601                 this->A = zeros ( xrv._dsize(), xrv._dsize() ); 
    602                 //create y block 
    603                 diagonal_part ( this->A, yrv._dsize(), 0, y_block_size - yrv._dsize() ); 
    604  
    605                 this->B = zeros ( xrv._dsize(), urv._dsize() ); 
    606                 //add diagonals for rgr 
    607                 int active_x = y_block_size; 
    608                 for ( int r = 0; r < urv.length(); r++ ) { 
    609                         diagonal_part ( this->A, active_x + urv.size ( r ), active_x, u_block_sizes ( r ) - urv.size ( r ) ); 
    610                         this->B.set_submatrix ( active_x, 0, eye ( urv.size ( r ) ) ); 
    611                         active_x += u_block_sizes ( r ); 
    612                 } 
    613                 this->C = zeros ( yrv._dsize(), xrv._dsize() ); 
    614                 this->C.set_submatrix ( 0, 0, eye ( yrv._dsize() ) ); 
    615                 this->D = zeros ( yrv._dsize(), urv._dsize() ); 
    616                 this->R.setCh ( zeros ( yrv._dsize(), yrv._dsize() ) ); 
    617                 this->Q.setCh ( zeros ( xrv._dsize(), xrv._dsize() ) ); 
    618                 // Q is set by update 
    619  
    620                 update_from ( ml ); 
    621                 validate(); 
    622         }; 
     498        void connect_mlnorm ( const mlnorm<chmat> &ml, RV &xrv, RV &urv ); 
     499 
    623500        //! fast function to update parameters from ml - not checked for compatibility!! 
    624         void update_from ( const mlnorm<chmat> &ml ) { 
    625  
    626                 vec Arow = zeros ( A.cols() ); 
    627                 vec Brow = zeros ( B.cols() ); 
    628                 //  ROW- WISE EVALUATION ===== 
    629                 for ( int i = 0; i < ml._rv()._dsize(); i++ ) { 
    630  
    631                         vec theta = ml._A().get_row ( i ); 
    632  
    633                         th2A.filldown ( theta, Arow ); 
    634                         if ( have_constant ) { 
    635                                 // constant is always at the end no need for datalink 
    636                                 Arow ( A.cols() - 1 ) = ml._mu_const() ( i ); 
    637                         } 
    638                         this->A.set_row ( i, Arow ); 
    639  
    640                         th2B.filldown ( theta, Brow ); 
    641                         this->B.set_row ( i, Brow ); 
    642                 } 
    643                 this->Q._Ch().set_submatrix ( 0, 0, ml.__R()._Ch() ); 
    644  
    645         }; 
     501        void update_from ( const mlnorm<chmat> &ml ); 
     502 
    646503        //! access function 
    647504        bool _have_constant() const {