From 2ec1b55b1dfc9e32d5a8ef3845d332ccb7597252 Mon Sep 17 00:00:00 2001 From: Forrest Voight Date: Mon, 23 Jan 2012 23:42:42 -0500 Subject: [PATCH] improved binomial_conf_interval --- p2pool/util/math.py | 14 ++++++++++++-- 1 files changed, 12 insertions(+), 2 deletions(-) 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) -- 1.7.1