Changeset 979

Show
Ignore:
Timestamp:
05/25/10 20:55:06 (14 years ago)
Author:
smidl
Message:

estimator returns the array of posterior estimators as second argument

Files:
7 modified

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/mex/estimator.cpp

    r966 r979  
    3838Execute command: 
    3939\code 
    40 >> estimator('config_file.cfg'); 
     40>> estimator 
    4141\endcode 
    42 when using loggers storing results on hard drives, and 
    43 \code 
    44 >> Res=estimator('config_file.cfg'); 
    45 \endcode 
    46 when using logger of the type \c "mex_logger". The results will be stored in structure \c M. 
     42and follow the help there. 
    4743 
    4844 */ 
     
    6965        // Check the number of inputs and output arguments 
    7066        if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" 
    71                                                 "result=estimator(system, estimators, experiment, logger)\n" 
     67                                                "[Res,estimators,Res2]=estimator(system, estimators, experiment, logger)\n" 
    7268                                                "  system     = struct('class','datasource',...);  % Estimated system\n" 
    7369                                                "  estimators = {struct('class','estimator',...),  % Estimators\n" 
     
    7672                                                "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n" 
    7773                                                "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n" 
     74                                                "Output:\n" 
     75                                                "  Res          Matlab structure with logged results, \n"   
     76                                                "  estimators   Array of estimators updated with data \n" 
     77                                                "  Res2         When logfull log_level is on, this structure is filled with structures of posterior densities\n\n" 
    7878                                                "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); 
    7979 
     
    206206                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); 
    207207                output[0] = mL->toCell(); 
    208                 if (n_output>1) { 
     208                if (n_output>1){ // write estimators 
     209                        UImxArray Ests; 
     210                        UI::save(Es, Ests,"estimators"); 
     211                        output[1]=UImxArray::create_mxArray(Ests); 
     212                } 
     213                if (n_output>2) { 
    209214                        mL->_setting_conf().setAutoConvert(true); 
    210                         output[1]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); 
     215                        output[2]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); 
    211216                } 
    212217        } 
    213218#endif 
    214219        for (int i=0;i<Dls.length(); i++){delete Dls(i); delete Dlsc(i);} 
     220        UIFile F; 
     221        UI::save(Es, F.getRoot(),"estimators"); 
     222        F.writeFile("estim_out.cfg"); 
    215223} 
  • applications/bdmtoolbox/tutorial/userguide/arx_basic_example.m

    r968 r979  
    1515A1.log_level = 'logbounds,logevidence'; 
    1616 
    17 M=estimator(DS,{A1}); 
     17[M,Apost]=estimator(DS,{A1}); 
    1818 
    1919%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  • library/bdm/base/bdmbase.cpp

    r965 r979  
    421421        root::log_register ( L, prefix ); 
    422422 
     423        if (dimension()==0) return; 
     424         
    423425        RV r; 
    424426        if ( isnamed() ) { 
  • library/bdm/estim/arx.cpp

    r973 r979  
    223223        BMEF::from_setting(set); 
    224224         
    225         RV rrv;  
    226         UI::get (rrv, set, "rgr", UI::compulsory ); 
     225        UI::get (rgr, set, "rgr", UI::compulsory ); 
    227226         
    228227        dimy = yrv._dsize(); 
    229228        bdm_assert(dimy>0,"ARX::yrv should not be empty"); 
    230229        // rgrlen - including constant!!! 
    231         rgrlen = rrv._dsize(); 
     230        rgrlen = rgr._dsize(); 
    232231 
    233232        int constant; 
     
    238237        } 
    239238        dimc = rgrlen; 
    240         rvc = rrv; 
     239        rvc = rgr; 
    241240        rgrlen += int ( have_constant == true ); 
    242241 
  • library/bdm/estim/arx.h

    r973 r979  
    4747        //! vector of dyadic update 
    4848        vec dyad; 
     49        //! RV of regressor 
     50        RV rgr; 
    4951        //! length of the regressor 
    5052        int rgrlen; 
     
    165167        {                        
    166168                BMEF::to_setting( set ); // takes care of rv, yrv, rvc 
     169                UI::save(rgr, set, "rgr"); 
    167170                int constant = have_constant ? 1 : 0; 
    168171                UI::save(constant, set, "constant"); 
    169                 UI::save(&est, set, "prior"); 
    170172                UI::save(&alter_est, set, "alternative"); 
    171                  
     173                UI::save(&posterior(), set, "posterior"); 
    172174                 
    173175        }  
  • library/bdm/estim/particles.h

    r974 r979  
    465465UIREGISTER ( PF ); 
    466466 
     467/*! Marginalized particle for state-space models with unknown parameters of distribuution of residues on \f$v_t\f$.  
     468 
     469\f{eqnarray*}{ 
     470        x_t &=& g(x_{t-1}) + v_t,\\ 
     471        y_t &\sim &fy(x_t), 
     472        \f} 
     473         
     474        This particle is a only a shell creating the residues calling internal estimator of their parameters. The internal estimator can be of any compatible type, e.g. ARX for Gaussian residues with unknown mean and variance. 
     475         
     476        */ 
     477class NoiseParticleX : public MarginalizedParticleBase{ 
     478        protected: 
     479                //! function transforming xt, ut -> x_t+1 
     480                shared_ptr<fnc> g; // pdf for non-linear part 
     481                //! function transforming xt,ut -> yt 
     482                shared_ptr<pdf> fy; // pdf for non-linear part 
     483                 
     484                RV rvx; 
     485                RV rvxc; 
     486                RV rvyc; 
     487                 
     488                //!link from condition to f 
     489                datalink_part cond2g; 
     490                //!link from condition to h 
     491                datalink_part cond2fy; 
     492                //!link from xt to f 
     493                datalink_part x2g; 
     494                //!link from xt to h 
     495                datalink_part x2fy; 
     496         
     497        public: 
     498                BM* _copy() const{return new NoiseParticleX(*this);}; 
     499                void bayes(const vec &dt, const vec &cond){ 
     500                        shared_ptr<epdf> pred_v=bm->epredictor(); 
     501                         
     502                        vec vt=pred_v->sample(); 
     503                         
     504                        //new sample 
     505                        vec &xtm=est_emp.point; 
     506                        vec g_args(g->dimensionc()); 
     507                        x2g.filldown(xtm,g_args); 
     508                        cond2g.filldown(cond,g_args); 
     509                        vec xt = g->eval(g_args) + vt; 
     510                        est_emp.point=xt; 
     511 
     512                        // the vector [v_t] updates bm,  
     513                        bm->bayes(vt); 
     514                         
     515                        // residue of observation 
     516                        vec fy_args(fy->dimensionc()); 
     517                        x2fy.filldown(xt,fy_args); 
     518                        cond2fy.filldown(cond,fy_args); 
     519                         
     520                        ll=bm->_ll() + fy->evallogcond(dt,fy_args); 
     521                } 
     522                void from_setting(const Setting &set){ 
     523                        MarginalizedParticleBase::from_setting(set); //reads bm, yrv,rvc, bm_rv, etc... 
     524                                 
     525                        g=UI::build<fnc>(set,"g",UI::compulsory); 
     526                        fy=UI::build<pdf>(set,"fy",UI::compulsory); 
     527                        UI::get(rvx,set,"rvx",UI::compulsory); 
     528                        est_emp.set_rv(rvx); 
     529                         
     530                        UI::get(rvxc,set,"rvxc",UI::compulsory); 
     531                        UI::get(rvyc,set,"rvyc",UI::compulsory); 
     532                         
     533                } 
     534                void validate(){ 
     535                        MarginalizedParticleBase::validate(); 
     536                         
     537                        dimy = fy->dimension(); 
     538                        bm->set_yrv(rvx); 
     539                         
     540                        est_emp.set_rv(rvx); 
     541                        est_emp.set_dim(rvx._dsize()); 
     542                        est.validate(); 
     543                        //  
     544                        //check dimensions 
     545                        rvc = rvxc.subt(rvx.copy_t(-1)); 
     546                        rvc.add( rvyc); 
     547                        rvc=rvc.subt(rvx); 
     548                         
     549                        bdm_assert(g->dimension()==rvx._dsize(),"rvx is not described"); 
     550                        bdm_assert(g->dimensionc()==rvxc._dsize(),"rvxc is not described"); 
     551                        bdm_assert(fy->dimensionc()==rvyc._dsize(),"rvyc is not described"); 
     552                         
     553                        bdm_assert(bm->dimensiony()==g->dimension(),  
     554                                           "Incompatible noise estimator of dimension " + 
     555                                                num2str(bm->dimensiony()) + " does not match dimension of g , " +  
     556                                                num2str(g->dimension())); 
     557                                                 
     558                        dimc = rvc._dsize(); 
     559                                         
     560                        //establish datalinks 
     561                        x2g.set_connection(rvxc, rvx.copy_t(-1)); 
     562                        cond2g.set_connection(rvxc, rvc); 
     563                         
     564                        x2fy.set_connection(rvyc, rvx); 
     565                        cond2fy.set_connection(rvyc, rvc); 
     566                } 
     567}; 
     568UIREGISTER(NoiseParticleX); 
     569 
    467570/*! Marginalized particle for state-space models with unknown parameters of residues distribution 
    468571 
    469 \f[ 
    470 \begin{eqnarray} 
    471 x_t = g(x_{t-1}) + v_t,\\ 
    472 z_t = h(x_{t-1}) + w_t,\\ 
    473 \end{eqnarray} 
    474 \f] 
    475  
    476 This particle is a only a shell creating the residues calling internal estimator of their parameters. The internal estimator can be of any compatible type, e.g. ARX for Gaussian residues with unknown mean and variance. 
    477  
    478 */ 
     572\f{eqnarray*}{ 
     573        x_t &=& g(x_{t-1}) + v_t,\\ 
     574        z_t &= &h(x_{t-1}) + w_t, 
     575        \f} 
     576         
     577        This particle is a only a shell creating the residues calling internal estimator of their parameters. The internal estimator can be of any compatible type, e.g. ARX for Gaussian residues with unknown mean and variance. 
     578         
     579        */ 
    479580class NoiseParticle : public MarginalizedParticleBase{ 
    480581        protected: 
     
    496597                //!link from xt to h 
    497598                datalink_part x2h; 
    498                  
     599         
    499600        public: 
    500601                BM* _copy() const{return new NoiseParticle(*this);}; 
     
    524625                void from_setting(const Setting &set){ 
    525626                        MarginalizedParticleBase::from_setting(set); //reads bm, yrv,rvc, bm_rv, etc... 
    526                                  
     627                         
    527628                        UI::get(g,set,"g",UI::compulsory); 
    528629                        UI::get(h,set,"h",UI::compulsory); 
     
    532633                        UI::get(rvxc,set,"rvxc",UI::compulsory); 
    533634                        UI::get(rvyc,set,"rvyc",UI::compulsory); 
     635                         
    534636                } 
    535637                void validate(){ 
     
    554656                        bdm_assert(bm->dimensiony()==g->dimension()+h->dimension(),  
    555657                                           "Incompatible noise estimator of dimension " + 
    556                                                 num2str(bm->dimensiony()) + " does not match dimension of g and h, " +  
    557                                                 num2str(g->dimension())+" and "+ num2str(h->dimension()) ); 
    558                                                  
    559                         dimc = rvc._dsize(); 
    560                                          
    561                         //establish datalinks 
    562                         x2g.set_connection(rvxc, rvx.copy_t(-1)); 
    563                         cond2g.set_connection(rvxc, rvc); 
    564                          
    565                         x2h.set_connection(rvyc, rvx); 
    566                         cond2h.set_connection(rvyc, rvc); 
     658                                           num2str(bm->dimensiony()) + " does not match dimension of g and h, " +  
     659                                           num2str(g->dimension())+" and "+ num2str(h->dimension()) ); 
     660                                            
     661                                           dimc = rvc._dsize(); 
     662                                            
     663                                           //establish datalinks 
     664                                           x2g.set_connection(rvxc, rvx.copy_t(-1)); 
     665                                           cond2g.set_connection(rvxc, rvc); 
     666                                            
     667                                           x2h.set_connection(rvyc, rvx); 
     668                                           cond2h.set_connection(rvyc, rvc); 
    567669                } 
    568670}; 
     
    570672 
    571673 
    572  
    573674} 
    574675#endif // KF_H 
  • library/bdm/stat/emix.h

    r956 r979  
    278278        Array<datalink*> dls; 
    279279        //! interface for a factor 
     280public: 
    280281        virtual const epdf* factor(int i) const NOT_IMPLEMENTED(NULL);  
    281282        //!number of factors 
    282         virtual const int no_factors() const =0; 
    283 public: 
     283        virtual const int no_factors() const NOT_IMPLEMENTED(0); 
    284284        //! Default constructor 
    285285        eprod_base () :  dls (0) {}; 
     
    394394UIREGISTER ( mmix ); 
    395395 
     396 
     397//! Base class for all BM running as parallel update of internal BMs 
     398 
     399class ProdBMBase : public BM { 
     400        protected : 
     401                Array<vec_from_vec> bm_yt; 
     402                Array<vec_from_2vec> bm_cond; 
     403                class eprod_bm : public eprod_base { 
     404                        ProdBMBase & pb; 
     405                        public : 
     406                        eprod_bm(ProdBMBase &pb0): pb(pb0){} 
     407                        const epdf* factor(int i ) const {return &(pb.bm(i)->posterior());} 
     408                        const int no_factors() const {return pb.no_bms();} 
     409                } est; 
     410        public: 
     411                ProdBMBase():est(*this){} 
     412                virtual BM* bm(int i) NOT_IMPLEMENTED(NULL); 
     413                virtual int no_bms() const {return 0;} 
     414                const epdf& posterior() const {return est;} 
     415                void set_prior(const epdf *pri){ 
     416                        const eprod_base* ep=dynamic_cast<const eprod_base*>(pri); 
     417                        if (ep){ 
     418                                bdm_assert(ep->no_factors()!=no_bms() , "Given prior has "+ num2str(ep->no_factors()) + " while this ProdBM has "+ 
     419                                        num2str(no_bms()) + "BMs"); 
     420                                for (int i=0; i<no_bms(); i++){ 
     421                                        bm(i)->set_prior(ep->factor(i)); 
     422                                } 
     423                        } 
     424                } 
     425                 
     426                void validate() { 
     427                        est.validate(); 
     428                        BM::validate(); 
     429                        // set links 
     430                        bm_yt.set_length(no_bms()); 
     431                        bm_cond.set_length(no_bms()); 
     432                         
     433                        //  
     434                         
     435                        for (int i=0; i<no_bms(); i++){ 
     436                                yrv.add(bm(i)->_yrv()); 
     437                                rvc.add(bm(i)->_rvc()); 
     438                        } 
     439                        rvc=rvc.subt(yrv); 
     440                         
     441                        dimy = yrv._dsize(); 
     442                        dimc = rvc._dsize(); 
     443                         
     444                        for (int i=0; i<no_bms(); i++){ 
     445                                bm_yt(i).connect(bm(i)->_yrv(), yrv); 
     446                                bm_cond(i).connect(bm(i)->_rvc(), yrv, rvc); 
     447                        } 
     448                } 
     449                void bayes(const vec &dt, const vec &cond){ 
     450                        ll=0; 
     451                        for(int i=0;i<no_bms(); i++){ 
     452                                bm_yt(i).update(dt); 
     453                                bm_cond(i).update(dt,cond); 
     454                                bm(i)->bayes(bm_yt(i), bm_cond(i)); 
     455                        } 
     456                } 
     457                 
     458}; 
     459 
     460class ProdBM: public ProdBMBase{ 
     461        protected: 
     462                Array<shared_ptr<BM> > BMs; 
     463        public: 
     464                virtual BM* bm(int i) {return BMs(i).get();} 
     465                virtual int no_bms() const {return BMs.length();} 
     466                void from_setting(const Setting &set){ 
     467                        BM::from_setting(set); 
     468                        UI::get(BMs,set,"BMs"); 
     469                } 
     470                void to_setting(Setting &set) const{ 
     471                        BM::to_setting(set); 
     472                        UI::save(BMs,set,"BMs"); 
     473                } 
     474                 
     475}; 
     476UIREGISTER(ProdBM); 
     477 
    396478} 
    397479#endif //MX_H