def testDegreesOfFreedom(self): """Tests calculation of estimated degrees of freedom.""" # The formula used to estimate degrees of freedom for independent-samples # t-test is called the Welch-Satterthwaite equation. Note that since the # Welch-Satterthwaite equation gives an estimate of degrees of freedom, # the result is a floating-point number and not an integer. stats1 = ttest.SampleStats(mean=0.299, var=0.05, size=150) stats2 = ttest.SampleStats(mean=0.307, var=0.08, size=165) self.assertAlmostEqual(307.19879975, ttest._DegreesOfFreedom(stats1, stats2))
def testTValue(self): """Tests calculation of the t-value using Welch's formula.""" # Results can be verified by directly plugging variables into Welch's # equation (e.g. using a calculator or the Python interpreter). stats1 = ttest.SampleStats(mean=0.299, var=0.05, size=150) stats2 = ttest.SampleStats(mean=0.307, var=0.08, size=165) # Note that a negative t-value is obtained when the first sample has a # smaller mean than the second, otherwise a positive value is returned. self.assertAlmostEqual(-0.27968236, ttest._TValue(stats1, stats2)) self.assertAlmostEqual(0.27968236, ttest._TValue(stats2, stats1))
def testDegreesOfFreedom_SmallSample_RaisesError(self): """Degrees of freedom can't be calculated if sample size is too small.""" size_0 = ttest.SampleStats(mean=0, var=0, size=0) size_1 = ttest.SampleStats(mean=1.0, var=0, size=1) size_5 = ttest.SampleStats(mean=2.0, var=0.5, size=5) # An error is raised if the size of one of the samples is too small. with self.assertRaises(RuntimeError): ttest._DegreesOfFreedom(size_0, size_5) with self.assertRaises(RuntimeError): ttest._DegreesOfFreedom(size_1, size_5) with self.assertRaises(RuntimeError): ttest._DegreesOfFreedom(size_5, size_0) with self.assertRaises(RuntimeError): ttest._DegreesOfFreedom(size_5, size_1) # If both of the samples have a variance of 0, no error is raised. self.assertEqual(1.0, ttest._DegreesOfFreedom(size_1, size_1))
def testTValue_ConstantSamples_ResultIsInfinity(self): """If there is no variation, infinity is used as the t-statistic value.""" stats = ttest.SampleStats(mean=1.0, var=0, size=10) self.assertEqual(float('inf'), ttest._TValue(stats, stats))
def testDegreesOfFreedom_ZeroVariance_ResultIsOne(self): """The lowest possible value is returned for df if variance is zero.""" stats = ttest.SampleStats(mean=1.0, var=0, size=10) self.assertEqual(1.0, ttest._DegreesOfFreedom(stats, stats))