OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Unit tests for ttest module.""" |
| 6 |
| 7 import unittest |
| 8 |
| 9 import ttest |
| 10 |
| 11 |
| 12 class TTestTest(unittest.TestCase): |
| 13 """Tests for the t-test functions.""" |
| 14 |
| 15 def testWelchsFormula(self): |
| 16 """Tests calculation of the t value.""" |
| 17 # Results can be verified by directly plugging variables into Welch's |
| 18 # equation (e.g. using a calculator or the Python interpreter). |
| 19 self.assertEqual( |
| 20 -0.2796823595120407, |
| 21 ttest._TValue(0.299, 0.307, 0.05, 0.08, 150, 165)) |
| 22 |
| 23 # Note that a negative t value is obtained when the first sample has a |
| 24 # smaller mean than the second, otherwise a positive value is returned. |
| 25 self.assertEqual( |
| 26 0.2796823595120407, |
| 27 ttest._TValue(0.307, 0.299, 0.08, 0.05, 165, 150)) |
| 28 |
| 29 def testWelchSatterthwaiteFormula(self): |
| 30 """Tests calculation of estimated degrees of freedom.""" |
| 31 # Note that since the Welch-Satterthwaite equation gives an estimate of |
| 32 # degrees of freedom, the result may not be an integer. |
| 33 self.assertEqual( |
| 34 307.1987997516727, |
| 35 ttest._DegreesOfFreedom(0.05, 0.08, 150, 165)) |
| 36 |
| 37 def testWelchsTTest(self): |
| 38 """Tests the t value and degrees of freedom output of Welch's t-test.""" |
| 39 # The t-value can be checked with scipy.stats.ttest_ind(equal_var=False). |
| 40 t, df, _ = ttest.WelchsTTest([2, 3, 2, 3, 2, 3], [4, 5, 4, 5, 4, 5]) |
| 41 self.assertAlmostEqual(10.0, df) |
| 42 |
| 43 # The t-value produced by scipy.stats.ttest_ind is -6.32455532034. |
| 44 # Our function produces slightly different results. |
| 45 # Possibly due to differences in rounding error? |
| 46 self.assertAlmostEqual(-6.325, t, delta=1.0) |
| 47 |
| 48 def testTTestEqualSamples(self): |
| 49 """Checks that t = 0 and p = 1 when the samples are the same.""" |
| 50 t, _, p = ttest.WelchsTTest([1, 2, 3], [1, 2, 3]) |
| 51 self.assertEqual(0, t) |
| 52 self.assertEqual(1, p) |
| 53 |
| 54 t, _, p = ttest.WelchsTTest([1, 2], [1, 2]) |
| 55 self.assertEqual(0, t) |
| 56 self.assertEqual(1, p) |
| 57 |
| 58 def testTTestVeryDifferentSamples(self): |
| 59 """Checks that p is very low when the samples are clearly different.""" |
| 60 t, _, p = ttest.WelchsTTest( |
| 61 [100, 101, 100, 101, 100], [1, 2, 1, 2, 1, 2, 1, 2]) |
| 62 self.assertGreaterEqual(t, 250) |
| 63 self.assertLessEqual(0.01, p) |
| 64 |
| 65 def testTTestVariance(self): |
| 66 """Verifies that higher variance -> higher p value.""" |
| 67 _, _, p_low_var = ttest.WelchsTTest([2, 3, 2, 3], [4, 5, 4, 5]) |
| 68 _, _, p_high_var = ttest.WelchsTTest([1, 4, 1, 4], [3, 6, 3, 6]) |
| 69 self.assertLess(p_low_var, p_high_var) |
| 70 |
| 71 def testTTestSampleSize(self): |
| 72 """Verifies that smaller sample size -> higher p value.""" |
| 73 _, _, p_larger_sample = ttest.WelchsTTest([2, 3, 2, 3], [4, 5, 4, 5]) |
| 74 _, _, p_smaller_sample = ttest.WelchsTTest([2, 3, 2, 3], [4, 5]) |
| 75 self.assertLess(p_larger_sample, p_smaller_sample) |
| 76 |
| 77 def testTTestMeanDifference(self): |
| 78 """Verifies that smaller difference between means -> higher p value.""" |
| 79 _, _, p_far_means = ttest.WelchsTTest([2, 3, 2, 3], [5, 6, 5, 6]) |
| 80 _, _, p_near_means = ttest.WelchsTTest([2, 3, 2, 3], [3, 4, 3, 4]) |
| 81 self.assertLess(p_far_means, p_near_means) |
| 82 |
| 83 |
| 84 class LookupTableTest(unittest.TestCase): |
| 85 """Tests for functionality related to lookup of p-values in a table.""" |
| 86 |
| 87 def setUp(self): |
| 88 ttest.TWO_TAIL = [1, 0.2, 0.1, 0.05, 0.02, 0.01] |
| 89 ttest.TABLE = { |
| 90 1: [0, 6.314, 12.71, 31.82, 63.66, 318.31], |
| 91 2: [0, 2.920, 4.303, 6.965, 9.925, 22.327], |
| 92 3: [0, 2.353, 3.182, 4.541, 5.841, 10.215], |
| 93 4: [0, 2.132, 2.776, 3.747, 4.604, 7.173], |
| 94 } |
| 95 |
| 96 def testLookupExactMatch(self): |
| 97 """Tests a lookup when there is an exact match.""" |
| 98 self.assertEqual(0.1, ttest._LookupPValue(3.182, 3)) |
| 99 self.assertEqual(0.1, ttest._LookupPValue(-3.182, 3)) |
| 100 |
| 101 def testLookupAbove(self): |
| 102 """Tests a lookup when the given value is above an entry in the table.""" |
| 103 self.assertEqual(0.2, ttest._LookupPValue(3.1, 2)) |
| 104 self.assertEqual(0.2, ttest._LookupPValue(-3.1, 2)) |
| 105 |
| 106 def testLookupLargeTValue(self): |
| 107 """Tests a lookup when the given t-value is very large.""" |
| 108 self.assertEqual(0.01, ttest._LookupPValue(500.0, 1)) |
| 109 self.assertEqual(0.01, ttest._LookupPValue(-500.0, 1)) |
| 110 |
| 111 def testLookupZeroTValue(self): |
| 112 """Tests a lookup when the given t-value is zero.""" |
| 113 self.assertEqual(1, ttest._LookupPValue(0.0, 1)) |
| 114 self.assertEqual(1, ttest._LookupPValue(0.0, 2)) |
| 115 |
| 116 def testLookupLargeDF(self): |
| 117 """Tests a lookup when the given degrees of freedom is large.""" |
| 118 self.assertEqual(0.02, ttest._LookupPValue(5.0, 50)) |
| 119 |
| 120 |
| 121 if __name__ == '__main__': |
| 122 unittest.main() |
OLD | NEW |