Changeset 695

Show
Ignore:
Timestamp:
11/03/09 00:03:19 (14 years ago)
Author:
smidl
Message:

StateDS is finished

Location:
library
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/datasources.cpp

    r693 r695  
    122122} 
    123123 
    124 void stateDS::from_setting ( const Setting &set ) { 
     124void StateDS::from_setting ( const Setting &set ) { 
    125125        IM = UI::build<pdf> ( set, "IM", UI::compulsory ); 
    126126        OM = UI::build<pdf> ( set, "OM", UI::compulsory ); 
    127127 
    128         dt.set_length ( OM->dimension() ); 
    129         xt.set_length ( IM->dimension() ); 
    130         ut.set_length ( 0 ); 
     128        //todo test if IM->rvc contains IM->rv 
     129        //todo test if OM->rvc contains IM->rv 
     130         
     131        UI::get(xt,set,"x0",UI::optional); 
     132         
     133        // prepare urv 
     134        validate(); 
     135} 
    131136 
    132 #if 0 
    133         RV* rvtmp = UI::build<RV> ( set["IM"], "rvu", UI::compulsory ); 
    134         //set_drv(rvtmp); 
    135 #endif 
     137void StateDS::validate(){ 
     138        Yrv = concat(OM->_rv() , IM->_rv()); // export also true state 
     139        ytsize = Yrv._dsize(); 
     140         
     141        RV tmp=concat(OM->_rvc(), IM->_rvc()); // what i not in rvc 
     142        Urv = tmp.subt(Yrv); // remove dt,xt and  
     143        Urv = Urv.subt(Yrv.copy_t(-1)); // remove dt,xt and  
     144        utsize= Urv._dsize(); 
     145         
     146        set_drv(Yrv,Urv); 
     147        dtsize = utsize + ytsize; 
     148         
     149        dt.set_length ( dtsize ); 
     150        if (xt.length()!= IM->dimension() ) { 
     151                xt=zeros(IM->dimension()); 
     152        } 
     153        ut.set_length ( Urv._dsize() ); 
     154         
     155        //create data links 
     156        u2imc.set_connection(IM->_rvc(), Urv); 
     157        u2omc.set_connection(OM->_rvc(), Urv); 
    136158} 
  • library/bdm/base/datasources.h

    r693 r695  
    304304 
    305305//! State-space data source simulating two densities 
    306 class stateDS : public DS { 
    307         private: 
     306class StateDS : public DS { 
     307        protected: 
    308308                //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 
    309                 shared_ptr<pdf> IM; 
     309                shared_ptr<mpdf> IM; 
    310310 
    311311                //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 
    312                 shared_ptr<pdf> OM; 
    313  
    314         protected: 
     312                shared_ptr<mpdf> OM; 
     313 
    315314                //! result storage 
    316315                vec dt; 
     
    319318                //! input storage 
    320319                vec ut; 
    321                 //! Logger 
    322                 int L_xt; 
    323  
    324         public: 
    325                 void getdata ( vec &dt0 ) { 
     320 
     321                //! datalink from ut to IM.rvc 
     322                datalink_part u2imc; 
     323                //! datalink from ut to OM.rvc 
     324                datalink_part u2omc; 
     325        public: 
     326                void getdata ( vec &dt0 ) const { 
    326327                        dt0 = dt; 
    327328                } 
    328  
     329                void write (const vec &ut0 ) { 
     330                        ut = ut0; 
     331                } 
     332                 
    329333                void getdata ( vec &dt0, const ivec &indices ) { 
    330334                        dt0 = dt ( indices ); 
    331335                } 
    332                 //! convenience constructor 
    333                 stateDS ( const shared_ptr<pdf> &IM0, const shared_ptr<pdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ), 
    334                                 dt ( OM0->dimension() ), xt ( IM0->dimension() ), 
    335                                 ut ( usize ), L_xt ( 0 ) { } 
    336  
    337                 stateDS() : L_xt ( 0 ) { } 
    338336 
    339337                virtual void step() { 
    340                         xt = IM->samplecond ( concat ( xt, ut ) ); 
    341                         dt = OM->samplecond ( concat ( xt, ut ) ); 
    342                 } 
    343  
    344                 virtual void log_register(logger &L, const string &prefix){ 
    345                         DS::log_register ( L, prefix ); //ids 0 and 1 
     338                        vec imc(IM->dimensionc()); 
     339                        imc.set_subvector(0,xt); 
     340                        u2imc.filldown(ut,imc); 
     341                        xt = IM->samplecond ( imc ); 
    346342                         
    347                         logrec->ids.set_size(3,true);//copy 
    348                         logrec->ids(2)=logrec->L.add ( IM->_rv(), "true" ); 
    349                 } 
    350                 virtual void log_write () { 
    351                         DS::log_write ( ); 
    352                         logrec->L.logit ( logrec->ids(2), xt ); 
    353                 } 
     343                        vec omc(OM->dimensionc()); 
     344                        omc.set_subvector(0,xt); 
     345                        u2omc.filldown(ut,omc); 
     346                        vec yt; 
     347                        yt = OM->samplecond ( omc ); 
     348                        //fill all data 
     349                        dt.set_subvector(0,yt); 
     350                        dt.set_subvector(yt.length(),xt); 
     351                        dt.set_subvector(ytsize,ut); 
     352                } 
     353 
     354                //! set parameters 
     355                void set_parameters(shared_ptr<mpdf> IM0, shared_ptr<mpdf> OM0){ 
     356                        IM=IM0; 
     357                        OM = OM0; 
     358                } 
     359                void set_initx(const vec &x0){xt=x0;} 
    354360 
    355361                /*! UI for stateDS 
     
    357363                The DS is constructed from a structure with fields: 
    358364                \code 
    359                 system = { 
    360                         type = "stateDS"; 
    361                         //Internal model 
    362                         IM = { type = "pdf"; //<-- valid offspring! e.g. "mlnorm" 
    363                                 rv = { //description of x_t 
    364                                         names=["name1",...]; 
    365                                         sizes=[2,1]; // optional default=[1,1...]; 
    366                                         times=[0,0]; // optional default=[0,0...]; 
    367                                         } 
    368                                 rvu= { //description of  u_t 
    369                                         //optional default=empty 
    370                                         } 
    371  
    372                                 // remaining fields depending on the chosen type 
    373                                 }; 
    374                         //Observation model 
    375                         OM = { type = "pdf-offspring"; 
    376                                 rv = {}; //description of d_t 
    377                                 rvu = {type="internal", path="system.IM.rvu"}; //description of u_t 
    378  
    379                                 //remaining fields 
    380                         } 
    381                 }; 
    382                 \endcode 
     365                class = "stateDS"; 
     366                //Internal model 
     367                IM = { type = "mpdf-offspring"; }; 
     368                //Observation model 
     369                OM = { type = "mpdf-offspring"; } 
     370                //initial state 
     371                x0 = [...]; //vector of data 
     372                \endcode 
     373                Both models must have defined \c rv. and \c rvc 
     374                Random variables not found in any rv are considered to be inputs. 
    383375                */ 
    384376                void from_setting ( const Setting &set ); 
     
    386378                // TODO dodelat void to_setting( Setting &set ) const; 
    387379 
    388 }; 
    389  
    390 UIREGISTER ( stateDS ); 
    391 SHAREDPTR ( stateDS ); 
     380                void validate(); 
     381}; 
     382 
     383UIREGISTER ( StateDS ); 
     384SHAREDPTR ( StateDS ); 
    392385 
    393386}; //namespace 
  • library/tests/datasource_test.cpp

    r693 r695  
    11#include "../bdm/base/datasources.h" 
     2#include "../bdm/base/loggers.h" 
    23#include "mat_checks.h" 
    34#include "UnitTest++.h" 
     
    4243        CHECK_CLOSE ( -0.4 , dt(0), 1e-4); 
    4344} 
     45 
     46TEST(StateDS_test){ 
     47        RV y("y",1); 
     48        RV u("u",1); 
     49        RV x("x",2); 
     50         
     51        shared_ptr<mlnorm<fsqmat> > IM = new mlnorm<fsqmat>; 
     52        IM->set_parameters(mat("1 2 0.5; 0 1 0.3"), zeros(2), fsqmat(1e-10*eye(2))); 
     53        IM->set_rv(x); 
     54        IM->set_rvc(concat(x.copy_t(-1), u)); 
     55        IM->validate(); 
     56 
     57        shared_ptr<mlnorm<fsqmat> > OM = new mlnorm<fsqmat>; 
     58        OM->set_parameters(mat("1 0"), zeros(1), fsqmat(1e-10*eye(1))); 
     59        OM->set_rv(y); 
     60        OM->set_rvc(x); 
     61        OM->validate(); 
     62         
     63        StateDS sds; 
     64        sds.set_parameters(IM,OM); 
     65        sds.validate(); 
     66         
     67        for (int t=1; t<10;t++){ 
     68                sds.write(vec_1(double(t))); 
     69                sds.step(); 
     70        } 
     71 
     72        vec dt; sds.getdata(dt); 
     73        CHECK_CLOSE(vec("94.5, 94.5, 13.5, 9"), dt, 1e-2);       
     74}