Changeset 229
- Timestamp:
- 01/15/09 10:53:54 (16 years ago)
- Files:
-
- 8 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/ekf_templ.h
r141 r229 29 29 }; 30 30 31 //!Extended Kalman filter in Choleski form with unknown \c Q 32 class EKFCh_unQ : public EKFCh , public BMcond { 33 public: 34 //! Default constructor 35 EKFCh_unQ ( RV rx, RV ry,RV ru,RV rQ ) :EKFCh ( rx,ry,ru ),BMcond ( rQ ) {}; 36 void condition ( const vec &Q0 ) { 37 Q.setD ( Q0,0 ); 38 //from EKF 39 preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); 40 }; 41 }; 31 42 32 43 #endif //EKF_TEMP_H -
bdm/estim/libPF.h
r225 r229 88 88 // ugly 89 89 vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() ); 90 91 90 for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );} 92 93 91 return concat ( E.mean(),pom ); 92 } 93 vec variance() const { 94 // ugly 95 vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() ); 96 vec pom2=zeros ( ( Coms ( 0 )->_rv() ).count() ); 97 for ( int i=0; i<_w.length(); i++ ) { 98 pom += Coms ( i )->mean() * _w ( i ); 99 pom2 += (Coms ( i )->variance() + pow(Coms(i)->mean(),2)) * _w ( i );} 100 return concat ( E.variance(),pom2-pow(pom,2) ); 94 101 } 95 102 -
bdm/estim/merger.h
r211 r229 102 102 return tmp; 103 103 } 104 mat variance() const {104 mat covariance() const { 105 105 const vec &w = eSmp._w(); 106 106 const Array<vec> &S = eSmp._samples(); … … 115 115 } 116 116 return Tmp-outer_product(mea,mea); 117 } 118 vec variance() const { 119 const vec &w = eSmp._w(); 120 const Array<vec> &S = eSmp._samples(); 121 122 vec tmp=zeros(rv.count()); 123 for ( int i=0; i<Ns; i++ ) { 124 tmp+=w ( i ) *pow(S ( i ),2); 125 } 126 return tmp-pow(mean(),2); 117 127 } 118 128 //! for future use -
bdm/stat/emix.h
r224 r229 104 104 for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); } 105 105 return mu; 106 } 107 vec variance() const { 108 //non-central moment 109 vec mom2 = zeros(rv.count()); 110 for ( int i = 0;i < w.length();i++ ) {mom2 += w ( i ) * pow(Coms ( i )->mean(),2); } 111 //central moment 112 return mom2-pow(mean(),2); 106 113 } 107 114 double evallog ( const vec &val ) const { … … 243 250 return tmp; 244 251 } 252 vec variance() const { 253 vec tmp ( rv.count() ); //second moment 254 for ( int i=0;i<epdfs.length();i++ ) { 255 vec pom = epdfs ( i )->mean(); 256 dls ( i )->fill_val ( tmp, pow(pom,2) ); 257 } 258 return tmp-pow(mean(),2); 259 } 245 260 vec sample() const { 246 261 vec tmp ( rv.count() ); -
bdm/stat/libBM.h
r225 r229 197 197 virtual vec mean() const =0; 198 198 199 //! return expected variance (not covariance!) 200 virtual vec variance() const = 0; 201 199 202 //! Destructor for future use; 200 203 virtual ~epdf() {}; -
bdm/stat/libEF.h
r225 r229 132 132 double lognc () const; 133 133 vec mean() const {return mu;} 134 vec variance() const {return diag(R.to_mat());} 134 135 // mlnorm<sq_T>* condition ( const RV &rvn ) const ; 135 136 mpdf* condition ( const RV &rvn ) const ; … … 192 193 vec sample() const; 193 194 vec mean() const; 195 vec variance() const{it_error("Not implemented"); return vec(0);}; 194 196 void mean_mat ( mat &M, mat&R ) const; 195 197 //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] … … 220 222 //!sufficient statistics 221 223 vec beta; 224 //!speedup variable 225 double gamma; 222 226 public: 223 227 //!Default constructor … … 226 230 eDirich ( const eDirich &D0 ) : eEF ( D0.rv ),beta ( D0.beta ) {}; 227 231 vec sample() const {it_error ( "Not implemented" );return vec_1 ( 0.0 );}; 228 vec mean() const {return beta/sum ( beta );}; 232 vec mean() const {return beta/gamma;}; 233 vec variance() const {return elem_mult(beta,(beta+1))/ (gamma*(gamma+1));} 229 234 //! In this instance, val is ... 230 235 double evallog_nn ( const vec &val ) const {double tmp; tmp=( beta-1 ) *log ( val ); it_assert_debug(std::isfinite(tmp),"Infinite value"); … … 248 253 } 249 254 beta= beta0; 255 gamma = sum(beta); 250 256 } 251 257 }; … … 327 333 //! Returns poiter to alpha and beta. Potentially dengerous: use with care! 328 334 void _param ( vec* &a, vec* &b ) {a=αb=β}; 329 vec mean() const {vec pom ( alpha ); pom/=beta; return pom;} 335 vec mean() const {return elem_div(alpha,beta);} 336 vec variance() const {return elem_div(alpha,elem_mult(beta,beta)); } 330 337 }; 331 338 … … 365 372 void _param ( vec* &a, vec* &b ) {a=alpha;b=beta;}; 366 373 vec mean() const {return elem_div(*beta,*alpha-1);} 374 vec variance() const {vec mea=mean(); return elem_div(elem_mult(mea,mea),*alpha-2);} 367 375 }; 368 376 /* … … 416 424 lnk = log ( nk ); 417 425 } 418 vec mean() const {vec pom=high; pom-=low; pom/=2.0; return pom;} 426 vec mean() const {return (high-low)/2.0;} 427 vec variance() const {return (pow(high,2)+pow(low,2)+elem_mult(high,low))/3.0;} 419 428 }; 420 429 … … 646 655 return pom; 647 656 } 657 vec variance() const { 658 vec pom=zeros ( rv.count() ); 659 for ( int i=0;i<n;i++ ) {pom+=pow(samples ( i ),2) *w ( i );} 660 return pom-pow(mean(),2); 661 } 648 662 }; 649 663 -
matlab/pmsm/mpf_test_disp.m
r225 r229 8 8 9 9 figure(2) 10 StdX = 2*sqrt(VarE); 10 11 for i=1:4 11 12 subplot(4,1,i) … … 13 14 hold on; 14 15 plot(xthE(:,i)','r') 16 plot(xthE(:,i) - StdX(:,i),'r:'); 17 plot(xthE(:,i) + StdX(:,i),'r:'); 15 18 plot(xthM(:,i+4)','g') 16 19 % plot(xthV(i,:)','m'); … … 18 21 19 22 figure(3) 23 StdM = 2*sqrt(VarM); 20 24 for i=1:4 21 25 subplot(4,1,i) 22 26 plot(xthM(:,i)') 27 hold on 28 plot(xthM(:,i)-StdM(:,i),':') 29 plot(xthM(:,i)+StdM(:,i),':') 23 30 if i<4 24 31 %set(gca,'XTick',[]); … … 27 34 end 28 35 ylabel(['Q(' num2str(i) ',' num2str(i) ')']); 29 hold on 30 plot(Qtr(:,i)','--') 36 plot(Qtr(:,i)','--') 31 37 end 32 38 -
pmsm/mpf_test.cpp
r227 r229 15 15 #include <estim/libKF.h> 16 16 #include <estim/libPF.h> 17 #include <estim/ekf_templ.h> 17 18 #include <stat/libFN.h> 18 19 … … 22 23 23 24 using namespace itpp; 24 //!Extended Kalman filter with unknown \c Q25 class EKF_unQ : public EKFCh , public BMcond {26 public:27 //! Default constructor28 EKF_unQ ( RV rx, RV ry,RV ru,RV rQ ) :EKFCh ( rx,ry,ru ),BMcond ( rQ ) {};29 void condition ( const vec &Q0 ) {30 Q.setD ( Q0,0 );31 //from EKF32 preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() );33 };34 };35 36 25 37 26 int main() { … … 59 48 60 49 RV rQ ( "{Q }","4" ); 61 EKF _unQ KFEp ( rx,ry,ru,rQ );50 EKFCh_unQ KFEp ( rx,ry,ru,rQ ); 62 51 KFEp.set_parameters ( &fxu,&hxu,Q,R ); 63 52 KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) ); … … 65 54 //mgamma_fix evolQ ( rQ,rQ ); 66 55 migamma_fix evolQ ( rQ,rQ ); 67 MPF<EKF _unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp );56 MPF<EKFCh_unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp ); 68 57 // initialize 69 58 evolQ.set_parameters ( 0.1, Qdiag, 1.0); //sigma = 1/10 mu … … 85 74 mat Dt=zeros ( Ndat,2+2 ); //observation 86 75 mat XtE=zeros ( Ndat, 4 ); 76 mat VarE=zeros ( Ndat, 4 ); 87 77 mat Qtr=zeros ( Ndat, 4 ); 88 78 mat XtM=zeros ( Ndat,4+4 ); //Q + x 79 mat VarM=zeros ( Ndat,4+4 ); //Q + x 89 80 90 81 // SET SIMULATOR … … 129 120 Qtr.set_row ( tK, Qdiag); 130 121 XtE.set_row ( tK,KFEep.mean() ); 122 VarE.set_row ( tK,KFEep.variance() ); 131 123 XtM.set_row ( tK,Mep.mean() ); 124 VarM.set_row ( tK,Mep.variance() ); 132 125 } 133 126 … … 139 132 fou << Name ( "xthE" ) << XtE; 140 133 fou << Name ( "xthM" ) << XtM; 134 fou << Name ( "VarE" ) << VarE; 135 fou << Name ( "VarM" ) << VarM; 141 136 //Exit program: 142 137