Example #1
0
  @Test
  public void testOneSampleTTest() {
    double[] oneSidedP = {
      2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d
    };
    SummaryStatistics oneSidedPStats = new SummaryStatistics();
    for (int i = 0; i < oneSidedP.length; i++) {
      oneSidedPStats.addValue(oneSidedP[i]);
    }
    // Target comparison values computed using R version 1.8.1 (Linux version)
    Assert.assertEquals("one sample t stat", 3.86485535541, TestUtils.t(0d, oneSidedP), 10E-10);
    Assert.assertEquals("one sample t stat", 3.86485535541, TestUtils.t(0d, oneSidedPStats), 1E-10);
    Assert.assertEquals(
        "one sample p value", 0.000521637019637, TestUtils.tTest(0d, oneSidedP) / 2d, 10E-10);
    Assert.assertEquals(
        "one sample p value", 0.000521637019637, TestUtils.tTest(0d, oneSidedPStats) / 2d, 10E-5);
    Assert.assertTrue("one sample t-test reject", TestUtils.tTest(0d, oneSidedP, 0.01));
    Assert.assertTrue("one sample t-test reject", TestUtils.tTest(0d, oneSidedPStats, 0.01));
    Assert.assertTrue("one sample t-test accept", !TestUtils.tTest(0d, oneSidedP, 0.0001));
    Assert.assertTrue("one sample t-test accept", !TestUtils.tTest(0d, oneSidedPStats, 0.0001));

    try {
      TestUtils.tTest(0d, oneSidedP, 95);
      Assert.fail("alpha out of range, OutOfRangeException expected");
    } catch (OutOfRangeException ex) {
      // expected
    }

    try {
      TestUtils.tTest(0d, oneSidedPStats, 95);
      Assert.fail("alpha out of range, OutOfRangeException expected");
    } catch (OutOfRangeException ex) {
      // expected
    }
  }
Example #2
0
  @Test
  public void testSmallSamples() {
    double[] sample1 = {1d, 3d};
    double[] sample2 = {4d, 5d};

    // Target values computed using R, version 1.8.1 (linux version)
    Assert.assertEquals(-2.2360679775, TestUtils.t(sample1, sample2), 1E-10);
    Assert.assertEquals(0.198727388935, TestUtils.tTest(sample1, sample2), 1E-10);
  }
Example #3
0
  @Test
  public void testTwoSampleTHeterscedastic() {
    double[] sample1 = {7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d};
    double[] sample2 = {-1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d};
    SummaryStatistics sampleStats1 = new SummaryStatistics();
    for (int i = 0; i < sample1.length; i++) {
      sampleStats1.addValue(sample1[i]);
    }
    SummaryStatistics sampleStats2 = new SummaryStatistics();
    for (int i = 0; i < sample2.length; i++) {
      sampleStats2.addValue(sample2[i]);
    }

    // Target comparison values computed using R version 1.8.1 (Linux version)
    Assert.assertEquals(
        "two sample heteroscedastic t stat", 1.60371728768, TestUtils.t(sample1, sample2), 1E-10);
    Assert.assertEquals(
        "two sample heteroscedastic t stat",
        1.60371728768,
        TestUtils.t(sampleStats1, sampleStats2),
        1E-10);
    Assert.assertEquals(
        "two sample heteroscedastic p value",
        0.128839369622,
        TestUtils.tTest(sample1, sample2),
        1E-10);
    Assert.assertEquals(
        "two sample heteroscedastic p value",
        0.128839369622,
        TestUtils.tTest(sampleStats1, sampleStats2),
        1E-10);
    Assert.assertTrue(
        "two sample heteroscedastic t-test reject", TestUtils.tTest(sample1, sample2, 0.2));
    Assert.assertTrue(
        "two sample heteroscedastic t-test reject",
        TestUtils.tTest(sampleStats1, sampleStats2, 0.2));
    Assert.assertTrue(
        "two sample heteroscedastic t-test accept", !TestUtils.tTest(sample1, sample2, 0.1));
    Assert.assertTrue(
        "two sample heteroscedastic t-test accept",
        !TestUtils.tTest(sampleStats1, sampleStats2, 0.1));

    try {
      TestUtils.tTest(sample1, sample2, .95);
      Assert.fail("alpha out of range, OutOfRangeException expected");
    } catch (OutOfRangeException ex) {
      // expected
    }

    try {
      TestUtils.tTest(sampleStats1, sampleStats2, .95);
      Assert.fail("alpha out of range, OutOfRangeException expected");
    } catch (OutOfRangeException ex) {
      // expected
    }

    try {
      TestUtils.tTest(sample1, tooShortObs, .01);
      Assert.fail("insufficient data, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }

    try {
      TestUtils.tTest(sampleStats1, (SummaryStatistics) null, .01);
      Assert.fail("insufficient data, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }

    try {
      TestUtils.tTest(sample1, tooShortObs);
      Assert.fail("insufficient data, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }

    try {
      TestUtils.tTest(sampleStats1, (SummaryStatistics) null);
      Assert.fail("insufficient data, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }

    try {
      TestUtils.t(sample1, tooShortObs);
      Assert.fail("insufficient data, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }

    try {
      TestUtils.t(sampleStats1, (SummaryStatistics) null);
      Assert.fail("insufficient data, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }
  }
Example #4
0
  @Test
  public void testOneSampleT() {
    double[] observed = {
      93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0
    };
    double mu = 100.0;
    SummaryStatistics sampleStats = null;
    sampleStats = new SummaryStatistics();
    for (int i = 0; i < observed.length; i++) {
      sampleStats.addValue(observed[i]);
    }

    // Target comparison values computed using R version 1.8.1 (Linux version)
    Assert.assertEquals("t statistic", -2.81976445346, TestUtils.t(mu, observed), 10E-10);
    Assert.assertEquals("t statistic", -2.81976445346, TestUtils.t(mu, sampleStats), 10E-10);
    Assert.assertEquals("p value", 0.0136390585873, TestUtils.tTest(mu, observed), 10E-10);
    Assert.assertEquals("p value", 0.0136390585873, TestUtils.tTest(mu, sampleStats), 10E-10);

    try {
      TestUtils.t(mu, (double[]) null);
      Assert.fail("arguments too short, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }

    try {
      TestUtils.t(mu, (SummaryStatistics) null);
      Assert.fail("arguments too short, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }

    try {
      TestUtils.t(mu, emptyObs);
      Assert.fail("arguments too short, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }

    try {
      TestUtils.t(mu, emptyStats);
      Assert.fail("arguments too short, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }

    try {
      TestUtils.t(mu, tooShortObs);
      Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }
    try {
      TestUtils.tTest(mu, tooShortObs);
      Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected");
    } catch (NumberIsTooSmallException ex) {
      // expected
    }

    try {
      TestUtils.t(mu, (SummaryStatistics) null);
      Assert.fail("insufficient data to compute t statistic, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }
    try {
      TestUtils.tTest(mu, (SummaryStatistics) null);
      Assert.fail("insufficient data to perform t test, NullArgumentException expected");
    } catch (NullArgumentException ex) {
      // expected
    }
  }