def t_test(x, y=None, mu=None): x = tensor(x) if y is not None: y = tensor(y) if y is None and mu is None: raise TypeError('Count of arguments must be 2, but only 1') t = 0 df = 0 if y is not None: n1 = len(x) n2 = len(y) if n1 <= 1 or n2 <= 1: raise ValueError mean1 = Tensor.mean(x) mean2 = Tensor.mean(y) var1 = Tensor.sum((x - mean1)**2) / (n1 - 1) var2 = Tensor.sum((y - mean2)**2) / (n2 - 1) t = (mean1 - mean2) / _math.sqrt(var1 / n1 + var2 / n2) df = (var1 / n1 + var2 / n2)**2 / (var1**2 / (n1**2 * (n1 - 1)) + var2**2 / (n2**2 * (n2 - 1))) if mu is not None: n = len(x) mean = Tensor.mean(x) sd = (Tensor.sum((x - mean)**2) / (n - 1))**0.5 t = (mean - mu) / (sd / _math.sqrt(n)) df = n - 1 p = betainc(df / (t**2 + df), df / 2, 1 / 2, regularized=True) return t, df, p
def chi2_uniform2d_distance(table, correct): row_sums = Tensor.sum(table, axis=1) col_sums = Tensor.sum(table, axis=0) all_sums = Tensor.sum(row_sums) expected = zeros_like(table) _correct = False if (table.shape[0] - 1) * (table.shape[1] - 1) == 1: _correct = True for i in range(table.shape[0]): for j in range(table.shape[1]): expected_ij = row_sums[i] * col_sums[j] / all_sums expected[i, j] = expected_ij if expected_ij < 10: _correct = correct and True if _correct: return Tensor.sum((abs(table - expected) - 0.5)**2 / expected) return Tensor.sum((table - expected)**2 / expected)
def norm(x): x = tensor(x) return sqrt((Tensor.sum(square(x))))
def chi2_uniform_distance(table): expected = Tensor.sum(table) / len(table) cntrd = table - expected return Tensor.sum(cntrd**2) / expected