Changeset 205
- Timestamp:
- 11/10/08 15:40:30 (16 years ago)
- Files:
-
- 2 added
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/merger.cpp
r204 r205 16 16 case 3://Ratio of Bessel 17 17 coef = sqrt ( ( 3*beta-2 ) /3*beta ); 18 return log ( besselk ( 0,sq2bl*coef )) - log( besselk ( 0,sq2bl ) ) + mu;18 return log ( besselk ( 0,sq2bl*coef ) ) - log ( besselk ( 0,sq2bl ) ) + mu; 19 19 break; 20 20 case 4: … … 31 31 it_assert_debug ( rv.equal ( g0->_rv() ),"Incompatible g0" ); 32 32 //Empirical density - samples 33 eEmp eSmp ( rv,Ns );34 33 eSmp.set_parameters ( ones ( Ns ), g0 ); 35 34 Array<vec> &Smp = eSmp._samples(); //aux … … 62 61 char str[100]; 63 62 64 epdf* Mpred ;63 epdf* Mpred=Mix.predictor(rv); 65 64 vec Mix_pdf ( Ns ); 66 65 while ( !converged ) { … … 69 68 Mix.flatten ( &Mix_init ); 70 69 Mix.bayesB ( Smp_ex, w*Ns ); 70 delete Mpred; 71 71 Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!! 72 73 // sprintf ( str,"Mpred_mean%d",niter ); 74 // dbg << Name ( str ) << Mpred->mean(); 75 76 if ( 1 ) { 72 73 if ( 1./sum_sqr ( w ) <0.5*Ns ) { 77 74 // Generate new samples 78 75 eSmp.set_samples ( Mpred ); 79 76 for ( int i=0;i<Ns;i++ ) { 80 77 //////////// !!!!!!!!!!!!! 81 if ( Smp ( i ) ( 1 ) <0 ) {Smp ( i ) ( 1 ) = GamRNG(); }78 if ( Smp ( i ) ( 1 ) <0 ) {Smp ( i ) ( 1 ) = 0.01; } 82 79 set_col_part ( Smp_ex,i,Smp ( i ) ); 83 80 } 84 {cout<<" Eff. sample size=" << 1./sum_sqr ( w ) << endl;}81 {cout<<"Resampling =" << 1./sum_sqr ( w ) << endl;} 85 82 cout << sum ( Smp_ex,2 ) /Ns <<endl; 83 cout << Smp_ex*Smp_ex.T()/Ns << endl; 86 84 } 87 else 88 {cout<<"Eff. sample size=" << 1./sum_sqr ( w ) << endl;} 85 // sprintf ( str,"Mpred_mean%d",niter ); 86 // dbg << Name ( str ) << Mpred->mean(); 87 89 88 90 89 // sprintf ( str,"Mpdf%d",niter ); … … 135 134 zdls ( i )->get_cond ( Smp ( k ) ) ) ); 136 135 if ( !std::isfinite ( lw_src ( k ) ) ) { 137 cout << endl;136 lw_src ( k ) = -1e16; cout << "!"; 138 137 } 139 138 } … … 166 165 //Importance weighting 167 166 lw -= lw_mix; // hoping that it is not numerically sensitive... 168 w = exp (lw-max(lw));167 w = exp ( lw-max ( lw ) ); 169 168 //renormalize 170 169 double sumw =sum ( w ); … … 173 172 } 174 173 else { 175 174 176 175 } 177 { 178 double eff = 1./sum_sqr ( w ); 179 if ( eff<2 ) { 180 int mi= max_index ( w ); 181 cout << w(mi) <<endl; 182 cout << lW.get_col ( mi ) <<endl; 183 mat mm(2,1);mm.set_col(0,lW.get_col(mi)); 184 cout << lognorm_merge(mm) <<endl; 185 cout << lw_mix ( mi ) <<endl; 186 cout << lw ( mi ) <<endl; 187 cout << Smp ( mi ) <<endl; 188 cout << Mix._Coms(0)->_e()->mean() <<endl; 189 } 190 cout << "Eff: " << eff <<endl; 191 } 176 192 177 // sprintf ( str,"w_is_%d",niter ); 193 178 // dbg << Name ( str ) << w; -
bdm/estim/merger.h
r198 r205 43 43 //!Prior on the log-normal merging model 44 44 double beta; 45 //! Projection to empirical density 46 eEmp eSmp; 47 45 48 public: 46 49 //!Default constructor 47 50 merger ( const Array<mpdf*> &S ) : 48 51 compositepdf ( S ), epdf ( getrv ( false ) ), 49 Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ), dls ( n ), rvzs ( n ), zdls ( n ) {52 Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ), dls ( n ), rvzs ( n ), zdls ( n ), eSmp(rv,0) { 50 53 RV ztmp; 51 54 // Extend rv by rvc! … … 68 71 } 69 72 //! Set internal parameters used in approximation 70 void set_parameters ( double beta0, int Ns0, int Nc0 ) { beta=beta0;Ns=Ns0;Nc=Nc0;}73 void set_parameters ( double beta0, int Ns0, int Nc0 ) {beta=beta0;Ns=Ns0;Nc=Nc0;eSmp.set_n(Ns0,false);} 71 74 //!Initialize the proposal density. This function must be called before merge()! 72 75 void init() { ////////////// NOT FINISHED … … 90 93 return Mix.logpred ( dtf ); 91 94 } 92 vec mean() const {return Mix._epdf().mean();} 95 vec mean() const { 96 const Vec<double> &w = eSmp._w(); 97 const Array<vec> &S = eSmp._samples(); 98 vec tmp=zeros ( rv.count() ); 99 for ( int i=0; i<Ns; i++ ) { 100 tmp+=w ( i ) *S ( i ); 101 } 102 return tmp; 103 } 104 mat variance() const { 105 const vec &w = eSmp._w(); 106 const Array<vec> &S = eSmp._samples(); 107 108 vec mea = mean(); 109 110 cout << sum(w) << "," << w*w <<endl; 111 112 mat Tmp=zeros(rv.count(), rv.count()); 113 for ( int i=0; i<Ns; i++ ) { 114 Tmp+=w ( i ) *outer_product(S ( i ), S(i)); 115 } 116 return Tmp-outer_product(mea,mea); 117 } 93 118 //! for future use 94 119 virtual ~merger() { … … 101 126 //! Access function 102 127 MixEF& _Mix() {return Mix;} 128 //! Access function 129 eEmp& _Smp() {return eSmp;} 103 130 }; 104 131 -
bdm/stat/libEF.h
r204 r205 200 200 ldmat& _V() {return V;} 201 201 //! returns a pointer to the internal statistics. Use with Care! 202 double& _nu() {return nu;} 202 const ldmat& _V() const {return V;} 203 //! returns a pointer to the internal statistics. Use with Care! 204 double& _nu() {return nu;} 205 const double& _nu() const {return nu;} 203 206 void pow ( double p ) {V*=p;nu*=p;}; 204 207 }; … … 520 523 //! Set sample 521 524 void set_samples ( const epdf* pdf0 ); 525 //! Set sample 526 void set_n ( int n0, bool copy=true ){w.set_size(n0,copy);samples.set_size(n0,copy);}; 522 527 //! Potentially dangerous, use with care. 523 528 vec& _w() {return w;}; 529 //! Potentially dangerous, use with care. 530 const vec& _w() const {return w;}; 524 531 //! access function 525 532 Array<vec>& _samples() {return samples;}; 533 //! access function 534 const Array<vec>& _samples() const {return samples;}; 526 535 //! Function performs resampling, i.e. removal of low-weight samples and duplication of high-weight samples such that the new samples represent the same density. 527 536 ivec resample ( RESAMPLING_METHOD method = SYSTEMATIC ); -
mpdm/merg_giw.cpp
r204 r205 17 17 RV uu=u1; uu.add ( u2 ); 18 18 19 double a1t = 1.5; 20 double a2t = 0.8; 21 double sqr=0.10; 19 22 // Full system 20 vec thg ( "1 1" ); //Simulated system - zero for constant term 21 double sqr=1.0; 23 vec thg =vec_2 ( a1t,a2t ); //Simulated system - zero for constant term 22 24 vec Th = concat ( thg, sqr ); //Full parameter 23 25 … … 28 30 RV all =a1; all.add ( a2 ); all.add ( r ); 29 31 RV allj =a1; allj.add ( r ); allj.add ( a2 ); 30 vec Thj ("1 1 1");32 vec Thj=vec_3 ( a1t,sqr,a2t ); 31 33 // Setup values 32 34 … … 37 39 ARX P1 ( concat ( a1,r ), V0, -1 ); 38 40 ARX P2 ( concat ( a2,r ), V0, -1 ); 39 ARX PG ( all, V0g, -1 ); 41 ARX PG ( all, V0g, -1 ); //or -1? 42 // ARX PGk ( all, V0g, -1 ); 40 43 41 44 //Test estimation … … 44 47 45 48 // Logging 46 dirfilelog L ( "exp/merg_giw", 1);49 dirfilelog L ( "exp/merg_giw",ndat ); 47 50 int Li_Data = L.add ( RV ( "{Y U1 U2 }" ), "" ); 48 int Li_LL = L.add ( RV ( "{1 2 G M }" ), "LL" ); 51 int Li_LL = L.add ( RV ( "{G M }" ), "LL" ); 52 int Li_P1m = L.add ( RV ( "{a1 r }" ), "P1" ); 53 int Li_P2m = L.add ( RV ( "{a2 r }" ), "P2" ); 49 54 int Li_Gm = L.add ( RV ( "{a1 a2 r }" ), "G" ); 50 55 int Li_Mm = L.add ( RV ( "{a1 r a2 }" ), "M" ); … … 54 59 vec yt ( 1 ); 55 60 56 vec LLs ( 4);61 vec LLs ( 2 ); 57 62 vec rgrg ( 2 ); 58 63 59 64 //Proposal 60 enorm<ldmat> g0 ( a1 ); g0.set_parameters ( "1 ",mat ("1") );65 enorm<ldmat> g0 ( a1 ); g0.set_parameters ( "1 ",mat ( "1" ) ); 61 66 egamma g1 ( r ); g1.set_parameters ( "2 ", "2" ); 62 enorm<ldmat> g2 ( a2 ); g2.set_parameters ( "1 ",mat ("1") );67 enorm<ldmat> g2 ( a2 ); g2.set_parameters ( "1 ",mat ( "1" ) ); 63 68 64 Array<const epdf*> A (3); A(0) = &g0; A(1)=&g1; A(2) = &g2;65 eprod G0 (A);66 69 Array<const epdf*> A ( 3 ); A ( 0 ) = &g0; A ( 1 ) =&g1; A ( 2 ) = &g2; 70 eprod G0 ( A ); 71 67 72 for ( t=0; t<ndat; t++ ) { 68 73 // True system 69 74 rgrg ( 0 ) = pow ( sin ( ( t/40.0 ) *pi ),3 ); 70 rgrg ( 1 ) = pow ( cos ( ( t/40.0 ) *pi ),3 );75 rgrg ( 1 ) = pow ( cos ( ( t/40.0 +0.1 ) *pi ),3 ); 71 76 72 77 Yt ( t ) = thg*rgrg + sqr * NorRNG(); 73 78 74 79 // Bayes for all 75 P1.bayes ( concat ( Yt ( t ),vec_1 (rgrg ( 0 ) )) );76 P2.bayes ( concat ( Yt ( t ),vec_1 (rgrg ( 1 ) )) );80 P1.bayes ( concat ( Yt ( t ),vec_1 ( rgrg ( 0 ) ) ) ); 81 P2.bayes ( concat ( Yt ( t ),vec_1 ( rgrg ( 1 ) ) ) ); 77 82 PG.bayes ( concat ( Yt ( t ),rgrg ) ); 78 83 84 85 // crippling PGk by substituting zeros. 86 /* ldmat &Vk=const_cast<egiw*>(PGk._e())->_V(); //PG ldmat does not like 0! 87 mat fVk=PG._e()->_V().to_mat(); 88 fVk(1,2) = 0.0; 89 fVk(2,1) = 0.0; 90 Vk = ldmat(fVk); 91 */ //PGk is now krippled 92 79 93 // Merge estimates 80 mepdf eG1 (P1._e());81 mepdf eG2 (P2._e());94 mepdf eG1 ( P1._e() ); 95 mepdf eG2 ( P2._e() ); 82 96 Array<mpdf*> A ( 2 ); A ( 0 ) =&eG1;A ( 1 ) =&eG2; 83 97 merger M ( A ); 84 M.set_parameters ( 10.0, 100,2 ); 98 M.set_parameters ( 1.5, 100,3 ); //M._Mix().set_method(QB); 99 //M2.set_parameters ( 100.0, 1000,3 ); //M2._Mix().set_method(QB); 85 100 M.merge ( &G0 ); 101 //M2.merge ( &G0 ); 86 102 87 103 //Likelihood 88 104 yt ( 0 ) = Yt ( t ); 89 105 90 LLs ( 0 ) = P1._e()->evalpdflog ( get_vec(Th, "1 2") ); 91 LLs ( 1 ) = P2._e()->evalpdflog ( get_vec(Th, "3 2") ); 92 LLs ( 2 ) = PG._e()->evalpdflog (Th ); 93 LLs ( 3 ) = M._Mix().logpred ( concat(Thj, vec_1(1.0)) ); 106 // LLs ( 0 ) = P1._e()->evalpdflog ( get_vec(Th, "1 2") ); 107 // LLs ( 1 ) = P2._e()->evalpdflog ( get_vec(Th, "3 2") ); 108 LLs ( 0 ) = PG._e()->evalpdflog ( Th ); 109 LLs ( 1 ) = M._Mix().logpred ( concat ( Thj, vec_1 ( 1.0 ) ) ); 110 // LLs ( 3 ) = M2._Mix().logpred ( concat(Thj, vec_1(1.0)) ); 94 111 L.logit ( Li_LL, LLs ); //log-normal 95 112 96 113 //Logger 97 114 L.logit ( Li_Data, vec_3 ( Yt ( t ), rgrg ( 0 ), rgrg ( 1 ) ) ); 115 L.logit ( Li_P1m, P1._e()->mean() ); 116 L.logit ( Li_P2m, P2._e()->mean() ); 98 117 L.logit ( Li_Gm, PG._e()->mean() ); 99 emix *tm =M._Mix().predictor(allj); 100 L.logit ( Li_Mm, tm->mean() ); 101 delete tm; 118 L.logit ( Li_Mm, M.mean() ); 119 102 120 L.step ( ); 121 122 cout << "Vg: " << PG._e()->_V().to_mat() <<endl; 123 vec mea = M.mean(); 124 cout << "Ve: " << M.variance() <<endl; 103 125 } 104 126 L.finalize( ); 105 127 L.itsave ( "merg_egiw.it" ); 128 cout << endl; 106 129 } -
tests/CMakeLists.txt
r201 r205 42 42 TEST(arx_elem_test) 43 43 TEST(merger_test) 44 TEST(merger_2d_test) 44 45 TEST(merger_iter_test) 45 46 TEST(mixef_test)