Changeset 739 for library/bdm/estim

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

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

Location:
library/bdm/estim
Files:
4 modified

Legend:

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

    r737 r739  
    150150} 
    151151 
     152void StateCanonical::connect_mlnorm ( const mlnorm<fsqmat> &ml ) { 
     153        //get ids of yrv 
     154        const RV &yrv = ml._rv(); 
     155        //need to determine u_t - it is all in _rvc that is not in ml._rv() 
     156        RV rgr0 = ml._rvc().remove_time(); 
     157        RV urv = rgr0.subt ( yrv ); 
     158 
     159        //We can do only 1d now... :( 
     160        bdm_assert ( yrv._dsize() == 1, "Only for SISO so far..." ); 
     161 
     162        // create names for 
     163        RV xrv; //empty 
     164        RV Crv; //empty 
     165        int td = ml._rvc().mint(); 
     166        // assuming strictly proper function!!! 
     167        for ( int t = -1; t >= td; t-- ) { 
     168                xrv.add ( yrv.copy_t ( t ) ); 
     169                Crv.add ( urv.copy_t ( t ) ); 
     170        } 
     171 
     172        // get mapp 
     173        th2A.set_connection ( xrv, ml._rvc() ); 
     174        th2C.set_connection ( Crv, ml._rvc() ); 
     175        th2D.set_connection ( urv, ml._rvc() ); 
     176 
     177        //set matrix sizes 
     178        this->A = zeros ( xrv._dsize(), xrv._dsize() ); 
     179        for ( int j = 1; j < xrv._dsize(); j++ ) { 
     180                A ( j, j - 1 ) = 1.0;    // off diagonal 
     181        } 
     182        this->B = zeros ( xrv._dsize(), 1 ); 
     183        this->B ( 0 ) = 1.0; 
     184        this->C = zeros ( 1, xrv._dsize() ); 
     185        this->D = zeros ( 1, urv._dsize() ); 
     186        this->Q = zeros ( xrv._dsize(), xrv._dsize() ); 
     187        // R is set by update 
     188 
     189        //set cache 
     190        this->A1row = zeros ( xrv._dsize() ); 
     191        this->C1row = zeros ( xrv._dsize() ); 
     192        this->D1row = zeros ( urv._dsize() ); 
     193 
     194        update_from ( ml ); 
     195        validate(); 
     196}; 
     197 
     198void StateCanonical::update_from ( const mlnorm<fsqmat> &ml ) { 
     199 
     200        vec theta = ml._A().get_row ( 0 ); // this 
     201 
     202        th2A.filldown ( theta, A1row ); 
     203        th2C.filldown ( theta, C1row ); 
     204        th2D.filldown ( theta, D1row ); 
     205 
     206        R = ml._R(); 
     207 
     208        A.set_row ( 0, A1row ); 
     209        C.set_row ( 0, C1row + D1row ( 0 ) *A1row ); 
     210        D.set_row ( 0, D1row ); 
     211} 
     212 
     213void StateFromARX::connect_mlnorm ( const mlnorm<chmat> &ml, RV &xrv, RV &urv ) { 
     214        //get ids of yrv 
     215        const RV &yrv = ml._rv(); 
     216        //need to determine u_t - it is all in _rvc that is not in ml._rv() 
     217        const RV &rgr = ml._rvc(); 
     218        RV rgr0 = rgr.remove_time(); 
     219        urv = rgr0.subt ( yrv ); 
     220 
     221        // create names for state variables 
     222        xrv = yrv; 
     223 
     224        int y_multiplicity = -rgr.mint ( yrv ); 
     225        int y_block_size = yrv.length() * ( y_multiplicity ); // current yt + all delayed yts 
     226        for ( int m = 0; m < y_multiplicity - 1; m++ ) { // ========= -1 is important see arx2statespace_notes 
     227                xrv.add ( yrv.copy_t ( -m - 1 ) ); //add delayed yt 
     228        } 
     229        //! temporary RV for connection to ml.rvc, since notation of xrv and ml.rvc does not match 
     230        RV xrv_ml = xrv.copy_t ( -1 ); 
     231 
     232        // add regressors 
     233        ivec u_block_sizes ( urv.length() ); // no_blocks = yt + unique rgr 
     234        for ( int r = 0; r < urv.length(); r++ ) { 
     235                RV R = urv.subselect ( vec_1 ( r ) ); //non-delayed element of rgr 
     236                int r_size = urv.size ( r ); 
     237                int r_multiplicity = -rgr.mint ( R ); 
     238                u_block_sizes ( r ) = r_size * r_multiplicity ; 
     239                for ( int m = 0; m < r_multiplicity; m++ ) { 
     240                        xrv.add ( R.copy_t ( -m - 1 ) ); //add delayed yt 
     241                        xrv_ml.add ( R.copy_t ( -m - 1 ) ); //add delayed yt 
     242                } 
     243        } 
     244        // add constant 
     245        if ( any ( ml._mu_const() != 0.0 ) ) { 
     246                have_constant = true; 
     247                xrv.add ( RV ( "bdm_reserved_constant_one", 1 ) ); 
     248        } else { 
     249                have_constant = false; 
     250        } 
     251 
     252 
     253        // get mapp 
     254        th2A.set_connection ( xrv_ml, ml._rvc() ); 
     255        th2B.set_connection ( urv, ml._rvc() ); 
     256 
     257        //set matrix sizes 
     258        this->A = zeros ( xrv._dsize(), xrv._dsize() ); 
     259        //create y block 
     260        diagonal_part ( this->A, yrv._dsize(), 0, y_block_size - yrv._dsize() ); 
     261 
     262        this->B = zeros ( xrv._dsize(), urv._dsize() ); 
     263        //add diagonals for rgr 
     264        int active_x = y_block_size; 
     265        for ( int r = 0; r < urv.length(); r++ ) { 
     266                diagonal_part ( this->A, active_x + urv.size ( r ), active_x, u_block_sizes ( r ) - urv.size ( r ) ); 
     267                this->B.set_submatrix ( active_x, 0, eye ( urv.size ( r ) ) ); 
     268                active_x += u_block_sizes ( r ); 
     269        } 
     270        this->C = zeros ( yrv._dsize(), xrv._dsize() ); 
     271        this->C.set_submatrix ( 0, 0, eye ( yrv._dsize() ) ); 
     272        this->D = zeros ( yrv._dsize(), urv._dsize() ); 
     273        this->R.setCh ( zeros ( yrv._dsize(), yrv._dsize() ) ); 
     274        this->Q.setCh ( zeros ( xrv._dsize(), xrv._dsize() ) ); 
     275        // Q is set by update 
     276 
     277        update_from ( ml ); 
     278        validate(); 
     279} 
     280         
     281void StateFromARX::update_from ( const mlnorm<chmat> &ml ) { 
     282        vec Arow = zeros ( A.cols() ); 
     283        vec Brow = zeros ( B.cols() ); 
     284        //  ROW- WISE EVALUATION ===== 
     285        for ( int i = 0; i < ml._rv()._dsize(); i++ ) { 
     286 
     287                vec theta = ml._A().get_row ( i ); 
     288 
     289                th2A.filldown ( theta, Arow ); 
     290                if ( have_constant ) { 
     291                        // constant is always at the end no need for datalink 
     292                        Arow ( A.cols() - 1 ) = ml._mu_const() ( i ); 
     293                } 
     294                this->A.set_row ( i, Arow ); 
     295 
     296                th2B.filldown ( theta, Brow ); 
     297                this->B.set_row ( i, Brow ); 
     298        } 
     299        this->Q._Ch().set_submatrix ( 0, 0, ml.__R()._Ch() ); 
     300 
     301} 
    152302 
    153303 
  • 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 { 
  • library/bdm/estim/particles.cpp

    r737 r739  
    7575// } 
    7676 
     77vec MPF::mpfepdf::mean() const { 
     78        const vec &w = pf->posterior()._w(); 
     79        vec pom = zeros ( BMs ( 0 )->posterior ().dimension() ); 
     80        //compute mean of BMs 
     81        for ( int i = 0; i < w.length(); i++ ) { 
     82                pom += BMs ( i )->posterior().mean() * w ( i ); 
     83        } 
     84        return concat ( pf->posterior().mean(), pom ); 
     85} 
     86 
     87vec MPF::mpfepdf::variance() const { 
     88        const vec &w = pf->posterior()._w(); 
     89 
     90        vec pom = zeros ( BMs ( 0 )->posterior ().dimension() ); 
     91        vec pom2 = zeros ( BMs ( 0 )->posterior ().dimension() ); 
     92        vec mea; 
     93 
     94        for ( int i = 0; i < w.length(); i++ ) { 
     95                // save current mean 
     96                mea = BMs ( i )->posterior().mean(); 
     97                pom += mea * w ( i ); 
     98                //compute variance 
     99                pom2 += ( BMs ( i )->posterior().variance() + pow ( mea, 2 ) ) * w ( i ); 
     100        } 
     101        return concat ( pf->posterior().variance(), pom2 - pow ( pom, 2 ) ); 
     102} 
     103 
     104void MPF::mpfepdf::qbounds ( vec &lb, vec &ub, double perc ) const { 
     105        //bounds on particles 
     106        vec lbp; 
     107        vec ubp; 
     108        pf->posterior().qbounds ( lbp, ubp ); 
     109 
     110        //bounds on Components 
     111        int dimC = BMs ( 0 )->posterior().dimension(); 
     112        int j; 
     113        // temporary 
     114        vec lbc ( dimC ); 
     115        vec ubc ( dimC ); 
     116        // minima and maxima 
     117        vec Lbc ( dimC ); 
     118        vec Ubc ( dimC ); 
     119        Lbc = std::numeric_limits<double>::infinity(); 
     120        Ubc = -std::numeric_limits<double>::infinity(); 
     121 
     122        for ( int i = 0; i < BMs.length(); i++ ) { 
     123                // check Coms 
     124                BMs ( i )->posterior().qbounds ( lbc, ubc ); 
     125                //save either minima or maxima 
     126                for ( j = 0; j < dimC; j++ ) { 
     127                        if ( lbc ( j ) < Lbc ( j ) ) { 
     128                                Lbc ( j ) = lbc ( j ); 
     129                        } 
     130                        if ( ubc ( j ) > Ubc ( j ) ) { 
     131                                Ubc ( j ) = ubc ( j ); 
     132                        } 
     133                } 
     134        } 
     135        lb = concat ( lbp, Lbc ); 
     136        ub = concat ( ubp, Ubc ); 
     137} 
     138 
     139 
     140 
    77141void MPF::bayes ( const vec &yt, const vec &cond ) { 
    78142        // follows PF::bayes in most places!!! 
  • library/bdm/estim/particles.h

    r737 r739  
    236236                        bdm_assert_debug ( dim == rv._dsize(), "Wrong name " ); 
    237237                } 
    238                 vec mean() const { 
    239                         const vec &w = pf->posterior()._w(); 
    240                         vec pom = zeros ( BMs ( 0 )->posterior ().dimension() ); 
    241                         //compute mean of BMs 
    242                         for ( int i = 0; i < w.length(); i++ ) { 
    243                                 pom += BMs ( i )->posterior().mean() * w ( i ); 
    244                         } 
    245                         return concat ( pf->posterior().mean(), pom ); 
    246                 } 
    247                 vec variance() const { 
    248                         const vec &w = pf->posterior()._w(); 
    249  
    250                         vec pom = zeros ( BMs ( 0 )->posterior ().dimension() ); 
    251                         vec pom2 = zeros ( BMs ( 0 )->posterior ().dimension() ); 
    252                         vec mea; 
    253  
    254                         for ( int i = 0; i < w.length(); i++ ) { 
    255                                 // save current mean 
    256                                 mea = BMs ( i )->posterior().mean(); 
    257                                 pom += mea * w ( i ); 
    258                                 //compute variance 
    259                                 pom2 += ( BMs ( i )->posterior().variance() + pow ( mea, 2 ) ) * w ( i ); 
    260                         } 
    261                         return concat ( pf->posterior().variance(), pom2 - pow ( pom, 2 ) ); 
    262                 } 
    263  
    264                 void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const { 
    265                         //bounds on particles 
    266                         vec lbp; 
    267                         vec ubp; 
    268                         pf->posterior().qbounds ( lbp, ubp ); 
    269  
    270                         //bounds on Components 
    271                         int dimC = BMs ( 0 )->posterior().dimension(); 
    272                         int j; 
    273                         // temporary 
    274                         vec lbc ( dimC ); 
    275                         vec ubc ( dimC ); 
    276                         // minima and maxima 
    277                         vec Lbc ( dimC ); 
    278                         vec Ubc ( dimC ); 
    279                         Lbc = std::numeric_limits<double>::infinity(); 
    280                         Ubc = -std::numeric_limits<double>::infinity(); 
    281  
    282                         for ( int i = 0; i < BMs.length(); i++ ) { 
    283                                 // check Coms 
    284                                 BMs ( i )->posterior().qbounds ( lbc, ubc ); 
    285                                 //save either minima or maxima 
    286                                 for ( j = 0; j < dimC; j++ ) { 
    287                                         if ( lbc ( j ) < Lbc ( j ) ) { 
    288                                                 Lbc ( j ) = lbc ( j ); 
    289                                         } 
    290                                         if ( ubc ( j ) > Ubc ( j ) ) { 
    291                                                 Ubc ( j ) = ubc ( j ); 
    292                                         } 
    293                                 } 
    294                         } 
    295                         lb = concat ( lbp, Lbc ); 
    296                         ub = concat ( ubp, Ubc ); 
    297                 } 
     238                vec mean() const; 
     239 
     240                vec variance() const; 
     241 
     242                void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const; 
    298243 
    299244                vec sample() const {