private double estimateP()

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


    private double estimateP(double[] x, double[] y, long dnm) {
        if (rng == null) {
            throw new IllegalStateException("No source of randomness");
        }

        // Test if the random statistic is greater (strict), or greater or equal to d
        final long d = strictInequality ? dnm : dnm - 1;

        final long plus;
        final long minus;
        if (alternative == AlternativeHypothesis.GREATER_THAN) {
            plus = d;
            minus = Long.MIN_VALUE;
        } else if (alternative == AlternativeHypothesis.LESS_THAN) {
            plus = Long.MAX_VALUE;
            minus = -d;
        } else {
            // two-sided
            plus = d;
            minus = -d;
        }

        // Test dnm=0. This occurs for example when x == y.
        if (0 < minus || 0 > plus) {
            // Edge case where all possible d will be outside the inclusive bounds
            return 1;
        }

        // Sample randomly with replacement from the combined distribution.
        final DoubleSupplier gen = createSampler(x, y, rng);
        int count = 0;
        for (int i = iterations; i > 0; i--) {
            for (int j = 0; j < x.length; j++) {
                x[j] = gen.getAsDouble();
            }
            for (int j = 0; j < y.length; j++) {
                y[j] = gen.getAsDouble();
            }
            if (testIntegralKolmogorovSmirnovStatistic(x, y, plus, minus)) {
                count++;
            }
        }
        return count / (double) iterations;
    }