def testVariance_ShortList(self): # Population variance is the average of squared deviations from the mean. # The deviations from the mean in this example are [3.5, 0.5, -0.5, -3.5], # and the squared deviations are [12.25, 0.25, 0.25, 12.25]. # With sample variance, however, 1 is subtracted from the sample size. # So the sample variance is sum([12.25, 0.25, 0.25, 12.25]) / 3.0. self.assertAlmostEqual(8.333333334, sum([12.25, 0.25, 0.25, 12.25]) / 3.0) self.assertAlmostEqual(8.333333334, math_utils.Variance([-3, 0, 1, 4]))
def WelchsTTest(sample1, sample2): """Performs Welch's t-test on the two samples. Welch's t-test is an adaptation of Student's t-test which is used when the two samples may have unequal variances. It is also an independent two-sample t-test. Args: sample1: A collection of numbers. sample2: Another collection of numbers. Returns: A 3-tuple (t-statistic, degrees of freedom, p-value). """ mean1 = math_utils.Mean(sample1) mean2 = math_utils.Mean(sample2) v1 = math_utils.Variance(sample1) v2 = math_utils.Variance(sample2) n1 = len(sample1) n2 = len(sample2) t = _TValue(mean1, mean2, v1, v2, n1, n2) df = _DegreesOfFreedom(v1, v2, n1, n2) p = _LookupPValue(t, df) return t, df, p
def testVariance_OneValue(self): self.assertEqual(0, math_utils.Variance([0])) self.assertEqual(0, math_utils.Variance([4.3]))