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

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

Introduction

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

Prototype

public static double pairedT(final double[] sample1, final double[] sample2)
        throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException 

Source Link

Usage

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

/**
 * Test from Sheskin (2004) in Chapter 17.
 */// ww w .  j  a  v a2  s . com
@Test
public void testDependentExample() {
    TwoSampleTTest test = new TwoSampleTTest(false);
    test.add(9, 0);
    test.add(2, 0);
    test.add(1, 0);
    test.add(4, 0);
    test.add(6, 0);
    test.add(4, 0);
    test.add(7, 0);
    test.add(8, 0);
    test.add(5, 0);
    test.add(1, 0);
    test.add(8, 1);
    test.add(2, 1);
    test.add(3, 1);
    test.add(2, 1);
    test.add(3, 1);
    test.add(0, 1);
    test.add(4, 1);
    test.add(5, 1);
    test.add(4, 1);
    test.add(0, 1);

    Assert.assertEquals(2.86, TestUtils.pairedT(test.categorize().get(0), test.categorize().get(1)), 0.02);
    Assert.assertTrue(test.test(0.05));
    Assert.assertFalse(test.test(0.01));
}

From source file:saffranexperiment.Main.java

/**
 * @param simulationData A 5D {@link java.util.Arrays Array} with the 
 * following structure:/*from  w w  w. ja v  a2  s.c o  m*/
 * 
 * <ul>
 *  <li>First dimension: participant types</li>
 *  <li>Second dimension: repeats</li>
 *  <li>Third dimension: experiments</li>
 *  <li>Fourth dimension: participants</li>
 *  <li>Fifth dimension:</li>
 *    <ol type="1">
 *      <li>Familiar word 1 presentation time</li>
 *      <li>Familiar word 2 presentation time</li>
 *      <li>Novel word 1 presentation time</li>
 *      <li>Novel word 2 presentation time</li>
 *    </ol>
 * </ul>
 * 
 * For example, simulationData[7][8][1][17][2] should return the presentation
 * time for the first novel word achieved by participant 18 in the 9th repeat
 * of experiment 2 when participant type is set to 8.
 * 
 * @return A 4D {@link java.util.Arrays Array} with the following structure:
 * 
 * <ul>
 *  <li>First dimension: participant types</li>
 *  <li>Second dimension: repeats</li>
 *  <li>Third dimension: experiments</li>
 *  <li>Fourth dimension:</li> 
 *    <ol type="1">
 *      <li>Familiar word presentation time mean</li>
 *      <li>Familiar word presentation time standard deviation</li>
 *      <li>Novel word presentation time mean</li>
 *      <li>Novel word presentation time standard deviation</li>
 *      <li>t value for familiar/novel word presentation time means</li>
 *      <li>p value for familiar/novel word presentation time means</li>
 *    </ol>
 * </ul>
 * 
 * For example, assigning the result to a variable j and invoking 
 * j[5][3][1][4] would return the t-value associated with the second 
 * experiment of the fourth repeat for participant type 6.
 */
private static double[][][][] calculateMeanSdTAndPValuesForEachExperimentRepeat(
        double[][][][][] simulationData) {

    double[][][][] values = new double[_totalParticipantTypes][_totalRepeats][2][6];

    for (int participantType = 0; participantType < _totalParticipantTypes; participantType++) {
        for (int repeat = 0; repeat < _totalRepeats; repeat++) {
            for (int experiment = 0; experiment < 2; experiment++) {

                List<Double> familWordPresentationTimes = new ArrayList();
                List<Double> novelWordPresentationTimes = new ArrayList();

                for (int participant = 0; participant < _totalParticipants; participant++) {
                    familWordPresentationTimes
                            .add(simulationData[participantType][repeat][experiment][participant][0]);
                    familWordPresentationTimes
                            .add(simulationData[participantType][repeat][experiment][participant][1]);
                    novelWordPresentationTimes
                            .add(simulationData[participantType][repeat][experiment][participant][2]);
                    novelWordPresentationTimes
                            .add(simulationData[participantType][repeat][experiment][participant][3]);
                }

                //Calculate familiar word presentation time mean for experiment.
                DescriptiveStatistics familWordDescStat = new DescriptiveStatistics();
                familWordPresentationTimes.forEach((value) -> familWordDescStat.addValue(value));
                values[participantType][repeat][experiment][0] = familWordDescStat.getMean();
                values[participantType][repeat][experiment][1] = familWordDescStat.getStandardDeviation();

                //Calculate novel word presentation time mean for experiment.
                DescriptiveStatistics novelWordDescStat = new DescriptiveStatistics();
                novelWordPresentationTimes.forEach((value) -> novelWordDescStat.addValue(value));
                values[participantType][repeat][experiment][2] = novelWordDescStat.getMean();
                values[participantType][repeat][experiment][3] = novelWordDescStat.getStandardDeviation();

                //Convert lists containing familiar and novel presentation times to 
                //arrays so we can use the Apache stats library functions for 
                //calculating t and p values. 
                double[] familWordPresentationTimesArr = convertDoubleListToDoubleArray(
                        familWordPresentationTimes);
                double[] novelWordPresentationTimesArr = convertDoubleListToDoubleArray(
                        novelWordPresentationTimes);

                //Calculate t value between familiar and novel word presentation times.
                values[participantType][repeat][0][4] = Math
                        .abs(TestUtils.pairedT(familWordPresentationTimesArr, novelWordPresentationTimesArr));

                //Calculate p value between familiar and novel word presentation times.
                values[participantType][repeat][0][5] = TestUtils.pairedTTest(familWordPresentationTimesArr,
                        novelWordPresentationTimesArr);
            }
        }
    }

    return values;
}