def eye(m, n=None): n = n or m array = [0] * m * n for i in range(m): if i >= n: break array[i * n + i] = 1 return Tensor(array, shape=(m, n))
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 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 tanh(x): return Tensor.map(x, _math.tanh)
def log2(x): return Tensor.map(x, _math.log2)
def expm1(x): return Tensor.map(x, _math.expm1)
def remainder(x, y): return Tensor.map2(x, y)
def ldexp(x, i): return Tensor.map2(x, i, _math.ldexp)
def isinf(x): return Tensor.map(x, _math.isinf)
def outer(x1, x2): return Tensor.map_outer(x1, x2, _operator.mul)
def factorial(x): return Tensor.map(x, _math.factorial)
def round(x, ndigits=None): return Tensor.map2(x, ndigits, _builtins.round)
def fabs(x): return Tensor.map(x, _math.fabs)
def lgamma(x): return Tensor.map(x, _math.lgamma)
def gamma(x): return Tensor.map(x, _math.gamma)
def erfc(x): return Tensor.map(x, _math.erfc)
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): _isclose = lambda a, b: _math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0) return Tensor.map2(a, b, _isclose)
def floor(x): return Tensor.map(x, _math.floor)
def isfinite(x): return Tensor.map(x, _math.isfinite)
def fmod(x, y): return Tensor.map2(x, y, _math.fmod)
def isnan(x): return Tensor.map(x, _math.isnan)
def chi2_uniform_distance(table): expected = Tensor.sum(table) / len(table) cntrd = table - expected return Tensor.sum(cntrd**2) / expected
def modf(x): return Tensor.map(x, _math.modf)
def frexp(x): return Tensor.map(x, _math.frexp)
def trunc(x): return Tensor.map(x, _math.trunc)
def gcd(a, b): return Tensor.map2(a, b, _math.gcd)
def log(x, base): return Tensor.map2(x, base, _math.log)
def norm(x): x = tensor(x) return sqrt((Tensor.sum(square(x))))
def log10(x): return Tensor.map(x, _math.log10)
def sinh(x): return Tensor.map(x, _math.sinh)