1 from __future__ import absolute_import, division
7 def median(x, use_float=True):
8 # there exist better algorithms...
11 raise ValueError('empty sequence!')
12 left = (len(y) - 1)//2
14 sum = y[left] + y[right]
39 def clip(x, (low, high)):
55 raise ValueError('p must be in the interval (0.0, 1.0]')
58 return int(math.log1p(-random.random()) / math.log1p(-p)) + 1
60 def add_dicts(*dicts):
63 for k, v in d.iteritems():
64 res[k] = res.get(k, 0) + v
65 return dict((k, v) for k, v in res.iteritems() if v)
70 while x >= 100000 and count < len(prefixes) - 2:
73 s = '' if count == 0 else prefixes[count - 1]
74 return '%i' % (x,) + s
79 if random.random() >= b:
101 y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
102 return sign*y # erf(-x) = -erf(x)
104 def ierf(z, steps=10):
106 for i in xrange(steps):
107 d = 2*math.e**(-guess**2)/math.sqrt(math.pi)
108 guess = guess - (erf(guess) - z)/d
111 def binomial_conf_interval(x, n, conf=0.95):
112 # approximate - Wilson score interval
113 z = math.sqrt(2)*ierf(conf)
116 topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
118 return (topa - topb)/bottom, (topa + topb)/bottom
121 def binomial_conf_center_radius(x, n, conf=0.95):
123 left, right = binomial_conf_interval(x, n, conf)
124 return p, max(p - left, right - p)
128 return __builtin__.reversed(x)
130 return reversed(list(x))
132 class Object(object):
133 def __init__(self, **kwargs):
134 for k, v in kwargs.iteritems():
137 def add_tuples(res, *tuples):
139 if len(t) != len(res):
140 raise ValueError('tuples must all be the same length')
141 res = tuple(a + b for a, b in zip(res, t))
144 def flatten_linked_list(x):
149 def weighted_choice(choices):
150 choices = list((item, weight) for item, weight in choices)
151 target = random.randrange(sum(weight for item, weight in choices))
152 for item, weight in choices:
156 raise AssertionError()
158 if __name__ == '__main__':
162 print a, format(a) + 'H/s'
163 a = a * random.randrange(2, 5)