Changeset 887 for library/bdm/stat
- Timestamp:
- 03/29/10 23:02:03 (14 years ago)
- Location:
- library/bdm/stat
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/stat/exp_family.cpp
r878 r887 423 423 } 424 424 425 void eEmp::resample ( ivec &ind, RESAMPLING_METHOD method ) { 425 void 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 437 void resample ( const vec &w, ivec &ind, RESAMPLING_METHOD method ) { 438 int n = w.length(); 426 439 ind = zeros_i ( n ); 427 440 ivec N_babies = zeros_i ( n ); … … 430 443 int i, j, parent; 431 444 double u0; 432 445 433 446 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 464 477 // U is now full 465 478 j = 0; 466 479 467 480 for ( i = 0; i < n; i++ ) { 468 481 while ( u ( i ) > cumDist ( j ) ) j++; 469 482 470 483 N_babies ( j ) ++; 471 484 } … … 474 487 // * particles with at least one baby should not move * 475 488 // This assures that reassignment can be done inplace; 476 489 477 490 // find the first parent; 478 491 parent = 0; 479 492 while ( N_babies ( parent ) == 0 ) parent++; 480 493 481 494 // Build index 482 495 for ( i = 0; i < n; i++ ) { … … 488 501 // if yes, find the next one 489 502 while ( ( N_babies ( parent ) == 0 ) || ( N_babies ( parent ) == 1 && parent > i ) ) parent++; 490 503 491 504 // Replicate parent 492 505 ind ( i ) = parent; 493 506 494 507 N_babies ( parent ) --; //this index was now replicated; 495 508 } 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 505 510 } 506 511 } -
library/bdm/stat/exp_family.h
r878 r887 97 97 virtual void flatten ( const BMEF * B ) NOT_IMPLEMENTED_VOID; 98 98 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);104 99 105 100 void to_setting ( Setting &set ) const … … 267 262 UIREGISTER2 ( enorm, fsqmat ); 268 263 SHAREDPTR2 ( enorm, fsqmat ); 264 265 typedef enorm<ldmat> egauss; 266 UIREGISTER(egauss); 267 269 268 270 269 //forward declaration … … 1596 1595 //! Switch between various resampling methods. 1597 1596 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; 1597 1598 //! Shortcut for multinomial.sample(int n). Various simplifications are allowed see RESAMPLING_METHOD 1599 void resample(const vec &w, ivec &ind, RESAMPLING_METHOD=SYSTEMATIC); 1600 1598 1601 /*! 1599 1602 \brief Weighted empirical density … … 1664 1667 }; 1665 1668 //! 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 ); 1674 1670 1675 1671 //! inherited operation : NOT implemented … … 1874 1870 } 1875 1871 1872 /*! Dirac delta function distribution */ 1873 class 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 1876 1887 //// 1877 1888 ///////