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)
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();
125void G4ConvergenceTester::calStat()
129 efficiency = double( nonzero_histories.size() ) / n;
139 std::map< G4int , G4double >::iterator it;
140 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
144 var += ( xi - mean ) * ( xi - mean );
145 shift += ( xi - mean ) * ( xi - mean ) * ( xi - mean );
146 vov += ( xi - mean ) * ( xi - mean ) * ( xi - mean ) * ( xi - mean );
149 var += ( n - nonzero_histories.size() ) * mean * mean;
150 shift += ( n - nonzero_histories.size() ) * mean * mean * mean * ( -1 );
151 vov += ( n - nonzero_histories.size() ) * mean * mean * mean * mean;
153 vov = vov / ( var * var ) - 1.0 / n;
157 sd = std::sqrt ( var );
159 r = sd / mean / std::sqrt (
G4double(n) );
161 r2eff = ( 1 - efficiency ) / ( efficiency * n );
162 r2int = sum_x2 / ( sum * sum ) - 1 / ( efficiency * n );
164 shift = shift / ( 2 * var *
n );
166 fom = 1 / (r*r) / cpu_time.back();
171 largest_score_happened = 0;
172 G4double spend_time_of_largest = 0.0;
173 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
175 if ( std::abs ( it->second ) > largest )
177 largest = it->second;
178 largest_score_happened = it->first;
179 spend_time_of_largest = cpu_time [ it->first+1 ] - cpu_time [ it->first ];
193 mean_1 = ( sum + largest ) / ( n + 1 );
195 for ( it = nonzero_histories.begin() ; it != nonzero_histories.end() ; it++ )
198 var_1 += ( xi - mean_1 ) * ( xi - mean_1 );
199 shift_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
200 vov_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
203 var_1 += ( xi - mean_1 ) * ( xi - mean_1 );
204 shift_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
205 vov_1 += ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 ) * ( xi - mean_1 );
207 var_1 += (
n - nonzero_histories.size() ) * mean_1 * mean_1;
208 shift_1 += (
n - nonzero_histories.size() ) * mean_1 * mean_1 * mean_1 * ( -1 );
209 vov_1 += (
n - nonzero_histories.size() ) * mean_1 * mean_1 * mean_1 * mean_1;
211 vov_1 = vov_1 / ( var_1 * var_1 ) - 1.0 / ( n + 1 );
215 sd_1 = std::sqrt ( var_1 );
217 r_1 = sd_1 / mean_1 / std::sqrt (
G4double(n + 1) );
219 shift_1 = shift_1 / ( 2 * var_1 * (
n + 1 ) );
221 fom_1 = 1 / ( r * r ) / ( cpu_time.back() + spend_time_of_largest );
223 if ( nonzero_histories.size() < 500 )
225 G4cout <<
"Number of non zero history too small to calcuarte SLOPE" <<
G4endl;
229 G4int i = int ( nonzero_histories.size() );
232 G4int j = int ( i * 0.05 );
233 while (
int( largest_scores.size() ) > j )
235 largest_scores.pop_back();
237 calc_slope_fit( largest_scores );
240 calc_grid_point_of_history();
246void G4ConvergenceTester::calc_grid_point_of_history()
255 for ( i = 1 ; i <= noBinOfHistory ; i++ )
257 history_grid [ i-1 ] = int ( n / (
double( noBinOfHistory ) ) * i - 0.1 );
265void G4ConvergenceTester::calc_stat_history()
270 for ( i = 1 ; i <= noBinOfHistory ; i++ )
273 G4int ith = history_grid [ i-1 ];
275 G4int nonzero_till_ith = 0;
278 std::map< G4int , G4double >::iterator it;
280 for ( it = nonzero_histories.begin() ; it !=nonzero_histories.end() ; it++ )
282 if ( it->first <= ith )
290 mean_till_ith = mean_till_ith / ( ith+1 );
291 mean_history [ i-1 ] = mean_till_ith;
298 for ( it = nonzero_histories.begin() ; it !=nonzero_histories.end() ; it++ )
300 if ( it->first <= ith )
303 sum_x2_till_ith += xi * xi;
304 var_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith );
305 shift_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith );
306 vov_till_ith += ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith ) * ( xi - mean_till_ith );
310 var_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith;
312 vov_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith * mean_till_ith * mean_till_ith ;
313 vov_till_ith = vov_till_ith / ( var_till_ith * var_till_ith ) - 1.0 / (ith+1);
314 vov_history [ i-1 ] = vov_till_ith;
316 var_till_ith = var_till_ith / ( ith+1 - 1 );
317 var_history [ i-1 ] = var_till_ith;
318 sd_history [ i-1 ] = std::sqrt( var_till_ith );
319 r_history [ i-1 ] = std::sqrt( var_till_ith ) / mean_till_ith / std::sqrt ( 1.0*(ith+1) );
321 fom_history [ i-1 ] = 1 / ( r_history [ i-1 ] * r_history [ i-1 ] ) / cpu_time [ ith ];
323 shift_till_ith += ( (ith+1) - nonzero_till_ith ) * mean_till_ith * mean_till_ith * mean_till_ith * ( -1 );
324 shift_till_ith = shift_till_ith / ( 2 * var_till_ith * (ith+1) );
325 shift_history [ i-1 ] = shift_till_ith;
327 e_history [ i-1 ] = 1.0*nonzero_till_ith / (ith+1);
328 r2eff_history [ i-1 ] = ( 1 - e_history [ i-1 ] ) / ( e_history [ i-1 ] * (ith+1) );
330 G4double sum_till_ith = mean_till_ith * (ith+1);
331 r2int_history [ i-1 ] = ( sum_x2_till_ith ) / ( sum_till_ith * sum_till_ith ) - 1 / ( e_history [ i-1 ] * (ith+1) );
352 G4cout <<
"THE LARGEST SCORE = " << largest <<
" and it happend at " << largest_score_happened <<
"th event" <<
G4endl;
353 G4cout <<
"Affected Mean = " << mean_1 <<
" and its ratio to orignal is " << mean_1/mean <<
G4endl;
354 G4cout <<
"Affected VAR = " << var_1 <<
" and its ratio to orignal is " << var_1/var <<
G4endl;
355 G4cout <<
"Affected R = " << r_1 <<
" and its ratio to orignal is " << r_1/r <<
G4endl;
356 G4cout <<
"Affected SHIFT = " << shift_1 <<
" and its ratio to orignal is " << shift_1/shift <<
G4endl;
357 G4cout <<
"Affected FOM = " << fom_1 <<
" and its ratio to orignal is " << fom_1/fom <<
G4endl;
359 check_stat_history();
372 G4cout <<
"This result passes " << noPass <<
" / "<< noTotal <<
" Convergence Test." <<
G4endl;
379 G4cout <<
"i/" << noBinOfHistory <<
" till_ith mean var sd r vov fom shift e r2eff r2int" <<
G4endl;
380 for (
G4int i = 1 ; i <= noBinOfHistory ; i++ )
383 << history_grid [ i-1 ] <<
" "
384 << mean_history [ i-1 ] <<
" "
385 << var_history [ i-1 ] <<
" "
386 << sd_history [ i-1 ] <<
" "
387 << r_history [ i-1 ] <<
" "
388 << vov_history [ i-1 ] <<
" "
389 << fom_history [ i-1 ] <<
" "
390 << shift_history [ i-1 ] <<
" "
391 << e_history [ i-1 ] <<
" "
392 << r2eff_history [ i-1 ] <<
" "
393 << r2int_history [ i-1 ] <<
" "
398void G4ConvergenceTester::check_stat_history()
403 std::vector<G4double> first_ally;
404 std::vector<G4double> second_ally;
407 G4int N = mean_history.size() / 2;
413 first_ally.resize( N );
414 second_ally.resize( N );
418 for ( i = 0 ; i < N ; i++ )
420 first_ally [ i ] = history_grid [ N + i ];
421 second_ally [ i ] = mean_history [ N + i ];
424 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
425 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
440 for ( i = 0 ; i < N ; i++ )
442 first_ally [ i ] = 1.0 / std::sqrt (
G4double(history_grid [ N + i ]) );
443 second_ally [ i ] = r_history [ N + i ];
446 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
447 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
459 if ( is_monotonically_decrease( second_ally ) ==
true )
468 if ( r_history.back() < 0.1 )
470 G4cout <<
"r is less than 0.1. r = " << r_history.back() <<
G4endl;
475 G4cout <<
"r is NOT less than 0.1. r = " << r_history.back() <<
G4endl;
480 for ( i = 0 ; i < N ; i++ )
482 first_ally [ i ] = 1.0 / history_grid [ N + i ];
483 second_ally [ i ] = vov_history [ N + i ];
486 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
487 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
496 G4cout <<
"VOV does not follow 1/std::sqrt(N)" <<
G4endl;
499 if ( is_monotonically_decrease( second_ally ) ==
true )
505 G4cout <<
"VOV is NOT monotonically decrease " <<
G4endl;
510 for ( i = 0 ; i < N ; i++ )
512 first_ally [ i ] = history_grid [ N + i ];
513 second_ally [ i ] = fom_history [ N + i ];
516 pearson_r = calc_Pearson_r ( N , first_ally , second_ally );
517 t = pearson_r * std::sqrt ( ( N - 2 ) / ( 1 - pearson_r * pearson_r ) );
533G4double G4ConvergenceTester::calc_Pearson_r (
G4int N , std::vector<G4double> first_ally , std::vector<G4double> second_ally )
539 for ( i = 0 ; i < N ; i++ )
541 first_mean += first_ally [ i ];
542 second_mean += second_ally [ i ];
544 first_mean = first_mean / N;
545 second_mean = second_mean / N;
548 for ( i = 0 ; i < N ; i++ )
550 a += ( first_ally [ i ] - first_mean ) * ( second_ally [ i ] - second_mean );
555 for ( i = 0 ; i < N ; i++ )
557 b1 += ( first_ally [ i ] - first_mean ) * ( first_ally [ i ] - first_mean );
558 b2 += ( second_ally [ i ] - second_mean ) * ( second_ally [ i ] - second_mean );
561 G4double rds = a / std::sqrt ( b1 * b2 );
568G4bool G4ConvergenceTester::is_monotonically_decrease ( std::vector<G4double> ally )
571 std::vector<G4double>::iterator it;
572 for ( it = ally.begin() ; it != ally.end() - 1 ; it++ )
574 if ( *it < *(it+1) )
return FALSE;
584void G4ConvergenceTester::calc_slope_fit ( std::vector<G4double> )
588 G4double max = largest_scores.front();
589 G4int last = int ( largest_scores.size() );
591 if ( largest_scores.back() != 0 )
593 min = largest_scores.back();
597 min = largest_scores[ last-1 ];
604 if ( max*0.99 < min )
611 std::vector < G4double > pdf_grid;
613 pdf_grid.resize( noBinOfPDF+1 );
615 pdf_grid[ noBinOfPDF ] = min;
616 G4double log10_max = std::log10( max );
617 G4double log10_min = std::log10( min );
618 G4double log10_delta = log10_max - log10_min;
619 for (
G4int i = 1 ; i < noBinOfPDF ; i++ )
621 pdf_grid[i] = std::pow ( 10.0 , log10_max - log10_delta/10.0*(i) );
625 std::vector < G4double > pdf;
626 pdf.resize( noBinOfPDF );
628 for (
G4int j=0 ; j < last ; j ++ )
630 for (
G4int i = 0 ; i < 11 ; i++ )
632 if ( largest_scores[j] >= pdf_grid[i+1] )
634 pdf[i] += 1.0 / ( pdf_grid[i] - pdf_grid[i+1] ) / n;
641 f_xi.resize( noBinOfPDF );
642 f_yi.resize( noBinOfPDF );
643 for (
G4int i = 0 ; i < noBinOfPDF ; i++ )
646 f_xi[i] = (pdf_grid[i]+pdf_grid[i+1])/2;
674G4double G4ConvergenceTester::slope_fitting_function ( std::vector< G4double > x )
682 return 3.402823466e+38;
686 return 3.402823466e+38;
693 for ( i = 0 ; i < int ( f_yi.size() ) ; i++ )
696 if ( ( 1 + k * f_xi [ i ] / a ) < 0 )
702 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 ) );
G4DLLIMPORT std::ostream G4cout
std::vector< G4double > GetMinimumPoint()
G4double GetSystemElapsed() const
G4double GetUserElapsed() const