def tptest(data1, data2): # Given the arrays "data1" and "data2" of length "n", returns # the Student's "t" for paired data and its significance as "prob". # Small values of "prob" indicates that the arrays have different # means. n1 = len(data1) n2 = len(data2) if (n1 != n2): print("Data arrays must have the same size") else: n = n1 ave1, var1 = nr.avevar(data1) ave2, var2 = nr.avevar(data2) cov = 0.0 for i in range(n): cov = cov + (data1[i] - ave1) * (data2[i] - ave2) # Degrees of freedom df = n - 1.0 cov = cov / df # Student's t t = (ave1 - ave2) / np.sqrt((var1 + var2 - 2.0 * cov) / n) # Significance prob = nr.betai(0.5 * df, 0.5, df / (df + t**2)) return t, prob
def ttest(data1, data2): # Given the arrays "data1" and "data2", returns the Student's # "t" and its significance as "prob". # Data are assumed to be drawn from populations with the same # true variance. # Small values of "prob" indicates that the arrays have different # means. n1 = len(data1) n2 = len(data2) ave1, var1 = nr.avevar(data1) ave2, var2 = nr.avevar(data2) # Degrees of freedom df = n1 + n2 - 2. # Pooled variance var = ((n1 - 1.) * var1 + (n2 - 1) * var2) / df # Student's t t = (ave1 - ave2) / np.sqrt(var * (1 / n1 + 1 / n2)) # Significance prob = nr.betai(0.5 * df, 0.5, df / (df + t**2)) return t, prob
def test_avevar_22(): """ Checks variance (sample) values of an input dataset """ data = [1, 3, 5, 7, 9] calculated_value = nr.avevar(data)[1] expected_value = 10.0 assert_almost_equal(calculated_value, expected_value, 4)
def test_avevar_21(): """ Checks variance (sample) values of an input dataset """ data = [1, 2, 3, 4, 5] calculated_value = nr.avevar(data)[1] expected_value = 2.5 assert_almost_equal(calculated_value, expected_value, 4)
def test_avevar_12(): """ Checks average values of an input dataset """ data = [1, 2, 3, 4, 5] calculated_value = nr.avevar(data)[0] expected_value = 3 assert_almost_equal(calculated_value, expected_value, 4)
def test_avevar_11(): """ Checks average values of an input dataset """ data = [-2, -1, 0, 1, 2] calculated_value = nr.avevar(data)[0] expected_value = 0.0 assert_almost_equal(calculated_value, expected_value, 4)
def tutest(data1, data2): # Given the arrays "data1" and "data2", returns the Student's # "t" and its significance as "prob". # Data are assumed to be drawn from populations with the same # true variance. # Small values of "prob" indicates that the arrays have different # means. n1 = len(data1) n2 = len(data2) ave1, var1 = nr.avevar(data1) ave2, var2 = nr.avevar(data2) # Student's t t = (ave1 - ave2) / np.sqrt(var1 / n1 + var2 / n2) df = (var1 / n1 + var2 / n2)**2 / ((var1 / n1)**2 / (n1 - 1) + (var2 / n2)**2 / (n2 - 1)) # Significance prob = nr.betai(0.5 * df, 0.5, df / (df + t**2)) return t, prob