Changeset 300
- Timestamp:
- 03/19/09 15:38:58 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/merger.cpp
r299 r300 36 36 it_assert_debug ( rv.equal ( g0->_rv() ),"Incompatible g0" ); 37 37 //Empirical density - samples 38 if (!fix_smp) 38 if ( !fix_smp ) 39 { 39 40 eSmp.set_statistics ( ones ( Ns ), g0 ); 40 41 } 42 41 43 Array<vec> &Smp = eSmp._samples(); //aux 42 44 vec &w = eSmp._w(); //aux … … 54 56 vec vec0 ( 0 ); 55 57 58 //initialize importance weights 59 if ( !fix_smp ) 60 for ( int i=0;i<Ns;i++ ){ 61 lw_mix ( i ) =g0->evallog ( Smp ( i ) ); 62 } 63 56 64 // Initial component in the mixture model 57 65 mat V0=1e-8*eye ( dim +1 ); … … 70 78 vec Mix_pdf ( Ns ); 71 79 while ( !converged ) 72 73 74 75 80 { 81 //Re-estimate Mix 82 //Re-Initialize Mixture model 83 Mix.flatten ( &Mix_init ); 76 84 Mix.bayesB ( Smp_ex, w*Ns ); 77 85 delete Mpred; … … 80 88 81 89 // This will be active only later in iterations!!! 82 if ( (!fix_smp)&( 1./sum_sqr ( w ) <0.5*Ns ))90 if ( ( !fix_smp ) & ( 1./sum_sqr ( w ) <0.5*Ns ) ) 83 91 { 84 92 // Generate new samples … … 89 97 if ( Smp ( i ) ( 2 ) <0 ) {Smp ( i ) ( 2 ) = 0.01; } 90 98 set_col_part ( Smp_ex,i,Smp ( i ) ); 91 } 92 if ( 0 ) 99 //Importance of the mixture 100 lw_mix ( i ) =Mix.logpred (Smp_ex.get_col(i) ); 101 } 102 if ( 1 ) 93 103 { 94 104 cout<<"Resampling =" << 1./sum_sqr ( w ) << endl; … … 176 186 lW.set_row ( i, lw_src ); // do not divide by mix 177 187 } 178 //Importance of the mixture179 for ( int j=0;j<Ns;j++ )180 {181 lw_mix ( j ) =Mix.logpred ( Smp_ex.get_col ( j ) );182 }183 188 lw = lognorm_merge ( lW ); //merge 189 190 //Importance weighting 191 if ( !fix_smp ) 192 lw -= lw_mix; // hoping that it is not numerically sensitive... 193 w = exp ( lw-max ( lw ) ); 194 195 //renormalize 196 double sumw =sum ( w ); 197 if ( std::isfinite ( sumw ) ) 198 { 199 w = w/sumw; 200 } 201 else 202 { 203 it_file itf ( "merg_err.it" ); 204 itf << Name ( "w" ) << w; 205 } 184 206 185 207 if ( DBG ) … … 192 214 *dbg << Name ( str ) << lw_mix; 193 215 } 194 //Importance weighting195 if (!fix_smp)196 lw -= lw_mix; // hoping that it is not numerically sensitive...197 w = exp ( lw-max ( lw ) );198 //renormalize199 double sumw =sum ( w );200 if ( std::isfinite ( sumw ) )201 {202 w = w/sumw;203 }204 else205 {206 it_file itf ( "merg_err.it" );207 itf << Name ( "w" ) << w;208 }209 210 if ( DBG )211 {212 sprintf ( str,"w_is_%d",niter );213 *dbg << Name ( str ) << w;214 }215 216 // ==== stopping rule === 216 217 niter++;