def test_betai_symmetry(): """ Checks relation Ix(a, b) = 1 - Ix-1(b, a) """ a, b, x = 1, 10, 0.15 calculated_value = nr.betai(a, b, x) + nr.betai(b, a, 1 - x) expected_value = 1 assert_almost_equal(calculated_value, expected_value)
def test_betai_recurrence(): """ Checks relation Ix(a, b) = xIx(a−1, b) + (1−x)Ix(a, b−1) """ a, b, x = 1, 10, 0.15 calculated_value = nr.betai(a, b, x) - x * nr.betai(a - 1, b, x) - \ (1 - x) * nr.betai(a, b - 1, x) expected_value = 0 assert_almost_equal(calculated_value, expected_value)
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) 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) # print(ave1, ave2) # print(var1, var2) # 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 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) # print(ave1, ave2) # print(var1, var2) # 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
def test_betai_x1(): """ Checks betai output for x = 1 """ a, b, x = 0.5, 0.5, 1 calculated_value = nr.betai(a, b, x) expected_value = 1 assert_almost_equal(calculated_value, expected_value)
def biwt_tutest(n1, ave1, var1, n2, ave2, var2): t = (ave1-ave2)/sqrt(var1/n1+var2/n2) df = (var1/n1+var2/n2)**2/((var1/n1)**2/(n1-1)+(var2/n2)**2/(n2-1)) df = 0.7 * df prob = nr.betai(0.5*df,0.5,df/(df+t**2)) return t, prob
def biwt_ttest(n1, ave1, var1, n2, ave2, var2): df = 0.7 * (n1+n2-2) svar = ((n1-1)*var1+(n2-1)*var2)/df t = (ave1-ave2)/sqrt(svar*(1.0/n1+1.0/n2)) prob = nr.betai(0.5*df,0.5,df/(df+t**2)) return t, prob
def prob(df, t): return (1 - nr.betai(0.5*df,0.5,df/(df+t**2))/2)