in commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/KolmogorovSmirnovTest.java [906:961]
private static boolean testIntegralKolmogorovSmirnovStatistic(double[] x, double[] y, long plus, long minus) {
// Sort the sample arrays
Arrays.sort(x);
Arrays.sort(y);
final int n = x.length;
final int m = y.length;
// CDFs range from 0 to 1 using increments of 1/n and 1/m for x and y respectively.
// Scale by n*m to use increments of m and n for x and y.
// Find the any difference that exceeds the specified bounds.
int i = 0;
int j = 0;
long d = 0;
do {
// No NaNs so compare using < and >
if (x[i] < y[j]) {
final double z = x[i];
do {
i++;
d += m;
} while (i < n && x[i] == z);
if (d > plus) {
return true;
}
} else if (x[i] > y[j]) {
final double z = y[j];
do {
j++;
d -= n;
} while (j < m && y[j] == z);
if (d < minus) {
return true;
}
} else {
// Traverse to the end of the tied section for d.
final double z = x[i];
do {
i++;
d += m;
} while (i < n && x[i] == z);
do {
j++;
d -= n;
} while (j < m && y[j] == z);
// End of tied section
if (d > plus || d < minus) {
return true;
}
}
} while (i < n && j < m);
// Note: Here d requires returning to zero. This is applicable to the one-sided
// cases since d may have always been above zero (favours D+) or always below zero
// (favours D-). This is ignored as the method is not called when dnm=0 is
// outside the inclusive bounds.
return false;
}