#include "libPF.h" using namespace itpp; using std::endl; PF::PF(vec w0){w=w0/sum(w0); n=w.length();} ivec PF::resample( RESAMPLING_METHOD method ) { ivec ind=zeros_i( n ); ivec N_babies = zeros_i( n ); vec cumDist = cumsum( w ); vec u( n ); int i,j,parent; double u0; switch ( method ) { case MULTINOMIAL: u( n - 1 ) = pow( URNG.sample(), 1.0 / n ); for ( i = n - 2;i >= 0;i-- ) { u( i ) = u( i + 1 ) * pow( URNG.sample(), 1.0 / ( i + 1 ) ); } break; case STRATIFIED: for ( i = 0;i < n;i++ ) { u( i ) = ( i + URNG.sample() ) / n; } break; case SYSTEMATIC: u0 = URNG.sample(); for ( i = 0;i < n;i++ ) { u( i ) = ( i + u0 ) / n; } break; default: it_error( "PF::resample(): Unknown resampling method" ); } // // <<<<<<<<<<< // using std::cout; // cout << "resampling:" << endl; // cout << w < cumDist( j ) ) j++; N_babies( j )++; } // // <<<<<<<<<<< // cout << N_babies < 0 ){ ind( i ) = i; N_babies( i ) --; //this index was now replicated; } else { // test if the parent has been fully replicated // if yes, find the next one while((N_babies(parent)==0) || (N_babies(parent)==1 && parent>i) ) parent++; // Replicate parent ind (i) = parent; N_babies( parent ) --; //this index was now replicated; } /* // <<<<<<<<<<< cout < S; ivec ind, iw; /* //generate new samples for ( i=0;ievalcond( ptcls( i ), &prop_cond ); ptcls( i ) = prop_cond.sample(); gl = prop_cond.eval( ptcls( i ) ); obs.evalcond( ptcls( i ), &obs_cond ); ll = obs_cond.eval( dt ); w( i ) *= ll/gl; } //renormalize for ( i=0;i