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

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

Introduction

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

Prototype

public ChiSquaredDistribution(double degreesOfFreedom) 

Source Link

Document

Create a Chi-Squared distribution with the given degrees of freedom.

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);//  ww w .  j  a v a2 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();//  www  . j  av  a2s  . c  om

    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: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/*  w w w.j  av a2s  . c  o 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:de.uniwuerzburg.info3.ofcprobe.vswitch.trafficgen.IATGen.java

/**
 * Constructor/* ww w.  jav  a 2 s . co  m*/
 *
 * @param distribution Distribution as String
 * @param para1 Parameter 1
 * @param para2 Parameter 2 (only needed when applicable)
 */
public IATGen(String distribution, double para1, double para2) {

    logger.trace("Distribution selected: {} with Parameters {} & {}", distribution, para1, para2);

    switch (distribution) {
    case "ChiSquared":
        this.distri = new ChiSquaredDistribution(para1);
        break;
    case "Exponential":
        this.distri = new ExponentialDistribution(para1);
        break;
    case "Gamma":
        this.distri = new GammaDistribution(para1, para2);
        break;
    case "Poisson":
        this.intDistri = new PoissonDistribution(para1, para2);
        break;
    default:
        this.distri = new NormalDistribution(para1, para2);
        break;
    }
}

From source file:jasima.core.random.continuous.DblChiSquared.java

public DblChiSquared(int degreesOfFreedom) {
    super();
    setDistribution(new ChiSquaredDistribution(degreesOfFreedom));
}

From source file:eu.betaas.taas.securitymanager.taastrustmanager.taastrustcalculator.StatisticsCalculator.java

public boolean calculateNumericVariance(double[] values) {
    double alpha = 0.05;
    double mean = StatUtils.mean(values);
    double variance = StatUtils.variance(values);
    double expected = Math.pow(mean * 0.05, 2);
    //double expected = 0.01;
    double degFreedom = values.length - 1.0;

    double T = (degFreedom * variance) / expected;
    logger.debug("Mean = " + mean);
    logger.debug("Standard Deviation = " + Math.sqrt(variance));
    logger.debug("Test Statistic calculated T = " + T);

    ChiSquaredDistribution myDist = new ChiSquaredDistribution(degFreedom);
    double myTLeft = myDist.inverseCumulativeProbability(alpha / 2.0);
    double myTRight = myDist.inverseCumulativeProbability(1.0 - alpha / 2.0);

    logger.debug("Boundaries: " + myTLeft + " to " + myTRight);

    // Determine if z score is in the region of acceptance
    if ((myTLeft <= T) && (T <= myTRight)) {
        // H0 -> Variance of the data is equal to the expected one
        return true;
    }/* ww  w  .j av a2 s.  c o  m*/

    // H1 -> Variance of the data is different to the expected one
    return false;
}

From source file:net.atos.ari.vital.taastrustcalculator.StatisticsCalculator.java

public boolean calculateNumericVariance(double[] values) {
    double alpha = 0.05;
    double mean = StatUtils.mean(values);
    double variance = StatUtils.variance(values);
    double expected = Math.pow(mean * 0.05, 2);
    //double expected = 0.01;
    double degFreedom = values.length - 1.0;

    double T = (degFreedom * variance) / expected;
    logger.debug("Mean = " + mean);
    logger.debug("Standard Deviation = " + Math.sqrt(variance));
    logger.debug("Test Statistic calculated T = " + T);

    if (degFreedom <= 0)
        return false;
    ChiSquaredDistribution myDist = new ChiSquaredDistribution(degFreedom);
    double myTLeft = myDist.inverseCumulativeProbability(alpha / 2.0);
    double myTRight = myDist.inverseCumulativeProbability(1.0 - alpha / 2.0);

    logger.debug("Boundaries: " + myTLeft + " to " + myTRight);

    // Determine if z score is in the region of acceptance
    if ((myTLeft <= T) && (T <= myTRight)) {
        // H0 -> Variance of the data is equal to the expected one
        return true;
    }/*w ww .j  a  v  a2s . co m*/

    // H1 -> Variance of the data is different to the expected one
    return false;
}

From source file:gedi.atac.DirichletTest.java

@Override
public void accept(ReferenceSequence reference, GenomicRegion region, PixelLocationMapping pixelMapping,
        PixelBlockToValuesMap data) {//from  w  ww.j  a v a  2 s .c om

    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:jasima.core.random.continuous.DblChiSquared.java

/**
 * Sets the degrees of freedom for this distribution.
 * /*from   w w w .jav  a2  s  .  co m*/
 * @param degreesOfFreedom
 *            The degrees of freedom to use.
 * @throws NotStrictlyPositiveException
 *             If the parameter value was {@code <=0.0}.
 */
public void setDegreesOfFreedom(int degreesOfFreedom) throws NotStrictlyPositiveException {
    setDistribution(new ChiSquaredDistribution(degreesOfFreedom));
}

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

@Override
public boolean test(double alpha) {
    update();/*from   w ww.  j ava2 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;
}