Example usage for org.apache.commons.math3.distribution ChiSquaredDistribution cumulativeProbability

List of usage examples for org.apache.commons.math3.distribution ChiSquaredDistribution cumulativeProbability

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution ChiSquaredDistribution cumulativeProbability.

Prototype

public double cumulativeProbability(double x) 

Source Link

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  av 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:com.actelion.research.spiritcore.util.StatUtils.java

/**
 * Calculate Kruskal-Walis (http://en.wikipedia.org/wiki/Kruskal%E2%80%93Wallis_one-way_analysis_of_variance)
 * @param values//from ww w.j a v a 2  s.  co  m
 * @return
 */
public static double getKruskalWallis(List<double[]> values) {

    //Assign ranks
    assert values.size() > 1;

    List<Double> allDoubles = new ArrayList<>();
    for (double[] a : values) {
        assert a.length > 0;
        for (double d : a) {
            allDoubles.add(d);
        }
    }
    int N = allDoubles.size();
    Collections.sort(allDoubles);
    double[] allDoublesArray = new double[allDoubles.size()];
    for (int i = 0; i < allDoubles.size(); i++)
        allDoublesArray[i] = allDoubles.get(i);
    List<double[]> ranks = new ArrayList<double[]>();
    for (double[] a : values) {
        double[] rankArray = new double[a.length];
        ranks.add(rankArray);
        for (int i = 0; i < a.length; i++) {
            int r = Arrays.binarySearch(allDoublesArray, a[i]);
            assert r >= 0;
            int r1 = r, r2 = r;
            while (r1 > 0 && allDoublesArray[r1 - 1] == a[i])
                r1--;
            while (r2 < allDoublesArray.length - 1 && allDoublesArray[r2 + 1] == a[i])
                r2++;
            rankArray[i] = (r1 + r2) / 2.0 + 1;
        }
    }

    //Calculate rank average per group
    List<Double> rankSum = new ArrayList<Double>();
    for (double[] a : ranks) {
        double sum = 0;
        for (double d : a)
            sum += d;
        rankSum.add(sum);
    }

    double sum = 0;
    for (int i = 0; i < ranks.size(); i++) {
        sum += rankSum.get(i) * rankSum.get(i) / ranks.get(i).length;
    }
    double H = 12.0 / (N * (N + 1)) * sum - 3 * (N + 1);

    ChiSquaredDistribution chi = new ChiSquaredDistribution(values.size() - 1);
    double K = 1 - chi.cumulativeProbability(H);

    return K;
}

From source file:hivemall.utils.math.StatsUtils.java

/**
 * @param observed means non-negative vector
 * @param expected means positive vector
 * @return p value//from  www  .jav a  2 s. co m
 */
public static double chiSquareTest(@Nonnull final double[] observed, @Nonnull final double[] expected) {
    final ChiSquaredDistribution distribution = new ChiSquaredDistribution(expected.length - 1.d);
    return 1.d - distribution.cumulativeProbability(chiSquare(observed, expected));
}

From source file:hivemall.utils.math.StatsUtils.java

/**
 * This method offers effective calculation for multiple entries rather than calculation
 * individually/*from  w  w  w . j  av a2s  .  co m*/
 * 
 * @param observeds means non-negative matrix
 * @param expecteds means positive matrix
 * @return (chi2 value[], p value[])
 */
public static Map.Entry<double[], double[]> chiSquare(@Nonnull final double[][] observeds,
        @Nonnull final double[][] expecteds) {
    Preconditions.checkArgument(observeds.length == expecteds.length);

    final int len = expecteds.length;
    final int lenOfEach = expecteds[0].length;

    final ChiSquaredDistribution distribution = new ChiSquaredDistribution(lenOfEach - 1.d);

    final double[] chi2s = new double[len];
    final double[] ps = new double[len];
    for (int i = 0; i < len; i++) {
        chi2s[i] = chiSquare(observeds[i], expecteds[i]);
        ps[i] = 1.d - distribution.cumulativeProbability(chi2s[i]);
    }

    return new AbstractMap.SimpleEntry<double[], double[]>(chi2s, ps);
}

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();/*from   w  w  w. j a v a 2  s  . 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:gedi.atac.DirichletTest.java

@Override
public void accept(ReferenceSequence reference, GenomicRegion region, PixelLocationMapping pixelMapping,
        PixelBlockToValuesMap data) {/*from   www .j  a  va2s . co  m*/

    int bases = 0;
    int bins = data.size();

    double[] alpha = new double[bins];

    for (int b = 0; b < data.size(); b++) {
        PixelLocationMappingBlock bl = data.getBlock(b);
        bases += bl.getStopBp() + 1 - bl.getStartBp();

        alpha[b] = NumericArrayFunction.Sum.applyAsDouble(data.getValues(b)) + 1;
    }

    double[] p = alpha.clone();
    ArrayUtils.normalize(p);

    double lh1 = logProbability(alpha, p);
    Arrays.fill(p, 1.0 / bins);
    double lh0 = logProbability(alpha, p);

    double lr = 2 * lh1 - 2 * lh0;

    ChiSquaredDistribution d = new ChiSquaredDistribution(bins - 2);

    System.out.printf("Bases=%d\nBins=%d\nLR=%.4g\np=%.5g", bases, bins, lr, 1 - d.cumulativeProbability(lr));

}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemFitG2.java

public void compute() {
    int validRowCount = 0;
    initializeExpectedFrequencies();/* w ww  .j  a  va 2 s.  c o m*/

    //For debugging
    //        System.out.println(printContingencyTable("ORIGINAL TABLE"));

    condenseTable();

    //For debugging
    //        System.out.println(printContingencyTable("CONDENSED TABLE"));

    double r = 0;
    G2 = 0;
    double expectedFrequency = 0;
    for (int i = 0; i < numberOfBins; i++) {
        if (validRow[i]) {
            for (int j = 0; j < numberOfCategories; j++) {
                r = table[i][j] + .001;//Add small amount to avoid taking log of zero
                expectedFrequency = expectedValues[i][j] + .001;//Add small amount to avoid taking log of zero
                G2 += r * Math.log(r / expectedFrequency);

                //For debugging
                //                    System.out.println("i: " + i + "  j: " + j + "  R: " + r + " EF: " + expectedFrequency );
            }
            validRowCount++;
        }
    }

    G2 = G2 * 2.0;
    dfG2 = ((double) validRowCount) * ((double) numberOfCategories - 1.0);

    try {
        ChiSquaredDistribution chiSquaredDistribution = new ChiSquaredDistribution(dfG2);
        pvalueG2 = 1.0 - chiSquaredDistribution.cumulativeProbability(G2);
    } catch (Exception ex) {
        pvalueG2 = Double.NaN;
    }

}

From source file:com.itemanalysis.psychometrics.cfa.AbstractConfirmatoryFactorAnalysisEstimator.java

public double pvalue() {
    ChiSquaredDistribution chi = new ChiSquaredDistribution(degreesOfFreedom());
    double p = 0.0;
    p = 1.0 - chi.cumulativeProbability(chisquare());
    return p;//  w ww .j  a  v  a2  s.com
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.statistics.KruskalWallisTest.java

@Override
public boolean test(double alpha) {
    update();/* w  w w . j av  a 2 s . c o  m*/

    ChiSquaredDistribution dist = new ChiSquaredDistribution(numberOfGroups - 1);
    double H = H();
    double C = C();

    if (C == 0.0) {
        // all observations the same
        return false;
    }

    return 1.0 - dist.cumulativeProbability(H / C) < alpha;
}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemFitGeneralizedSX2.java

public void compute() {
    int validRowCount = 0;
    initializeExpectedFrequencies();// www . j a  va  2  s . com

    //For debugging
    //        System.out.println(this.printMirtFormatTable(false));

    condenseTable();

    //For debugging
    System.out.println(this.printMirtFormatTable(true));

    //Compute statistic.
    double d = 0;
    for (int x = 1; x < numberOfBins - 1; x++) {
        if (validRow[x]) {
            validRowCount++;
            for (int h = 0; h < numberOfCategories; h++) {
                if (expectedValues[x][h] > 0) {
                    d = table[x][h] - expectedValues[x][h];
                    SX2 += (d * d) / expectedValues[x][h];
                }
                //                    else{
                //                        numberOfCombinedCategories++;
                //                    }
            }
        }
    }

    //        System.out.println("  VALROW: " + validRowCount + "  NPAR: " + irm.getNumberOfEstimatedParameters() + "  COMBINED: " + numberOfCombinedCategories);

    //Compute degrees of freedom
    double K = maxTestScore - 2.0 * maxItemScore + 1;
    dfSX2 = maxItemScore * validRowCount - irm.getNumberOfEstimatedParameters() - numberOfCombinedCategories;
    try {
        ChiSquaredDistribution chiSquaredDistribution = new ChiSquaredDistribution(dfSX2);
        pvalueSX2 = 1.0 - chiSquaredDistribution.cumulativeProbability(SX2);
    } catch (Exception ex) {
        pvalueSX2 = Double.NaN;
    }
}