Show
Ignore:
Timestamp:
10/23/09 00:05:25 (15 years ago)
Author:
smidl
Message:

Major changes in BM -- OK is only test suite and tests/tutorial -- the rest is broken!!!

Files:
1 modified

Legend:

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

    r675 r679  
    3434{ 
    3535        protected: 
    36                 //! cache of rv.count() 
    37                 int dimx; 
    38                 //! cache of rvy.count() 
    39                 int dimy; 
    40                 //! cache of rvu.count() 
    41                 int dimu; 
    4236                //! Matrix A 
    4337                mat A; 
     
    5347                sq_T R; 
    5448        public: 
    55                 StateSpace() : dimx (0), dimy (0), dimu (0), A(), B(), C(), D(), Q(), R() {} 
     49                StateSpace() : A(), B(), C(), D(), Q(), R() {} 
    5650                //!copy constructor 
    57                 StateSpace(const StateSpace<sq_T> &S0) : dimx (S0.dimx), dimy (S0.dimy), dimu (S0.dimu), A(S0.A), B(S0.B), C(S0.C), D(S0.D), Q(S0.Q), R(S0.R) {} 
     51                StateSpace(const StateSpace<sq_T> &S0) : A(S0.A), B(S0.B), C(S0.C), D(S0.D), Q(S0.Q), R(S0.R) {} 
    5852                //! set all matrix parameters 
    5953                void set_parameters (const mat &A0, const  mat &B0, const  mat &C0, const  mat &D0, const  sq_T &Q0, const sq_T &R0); 
     
    8377                }                
    8478                //! access function 
    85                 int _dimx(){return dimx;} 
    86                 //! access function 
    87                 int _dimy(){return dimy;} 
    88                 //! access function 
    89                 int _dimu(){return dimu;} 
    90                 //! access function 
    9179                const mat& _A() const {return A;} 
    9280                //! access function 
     
    10997                //! id of output 
    11098                RV yrv; 
    111                 //! id of input 
    112                 RV urv; 
    11399                //! Kalman gain 
    114100                mat  _K; 
    115101                //!posterior 
    116                 shared_ptr<enorm<sq_T> > est; 
     102                enorm<sq_T> est; 
    117103                //!marginal on data f(y|y) 
    118104                enorm<sq_T>  fy; 
    119105        public: 
    120                 Kalman<sq_T>() : BM(), StateSpace<sq_T>(), yrv(),urv(), _K(),  est(new enorm<sq_T>){} 
     106                Kalman<sq_T>() : BM(), StateSpace<sq_T>(), yrv(), _K(),  est(){} 
    121107                //! Copy constructor 
    122                 Kalman<sq_T>(const Kalman<sq_T> &K0) : BM(K0), StateSpace<sq_T>(K0), yrv(K0.yrv),urv(K0.urv), _K(K0._K),  est(new enorm<sq_T>(*K0.est)), fy(K0.fy){} 
     108                Kalman<sq_T>(const Kalman<sq_T> &K0) : BM(K0), StateSpace<sq_T>(K0), yrv(K0.yrv), _K(K0._K),  est(K0.est), fy(K0.fy){} 
    123109                //!set statistics of the posterior 
    124                 void set_statistics (const vec &mu0, const mat &P0) {est->set_parameters (mu0, P0); }; 
     110                void set_statistics (const vec &mu0, const mat &P0) {est.set_parameters (mu0, P0); }; 
    125111                //!set statistics of the posterior 
    126                 void set_statistics (const vec &mu0, const sq_T &P0) {est->set_parameters (mu0, P0); }; 
     112                void set_statistics (const vec &mu0, const sq_T &P0) {est.set_parameters (mu0, P0); }; 
    127113                //! return correctly typed posterior (covariant return) 
    128                 const enorm<sq_T>& posterior() const {return *est.get();} 
    129                 //! shared posterior 
    130                 shared_ptr<epdf> shared_posterior() {return est;} 
     114                const enorm<sq_T>& posterior() const {return est;} 
    131115                //! load basic elements of Kalman from structure 
    132116                void from_setting (const Setting &set) { 
     
    139123                        // Initial values 
    140124                        UI::get (yrv, set, "yrv", UI::optional); 
    141                         UI::get (urv, set, "urv", UI::optional); 
    142                         set_drv(concat(yrv,urv)); 
     125                        UI::get (rvc, set, "urv", UI::optional); 
     126                        set_yrv(concat(yrv,rvc)); 
    143127                         
    144128                        validate(); 
     
    147131                void validate() { 
    148132                        StateSpace<sq_T>::validate(); 
    149                         bdm_assert(est->dimension(), "Statistics and model parameters mismatch"); 
     133                        dimy = this->C.rows(); 
     134                        dimc = this->B.cols(); 
     135                        set_dim(this->A.rows()); 
     136                         
     137                        bdm_assert(est.dimension(), "Statistics and model parameters mismatch"); 
    150138                } 
    151139}; 
     
    160148                KalmanFull() :Kalman<fsqmat>(){}; 
    161149                //! Here dt = [yt;ut] of appropriate dimensions 
    162                 void bayes (const vec &dt); 
     150                void bayes (const vec &yt, const vec &cond=empty_vec); 
    163151                BM* _copy_() const { 
    164152                        KalmanFull* K = new KalmanFull; 
    165153                        K->set_parameters (A, B, C, D, Q, R); 
    166                         K->set_statistics (est->_mu(), est->_R()); 
     154                        K->set_statistics (est._mu(), est._R()); 
    167155                        return K; 
    168156                } 
     
    192180                        KalmanCh* K = new KalmanCh; 
    193181                        K->set_parameters (A, B, C, D, Q, R); 
    194                         K->set_statistics (est->_mu(), est->_R()); 
     182                        K->set_statistics (est._mu(), est._R()); 
    195183                        return K; 
    196184                } 
     
    213201                Thus this object evaluates only predictors! Not filtering densities. 
    214202                */ 
    215                 void bayes (const vec &dt); 
     203                void bayes (const vec &yt, const vec &cond=empty_vec); 
    216204 
    217205                void from_setting(const Setting &set){ 
    218206                        Kalman<chmat>::from_setting(set); 
     207                        validate(); 
     208                } 
     209                void validate() { 
     210                        Kalman<chmat>::validate(); 
    219211                        initialize(); 
    220212                } 
     
    244236 
    245237                //! Here dt = [yt;ut] of appropriate dimensions 
    246                 void bayes (const vec &dt); 
     238                void bayes (const vec &yt, const vec &cond=empty_vec); 
    247239                //! set estimates 
    248240                void set_statistics (const vec &mu0, const mat &P0) { 
    249                         est->set_parameters (mu0, P0); 
     241                        est.set_parameters (mu0, P0); 
    250242                }; 
    251243                //! access function 
    252244                const mat _R() { 
    253                         return est->_R().to_mat(); 
     245                        return est._R().to_mat(); 
    254246                } 
    255247                void from_setting (const Setting &set) { 
     
    280272                        //connect 
    281273                        shared_ptr<RV> drv = UI::build<RV> ( set, "drv", UI::compulsory ); 
    282                         set_drv ( *drv ); 
     274                        set_yrv ( *drv ); 
    283275                        shared_ptr<RV> rv = UI::build<RV> ( set, "rv", UI::compulsory ); 
    284276                        set_rv ( *rv ); 
     
    337329                        EKFCh* E = new EKFCh; 
    338330                        E->set_parameters (pfxu, phxu, Q, R); 
    339                         E->set_statistics (est->_mu(), est->_R()); 
     331                        E->set_statistics (est._mu(), est._R()); 
    340332                        return E; 
    341333                } 
     
    344336 
    345337                //! Here dt = [yt;ut] of appropriate dimensions 
    346                 void bayes (const vec &dt); 
     338                void bayes (const vec &yt, const vec &cond=empty_vec); 
    347339 
    348340                void from_setting (const Setting &set); 
     
    389381                        est.set_parameters (A (0)->posterior().mean(), A (0)->posterior()._R()); 
    390382                } 
    391                 void bayes (const vec &dt) { 
     383                void bayes (const vec &yt, const vec &cond=empty_vec) { 
    392384                        int n = Models.length(); 
    393385                        int i; 
    394386                        for (i = 0; i < n; i++) { 
    395                                 Models (i)->bayes (dt); 
     387                                Models (i)->bayes (yt); 
    396388                                _lls (i) = Models (i)->_ll(); 
    397389                        } 
     
    475467                                Crv.add(urv.copy_t(t)); 
    476468                        } 
    477                          
    478                         this->dimx = xrv._dsize(); 
    479                         this->dimy = yrv._dsize(); 
    480                         this->dimu = urv._dsize(); 
    481                          
     469                                                 
    482470                        // get mapp 
    483471                        th2A.set_connection(xrv, ml._rvc()); 
     
    486474 
    487475                        //set matrix sizes 
    488                         this->A=zeros(dimx,dimx); 
    489                         for (int j=1; j<dimx; j++){A(j,j-1)=1.0;} // off diagonal 
    490                                 this->B=zeros(dimx,1); 
     476                        this->A=zeros(xrv._dsize(),xrv._dsize()); 
     477                        for (int j=1; j<xrv._dsize(); j++){A(j,j-1)=1.0;} // off diagonal 
     478                                this->B=zeros(xrv._dsize(),1); 
    491479                                this->B(0) = 1.0; 
    492                                 this->C=zeros(1,dimx); 
     480                                this->C=zeros(1,xrv._dsize()); 
    493481                                this->D=zeros(1,urv._dsize()); 
    494                                 this->Q = zeros(dimx,dimx); 
     482                                this->Q = zeros(xrv._dsize(),xrv._dsize()); 
    495483                        // R is set by update 
    496484                         
     
    538526template<class sq_T> 
    539527void StateSpace<sq_T>::validate(){ 
    540         dimx = A.rows(); 
    541         dimu = B.cols(); 
    542         dimy = C.rows(); 
    543         bdm_assert (A.cols() == dimx, "KalmanFull: A is not square"); 
    544         bdm_assert (B.rows() == dimx, "KalmanFull: B is not compatible"); 
    545         bdm_assert (C.cols() == dimx, "KalmanFull: C is not square"); 
    546         bdm_assert ( (D.rows() == dimy) && (D.cols() == dimu), "KalmanFull: D is not compatible"); 
    547         bdm_assert ( (Q.cols() == dimx) && (Q.rows() == dimx), "KalmanFull: Q is not compatible"); 
    548         bdm_assert ( (R.cols() == dimy) && (R.rows() == dimy), "KalmanFull: R is not compatible"); 
     528        bdm_assert (A.cols() == A.rows(), "KalmanFull: A is not square"); 
     529        bdm_assert (B.rows() == A.rows(), "KalmanFull: B is not compatible"); 
     530        bdm_assert (C.cols() == A.rows(), "KalmanFull: C is not compatible"); 
     531        bdm_assert ( (D.rows() == C.rows()) && (D.cols() == B.cols()), "KalmanFull: D is not compatible"); 
     532        bdm_assert ( (Q.cols() == A.rows()) && (Q.rows() == A.rows()), "KalmanFull: Q is not compatible"); 
     533        bdm_assert ( (R.cols() == C.rows()) && (R.rows() == C.rows()), "KalmanFull: R is not compatible"); 
    549534} 
    550535