Changeset 300

Show
Ignore:
Timestamp:
03/19/09 15:38:58 (15 years ago)
Author:
smidl
Message:

mistake corrected

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/estim/merger.cpp

    r299 r300  
    3636                it_assert_debug ( rv.equal ( g0->_rv() ),"Incompatible g0" ); 
    3737                //Empirical density - samples 
    38                 if (!fix_smp) 
     38                if ( !fix_smp ) 
     39                { 
    3940                        eSmp.set_statistics ( ones ( Ns ), g0 ); 
    40                  
     41                } 
     42 
    4143                Array<vec> &Smp = eSmp._samples(); //aux 
    4244                vec &w = eSmp._w(); //aux 
     
    5456                vec vec0 ( 0 ); 
    5557 
     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 
    5664                // Initial component in the mixture model 
    5765                mat V0=1e-8*eye ( dim +1 ); 
     
    7078                vec Mix_pdf ( Ns ); 
    7179                while ( !converged ) 
    72                 { 
    73                         //Re-estimate Mix 
    74                         //Re-Initialize Mixture model 
    75                         Mix.flatten ( &Mix_init ); 
     80        { 
     81                //Re-estimate Mix 
     82                //Re-Initialize Mixture model 
     83                Mix.flatten ( &Mix_init ); 
    7684                        Mix.bayesB ( Smp_ex, w*Ns ); 
    7785                        delete Mpred; 
     
    8088 
    8189                        // 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 ) ) 
    8391                        { 
    8492                                // Generate new samples 
     
    8997                                        if ( Smp ( i ) ( 2 ) <0 ) {Smp ( i ) ( 2 ) = 0.01; } 
    9098                                        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 ) 
    93103                                { 
    94104                                        cout<<"Resampling =" << 1./sum_sqr ( w ) << endl; 
     
    176186                                lW.set_row ( i, lw_src ); // do not divide by mix 
    177187                        } 
    178                         //Importance of the mixture 
    179                         for ( int j=0;j<Ns;j++ ) 
    180                         { 
    181                                 lw_mix ( j ) =Mix.logpred ( Smp_ex.get_col ( j ) ); 
    182                         } 
    183188                        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                        } 
    184206 
    185207                        if ( DBG ) 
     
    192214                                *dbg << Name ( str ) << lw_mix; 
    193215                        } 
    194                         //Importance weighting 
    195                         if (!fix_smp) 
    196                                 lw -=  lw_mix; // hoping that it is not numerically sensitive... 
    197                         w = exp ( lw-max ( lw ) ); 
    198                         //renormalize 
    199                         double sumw =sum ( w ); 
    200                         if ( std::isfinite ( sumw ) ) 
    201                         { 
    202                                 w = w/sumw; 
    203                         } 
    204                         else 
    205                         { 
    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                         } 
    215216                        // ==== stopping rule === 
    216217                        niter++;