def test_log1p(self): """log1p should give same results as cephes""" p_s = [1e-10, 1e-5, 0.1, 0.8, 0.9, 0.95, 0.999, 0.9999999, 1, \ 1.000000001, 1.01, 2] exp = [ 9.9999999995e-11, 9.99995000033e-06, 0.0953101798043, 0.587786664902, 0.641853886172, 0.667829372576, 0.692647055518, 0.69314713056, 0.69314718056, 0.69314718106, 0.698134722071, 1.09861228867,] for p, e in zip(p_s, exp): self.assertFloatEqual(log1p(p), e)
def bdtri(k, n, y): """Inverse of binomial distribution. Finds binomial p such that sum of terms 0-k reaches cum probability y. """ y = fix_rounding_error(y) if y < 0.0 or y > 1.0: raise ZeroDivisionError, "y must be between 1 and 0." if k < 0 or n <= k: raise ZeroDivisionError, "k must be between 0 and n" dn = n - k if k == 0: if y > 0.8: p = -expm1(log1p(y-1.0) / dn) else: p = 1.0 - y**(1.0/dn) else: dk = k + 1; p = incbet(dn, dk, 0.5) if p > 0.5: p = incbi(dk, dn, 1.0-y) else: p = 1.0 - incbi(dn, dk, y) return p
def bdtrc(k, n, p): """Complement of binomial distribution, k+1 through n. Uses formula bdtrc(k, n, p) = betai(k+1, n-k, p) See Cephes docs for details. """ p = fix_rounding_error(p) if (p < 0) or (p > 1): raise ValueError, "Binomial p must be between 0 and 1." if (k < 0) or (n < k): raise ValueError, "Binomial k must be between 0 and n." if k == n: return 0 dn = n - k if k == 0: if p < .01: dk = -expm1(dn * log1p(-p)) else: dk = 1 - pow(1.0-p, dn) else: dk = k + 1 dk = betai(dk, dn, p) return dk