static double twoSampleExactP()

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


    static double twoSampleExactP(long dnm, int n, int m, int gcd, boolean strict, boolean twoSided) {
        // Create the statistic in [0, lcm]
        // For strict inequality D > d the result is the same if we compute for D >= (d+1)
        final long d = dnm / gcd + (strict ? 1 : 0);

        // P-value methods compute for d <= lcm (least common multiple)
        final long lcm = (long) n * (m / gcd);
        if (d > lcm) {
            return 0;
        }

        // Note: Some methods require m >= n, others n >= m
        final int a = Math.min(n, m);
        final int b = Math.max(n, m);

        if (twoSided) {
            // Any two-sided statistic dnm cannot be less than min(n, m) in the absence of ties.
            if (d * gcd <= a) {
                return 1;
            }
            // Here d in [2, lcm]
            if (n == m) {
                return twoSampleTwoSidedPOutsideSquare(d, n);
            }
            return twoSampleTwoSidedPStabilizedInner(d, b, a, gcd);
        }
        // Any one-sided statistic cannot be less than 0
        if (d <= 0) {
            return 1;
        }
        // Here d in [1, lcm]
        if (n == m) {
            return twoSampleOneSidedPOutsideSquare(d, n);
        }
        return twoSampleOneSidedPOutside(d, a, b, gcd);
    }