def _eval_apply_evalf(self, arg): arg = arg.evalf() if isinstance(arg, Basic.Number): # Temporary hack from sympy.core.numbers import Real from sympy.numerics import evalf from sympy.numerics.functions2 import erf e = erf(evalf(arg)) return Real(str(e))
def confidence(s, p): """Return a symmetric (p*100)% confidence interval. For example, p=0.95 gives a 95% confidence interval. Currently this function only handles numerical values except in the trivial case p=1. Examples usage: # One standard deviation >>> N = Normal(0, 1) >>> N.confidence(0.68) (-0.994457883209753, 0.994457883209753) >>> N.probability(*_).evalf() 0.68 # Two standard deviations >>> N = Normal(0, 1) >>> N.confidence(0.95) (-1.95996398454005, 1.95996398454005) >>> N.probability(*_).evalf() 0.95 """ if p == 1: return (-oo, oo) assert p <= 1 # In terms of n*sigma, we have n = sqrt(2)*ierf(p). The inverse # error function is not yet implemented in SymPy but can easily be # computed numerically from sympy.numerics import Float, secant, evalf from sympy.numerics.functions2 import erf p = evalf(p) # calculate y = ierf(p) by solving erf(y) - p = 0 y = secant(lambda y: erf(y) - p, 0) t = Real(str(evalf(s.sigma) * Float(2)**0.5 * y)) mu = s.mu.evalf() return (mu-t, mu+t)