return '%i' % (x,) + s
def format_dt(dt):
- for value, name in [(60*60*24, 'days'), (60*60, 'hours'), (60, 'minutes'), (1, 'seconds')]:
+ for value, name in [(365.2425*60*60*24, 'years'), (60*60*24, 'days'), (60*60, 'hours'), (60, 'minutes'), (1, 'seconds')]:
if dt > value:
break
return '%.01f %s' % (dt/value, name)
def ierf(z):
return find_root(lambda x: (erf(x) - z)/(2*math.e**(-x**2)/math.sqrt(math.pi)), 0)
-try:
- from scipy import special
-except ImportError:
- def binomial_conf_interval(x, n, conf=0.95):
- assert 0 <= x <= n and 0 <= conf < 1
- if n == 0:
- left = random.random()*(1 - conf)
- return left, left + conf
- # approximate - Wilson score interval
- z = math.sqrt(2)*ierf(conf)
- p = x/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 [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
- if n == 0:
- left = random.random()*(1 - conf)
- return left, left + conf
- bl = float(special.betaln(x+1, n-x+1))
- def f(left_a):
- left, right = max(1e-8, float(special.betaincinv(x+1, n-x+1, left_a))), min(1-1e-8, float(special.betaincinv(x+1, n-x+1, left_a + conf)))
- top = math.exp(math.log(right)*(x+1) + math.log(1-right)*(n-x+1) + math.log(left) + math.log(1-left) - bl) - math.exp(math.log(left)*(x+1) + math.log(1-left)*(n-x+1) + math.log(right) + math.log(1-right) - bl)
- bottom = (x - n*right)*left*(1-left) - (x - n*left)*right*(1-right)
- return top/bottom
- left_a = find_root(f, (1-conf)/2, bounds=(0, 1-conf))
- return float(special.betaincinv(x+1, n-x+1, left_a)), float(special.betaincinv(x+1, n-x+1, left_a + conf))
+def binomial_conf_interval(x, n, conf=0.95):
+ assert 0 <= x <= n and 0 <= conf < 1
+ if n == 0:
+ left = random.random()*(1 - conf)
+ return left, left + conf
+ # approximate - Wilson score interval
+ z = math.sqrt(2)*ierf(conf)
+ p = x/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 [clip(x, (0, 1)) for x in add_to_range(x/n, [(topa - topb)/bottom, (topa + topb)/bottom])]
minmax = lambda x: (min(x), max(x))
if n < 0:
raise TypeError('n must be a natural')
if alphabet is None:
- s = '%x' % (n,)
+ s = ('%x' % (n,)).lstrip('0')
if len(s) % 2:
s = '0' + s
return s.decode('hex')
def add_datum(self, datum):
self._prune()
t = time.time()
- self.datums.append((t, datum))
if self.first_timestamp is None:
self.first_timestamp = t
-
-if __name__ == '__main__':
- import random
- a = 1
- while True:
- print a, format(a) + 'H/s'
- a = a * random.randrange(2, 5)
+ else:
+ self.datums.append((t, datum))