Changeset 974

Show
Ignore:
Timestamp:
05/23/10 11:40:48 (14 years ago)
Author:
smidl
Message:

Noise Particle

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/sandbox/mpf_arx_3.m

    r971 r974  
    88g.function = 'test_function'; 
    99 
    10 g.class = 'linfn'; 
    11 g.A     = eye(2); 
    12 g.B     = [1;0]; 
     10%     g.class = 'linfn'; 
     11%     g.A     = eye(2); 
     12%     g.B     = [1;0]; 
    1313 
    1414h.class = 'linfn'; 
     
    1717 
    1818fx.class = 'mgnorm<chmat>'; 
    19 fx.R = [0.3 -0.2; -0.2 0.5]; 
     19fx.R = [0.03 -0.02; -0.02 0.05]; 
    2020fx.g = g; 
    2121fx.rv = x; 
     
    2323 
    2424fy.class = 'mgnorm<chmat>'; 
    25 fy.R = 0.1*eye(2); 
     25fy.R = 0.01*eye(2); 
    2626fy.g = h; 
    2727fy.rv = y; 
     
    3939A.class = 'ARX'; 
    4040A.yrv = RV('vw',4); 
     41A.rv = RV('R',16); 
    4142A.rgr = RV({}); 
    4243A.dimx=4; 
    4344A.constant = 0; 
    44 A.frg=0.99; 
     45A.frg=1.0; 
     46A.prior.class='egiw'; 
     47A.prior.dimx=4; 
     48A.prior.V =1e-3*eye(4); 
     49 
    4550 
    4651M.class = 'NoiseParticle'; 
    4752M.g = g; 
    4853M.h = h; 
     54M.yrv = y; 
    4955M.rvx = x; 
    5056M.rvxc = RVtimes(x,-1); 
     
    5460PF.class='PF'; 
    5561PF.particle = M; 
    56 PF.n = 100; 
    57 PF.res_threshold = 1.0; 
     62PF.n = 1000; 
     63PF.res_threshold = 0.9; 
    5864PF.prior.class = 'enorm<ldmat>'; 
    5965PF.prior.mu = [0.2;0.3]; 
     
    6167 
    6268 
    63 exper.ndat = 2000; 
     69exper.ndat = 200; 
    6470O = estimator(DS,{PF},exper); 
    6571%%%%%% ARX estimator conditioned on frg 
     
    6975figure(1); 
    7076hold off 
    71 plot(M.Est0_Q_Q); 
    72 hold on 
    73 plot(ones(size(M.Est0_Q_Q,1),1)* DS.pdf.pdfs{2}.R(:)','--'); 
     77plot(O.Est0_apost_mean_R); 
    7478 
    7579figure(2) 
    7680hold off 
    77 plot(M.Est0_apost_mean_); 
     81plot(O.Est0_apost_mean_x); 
    7882hold on 
    79 plot(M.DS_x,'--'); 
     83plot(O.DS_dt_x,'--'); 
    8084 
  • library/bdm/estim/particles.h

    r971 r974  
    4343                        bm = m2.bm->_copy(); 
    4444                        est_emp = m2.est_emp; 
     45                        est.validate();; 
    4546                        validate(); 
    4647                }; 
     
    6667                void validate() { 
    6768                        BM::validate(); 
     69                        //est.validate(); --pdfs not known 
    6870                        bdm_assert(bm,"Internal BM is not given"); 
    6971                } 
     
    118120                } 
    119121                void validate(){ 
     122                        MarginalizedParticleBase::validate(); 
     123                        est_emp.set_rv(par->_rv()); 
    120124                        if (est_emp.point.length()!=par->dimension()) 
    121125                                est_emp.set_point(zeros(par->dimension())); 
     
    324328                shared_ptr<BM> bm0 = UI::build<BM>(set, "particle",UI::compulsory); 
    325329                 
    326                 shared_ptr<epdf> pri = UI::build<epdf> ( set, "prior", UI::compulsory ); 
    327330                n =0; 
    328331                UI::get(n,set,"n",UI::optional);; 
     
    332335                        w = ones(n)/n; 
    333336                } 
    334                 set_prior(pri.get()); 
    335337                // set resampling method 
    336338                resmethod_from_set ( set ); 
     
    435437 
    436438                if ( particles(0)->_rv()._dsize() > 0 ) { 
    437                         bdm_assert (  particles(0)->_rv()._dsize() == est.dimension(), "Mismatch of RV and dimension of posterior" ); 
     439                        bdm_assert (  particles(0)->_rv()._dsize() == est.dimension(), "Mismatch of RV " +particles(0)->_rv().to_string() +  
     440                        " of size (" +num2str(particles(0)->_rv()._dsize())+"and dimension of posterior ("+num2str(est.dimension()) + ")" ); 
    438441                } 
    439442        } 
     
    445448                for (int i = 0; i < n; i++ ) { 
    446449                        if ( ind ( i ) != i ) { 
     450                                delete particles(i); 
    447451                                particles( i ) = particles( ind ( i ) )->_copy(); 
    448452                        } 
     
    453457        Array<BM*>& _particles() { 
    454458                return particles; 
     459        } 
     460        ~PF(){ 
     461                for (int i=0; i<particles.length(); i++){delete particles(i);} 
    455462        } 
    456463 
     
    476483                //! function transforming xt,ut -> yt 
    477484                shared_ptr<fnc> h; // pdf for non-linear part 
    478  
    479                 RV rvv; 
    480485                 
    481486                RV rvx; 
     
    493498                 
    494499        public: 
    495                 BM* _copy() const{return new NoiseParticle();}; 
     500                BM* _copy() const{return new NoiseParticle(*this);}; 
    496501                void bayes(const vec &dt, const vec &cond){ 
    497                         epdf* pred_vw=bm->epredictor(); 
    498                         shared_ptr<epdf> pred_v = pred_vw->marginal(rvv); 
     502                        shared_ptr<epdf> pred_vw=bm->epredictor(); 
     503                        shared_ptr<epdf> pred_v = pred_vw->marginal(rvx); 
    499504                         
    500505                        vec vt=pred_v->sample(); 
     
    506511                        cond2g.filldown(cond,g_args); 
    507512                        vec xt = g->eval(g_args) + vt; 
     513                        est_emp.point=xt; 
    508514                         
    509515                        // residue of observation 
     
    521527                        UI::get(g,set,"g",UI::compulsory); 
    522528                        UI::get(h,set,"h",UI::compulsory); 
    523                         RV rvx; 
    524529                        UI::get(rvx,set,"rvx",UI::compulsory); 
    525530                        est_emp.set_rv(rvx); 
    526531                         
    527                         RV rvxc; 
    528532                        UI::get(rvxc,set,"rvxc",UI::compulsory); 
    529                         RV rvyc; 
    530533                        UI::get(rvyc,set,"rvyc",UI::compulsory); 
    531534                } 
    532535                void validate(){ 
    533536                        MarginalizedParticleBase::validate(); 
     537                         
     538                        dimy = h->dimension(); 
     539                        bm->set_yrv(concat(rvx,yrv)); 
     540                         
     541                        est_emp.set_rv(rvx); 
     542                        est_emp.set_dim(rvx._dsize()); 
     543                        est.validate(); 
     544                        //  
    534545                        //check dimensions 
    535                         rvc = rvxc; 
     546                        rvc = rvxc.subt(rvx.copy_t(-1)); 
    536547                        rvc.add( rvyc); 
    537                         rvc.subt(rvx); 
    538                  
     548                        rvc=rvc.subt(rvx); 
     549                         
     550                        bdm_assert(g->dimension()==rvx._dsize(),"rvx is not described"); 
     551                        bdm_assert(g->dimensionc()==rvxc._dsize(),"rvxc is not described"); 
     552                        bdm_assert(h->dimension()==rvyc._dsize(),"rvyc is not described"); 
     553                         
     554                        bdm_assert(bm->dimensiony()==g->dimension()+h->dimension(),  
     555                                           "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                                         
    539561                        //establish datalinks 
    540562                        x2g.set_connection(rvxc, rvx.copy_t(-1));