private double computeStatistic()

in commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/KolmogorovSmirnovTest.java [702:742]


    private double computeStatistic(double[] x, DoubleUnaryOperator cdf, int[] sign) {
        final int n = checkArrayLength(x);
        final double nd = n;
        final double[] sx = sort(x.clone(), "Sample");
        // Note: ties in the data do not matter as we compare the empirical CDF
        // immediately before the value (i/n) and at the value (i+1)/n. For ties
        // of length m this would be (i-m+1)/n and (i+1)/n and the result is the same.
        double d = 0;
        if (alternative == AlternativeHypothesis.GREATER_THAN) {
            // Compute D+
            for (int i = 0; i < n; i++) {
                final double yi = cdf.applyAsDouble(sx[i]);
                final double dp = (i + 1) / nd - yi;
                d = dp > d ? dp : d;
            }
            sign[0] = 1;
        } else if (alternative == AlternativeHypothesis.LESS_THAN) {
            // Compute D-
            for (int i = 0; i < n; i++) {
                final double yi = cdf.applyAsDouble(sx[i]);
                final double dn = yi - i / nd;
                d = dn > d ? dn : d;
            }
            sign[0] = -1;
        } else {
            // Two sided.
            // Compute both (as unsigned) and return the sign indicating the largest result.
            double plus = 0;
            double minus = 0;
            for (int i = 0; i < n; i++) {
                final double yi = cdf.applyAsDouble(sx[i]);
                final double dn = yi - i / nd;
                final double dp = (i + 1) / nd - yi;
                minus = dn > minus ? dn : minus;
                plus = dp > plus ? dp : plus;
            }
            sign[0] = Double.compare(plus, minus);
            d = Math.max(plus, minus);
        }
        return d;
    }