| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Unit tests for ttest module.""" | 5 """Unit tests for ttest module.""" |
| 6 | 6 |
| 7 import unittest | 7 import unittest |
| 8 | 8 |
| 9 import ttest | 9 import ttest |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 t, _, p = ttest.WelchsTTest([1, 2], [1, 2]) | 56 t, _, p = ttest.WelchsTTest([1, 2], [1, 2]) |
| 57 self.assertEqual(0, t) | 57 self.assertEqual(0, t) |
| 58 self.assertEqual(1, p) | 58 self.assertEqual(1, p) |
| 59 | 59 |
| 60 def testTTestVeryDifferentSamples(self): | 60 def testTTestVeryDifferentSamples(self): |
| 61 """Checks that p is very low when the samples are clearly different.""" | 61 """Checks that p is very low when the samples are clearly different.""" |
| 62 t, _, p = ttest.WelchsTTest( | 62 t, _, p = ttest.WelchsTTest( |
| 63 [100, 101, 100, 101, 100], [1, 2, 1, 2, 1, 2, 1, 2]) | 63 [100, 101, 100, 101, 100], [1, 2, 1, 2, 1, 2, 1, 2]) |
| 64 self.assertGreaterEqual(t, 250) | 64 self.assertGreaterEqual(t, 250) |
| 65 self.assertLessEqual(0.01, p) | 65 self.assertLessEqual(p, 0.01) |
| 66 | 66 |
| 67 def testTTestVariance(self): | 67 def testTTestVariance(self): |
| 68 """Verifies that higher variance -> higher p value.""" | 68 """Verifies that higher variance -> higher p value.""" |
| 69 _, _, p_low_var = ttest.WelchsTTest([2, 3, 2, 3], [4, 5, 4, 5]) | 69 _, _, p_low_var = ttest.WelchsTTest([2, 3, 2, 3], [4, 5, 4, 5]) |
| 70 _, _, p_high_var = ttest.WelchsTTest([1, 4, 1, 4], [3, 6, 3, 6]) | 70 _, _, p_high_var = ttest.WelchsTTest([1, 4, 1, 4], [3, 6, 3, 6]) |
| 71 self.assertLess(p_low_var, p_high_var) | 71 self.assertLess(p_low_var, p_high_var) |
| 72 | 72 |
| 73 def testTTestSampleSize(self): | 73 def testTTestSampleSize(self): |
| 74 """Verifies that smaller sample size -> higher p value.""" | 74 """Verifies that smaller sample size -> higher p value.""" |
| 75 _, _, p_larger_sample = ttest.WelchsTTest([2, 3, 2, 3], [4, 5, 4, 5]) | 75 _, _, p_larger_sample = ttest.WelchsTTest([2, 3, 2, 3], [4, 5, 4, 5]) |
| 76 _, _, p_smaller_sample = ttest.WelchsTTest([2, 3, 2, 3], [4, 5]) | 76 _, _, p_smaller_sample = ttest.WelchsTTest([2, 3, 2, 3], [4, 5]) |
| 77 self.assertLess(p_larger_sample, p_smaller_sample) | 77 self.assertLess(p_larger_sample, p_smaller_sample) |
| 78 | 78 |
| 79 def testTTestMeanDifference(self): | 79 def testTTestMeanDifference(self): |
| 80 """Verifies that smaller difference between means -> higher p value.""" | 80 """Verifies that smaller difference between means -> higher p value.""" |
| 81 _, _, p_far_means = ttest.WelchsTTest([2, 3, 2, 3], [5, 6, 5, 6]) | 81 _, _, p_far_means = ttest.WelchsTTest([2, 3, 2, 3], [5, 6, 5, 6]) |
| 82 _, _, p_near_means = ttest.WelchsTTest([2, 3, 2, 3], [3, 4, 3, 4]) | 82 _, _, p_near_means = ttest.WelchsTTest([2, 3, 2, 3], [3, 4, 3, 4]) |
| 83 self.assertLess(p_far_means, p_near_means) | 83 self.assertLess(p_far_means, p_near_means) |
| 84 | 84 |
| 85 | 85 |
| 86 class LookupTableTest(unittest.TestCase): | 86 class LookupTableTest(unittest.TestCase): |
| 87 """Tests for functionality related to lookup of p-values in a table.""" | 87 """Tests for functionality related to lookup of p-values in a table.""" |
| 88 | 88 |
| 89 def setUp(self): | 89 def setUp(self): |
| 90 self.original_TWO_TAIL = ttest.TWO_TAIL |
| 91 self.original_TABLE = ttest.TABLE |
| 90 ttest.TWO_TAIL = [1, 0.2, 0.1, 0.05, 0.02, 0.01] | 92 ttest.TWO_TAIL = [1, 0.2, 0.1, 0.05, 0.02, 0.01] |
| 91 ttest.TABLE = { | 93 ttest.TABLE = { |
| 92 1: [0, 6.314, 12.71, 31.82, 63.66, 318.31], | 94 1: [0, 6.314, 12.71, 31.82, 63.66, 318.31], |
| 93 2: [0, 2.920, 4.303, 6.965, 9.925, 22.327], | 95 2: [0, 2.920, 4.303, 6.965, 9.925, 22.327], |
| 94 3: [0, 2.353, 3.182, 4.541, 5.841, 10.215], | 96 3: [0, 2.353, 3.182, 4.541, 5.841, 10.215], |
| 95 4: [0, 2.132, 2.776, 3.747, 4.604, 7.173], | 97 4: [0, 2.132, 2.776, 3.747, 4.604, 7.173], |
| 96 } | 98 } |
| 97 | 99 |
| 100 def tearDown(self): |
| 101 ttest.TWO_TAIL = self.original_TWO_TAIL |
| 102 ttest.TABLE = self.original_TABLE |
| 103 |
| 98 def testLookupExactMatch(self): | 104 def testLookupExactMatch(self): |
| 99 """Tests a lookup when there is an exact match.""" | 105 """Tests a lookup when there is an exact match.""" |
| 100 self.assertEqual(0.1, ttest._LookupPValue(3.182, 3)) | 106 self.assertEqual(0.1, ttest._LookupPValue(3.182, 3)) |
| 101 self.assertEqual(0.1, ttest._LookupPValue(-3.182, 3)) | 107 self.assertEqual(0.1, ttest._LookupPValue(-3.182, 3)) |
| 102 | 108 |
| 103 def testLookupAbove(self): | 109 def testLookupAbove(self): |
| 104 """Tests a lookup when the given value is above an entry in the table.""" | 110 """Tests a lookup when the given value is above an entry in the table.""" |
| 105 self.assertEqual(0.2, ttest._LookupPValue(3.1, 2)) | 111 self.assertEqual(0.2, ttest._LookupPValue(3.1, 2)) |
| 106 self.assertEqual(0.2, ttest._LookupPValue(-3.1, 2)) | 112 self.assertEqual(0.2, ttest._LookupPValue(-3.1, 2)) |
| 107 | 113 |
| 108 def testLookupLargeTValue(self): | 114 def testLookupLargeTValue(self): |
| 109 """Tests a lookup when the given t-value is very large.""" | 115 """Tests a lookup when the given t-value is very large.""" |
| 110 self.assertEqual(0.01, ttest._LookupPValue(500.0, 1)) | 116 self.assertEqual(0.01, ttest._LookupPValue(500.0, 1)) |
| 111 self.assertEqual(0.01, ttest._LookupPValue(-500.0, 1)) | 117 self.assertEqual(0.01, ttest._LookupPValue(-500.0, 1)) |
| 112 | 118 |
| 113 def testLookupZeroTValue(self): | 119 def testLookupZeroTValue(self): |
| 114 """Tests a lookup when the given t-value is zero.""" | 120 """Tests a lookup when the given t-value is zero.""" |
| 115 self.assertEqual(1, ttest._LookupPValue(0.0, 1)) | 121 self.assertEqual(1, ttest._LookupPValue(0.0, 1)) |
| 116 self.assertEqual(1, ttest._LookupPValue(0.0, 2)) | 122 self.assertEqual(1, ttest._LookupPValue(0.0, 2)) |
| 117 | 123 |
| 118 def testLookupLargeDF(self): | 124 def testLookupLargeDF(self): |
| 119 """Tests a lookup when the given degrees of freedom is large.""" | 125 """Tests a lookup when the given degrees of freedom is large.""" |
| 120 self.assertEqual(0.02, ttest._LookupPValue(5.0, 50)) | 126 self.assertEqual(0.02, ttest._LookupPValue(5.0, 50)) |
| 121 | 127 |
| 122 | 128 |
| 123 if __name__ == '__main__': | 129 if __name__ == '__main__': |
| 124 unittest.main() | 130 unittest.main() |
| OLD | NEW |