Show
Ignore:
Timestamp:
03/29/10 23:02:03 (14 years ago)
Author:
smidl
Message:

new base for particle filtering

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/exp_family.cpp

    r878 r887  
    423423} 
    424424 
    425 void eEmp::resample ( ivec &ind, RESAMPLING_METHOD method ) { 
     425void eEmp::resample ( RESAMPLING_METHOD method ) { 
     426        ivec ind = zeros_i ( n ); 
     427        bdm::resample(w,ind,method); 
     428        // copy the internals according to ind 
     429        for (int i = 0; i < n; i++ ) { 
     430                if ( ind ( i ) != i ) { 
     431                        samples ( i ) = samples ( ind ( i ) ); 
     432                } 
     433                w ( i ) = 1.0 / n; 
     434        } 
     435} 
     436 
     437void resample ( const vec &w, ivec &ind, RESAMPLING_METHOD method ) { 
     438        int n = w.length(); 
    426439        ind = zeros_i ( n ); 
    427440        ivec N_babies = zeros_i ( n ); 
     
    430443        int i, j, parent; 
    431444        double u0; 
    432  
     445         
    433446        switch ( method ) { 
    434         case MULTINOMIAL: 
    435                 u ( n - 1 ) = pow ( UniRNG.sample(), 1.0 / n ); 
    436  
    437                 for ( i = n - 2; i >= 0; i-- ) { 
    438                         u ( i ) = u ( i + 1 ) * pow ( UniRNG.sample(), 1.0 / ( i + 1 ) ); 
    439                 } 
    440  
    441                 break; 
    442  
    443         case STRATIFIED: 
    444  
    445                 for ( i = 0; i < n; i++ ) { 
    446                         u ( i ) = ( i + UniRNG.sample() ) / n; 
    447                 } 
    448  
    449                 break; 
    450  
    451         case SYSTEMATIC: 
    452                 u0 = UniRNG.sample(); 
    453  
    454                 for ( i = 0; i < n; i++ ) { 
    455                         u ( i ) = ( i + u0 ) / n; 
    456                 } 
    457  
    458                 break; 
    459  
    460         default: 
    461                 bdm_error ( "PF::resample(): Unknown resampling method" ); 
    462         } 
    463  
     447                case MULTINOMIAL: 
     448                        u ( n - 1 ) = pow ( UniRNG.sample(), 1.0 / n ); 
     449                         
     450                        for ( i = n - 2; i >= 0; i-- ) { 
     451                                u ( i ) = u ( i + 1 ) * pow ( UniRNG.sample(), 1.0 / ( i + 1 ) ); 
     452                        } 
     453                         
     454                        break; 
     455                         
     456                case STRATIFIED: 
     457                         
     458                        for ( i = 0; i < n; i++ ) { 
     459                                u ( i ) = ( i + UniRNG.sample() ) / n; 
     460                        } 
     461                         
     462                        break; 
     463                         
     464                case SYSTEMATIC: 
     465                        u0 = UniRNG.sample(); 
     466                         
     467                        for ( i = 0; i < n; i++ ) { 
     468                                u ( i ) = ( i + u0 ) / n; 
     469                        } 
     470                         
     471                        break; 
     472                         
     473                default: 
     474                        bdm_error ( "PF::resample(): Unknown resampling method" ); 
     475        } 
     476         
    464477        // U is now full 
    465478        j = 0; 
    466  
     479         
    467480        for ( i = 0; i < n; i++ ) { 
    468481                while ( u ( i ) > cumDist ( j ) ) j++; 
    469  
     482                 
    470483                N_babies ( j ) ++; 
    471484        } 
     
    474487        // * particles with at least one baby should not move * 
    475488        // This assures that reassignment can be done inplace; 
    476  
     489         
    477490        // find the first parent; 
    478491        parent = 0; 
    479492        while ( N_babies ( parent ) == 0 ) parent++; 
    480  
     493         
    481494        // Build index 
    482495        for ( i = 0; i < n; i++ ) { 
     
    488501                        // if yes, find the next one 
    489502                        while ( ( N_babies ( parent ) == 0 ) || ( N_babies ( parent ) == 1 && parent > i ) ) parent++; 
    490  
     503                         
    491504                        // Replicate parent 
    492505                        ind ( i ) = parent; 
    493  
     506                         
    494507                        N_babies ( parent ) --; //this index was now replicated; 
    495508                } 
    496  
    497         } 
    498  
    499 // copy the internals according to ind 
    500         for ( i = 0; i < n; i++ ) { 
    501                 if ( ind ( i ) != i ) { 
    502                         samples ( i ) = samples ( ind ( i ) ); 
    503                 } 
    504                 w ( i ) = 1.0 / n; 
     509                 
    505510        } 
    506511}