some transformations to speed up binomial_conf_interval
[p2pool.git] / p2pool / util / math.py
1 from __future__ import absolute_import, division
2
3 import __builtin__
4 import math
5 import random
6
7 def median(x, use_float=True):
8     # there exist better algorithms...
9     y = sorted(x)
10     if not y:
11         raise ValueError('empty sequence!')
12     left = (len(y) - 1)//2
13     right = len(y)//2
14     sum = y[left] + y[right]
15     if use_float:
16         return sum/2
17     else:
18         return sum//2
19
20 def mean(x):
21     total = 0
22     count = 0
23     for y in x:
24         total += y
25         count += 1
26     return total/count
27
28 def shuffled(x):
29     x = list(x)
30     random.shuffle(x)
31     return x
32
33 def shift_left(n, m):
34     # python: :(
35     if m >= 0:
36         return n << m
37     return n >> -m
38
39 def clip(x, (low, high)):
40     if x < low:
41         return low
42     elif x > high:
43         return high
44     else:
45         return x
46
47 def nth(i, n=0):
48     i = iter(i)
49     for _ in xrange(n):
50         i.next()
51     return i.next()
52
53 def geometric(p):
54     if p <= 0 or p > 1:
55         raise ValueError('p must be in the interval (0.0, 1.0]')
56     if p == 1:
57         return 1
58     return int(math.log1p(-random.random()) / math.log1p(-p)) + 1
59
60 def add_dicts(*dicts):
61     res = {}
62     for d in 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)
66
67 def format(x):
68     prefixes = 'kMGTPEZY'
69     count = 0
70     while x >= 100000 and count < len(prefixes) - 2:
71         x = x//1000
72         count += 1
73     s = '' if count == 0 else prefixes[count - 1]
74     return '%i' % (x,) + s
75
76 perfect_round = lambda x: int(x + random.random())
77
78 def erf(x):
79     # save the sign of x
80     sign = 1
81     if x < 0:
82         sign = -1
83     x = abs(x)
84     
85     # constants
86     a1 =  0.254829592
87     a2 = -0.284496736
88     a3 =  1.421413741
89     a4 = -1.453152027
90     a5 =  1.061405429
91     p  =  0.3275911
92     
93     # A&S formula 7.1.26
94     t = 1.0/(1.0 + p*x)
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)
97
98 def find_root(f, fp, start, steps=10, bounds=(None, None)):
99     guess = start
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]
104     return guess
105
106 def ierf(z):
107     return find_root(lambda x: erf(x) - z, lambda guess: 2*math.e**(-guess**2)/math.sqrt(math.pi), 0)
108
109 try:
110     from scipy import special
111 except ImportError:
112     print 'Install SciPy for more accurate confidence intervals!'
113     def binomial_conf_interval(x, n, conf=0.95):
114         if n == 0:
115             return (1-conf)/2, 1-(1-conf)/2
116         # approximate - Wilson score interval
117         z = math.sqrt(2)*ierf(conf)
118         p = x/n
119         topa = p + z**2/2/n
120         topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
121         bottom = 1 + z**2/n
122         return (topa - topb)/bottom, (topa + topb)/bottom
123 else:
124     def binomial_conf_interval(x, n, conf=0.95):
125         if n == 0:
126             left = random.random()*(1 - conf)
127             return left, left + conf
128         kpdf = lambda p: p**x * (1-p)**(n-x)
129         dkpdf = lambda p: ((x*p**(x-1) * (1-p)**(n-x) - p**x * (n-x)*(1-p)**(n-x-1)) \
130             if p != 0 else {0: -n, 1: 1}.get(x, 0)*special.beta(x+1, n-x+1)) \
131             if p != 1 else {n-1: -1, n: n}.get(x, 0)*special.beta(x+1, n-x+1)
132         cdf = lambda p: special.betainc(x+1, n-x+1, p)
133         invcdf = lambda i: special.betaincinv(x+1, n-x+1, i)
134         left_to_right = lambda left: invcdf(cdf(left) + conf)
135         dleft_to_right = lambda left: kpdf(left)/kpdf(invcdf(cdf(left) + conf))
136         f = lambda left: kpdf(left_to_right(left)) - kpdf(left)
137         df = lambda left: dkpdf(left_to_right(left))*dleft_to_right(left) - dkpdf(left)
138         left = find_root(f, df, invcdf((1-conf)/2), 8, (0,  invcdf(1-conf)))
139         return left, left_to_right(left)
140
141 def binomial_conf_center_radius(x, n, conf=0.95):
142     left, right = binomial_conf_interval(x, n, conf)
143     if n == 0:
144         return (left+right)/2, (right-left)/2
145     p = x/n
146     return p, max(p - left, right - p)
147
148 def reversed(x):
149     try:
150         return __builtin__.reversed(x)
151     except TypeError:
152         return reversed(list(x))
153
154 class Object(object):
155     def __init__(self, **kwargs):
156         for k, v in kwargs.iteritems():
157             setattr(self, k, v)
158
159 def add_tuples(res, *tuples):
160     for t in tuples:
161         if len(t) != len(res):
162             raise ValueError('tuples must all be the same length')
163         res = tuple(a + b for a, b in zip(res, t))
164     return res
165
166 def flatten_linked_list(x):
167     while x is not None:
168         x, cur = x
169         yield cur
170
171 def weighted_choice(choices):
172     choices = list((item, weight) for item, weight in choices)
173     target = random.randrange(sum(weight for item, weight in choices))
174     for item, weight in choices:
175         if weight > target:
176             return item
177         target -= weight
178     raise AssertionError()
179
180 if __name__ == '__main__':
181     import random
182     a = 1
183     while True:
184         print a, format(a) + 'H/s'
185         a = a * random.randrange(2, 5)