+from __future__ import division
+
import random
import unittest
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)
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):
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