Changeset 887 for library/bdm/stat

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

new base for particle filtering

Location:
library/bdm/stat
Files:
2 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} 
  • library/bdm/stat/exp_family.h

    r878 r887  
    9797        virtual void flatten ( const BMEF * B ) NOT_IMPLEMENTED_VOID; 
    9898 
    99         double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0); 
    100          
    101         virtual epdf* epredictor() const NOT_IMPLEMENTED(NULL); 
    102  
    103         virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); 
    10499 
    105100        void to_setting ( Setting &set ) const 
     
    267262UIREGISTER2 ( enorm, fsqmat ); 
    268263SHAREDPTR2 ( enorm, fsqmat ); 
     264 
     265typedef enorm<ldmat> egauss; 
     266UIREGISTER(egauss); 
     267 
    269268 
    270269//forward declaration 
     
    15961595//! Switch between various resampling methods. 
    15971596enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; 
     1597 
     1598//! Shortcut for multinomial.sample(int n). Various simplifications are allowed see RESAMPLING_METHOD  
     1599void resample(const vec &w, ivec &ind, RESAMPLING_METHOD=SYSTEMATIC); 
     1600 
    15981601/*! 
    15991602\brief Weighted empirical density 
     
    16641667        }; 
    16651668        //! 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. 
    1666         //! The vector with indeces of new samples is returned in variable \c index. 
    1667         void resample ( ivec &index, RESAMPLING_METHOD method = SYSTEMATIC ); 
    1668  
    1669         //! Resampling without returning index of new particles. 
    1670         void resample ( RESAMPLING_METHOD method = SYSTEMATIC ) { 
    1671                 ivec ind; 
    1672                 resample ( ind, method ); 
    1673         }; 
     1669        void resample ( RESAMPLING_METHOD method = SYSTEMATIC ); 
    16741670 
    16751671        //! inherited operation : NOT implemented 
     
    18741870} 
    18751871 
     1872/*! Dirac delta function distribution */ 
     1873class dirac: public epdf{ 
     1874        protected:  
     1875                vec point; 
     1876        public: 
     1877                double evallog (const vec &dt) const {return -inf;} 
     1878                vec mean () const {return point;} 
     1879                vec variance () const {return pow(point,2);} 
     1880                void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const { lb = point; ub = point;} 
     1881                //! access 
     1882                const vec& _point() {return point;} 
     1883                void set_point(const vec& p){point =p; dim=p.length();} 
     1884                vec sample() const {return point;} 
     1885        }; 
     1886 
    18761887//// 
    18771888///////