fixed binom_conf_interval and test case
authorForrest Voight <forrest@forre.st>
Fri, 13 Apr 2012 17:19:41 +0000 (13:19 -0400)
committerForrest Voight <forrest@forre.st>
Fri, 13 Apr 2012 17:19:41 +0000 (13:19 -0400)
p2pool/test/util/test_math.py
p2pool/util/math.py

index 30f6212..d81d9a0 100644 (file)
@@ -1,3 +1,5 @@
+from __future__ import division
+
 import random
 import unittest
 
@@ -26,7 +28,7 @@ class Test(unittest.TestCase):
                 self.assertEquals(n, n2)
     
     def test_binom(self):
-        for n in xrange(100):
+        for n in xrange(1, 100):
             for x in xrange(n + 1):
                 left, right = math.binomial_conf_interval(x, n)
-                assert left >= 0 and right <= 1
+                assert 0 <= left <= x/n <= right <= 1, (left, right, x, n)
index aa5ea8a..1d71bb0 100644 (file)
@@ -45,6 +45,8 @@ def clip(x, (low, high)):
     else:
         return x
 
+add_to_range = lambda x, (low, high): (min(low, x), max(high, x))
+
 def nth(i, n=0):
     i = iter(i)
     for _ in xrange(n):
@@ -131,7 +133,7 @@ except ImportError:
         topa = p + z**2/2/n
         topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
         bottom = 1 + z**2/n
-        return (topa - topb)/bottom, (topa + topb)/bottom
+        return [clip(x, (0, 1)) for x in add_to_range(x/n, [(topa - topb)/bottom, (topa + topb)/bottom])]
 else:
     def binomial_conf_interval(x, n, conf=0.95):
         assert 0 <= x <= n and 0 <= conf < 1