Changeset 11
- Timestamp:
- 01/25/08 10:31:08 (17 years ago)
- Files:
-
- 2 added
- 8 modified
Legend:
- Unmodified
- Added
- Removed
-
Makefile
r9 r11 1 LDFLAGS=- litpp1 LDFLAGS=-Ldebug/usr/lib/ -litpp 2 2 CPPFLAGS=-g 3 3 4 all: test KF4 all: testPF 5 5 6 6 test: test0 … … 13 13 testKF: testKF.o libKF.o libBM.o libDC.o itpp_ext.o 14 14 testPF: testPF.o libPF.o libBM.o libDC.o itpp_ext.o 15 16 itpp_ext.o: itpp_ext.cpp itpp_ext.h -
itpp_ext.cpp
r6 r11 23 23 } 24 24 25 25 26 } -
libPF.cpp
r8 r11 5 5 using std::endl; 6 6 7 PF::PF(vec w0){w=w0/sum(w0); n=w.length();} 8 7 9 ivec PF::resample( RESAMPLING_METHOD method ) { 8 ivec ind( n ); 10 ivec ind=zeros_i( n ); 11 ivec N_babies = zeros_i( n ); 12 vec cumDist = cumsum( w ); 13 vec u( n ); 14 int i,j,parent; 15 double u0; 16 17 switch ( method ) { 18 case MULTINOMIAL: 19 u( n - 1 ) = pow( URNG.sample(), 1.0 / n ); 20 for ( i = n - 2;i >= 0;i-- ) { 21 u( i ) = u( i + 1 ) * pow( URNG.sample(), 1.0 / ( i + 1 ) ); 22 } 23 break; 24 case STRATIFIED: 25 for ( i = 0;i < n;i++ ) { 26 u( i ) = ( i + URNG.sample() ) / n; 27 } 28 break; 29 case SYSTEMATIC: 30 u0 = URNG.sample(); 31 for ( i = 0;i < n;i++ ) { 32 u( i ) = ( i + u0 ) / n; 33 } 34 break; 35 default: 36 it_error( "PF::resample(): Unknown resampling method" ); 37 } 38 39 // // <<<<<<<<<<< 40 // using std::cout; 41 // cout << "resampling:" << endl; 42 // cout << w <<endl; 43 // cout << cumDist <<endl; 44 // cout << u <<endl; 45 46 // U is now full 47 j = 0; 48 for ( i = 0;i < n;i++ ) { 49 while ( u( i ) > cumDist( j ) ) j++; 50 N_babies( j )++; 51 } 52 53 // // <<<<<<<<<<< 54 // cout << N_babies <<endl; 55 56 // We have assigned new babies for each Particle 57 // Now, we fill the resulting index such that: 58 // * particles with at least one baby should not move * 59 // This assures that reassignment can be done inplace; 60 61 // find the first parent; 62 parent=0; while(N_babies(parent)==0) parent++; 63 // Build index 64 for ( i = 0;i < n;i++ ) { 65 if ( N_babies( i ) > 0 ){ 66 ind( i ) = i; 67 N_babies( i ) --; //this index was now replicated; 68 } 69 else { 70 // test if the parent has been fully replicated 71 // if yes, find the next one 72 while((N_babies(parent)==0) || (N_babies(parent)==1 && parent>i) ) parent++; 73 // Replicate parent 74 ind (i) = parent; 75 N_babies( parent ) --; //this index was now replicated; 76 } 77 /* // <<<<<<<<<<< 78 cout <<ind << endl;*/ 79 } 9 80 return ind; 10 81 } 11 82 12 TrivialPF::TrivialPF( mpdf &par0, mpdf &obs0, mpdf &prop0, int n0){83 TrivialPF::TrivialPF( mpdf &par0, mpdf &obs0, mpdf &prop0, int n0 ) : PF(1/n*ones(n)) { 13 84 is_proposal = true; 14 85 prop = &prop0; … … 16 87 obs = &obs0; 17 88 } 18 TrivialPF::TrivialPF( mpdf &par0, mpdf &obs0, int n0){89 TrivialPF::TrivialPF( mpdf &par0, mpdf &obs0, int n0 ) : PF(1/n*ones(n)) { 19 90 is_proposal = false; 20 91 par = &par0; … … 22 93 } 23 94 24 void TrivialPF::bayes( const vec &dt , bool evalll ) {95 void TrivialPF::bayes( const vec &dt , bool evalll ) { 25 96 int i; 26 97 vec oldp; 27 double ll, gl, sum =0.0;98 double ll, gl, sum = 0.0; 28 99 Sort<double> S; 29 100 ivec ind, iw; -
libPF.h
r8 r11 20 20 using namespace itpp; 21 21 22 enum RESAMPLING_METHOD { MULTINOMIAL = 0, DETERMINISTIC = 1, RESIDUAL = 2, SYSTEMATIC = 3 };22 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; 23 23 24 24 /*! … … 31 31 int n; //number of particles 32 32 vec w; //particle weights 33 Uniform_RNG URNG; //used for resampling 33 34 34 35 public: 35 //! Returns indexes of particles that should be resampled 36 //! Returns indexes of particles that should be resampled. The ordering MUST guarantee inplace replacement. (Important for MPF.) 36 37 ivec resample(RESAMPLING_METHOD method = SYSTEMATIC); 37 //TODO get them on the web 38 PF (vec w); 39 //TODO remove or implement bayes()! 40 void bayes(const vec &dt, bool evell){}; 38 41 }; 39 42 -
mixpp.kdevelop
r7 r11 18 18 <run> 19 19 <directoryradio>executable</directoryradio> 20 <mainprogram> ./testKF</mainprogram>20 <mainprogram>/home/smidl/work/mixpp/testPF</mainprogram> 21 21 <programargs></programargs> 22 22 <globaldebugarguments></globaldebugarguments> … … 200 200 <orientation>Vertical</orientation> 201 201 </splitheadersource> 202 <references/> 202 203 </kdevcppsupport> 203 204 <cppsupportpart> … … 215 216 </kdevdocumentation> 216 217 <kdevfileview> 217 <groups/> 218 <groups> 219 <hidenonprojectfiles>false</hidenonprojectfiles> 220 <hidenonlocation>false</hidenonlocation> 221 </groups> 222 <tree> 223 <showvcsfields>false</showvcsfields> 224 <hidenonprojectfiles>true</hidenonprojectfiles> 225 <hidepatterns>*.o,*.lo,CVS</hidepatterns> 226 </tree> 218 227 </kdevfileview> 219 228 <ctagspart> 220 <customArguments ></customArguments>229 <customArguments/> 221 230 <customTagfilePath>/home/smidl/work/mixpp/tags</customTagfilePath> 222 231 <activeTagsFiles/> -
mixpp.kdevelop.filelist
r8 r11 1 1 # KDevelop Custom Project File List 2 2 Makefile 3 arx.cpp4 3 doc 5 4 doc/latex -
testKF.cpp
r9 r11 17 17 // input from Matlab 18 18 it_file fin( "testKF.it" ); 19 19 20 mat Dt, Xt,Xt2; 20 21 int Ndat; -
testPF.cpp
r8 r11 19 19 ldmat R("1"); 20 20 21 22 vec ptc = randn(10); 23 vec w = exp(-0.5*(pow(ptc,2))/0.2); 24 25 cout << "p:" << ptc << endl; 26 cout << "w:" << w << endl; 27 28 PF pf(w); 29 ivec ind = pf.resample(); 30 31 cout << ind << endl; 32 /* 21 33 mlnorm<ldmat> obs(x,xm,A,R); 22 34 mlnorm<ldmat> par(y,x,A,R); 23 35 24 36 TrivialPF TPF(obs,par,10); 37 */ 25 38 //Exit program: 26 39 return 0;