47 :
name(theName),
n(0), sum(0.), mean(0.),
var(0.), sd(0.),
r(0.), efficiency(0.),
48 r2eff(0.), r2int(0.), shift(0.), vov(0.), fom(0.), largest(0.),
49 largest_score_happened(0), mean_1(0.), var_1(0.), sd_1(0.), r_1(0.),
50 shift_1(0.), vov_1(0.), fom_1(0.), noBinOfHistory(16), slope(0.),
51 noBinOfPDF(10), minimizer(0), noPass(0), noTotal(8), statsAreUpdated(true)
53 nonzero_histories.clear();
54 largest_scores.clear();
55 largest_scores.push_back( 0.0 );
57 history_grid.resize( noBinOfHistory , 0 );
58 mean_history.resize( noBinOfHistory , 0.0 );
59 var_history.resize( noBinOfHistory , 0.0 );
60 sd_history.resize( noBinOfHistory , 0.0 );
61 r_history.resize( noBinOfHistory , 0.0 );
62 vov_history.resize( noBinOfHistory , 0.0 );
63 fom_history.resize( noBinOfHistory , 0.0 );
64 shift_history.resize( noBinOfHistory , 0.0 );
65 e_history.resize( noBinOfHistory , 0.0 );
66 r2eff_history.resize( noBinOfHistory , 0.0 );
67 r2int_history.resize( noBinOfHistory , 0.0 );
72 cpu_time.push_back( 0.0 );
97 nonzero_histories.insert( std::pair< G4int , G4double > ( n , x ) );
98 if ( x > largest_scores.back() )
101 std::vector< G4double >::iterator it;
102 for ( it = largest_scores.begin() ; it != largest_scores.end() ; it++ )
106 largest_scores.insert( it , x );
111 if ( largest_scores.size() > 201 )
113 largest_scores.pop_back();
121 statsAreUpdated =
false;
128 void G4ConvergenceTester::calStat()
132 efficiency = double( nonzero_histories.size() ) / n;
142 std::map< G4int , G4double >::iterator it;
143 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
147 var += ( xi - mean ) * ( xi - mean );
148 shift += ( xi - mean ) * ( xi - mean ) * ( xi - mean );
149 vov += ( xi - mean ) * ( xi - mean ) * ( xi - mean ) * ( xi - mean );
152 var += ( n - nonzero_histories.size() ) * mean * mean;
153 shift += ( n - nonzero_histories.size() ) * mean * mean * mean * ( -1 );
154 vov += ( n - nonzero_histories.size() ) * mean * mean * mean * mean;
156 vov = vov / ( var * var ) - 1.0 / n;
160 sd = std::sqrt ( var );
162 r = sd / mean / std::sqrt (
G4double(n) );
164 r2eff = ( 1 - efficiency ) / ( efficiency * n );
165 r2int = sum_x2 / ( sum * sum ) - 1 / ( efficiency * n );
167 shift = shift / ( 2 * var * n );
169 fom = 1 / (r*r) / cpu_time.back();
174 largest_score_happened = 0;
175 G4double spend_time_of_largest = 0.0;
176 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
178 if ( std::abs ( it->second ) > largest )
180 largest = it->second;
181 largest_score_happened = it->first;
182 spend_time_of_largest = cpu_time [ it->first+1 ] - cpu_time [ it->first ];
196 mean_1 = ( sum + largest ) / ( n + 1 );
198 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
201 var_1 += ( xi - mean_1 ) * ( xi - mean_1 );
202 shift_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
203 vov_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
206 var_1 += ( xi - mean_1 ) * ( xi - mean_1 );
207 shift_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
208 vov_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
210 var_1 += ( n - nonzero_histories.size() ) * mean_1 * mean_1;
211 shift_1 += ( n - nonzero_histories.size() ) * mean_1 * mean_1 * mean_1 * ( -1 );
212 vov_1 += ( n - nonzero_histories.size() ) * mean_1 * mean_1 * mean_1 * mean_1;
214 vov_1 = vov_1 / ( var_1 * var_1 ) - 1.0 / ( n + 1 );
218 sd_1 = std::sqrt ( var_1 );
220 r_1 = sd_1 / mean_1 / std::sqrt (
G4double(n + 1) );
222 shift_1 = shift_1 / ( 2 * var_1 * ( n + 1 ) );
224 fom_1 = 1 / ( r * r ) / ( cpu_time.back() + spend_time_of_largest );
226 if ( nonzero_histories.size() < 500 )
228 G4cout <<
"Number of non zero history too small to calculate SLOPE" <<
G4endl;
232 G4int i =
int ( nonzero_histories.size() );
236 while (
int( largest_scores.size() ) > j )
238 largest_scores.pop_back();
240 calc_slope_fit( largest_scores );
243 calc_grid_point_of_history();
248 statsAreUpdated =
true;
253 void G4ConvergenceTester::calc_grid_point_of_history()
262 for ( i = 1 ; i <= noBinOfHistory ; i++ )
264 history_grid [ i-1 ] =
int ( n / (
double( noBinOfHistory ) ) * i - 0.1 );
272 void G4ConvergenceTester::calc_stat_history()
277 for ( i = 1 ; i <= noBinOfHistory ; i++ )
280 G4int ith = history_grid [ i-1 ];
282 G4int nonzero_till_ith = 0;
285 std::map< G4int , G4double >::iterator it;
287 for ( it = nonzero_histories.begin() ; it !=nonzero_histories.end() ; it++ )
289 if ( it->first <= ith )
297 mean_till_ith = mean_till_ith / ( ith+1 );
298 mean_history [ i-1 ] = mean_till_ith;
305 for ( it = nonzero_histories.begin() ; it !=nonzero_histories.end() ; it++ )
307 if ( it->first <= ith )
310 sum_x2_till_ith += xi * xi;
311 var_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith );
312 shift_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith );
313 vov_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith );
317 var_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith;
319 vov_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith * mean_till_ith * mean_till_ith ;
320 vov_till_ith = vov_till_ith / ( var_till_ith * var_till_ith ) - 1.0 / (ith+1);
321 vov_history [ i-1 ] = vov_till_ith;
323 var_till_ith = var_till_ith / ( ith+1 - 1 );
324 var_history [ i-1 ] = var_till_ith;
325 sd_history [ i-1 ] = std::sqrt( var_till_ith );
326 r_history [ i-1 ] = std::sqrt( var_till_ith ) / mean_till_ith / std::sqrt ( 1.0*(ith+1) );
328 fom_history [ i-1 ] = 1 / ( r_history [ i-1 ] * r_history [ i-1 ] ) / cpu_time [ ith ];
330 shift_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith * mean_till_ith * ( -1 );
331 shift_till_ith = shift_till_ith / ( 2 * var_till_ith * (ith+1) );
332 shift_history [ i-1 ] = shift_till_ith;
334 e_history [ i-1 ] = 1.0*nonzero_till_ith / (ith+1);
335 r2eff_history [ i-1 ] = ( 1 - e_history [ i-1 ] ) / ( e_history [ i-1 ] * (ith+1) );
337 G4double sum_till_ith = mean_till_ith * (ith+1);
338 r2int_history [ i-1 ] = ( sum_x2_till_ith ) / ( sum_till_ith * sum_till_ith ) - 1 / ( e_history [ i-1 ] * (ith+1) );
350 if(!statsAreUpdated) { calStat(); }
353 out <<
"G4ConvergenceTester Output Result of " <<
name <<
G4endl;
354 out << std::setw(20) <<
"EFFICIENCY = " << std::setw(13) << efficiency <<
G4endl;
355 out << std::setw(20) <<
"MEAN = " << std::setw(13) << mean <<
G4endl;
356 out << std::setw(20) <<
"VAR = " << std::setw(13) << var <<
G4endl;
357 out << std::setw(20) <<
"SD = " << std::setw(13) << sd <<
G4endl;
358 out << std::setw(20) <<
"R = " << std::setw(13) << r <<
G4endl;
359 out << std::setw(20) <<
"SHIFT = "<< std::setw(13) << shift <<
G4endl;
360 out << std::setw(20) <<
"VOV = "<< std::setw(13) << vov <<
G4endl;
361 out << std::setw(20) <<
"FOM = "<< std::setw(13) << fom <<
G4endl;
363 out << std::setw(20) <<
"THE LARGEST SCORE = " << std::setw(13) << largest <<
" and it happend at " << largest_score_happened <<
"th event" <<
G4endl;
364 out << std::setw(20) <<
"Affected Mean = " << std::setw(13) << mean_1 <<
" and its ratio to orignal is " << mean_1/mean <<
G4endl;
365 out << std::setw(20) <<
"Affected VAR = " << std::setw(13) << var_1 <<
" and its ratio to orignal is " << var_1/var <<
G4endl;
366 out << std::setw(20) <<
"Affected R = " << std::setw(13) << r_1 <<
" and its ratio to orignal is " << r_1/r <<
G4endl;
367 out << std::setw(20) <<
"Affected SHIFT = " << std::setw(13) << shift_1 <<
" and its ratio to orignal is " << shift_1/shift <<
G4endl;
368 out << std::setw(20) <<
"Affected FOM = " << std::setw(13) << fom_1 <<
" and its ratio to orignal is " << fom_1/fom <<
G4endl;
370 check_stat_history(out);
376 out <<
"SLOPE is large enough" <<
G4endl;
380 out <<
"SLOPE is not large enough" <<
G4endl;
383 out <<
"This result passes " << noPass <<
" / "<< noTotal <<
" Convergence Test." <<
G4endl;
391 out <<
"G4ConvergenceTester Output History of " <<
name <<
G4endl;
392 out <<
"i/" << noBinOfHistory <<
" till_ith mean"
393 << std::setw(13) <<
"var"
394 << std::setw(13) <<
"sd"
395 << std::setw(13) <<
"r"
396 << std::setw(13) <<
"vov"
397 << std::setw(13) <<
"fom"
398 << std::setw(13) <<
"shift"
399 << std::setw(13) <<
"e"
400 << std::setw(13) <<
"r2eff"
401 << std::setw(13) <<
"r2int"
403 for (
G4int i = 1 ; i <= noBinOfHistory ; i++ )
405 out << std::setw( 4) << i <<
" "
406 << std::setw( 5) << history_grid [ i-1 ]
407 << std::setw(13) << mean_history [ i-1 ]
408 << std::setw(13) << var_history [ i-1 ]
409 << std::setw(13) << sd_history [ i-1 ]
410 << std::setw(13) << r_history [ i-1 ]
411 << std::setw(13) << vov_history [ i-1 ]
412 << std::setw(13) << fom_history [ i-1 ]
413 << std::setw(13) << shift_history [ i-1 ]
414 << std::setw(13) << e_history [ i-1 ]
415 << std::setw(13) << r2eff_history [ i-1 ]
416 << std::setw(13) << r2int_history [ i-1 ]
421 void G4ConvergenceTester::check_stat_history(std::ostream& out)
426 std::vector<G4double> first_ally;
427 std::vector<G4double> second_ally;
430 G4int N = mean_history.size() / 2;
436 first_ally.resize( N );
437 second_ally.resize( N );
441 for ( i = 0 ; i <
N ; i++ )
443 first_ally [ i ] = history_grid [ N + i ];
444 second_ally [ i ] = mean_history [ N + i ];
447 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
448 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
452 out <<
"MEAN distribution is RANDOM" <<
G4endl;
457 out <<
"MEAN distribution is not RANDOM" <<
G4endl;
463 for ( i = 0 ; i <
N ; i++ )
465 first_ally [ i ] = 1.0 / std::sqrt (
G4double(history_grid [ N + i ]) );
466 second_ally [ i ] = r_history [ N + i ];
469 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
470 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
474 out <<
"r follows 1/std::sqrt(N)" <<
G4endl;
479 out <<
"r does not follow 1/std::sqrt(N)" <<
G4endl;
482 if ( is_monotonically_decrease( second_ally ) ==
true )
484 out <<
"r is monotonically decrease " <<
G4endl;
488 out <<
"r is NOT monotonically decrease " <<
G4endl;
491 if ( r_history.back() < 0.1 )
493 out <<
"r is less than 0.1. r = " << r_history.back() <<
G4endl;
498 out <<
"r is NOT less than 0.1. r = " << r_history.back() <<
G4endl;
503 for ( i = 0 ; i <
N ; i++ )
505 first_ally [ i ] = 1.0 / history_grid [ N + i ];
506 second_ally [ i ] = vov_history [ N + i ];
509 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
510 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
514 out <<
"VOV follows 1/std::sqrt(N)" <<
G4endl;
519 out <<
"VOV does not follow 1/std::sqrt(N)" <<
G4endl;
522 if ( is_monotonically_decrease( second_ally ) ==
true )
524 out <<
"VOV is monotonically decrease " <<
G4endl;
528 out <<
"VOV is NOT monotonically decrease " <<
G4endl;
533 for ( i = 0 ; i <
N ; i++ )
535 first_ally [ i ] = history_grid [ N + i ];
536 second_ally [ i ] = fom_history [ N + i ];
539 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
540 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
544 out <<
"FOM distribution is RANDOM" <<
G4endl;
549 out <<
"FOM distribution is not RANDOM" <<
G4endl;
556 G4double G4ConvergenceTester::calc_Pearson_r (
G4int N , std::vector<G4double> first_ally , std::vector<G4double> second_ally )
562 for ( i = 0 ; i <
N ; i++ )
564 first_mean += first_ally [ i ];
565 second_mean += second_ally [ i ];
567 first_mean = first_mean /
N;
568 second_mean = second_mean /
N;
571 for ( i = 0 ; i <
N ; i++ )
573 a += ( first_ally [ i ] - first_mean ) * ( second_ally [ i ] - second_mean );
578 for ( i = 0 ; i <
N ; i++ )
580 b1 += ( first_ally [ i ] - first_mean ) * ( first_ally [ i ] - first_mean );
581 b2 += ( second_ally [ i ] - second_mean ) * ( second_ally [ i ] - second_mean );
584 G4double rds = a / std::sqrt ( b1 * b2 );
591 G4bool G4ConvergenceTester::is_monotonically_decrease ( std::vector<G4double> ally )
594 std::vector<G4double>::iterator it;
595 for ( it = ally.begin() ; it != ally.end() - 1 ; it++ )
597 if ( *it < *(it+1) )
return FALSE;
607 void G4ConvergenceTester::calc_slope_fit ( std::vector<G4double> )
612 G4int last =
int ( largest_scores.size() );
614 if ( largest_scores.back() != 0 )
616 min = largest_scores.back();
620 min = largest_scores[ last-1 ];
627 if ( max*0.99 < min )
634 std::vector < G4double > pdf_grid;
636 pdf_grid.resize( noBinOfPDF+1 );
638 pdf_grid[ noBinOfPDF ] =
min;
639 G4double log10_max = std::log10( max );
640 G4double log10_min = std::log10( min );
641 G4double log10_delta = log10_max - log10_min;
642 for (
G4int i = 1 ; i < noBinOfPDF ; i++ )
644 pdf_grid[i] = std::pow ( 10.0 , log10_max - log10_delta/10.0*(i) );
648 std::vector < G4double > pdf;
649 pdf.resize( noBinOfPDF );
651 for (
G4int j=0 ; j < last ; j ++ )
653 for (
G4int i = 0 ; i < 11 ; i++ )
655 if ( largest_scores[j] >= pdf_grid[i+1] )
657 pdf[i] += 1.0 / ( pdf_grid[i] - pdf_grid[i+1] ) / n;
664 f_xi.resize( noBinOfPDF );
665 f_yi.resize( noBinOfPDF );
666 for (
G4int i = 0 ; i < noBinOfPDF ; i++ )
669 f_xi[i] = (pdf_grid[i]+pdf_grid[i+1])/2;
697 G4double G4ConvergenceTester::slope_fitting_function ( std::vector< G4double >
x )
705 return 3.402823466e+38;
709 return 3.402823466e+38;
716 for ( i = 0 ; i <
int ( f_yi.size() ) ; i++ )
719 if ( ( 1 + k * f_xi [ i ] / a ) < 0 )
725 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 ) );
G4double GetSystemElapsed() const
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
REAL *8 function var(A, B, C, D)
G4GLOB_DLL std::ostream G4cout
std::vector< G4double > GetMinimumPoint()
G4double GetUserElapsed() const
void ShowHistory(std::ostream &out=G4cout)
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
void ShowResult(std::ostream &out=G4cout)
G4ConvergenceTester(G4String theName="NONAME")