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
76 perfect_round = lambda x: int(x + random.random())
95 y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
96 return sign*y # erf(-x) = -erf(x)
98 def find_root(f, fp, start, steps=10, bounds=(None, None)):
100 for i in xrange(steps):
101 guess = guess - f(guess)/fp(guess)
102 if bounds[0] is not None and guess < bounds[0]: guess = bounds[0]
103 if bounds[1] is not None and guess > bounds[1]: guess = bounds[1]
107 return find_root(lambda x: erf(x) - z, lambda guess: 2*math.e**(-guess**2)/math.sqrt(math.pi), 0)
110 from scipy import special
112 print 'Install SciPy for more accurate confidence intervals!'
113 def binomial_conf_interval(x, n, conf=0.95):
115 return (1-conf)/2, 1-(1-conf)/2
116 # approximate - Wilson score interval
117 z = math.sqrt(2)*ierf(conf)
120 topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
122 return (topa - topb)/bottom, (topa + topb)/bottom
124 def binomial_conf_interval(x, n, conf=0.95):
126 left = random.random()*(1 - conf)
127 return left, left + conf
128 pdf = lambda p: p**x * (1-p)**(n-x) /special.beta(x+1, n-x+1)
129 dpdf = lambda p: ((x*p**(x-1) * (1-p)**(n-x) - p**x * (n-x)*(1-p)**(n-x-1))/special.beta(x+1, n-x+1) \
130 if p != 0 else {0: -n, 1: 1}.get(x, 0)) \
131 if p != 1 else {n-1: -1, n: n}.get(x, 0)
132 cdf = lambda p: special.betainc(x+1, n-x+1, p)
134 invcdf = lambda i: special.betaincinv(x+1, n-x+1, i)
135 dinvcdf = lambda i: 1/pdf(invcdf(i))
136 left_to_right = lambda left: invcdf(cdf(left) + conf)
137 dleft_to_right = lambda left: dinvcdf(cdf(left) + conf)*dcdf(left)
138 f = lambda left: pdf(left_to_right(left)) - pdf(left)
139 df = lambda left: dpdf(left_to_right(left))*dleft_to_right(left) - dpdf(left)
140 left = find_root(f, df, invcdf((1-conf)/2), 8, (0, invcdf(1-conf)))
141 return left, left_to_right(left)
143 def binomial_conf_center_radius(x, n, conf=0.95):
144 left, right = binomial_conf_interval(x, n, conf)
146 return (left+right)/2, (right-left)/2
148 return p, max(p - left, right - p)
152 return __builtin__.reversed(x)
154 return reversed(list(x))
156 class Object(object):
157 def __init__(self, **kwargs):
158 for k, v in kwargs.iteritems():
161 def add_tuples(res, *tuples):
163 if len(t) != len(res):
164 raise ValueError('tuples must all be the same length')
165 res = tuple(a + b for a, b in zip(res, t))
168 def flatten_linked_list(x):
173 def weighted_choice(choices):
174 choices = list((item, weight) for item, weight in choices)
175 target = random.randrange(sum(weight for item, weight in choices))
176 for item, weight in choices:
180 raise AssertionError()
182 if __name__ == '__main__':
186 print a, format(a) + 'H/s'
187 a = a * random.randrange(2, 5)