From: Forrest Voight Date: Tue, 24 Jan 2012 04:42:42 +0000 (-0500) Subject: improved binomial_conf_interval X-Git-Tag: 0.8.3~65 X-Git-Url: https://git.novaco.in/?a=commitdiff_plain;h=2ec1b55b1dfc9e32d5a8ef3845d332ccb7597252;p=p2pool.git improved binomial_conf_interval --- diff --git a/p2pool/util/math.py b/p2pool/util/math.py index 5b6aba7..03c7936 100644 --- a/p2pool/util/math.py +++ b/p2pool/util/math.py @@ -120,8 +120,18 @@ except ImportError: bottom = 1 + z**2/n return (topa - topb)/bottom, (topa + topb)/bottom else: - def binomial_conf_interval(x, n, conf=0.95): - return special.betaincinv(x+1, n-x+1, (1-conf)/2), special.betaincinv(x+1, n-x+1, 1-(1-conf)/2) + def binomial_conf_interval(x, n, conf=0.95, steps=11): + a = 1 - conf + if n == 0: + left = random.random()*a + return left, left + conf + res = None + for left_a_i in xrange(steps): + left_a = a * left_a_i / (steps - 1) + this_left, this_right = special.betaincinv(x+1, n-x+1, left_a), special.betaincinv(x+1, n-x+1, 1 - (a - left_a)) + if res is None or this_right - this_left < res[1] - res[0]: + res = this_left, this_right + return res def binomial_conf_center_radius(x, n, conf=0.95): left, right = binomial_conf_interval(x, n, conf)