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(y_over_dy, start, steps=10, bounds=(None, None)):
100 for i in xrange(steps):
101 prev, guess = guess, guess - y_over_dy(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]
109 return find_root(lambda x: (erf(x) - z)/(2*math.e**(-x**2)/math.sqrt(math.pi)), 0)
112 from scipy import special
114 print 'Install SciPy for more accurate confidence intervals!'
115 def binomial_conf_interval(x, n, conf=0.95):
116 assert 0 <= x <= n and 0 <= conf < 1
118 left = random.random()*(1 - conf)
119 # approximate - Wilson score interval
120 z = math.sqrt(2)*ierf(conf)
123 topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
125 return (topa - topb)/bottom, (topa + topb)/bottom
127 def binomial_conf_interval(x, n, conf=0.95):
128 assert 0 <= x <= n and 0 <= conf < 1
130 left = random.random()*(1 - conf)
131 return left, left + conf
132 b = special.beta(x+1, n-x+1)
134 left, right = max(1e-8, special.betaincinv(x+1, n-x+1, left_a)), min(1-1e-8, special.betaincinv(x+1, n-x+1, left_a + conf))
135 top = right**(x+1) * (1-right)**(n-x+1) * left*(1-left) - left**(x+1) * (1-left)**(n-x+1) * right * (1-right)
136 bottom = (x - n*right)*left*(1-left) - (x - n*left)*right*(1-right)
138 left_a = find_root(f, (1-conf)/2, bounds=(0, 1-conf))
139 return special.betaincinv(x+1, n-x+1, left_a), special.betaincinv(x+1, n-x+1, left_a + conf)
141 def binomial_conf_center_radius(x, n, conf=0.95):
142 assert 0 <= x <= n and 0 <= conf < 1
143 left, right = binomial_conf_interval(x, n, conf)
145 return (left+right)/2, (right-left)/2
147 return p, max(p - left, right - p)
149 minmax = lambda x: (min(x), max(x))
151 def format_binomial_conf(x, n, conf=0.95, f=lambda x: x):
154 left, right = minmax(map(f, binomial_conf_interval(x, n, conf)))
155 return '~%.1f%% (%i-%i%%)' % (100*f(x/n), int(100*left), 100-int(100-100*right))
159 return __builtin__.reversed(x)
161 return reversed(list(x))
163 class Object(object):
164 def __init__(self, **kwargs):
165 for k, v in kwargs.iteritems():
168 def add_tuples(res, *tuples):
170 if len(t) != len(res):
171 raise ValueError('tuples must all be the same length')
172 res = tuple(a + b for a, b in zip(res, t))
175 def flatten_linked_list(x):
180 def weighted_choice(choices):
181 choices = list((item, weight) for item, weight in choices)
182 target = random.randrange(sum(weight for item, weight in choices))
183 for item, weight in choices:
187 raise AssertionError()
189 def natural_to_string(n, alphabet=None):
191 raise TypeError('n must be a natural')
196 return s.decode('hex')
198 assert len(set(alphabet)) == len(alphabet)
201 n, x = divmod(n, len(alphabet))
202 res.append(alphabet[x])
206 def string_to_natural(s, alphabet=None):
208 assert not s.startswith('\x00')
209 return int(s.encode('hex'), 16) if s else 0
211 assert len(set(alphabet)) == len(alphabet)
212 assert not s.startswith(alphabet[0])
213 return sum(alphabet.index(char) * len(alphabet)**i for i, char in enumerate(reversed(s)))
215 if __name__ == '__main__':
219 print a, format(a) + 'H/s'
220 a = a * random.randrange(2, 5)