Example usage for org.apache.commons.math3.stat.inference ChiSquareTest chiSquare

List of usage examples for org.apache.commons.math3.stat.inference ChiSquareTest chiSquare

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.inference ChiSquareTest chiSquare.

Prototype

public double chiSquare(final double[] expected, final long[] observed)
        throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException 

Source Link

Document

Computes the <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm"> Chi-Square statistic</a> comparing <code>observed</code> and <code>expected</code> frequency counts.

Usage

From source file:info.rmarcus.birkhoffvonneumann.ChiSquaredTest.java

public static void testMethod(SamplingAlgorithm samp, int n, int samples) throws BVNException {
    double[][] bistoc = MatrixUtils.uniformBistoc(n);

    long[] classes = new long[factorial(bistoc.length)];

    BVNDecomposer bvn = new BVNDecomposer();
    bvn.setSamplingAlgorithm(samp);/*from ww  w .j a v a 2  s  .c o m*/
    Random r = new Random(42);

    for (int i = 0; i < samples; i++) {
        int[] p = CoeffAndMatrix.asFlatPerm(bvn.sample(r, bistoc));
        classes[inv(p)]++;
    }

    double[] expected = new double[classes.length];
    for (int i = 0; i < expected.length; i++)
        expected[i] = (double) classes.length / (double) samples;

    ChiSquareTest chiT = new ChiSquareTest();
    double testStat = chiT.chiSquare(expected, classes);

    ChiSquaredDistribution chi = new ChiSquaredDistribution(classes.length - 1);
    System.out.println(Arrays.toString(classes));
    System.out.println(chi.cumulativeProbability(testStat) + "\t" + testStat);

}

From source file:edu.brandeis.wisedb.scheduler.experiments.SkewDistributionExperiment.java

public static void calculateBurn(int samples) throws Exception {
    TightenableSLA sla = PercentSLA.nintyTenSLA();
    //TightenableSLA sla = new SimpleLatencyModelSLA(9 * 60 * 1000, 1);
    //TightenableSLA sla = PerQuerySLA.getLatencyTimesN(2.0);
    //TightenableSLA sla = new AverageLatencyModelSLA(7 * 60 * 1000, 1);
    QueryTimePredictor qtp = new QueryTimePredictor();

    File f = new File("distSkew.csv");
    if (f.exists())
        f.delete();//  w  w  w . j  a  va 2s  .  c  o m

    try (Trainer t = new Trainer("distSkew.csv", sla)) {
        t.train(2000, 12);
    }

    DTSearcher dt = new DTSearcher("distSkew.csv", qtp, sla);
    AStarGraphSearch astar = new AStarGraphSearch(new UnassignedQueryTimeHeuristic(qtp), sla, qtp);
    //FirstFitDecreasingGraphSearch astar = new FirstFitDecreasingGraphSearch(sla, qtp);

    ChiSquareTest cst = new ChiSquareTest();
    ChiSquaredDistribution cqd = new ChiSquaredDistribution(qtp.QUERY_TYPES.length - 1);
    double[] expceted = Arrays.stream(qtp.QUERY_TYPES).mapToDouble(i -> 20.0 / (qtp.QUERY_TYPES.length))
            .toArray();

    System.out.println("Chi\tDT\tOpt");

    for (int i = 0; i < samples; i++) {
        Set<ModelQuery> smp = ModelWorkloadGenerator.randomQueries(20);

        // reject samples that don't have at least one of each query type
        long repr = smp.stream().mapToInt(q -> q.getType()).distinct().count();
        if (repr != qtp.QUERY_TYPES.length) {
            i--;
            continue;
        }

        Map<Integer, List<ModelQuery>> groups = smp.stream().collect(Collectors.groupingBy(q -> q.getType()));

        long obs[] = Arrays.stream(qtp.QUERY_TYPES).mapToLong(v -> groups.get(v).size()).toArray();

        double chi = cst.chiSquare(expceted, obs);
        chi = cqd.cumulativeProbability(chi);

        Cost dtCost = dt.getCostForQueries(smp, sla);
        Cost optCost = astar.getCostForQueries(smp, sla);

        System.out.println(chi + "\t" + dtCost.getTotalCost() + "\t" + optCost.getTotalCost());
    }

}

From source file:lirmm.inria.fr.math.TestUtils.java

/**
 * Asserts the null hypothesis for a ChiSquare test.  Fails and dumps arguments and test
 * statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)%
 *
 * @param valueLabels labels for the values of the discrete distribution under test
 * @param expected expected counts//from  www .  ja  v  a2  s  . c  om
 * @param observed observed counts
 * @param alpha significance level of the test
 */
public static void assertChiSquareAccept(String[] valueLabels, double[] expected, long[] observed,
        double alpha) {
    ChiSquareTest chiSquareTest = new ChiSquareTest();

    // Fail if we can reject null hypothesis that distributions are the same
    if (chiSquareTest.chiSquareTest(expected, observed, alpha)) {
        StringBuilder msgBuffer = new StringBuilder();
        DecimalFormat df = new DecimalFormat("#.##");
        msgBuffer.append("Chisquare test failed");
        msgBuffer.append(" p-value = ");
        msgBuffer.append(chiSquareTest.chiSquareTest(expected, observed));
        msgBuffer.append(" chisquare statistic = ");
        msgBuffer.append(chiSquareTest.chiSquare(expected, observed));
        msgBuffer.append(". \n");
        msgBuffer.append("value\texpected\tobserved\n");
        for (int i = 0; i < expected.length; i++) {
            msgBuffer.append(valueLabels[i]);
            msgBuffer.append("\t");
            msgBuffer.append(df.format(expected[i]));
            msgBuffer.append("\t\t");
            msgBuffer.append(observed[i]);
            msgBuffer.append("\n");
        }
        msgBuffer.append("This test can fail randomly due to sampling error with probability ");
        msgBuffer.append(alpha);
        msgBuffer.append(".");
        Assert.fail(msgBuffer.toString());
    }
}

From source file:VGL.SummaryChartUI.java

private void updateChiSqValues() {
    // be sure all "expected" values are non blank
    boolean haveAllEntries = true;
    for (int i = 0; i < data.length; i++) {
        if ((data[i][2] == "") || (data[i][2] == null)) {
            haveAllEntries = false;//from www.  j a v  a2s  .  c o m
            break;
        }
    }

    if (haveAllEntries) {
        long[] observedCounts = new long[data.length];
        double[] expectedCounts = new double[data.length];
        for (int i = 0; i < data.length; i++) {
            observedCounts[i] = new Long((Integer) data[i][1]);
            try {
                expectedCounts[i] = Double.parseDouble((String) data[i][2]);
            } catch (NumberFormatException e) {
                data[i][2] = "";
                expectedCounts[i] = 0.0f;
            }
        }

        ChiSquareTest cst = new ChiSquareTest();

        double chiSq;
        double pVal;
        try {
            chiSq = cst.chiSquare(expectedCounts, observedCounts);
            pVal = cst.chiSquareTest(expectedCounts, observedCounts);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Messages.getInstance().getString("VGLII.Chi-sqZeroText"),
                    Messages.getInstance().getString("VGLII.Chi-sqZeroTitle"), JOptionPane.ERROR_MESSAGE);
            chiSquaredLabel.setText(CHI_SQUARE_DEFAULT);
            return;
        }

        chiSquaredLabel.setText("<html>\u03C7<sup>2</sup>= " + String.format("%7.3g", chiSq) + " <br><i>p</i>= "
                + String.format("%7.3g", pVal) + "</html>");

    } else {
        chiSquaredLabel.setText(CHI_SQUARE_DEFAULT);
    }
}