public double statistic()

in commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/ChiSquareTest.java [219:257]


    public double statistic(long[] observed1, long[] observed2) {
        Arguments.checkValuesRequiredSize(observed1.length, 2);
        Arguments.checkValuesSizeMatch(observed1.length, observed2.length);
        Arguments.checkNonNegative(observed1);
        Arguments.checkNonNegative(observed2);

        // Compute and compare count sums
        long colSum1 = 0;
        long colSum2 = 0;
        for (int i = 0; i < observed1.length; i++) {
            final long obs1 = observed1[i];
            final long obs2 = observed2[i];
            checkNonZero(obs1 | obs2, ROW, i);
            colSum1 += obs1;
            colSum2 += obs2;
        }
        // Create the same exception message as chiSquare(long[][])
        checkNonZero(colSum1, COLUMN, 0);
        checkNonZero(colSum2, COLUMN, 1);

        // Compare and compute weight only if different
        final boolean unequalCounts = colSum1 != colSum2;
        final double weight = unequalCounts ?
            Math.sqrt((double) colSum1 / (double) colSum2) : 1;
        // Compute chi-square
        // This exploits an algebraic rearrangement of the generic n*m contingency table case
        // for a single sum squared addition per row.
        double chi2 = 0;
        for (int i = 0; i < observed1.length; i++) {
            final double obs1 = observed1[i];
            final double obs2 = observed2[i];
            // apply weights
            final double d = unequalCounts ?
                    obs1 / weight - obs2 * weight :
                    obs1 - obs2;
            chi2 += (d * d) / (obs1 + obs2);
        }
        return chi2;
    }