libPF.h itpp/itbase.h ../stat/libEF.h ../math/libDC.h work/git/mixpp/bdm/estim/libPF.cpp work/git/mixpp/pmsm/pmsm_mix.cpp work/git/mixpp/pmsm/pmsm_sim.cpp work/git/mixpp/pmsm/pmsm_sim2.cpp work/git/mixpp/pmsm/pmsm_sim3.cpp work/git/mixpp/pmsm/pmsm_unkQ.cpp work/git/mixpp/pmsm/pmsm_unkQpf.cpp work/git/mixpp/tests/testKF_QR.cpp work/git/mixpp/tests/testKF_QRexh.cpp work/git/mixpp/tests/testPF.cpp work/git/mixpp/tests/testResample.cpp PF MPF MPF::mpfepdf double double PF_SSAT PF_SSAT Bayesian Filtering using stochastic sampling (Particle Filters). Vaclav Smidl. ----------------------------------- BDM++ - C++ library for Bayesian Decision Making under UncertaintyUsing IT++ for numerical operations ----------------------------------- #ifndefPF_H #definePF_H #include<itpp/itbase.h> #include"../stat/libEF.h" #include"../math/libDC.h" usingnamespaceitpp; //UGLYHACK externdoublePF_SSAT;//usedforStrSim:06test...iflength>0thevalueiswritten. classPF:publicBM{ protected: intn; eEmpest; vec&_w; Array<vec>&_samples; mpdf&par; mpdf&obs; public: PF(constRV&rv0,mpdf&par0,mpdf&obs0,intn0):BM(rv0), n(n0),est(rv0,n),_w(est._w()),_samples(est._samples()), par(par0),obs(obs0){}; voidset_est(constepdf&epdf0); voidbayes(constvec&dt); }; template<classBM_T> classMPF:publicPF{ BM_T*Bms[10000]; classmpfepdf:publicepdf{ protected: eEmp&E; vec&_w; Array<epdf*>Coms; public: mpfepdf(eEmp&E0,constRV&rvc): epdf(RV()),E(E0),_w(E._w()), Coms(_w.length()){ rv.add(E._rv()); rv.add(rvc); }; voidset_elements(int&i,doublewi,epdf*ep) {_w(i)=wi;Coms(i)=ep;}; vecmean()const{ //ugly vecpom=zeros((Coms(0)->_rv()).count()); for(inti=0;i<_w.length();i++){pom+=Coms(i)->mean()*_w(i);} returnconcat(E.mean(),pom); } vecsample()const{it_error("Notimplemented");return0;} doubleevalpdflog(constvec&val)const{it_error("notimplemented");return0.0;} }; mpfepdfjest; public: MPF(constRV&rvlin,constRV&rvpf,mpdf&par0,mpdf&obs0,intn,constBM_T&BMcond0):PF(rvpf,par0,obs0,n),jest(est,rvlin){ // //TODOtestifrvandBMcond.rvarecompatible. rv.add(rvlin); // if(n>10000){it_error("increase10000here!");} for(inti=0;i<n;i++){ Bms[i]=newBM_T(BMcond0);//copyconstructor epdf&pom=Bms[i]->_epdf(); jest.set_elements(i,1.0/n,&pom); } }; ~MPF(){ } voidbayes(constvec&dt); epdf&_epdf(){returnjest;} voidset_est(constepdf&epdf0){ PF::set_est(epdf0);//sampleparamsincondition //copyconditionstoBMs for(inti=0;i<n;i++){Bms[i]->condition(_samples(i));} } //SimStr: doubleSSAT; }; template<classBM_T> voidMPF<BM_T>::bayes(constvec&dt){ inti; veclls(n); vecllsP(n); ivecind; doublemlls=-std::numeric_limits<double>::infinity(); //StrSim:06 doublesumLWL=0.0; doublesumL2WL=0.0; doubleWL=0.0; #pragmaompparallelfor for(i=0;i<n;i++){ //generatenewsamplesfromparamaterevolutionmodel; _samples(i)=par.samplecond(_samples(i),llsP(i)); Bms[i]->condition(_samples(i)); Bms[i]->bayes(dt); lls(i)=Bms[i]->_ll();//llsaboveisalsoinproposalhermustbells(i)=,not+=!! if(lls(i)>mlls)mlls=lls(i);//findmaximumlikelihood(fornumericalstability) } if(false){ #pragmaompparallelforreduction(+:sumLWL,sumL2WL)private(WL) for(i=0;i<n;i++){ WL=_w(i)*exp(llsP(i));//usingoldweights! sumLWL+=exp(lls(i))*WL; sumL2WL+=exp(2*lls(i))*WL; } SSAT=sumL2WL/(sumLWL*sumLWL); } doublesum_w=0.0; //computeweights #pragmaompparallelfor for(i=0;i<n;i++){ _w(i)*=exp(lls(i)-mlls);//multiplywbylikelihood sum_w+=_w(i); } if(sum_w>0.0){ _w/=sum_w;//? }else{ cout<<"sum(w)==0"<<endl; } doubleeff=1.0/(_w*_w); if(eff<(0.3*n)){ ind=est.resample(); //ResampleBms! #pragmaompparallelfor for(i=0;i<n;i++){ if(ind(i)!=i){//replacethecurrentBmbyanewone //fixmethiswouldrequirenewassignmentoperator //*Bms[i]=*Bms[ind(i)]; //poor-man'ssolution:replicateconstructorhere //copiedfromMPF::MPF deleteBms[i]; Bms[i]=newBM_T(*Bms[ind(i)]);//copyconstructor epdf&pom=Bms[i]->_epdf(); jest.set_elements(i,1.0/n,&pom); } }; cout<<'.'; } } #endif//KF_H