46 :
n(0), sum(0.), mean(0.),
var(0.), sd(0.),
r(0.), efficiency(0.),
47 r2eff(0.), r2int(0.), shift(0.), vov(0.), fom(0.), largest(0.),
48 largest_score_happened(0), mean_1(0.), var_1(0.), sd_1(0.), r_1(0.),
49 shift_1(0.), vov_1(0.), fom_1(0.), noBinOfHistory(16), slope(0.),
50 noBinOfPDF(10), minimizer(0), noPass(0), noTotal(8), statsAreUpdated(true)
52 nonzero_histories.clear();
53 largest_scores.clear();
54 largest_scores.push_back( 0.0 );
56 history_grid.resize( noBinOfHistory , 0 );
57 mean_history.resize( noBinOfHistory , 0.0 );
58 var_history.resize( noBinOfHistory , 0.0 );
59 sd_history.resize( noBinOfHistory , 0.0 );
60 r_history.resize( noBinOfHistory , 0.0 );
61 vov_history.resize( noBinOfHistory , 0.0 );
62 fom_history.resize( noBinOfHistory , 0.0 );
63 shift_history.resize( noBinOfHistory , 0.0 );
64 e_history.resize( noBinOfHistory , 0.0 );
65 r2eff_history.resize( noBinOfHistory , 0.0 );
66 r2int_history.resize( noBinOfHistory , 0.0 );
71 cpu_time.push_back( 0.0 );
96 nonzero_histories.insert( std::pair< G4int , G4double > ( n , x ) );
97 if ( x > largest_scores.back() )
100 std::vector< G4double >::iterator it;
101 for ( it = largest_scores.begin() ; it != largest_scores.end() ; it++ )
105 largest_scores.insert( it , x );
110 if ( largest_scores.size() > 201 )
112 largest_scores.pop_back();
120 statsAreUpdated =
false;
127 void G4ConvergenceTester::calStat()
131 efficiency = double( nonzero_histories.size() ) / n;
141 std::map< G4int , G4double >::iterator it;
142 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
146 var += ( xi - mean ) * ( xi - mean );
147 shift += ( xi - mean ) * ( xi - mean ) * ( xi - mean );
148 vov += ( xi - mean ) * ( xi - mean ) * ( xi - mean ) * ( xi - mean );
151 var += ( n - nonzero_histories.size() ) * mean * mean;
152 shift += ( n - nonzero_histories.size() ) * mean * mean * mean * ( -1 );
153 vov += ( n - nonzero_histories.size() ) * mean * mean * mean * mean;
155 vov = vov / ( var * var ) - 1.0 / n;
159 sd = std::sqrt ( var );
161 r = sd / mean / std::sqrt (
G4double(n) );
163 r2eff = ( 1 - efficiency ) / ( efficiency * n );
164 r2int = sum_x2 / ( sum * sum ) - 1 / ( efficiency * n );
166 shift = shift / ( 2 * var * n );
168 fom = 1 / (r*r) / cpu_time.back();
173 largest_score_happened = 0;
174 G4double spend_time_of_largest = 0.0;
175 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
177 if ( std::abs ( it->second ) > largest )
179 largest = it->second;
180 largest_score_happened = it->first;
181 spend_time_of_largest = cpu_time [ it->first+1 ] - cpu_time [ it->first ];
195 mean_1 = ( sum + largest ) / ( n + 1 );
197 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
200 var_1 += ( xi - mean_1 ) * ( xi - mean_1 );
201 shift_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
202 vov_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
205 var_1 += ( xi - mean_1 ) * ( xi - mean_1 );
206 shift_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
207 vov_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
209 var_1 += ( n - nonzero_histories.size() ) * mean_1 * mean_1;
210 shift_1 += ( n - nonzero_histories.size() ) * mean_1 * mean_1 * mean_1 * ( -1 );
211 vov_1 += ( n - nonzero_histories.size() ) * mean_1 * mean_1 * mean_1 * mean_1;
213 vov_1 = vov_1 / ( var_1 * var_1 ) - 1.0 / ( n + 1 );
217 sd_1 = std::sqrt ( var_1 );
219 r_1 = sd_1 / mean_1 / std::sqrt (
G4double(n + 1) );
221 shift_1 = shift_1 / ( 2 * var_1 * ( n + 1 ) );
223 fom_1 = 1 / ( r * r ) / ( cpu_time.back() + spend_time_of_largest );
225 if ( nonzero_histories.size() < 500 )
227 G4cout <<
"Number of non zero history too small to calculate SLOPE" <<
G4endl;
231 G4int i =
int ( nonzero_histories.size() );
235 while (
int( largest_scores.size() ) > j )
237 largest_scores.pop_back();
239 calc_slope_fit( largest_scores );
242 calc_grid_point_of_history();
247 statsAreUpdated =
true;
252 void G4ConvergenceTester::calc_grid_point_of_history()
261 for ( i = 1 ; i <= noBinOfHistory ; i++ )
263 history_grid [ i-1 ] =
int ( n / (
double( noBinOfHistory ) ) * i - 0.1 );
271 void G4ConvergenceTester::calc_stat_history()
276 for ( i = 1 ; i <= noBinOfHistory ; i++ )
279 G4int ith = history_grid [ i-1 ];
281 G4int nonzero_till_ith = 0;
284 std::map< G4int , G4double >::iterator it;
286 for ( it = nonzero_histories.begin() ; it !=nonzero_histories.end() ; it++ )
288 if ( it->first <= ith )
296 mean_till_ith = mean_till_ith / ( ith+1 );
297 mean_history [ i-1 ] = mean_till_ith;
304 for ( it = nonzero_histories.begin() ; it !=nonzero_histories.end() ; it++ )
306 if ( it->first <= ith )
309 sum_x2_till_ith += xi * xi;
310 var_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith );
311 shift_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith );
312 vov_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith );
316 var_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith;
318 vov_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith * mean_till_ith * mean_till_ith ;
319 vov_till_ith = vov_till_ith / ( var_till_ith * var_till_ith ) - 1.0 / (ith+1);
320 vov_history [ i-1 ] = vov_till_ith;
322 var_till_ith = var_till_ith / ( ith+1 - 1 );
323 var_history [ i-1 ] = var_till_ith;
324 sd_history [ i-1 ] = std::sqrt( var_till_ith );
325 r_history [ i-1 ] = std::sqrt( var_till_ith ) / mean_till_ith / std::sqrt ( 1.0*(ith+1) );
327 fom_history [ i-1 ] = 1 / ( r_history [ i-1 ] * r_history [ i-1 ] ) / cpu_time [ ith ];
329 shift_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith * mean_till_ith * ( -1 );
330 shift_till_ith = shift_till_ith / ( 2 * var_till_ith * (ith+1) );
331 shift_history [ i-1 ] = shift_till_ith;
333 e_history [ i-1 ] = 1.0*nonzero_till_ith / (ith+1);
334 r2eff_history [ i-1 ] = ( 1 - e_history [ i-1 ] ) / ( e_history [ i-1 ] * (ith+1) );
336 G4double sum_till_ith = mean_till_ith * (ith+1);
337 r2int_history [ i-1 ] = ( sum_x2_till_ith ) / ( sum_till_ith * sum_till_ith ) - 1 / ( e_history [ i-1 ] * (ith+1) );
349 if(!statsAreUpdated) { calStat(); }
351 out <<
"EFFICIENCY = " << efficiency <<
G4endl;
352 out <<
"MEAN = " << mean <<
G4endl;
353 out <<
"VAR = " << var <<
G4endl;
354 out <<
"SD = " << sd <<
G4endl;
355 out <<
"R = "<< r <<
G4endl;
356 out <<
"SHIFT = "<< shift <<
G4endl;
357 out <<
"VOV = "<< vov <<
G4endl;
358 out <<
"FOM = "<< fom <<
G4endl;
360 out <<
"THE LARGEST SCORE = " << largest <<
" and it happend at " << largest_score_happened <<
"th event" <<
G4endl;
361 out <<
"Affected Mean = " << mean_1 <<
" and its ratio to orignal is " << mean_1/mean <<
G4endl;
362 out <<
"Affected VAR = " << var_1 <<
" and its ratio to orignal is " << var_1/var <<
G4endl;
363 out <<
"Affected R = " << r_1 <<
" and its ratio to orignal is " << r_1/r <<
G4endl;
364 out <<
"Affected SHIFT = " << shift_1 <<
" and its ratio to orignal is " << shift_1/shift <<
G4endl;
365 out <<
"Affected FOM = " << fom_1 <<
" and its ratio to orignal is " << fom_1/fom <<
G4endl;
367 check_stat_history(out);
373 out <<
"SLOPE is large enough" <<
G4endl;
377 out <<
"SLOPE is not large enough" <<
G4endl;
380 out <<
"This result passes " << noPass <<
" / "<< noTotal <<
" Convergence Test." <<
G4endl;
387 out <<
"i/" << noBinOfHistory <<
" till_ith mean var sd r vov fom shift e r2eff r2int" <<
G4endl;
388 for (
G4int i = 1 ; i <= noBinOfHistory ; i++ )
391 << history_grid [ i-1 ] <<
" "
392 << mean_history [ i-1 ] <<
" "
393 << var_history [ i-1 ] <<
" "
394 << sd_history [ i-1 ] <<
" "
395 << r_history [ i-1 ] <<
" "
396 << vov_history [ i-1 ] <<
" "
397 << fom_history [ i-1 ] <<
" "
398 << shift_history [ i-1 ] <<
" "
399 << e_history [ i-1 ] <<
" "
400 << r2eff_history [ i-1 ] <<
" "
401 << r2int_history [ i-1 ] <<
" "
406 void G4ConvergenceTester::check_stat_history(std::ostream& out)
411 std::vector<G4double> first_ally;
412 std::vector<G4double> second_ally;
415 G4int N = mean_history.size() / 2;
421 first_ally.resize( N );
422 second_ally.resize( N );
426 for ( i = 0 ; i <
N ; i++ )
428 first_ally [ i ] = history_grid [ N + i ];
429 second_ally [ i ] = mean_history [ N + i ];
432 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
433 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
437 out <<
"MEAN distribution is RANDOM" <<
G4endl;
442 out <<
"MEAN distribution is not RANDOM" <<
G4endl;
448 for ( i = 0 ; i <
N ; i++ )
450 first_ally [ i ] = 1.0 / std::sqrt (
G4double(history_grid [ N + i ]) );
451 second_ally [ i ] = r_history [ N + i ];
454 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
455 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
459 out <<
"r follows 1/std::sqrt(N)" <<
G4endl;
464 out <<
"r does not follow 1/std::sqrt(N)" <<
G4endl;
467 if ( is_monotonically_decrease( second_ally ) ==
true )
469 out <<
"r is monotonically decrease " <<
G4endl;
473 out <<
"r is NOT monotonically decrease " <<
G4endl;
476 if ( r_history.back() < 0.1 )
478 out <<
"r is less than 0.1. r = " << r_history.back() <<
G4endl;
483 out <<
"r is NOT less than 0.1. r = " << r_history.back() <<
G4endl;
488 for ( i = 0 ; i <
N ; i++ )
490 first_ally [ i ] = 1.0 / history_grid [ N + i ];
491 second_ally [ i ] = vov_history [ N + i ];
494 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
495 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
499 out <<
"VOV follows 1/std::sqrt(N)" <<
G4endl;
504 out <<
"VOV does not follow 1/std::sqrt(N)" <<
G4endl;
507 if ( is_monotonically_decrease( second_ally ) ==
true )
509 out <<
"VOV is monotonically decrease " <<
G4endl;
513 out <<
"VOV is NOT monotonically decrease " <<
G4endl;
518 for ( i = 0 ; i <
N ; i++ )
520 first_ally [ i ] = history_grid [ N + i ];
521 second_ally [ i ] = fom_history [ N + i ];
524 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
525 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
529 out <<
"FOM distribution is RANDOM" <<
G4endl;
534 out <<
"FOM distribution is not RANDOM" <<
G4endl;
541 G4double G4ConvergenceTester::calc_Pearson_r (
G4int N , std::vector<G4double> first_ally , std::vector<G4double> second_ally )
547 for ( i = 0 ; i <
N ; i++ )
549 first_mean += first_ally [ i ];
550 second_mean += second_ally [ i ];
552 first_mean = first_mean /
N;
553 second_mean = second_mean /
N;
556 for ( i = 0 ; i <
N ; i++ )
558 a += ( first_ally [ i ] - first_mean ) * ( second_ally [ i ] - second_mean );
563 for ( i = 0 ; i <
N ; i++ )
565 b1 += ( first_ally [ i ] - first_mean ) * ( first_ally [ i ] - first_mean );
566 b2 += ( second_ally [ i ] - second_mean ) * ( second_ally [ i ] - second_mean );
569 G4double rds = a / std::sqrt ( b1 * b2 );
576 G4bool G4ConvergenceTester::is_monotonically_decrease ( std::vector<G4double> ally )
579 std::vector<G4double>::iterator it;
580 for ( it = ally.begin() ; it != ally.end() - 1 ; it++ )
582 if ( *it < *(it+1) )
return FALSE;
592 void G4ConvergenceTester::calc_slope_fit ( std::vector<G4double> )
596 G4double max = largest_scores.front();
597 G4int last =
int ( largest_scores.size() );
599 if ( largest_scores.back() != 0 )
601 min = largest_scores.back();
605 min = largest_scores[ last-1 ];
612 if ( max*0.99 < min )
619 std::vector < G4double > pdf_grid;
621 pdf_grid.resize( noBinOfPDF+1 );
623 pdf_grid[ noBinOfPDF ] = min;
624 G4double log10_max = std::log10( max );
625 G4double log10_min = std::log10( min );
626 G4double log10_delta = log10_max - log10_min;
627 for (
G4int i = 1 ; i < noBinOfPDF ; i++ )
629 pdf_grid[i] = std::pow ( 10.0 , log10_max - log10_delta/10.0*(i) );
633 std::vector < G4double > pdf;
634 pdf.resize( noBinOfPDF );
636 for (
G4int j=0 ; j < last ; j ++ )
638 for (
G4int i = 0 ; i < 11 ; i++ )
640 if ( largest_scores[j] >= pdf_grid[i+1] )
642 pdf[i] += 1.0 / ( pdf_grid[i] - pdf_grid[i+1] ) / n;
649 f_xi.resize( noBinOfPDF );
650 f_yi.resize( noBinOfPDF );
651 for (
G4int i = 0 ; i < noBinOfPDF ; i++ )
654 f_xi[i] = (pdf_grid[i]+pdf_grid[i+1])/2;
682 G4double G4ConvergenceTester::slope_fitting_function ( std::vector< G4double >
x )
690 return 3.402823466e+38;
694 return 3.402823466e+38;
701 for ( i = 0 ; i <
int ( f_yi.size() ) ; i++ )
704 if ( ( 1 + k * f_xi [ i ] / a ) < 0 )
710 y += ( f_yi [ i ] - 1/a*std::pow ( 1 + k * f_xi [ i ] / a , - 1/k - 1 ) ) * ( f_yi [ i ] - 1/a*std::pow ( 1 + k * f_xi [ i ] / a , - 1/k - 1 ) );