Example usage for org.apache.commons.math3.stat.inference TestUtils t

List of usage examples for org.apache.commons.math3.stat.inference TestUtils t

Introduction

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

Prototype

public static double t(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
        throws NullArgumentException, NumberIsTooSmallException 

Source Link

Usage

From source file:cn.edu.suda.core.stats.StatsUtils.java

public static DataMatrix addTTest(DataMatrix dm, int m, int n) {
    int col = dm.getDcol();
    dm.addCols(2);/*  ww w.j  a v  a2s  .c o  m*/
    dm.setColname(col, "ABS_t_value");
    dm.setColname(col + 1, "P_value");
    for (int i = 0; i < dm.getDrow(); i++) {
        double[] array = dm.getRow(i);
        SummaryStatistics stats1 = new SummaryStatistics();
        SummaryStatistics stats2 = new SummaryStatistics();
        for (int j = 0; j < m; j++) {
            stats1.addValue(array[j]);
        }
        for (int j = m; j < m + n; j++) {
            stats2.addValue(array[j]);
        }
        double var1 = stats1.getVariance();
        double var2 = stats2.getVariance();
        if (var1 == 0 && var2 == 0) {
            dm.setValue(i, col, 0);
            dm.setValue(i, col + 1, 1);
        } else {
            double t = Math.abs(TestUtils.t(stats1, stats2));
            double p = TestUtils.tTest(stats1, stats2);
            t = Utils.formatNumber(t, 4);
            p = Utils.formatNumber(p, 4);
            dm.setValue(i, col, t);
            dm.setValue(i, col + 1, p);
        }
    }
    return dm;
}

From source file:org.asoem.greyfish.impl.environment.AgentObjectPoolAT.java

@Test
public void testSignificantPerformanceBenefitInSimulationContext() throws Exception {
    // given//from ww w . ja va  2s. c  om
    final int populationSize = 400;
    final int steps = 30000;
    final int runs = 20;
    final DescriptiveStatistics statisticsWithoutObjectPool = new DescriptiveStatistics();
    final DescriptiveStatistics statisticsWithObjectPool = new DescriptiveStatistics();

    final ExecutorService executorService = MoreExecutors.sameThreadExecutor();

    // when
    for (int i = 0; i < runs; i++) {
        // randomize execution order
        if (RandomGenerators.rng().nextBoolean()) {
            statisticsWithoutObjectPool.addValue(measureExecutionTime(
                    new SimulationWithoutObjectPoolFactory(populationSize, executorService).newSimulation(),
                    steps));

            statisticsWithObjectPool.addValue(measureExecutionTime(
                    new SimulationWithObjectPoolFactory(populationSize, executorService).newSimulation(),
                    steps));
        } else {
            statisticsWithObjectPool.addValue(measureExecutionTime(
                    new SimulationWithObjectPoolFactory(populationSize, executorService).newSimulation(),
                    steps));

            statisticsWithoutObjectPool.addValue(measureExecutionTime(
                    new SimulationWithoutObjectPoolFactory(populationSize, executorService).newSimulation(),
                    steps));
        }
    }

    // then
    logger.info("Simulation with object pool vs. without object pool: {}, {}", statisticsWithObjectPool,
            statisticsWithoutObjectPool);

    assertThat(
            "The mean elapsed time of the version with an object pool "
                    + "is not less than the mean elapsed time of the version with an object pool",
            statisticsWithObjectPool.getMean(), is(lessThan(statisticsWithoutObjectPool.getMean())));

    // Is it also significantly faster? Make a t-test.
    // Test assumptions for t-test: normality
    assertThat("Is not normal distributed",
            StatisticalTests.shapiroWilk(statisticsWithObjectPool.getValues()).p(), is(lessThan(0.05)));
    assertThat("Is not normal distributed",
            StatisticalTests.shapiroWilk(statisticsWithoutObjectPool.getValues()).p(), is(lessThan(0.05)));

    final double t = TestUtils.t(statisticsWithObjectPool, statisticsWithoutObjectPool);
    final double p = TestUtils.tTest(statisticsWithObjectPool, statisticsWithoutObjectPool);
    logger.info("t-test: t={}, p={}", t, p);
    double qt = new TDistribution(statisticsWithObjectPool.getN() - 1 + statisticsWithoutObjectPool.getN() - 1)
            .inverseCumulativeProbability(0.975);
    assertThat("The means are not significantly different", Math.abs(t), is(greaterThan(qt)));
}

From source file:org.moeaframework.util.statistics.SingleSampleTTestTest.java

/**
 * Test from Sheskin (2004) in Chapter 2.
 *//*from   ww  w . ja v  a 2 s  .c  o m*/
@Test
public void testExample() {
    SingleSampleTTest test = new SingleSampleTTest(5.0);
    test.add(9);
    test.add(10);
    test.add(8);
    test.add(4);
    test.add(8);
    test.add(3);
    test.add(0);
    test.add(10);
    test.add(15);
    test.add(9);

    Assert.assertEquals(1.94, TestUtils.t(test.getMean(), test.categorize().get(0)), 0.01);
    Assert.assertFalse(test.test(0.05));
}

From source file:org.moeaframework.util.statistics.TwoSampleTTestTest.java

/**
 * Test from Sheskin (2004) in Chapter 11.
 *//*from   w  w  w. j a v  a2  s. c o m*/
@Test
public void testIndependentExample() {
    TwoSampleTTest test = new TwoSampleTTest(true);
    test.add(11, 0);
    test.add(1, 0);
    test.add(0, 0);
    test.add(2, 0);
    test.add(0, 0);
    test.add(11, 1);
    test.add(11, 1);
    test.add(5, 1);
    test.add(8, 1);
    test.add(4, 1);

    Assert.assertEquals(-1.96, TestUtils.t(test.categorize().get(0), test.categorize().get(1)), 0.01);
    Assert.assertFalse(test.test(0.05));
}

From source file:wsattacker.library.intelligentdos.success.TTestSuccessDecider.java

private double calculateProbability(Long[] run1, Long[] run2) {
    SummaryStatistics statisticsX = new SummaryStatistics();
    for (double value : run1) {
        statisticsX.addValue(value);/*ww w  . j  av  a 2s . c om*/
    }

    SummaryStatistics statisticsY = new SummaryStatistics();
    for (double value : run2) {
        statisticsY.addValue(value);
    }

    // two-sample
    double t = TestUtils.t(statisticsX, statisticsY);
    long degreesOfFreedom = statisticsX.getN() + statisticsY.getN() - 2;
    // p-value = 0.3002
    TDistribution tDistribution = new TDistribution(degreesOfFreedom);
    double pValue = tDistribution.cumulativeProbability(t);
    return pValue;
}

From source file:wsattacker.plugin.intelligentdos.StatisticTest.java

private static void statistics(double[] x, double[] y) {

    DecimalFormat df = new DecimalFormat("#.######");

    SummaryStatistics statisticsX = new SummaryStatistics();
    for (double value : x) {
        statisticsX.addValue(value);/*  www.j  a  v  a2  s  . com*/
    }

    SummaryStatistics statisticsY = new SummaryStatistics();
    for (double value : y) {
        statisticsY.addValue(value);
    }

    // t = -0.5331
    double t = TestUtils.t(statisticsX, statisticsY);
    // df = 18
    long degreesOfFreedom = statisticsX.getN() + statisticsY.getN() - 2;
    // p-value = 0.3002
    TDistribution tDistribution = new TDistribution(degreesOfFreedom);
    double pValue = tDistribution.cumulativeProbability(t);

    // t = -0.5331, df = 18, p-value = 0.3002
    System.out.print("t = " + df.format(t));
    System.out.print(", df = " + degreesOfFreedom);
    System.out.println(", p-value = " + df.format(pValue));

    System.out.println("mean of x mean of y");
    System.out.println(df.format(statisticsX.getMean()) + " - " + df.format(statisticsY.getMean()));

    // Calculate 95% confidence interval
    double ci = calcMeanCI(statisticsY, 0.95);
    System.out.println(String.format("Confidence inteval 95%%: %f", ci));
}