fixed binom_conf_interval and test case
[p2pool.git] / p2pool / util / math.py
1 from __future__ import absolute_import, division
2
3 import __builtin__
4 import math
5 import random
6 import time
7
8 def median(x, use_float=True):
9     # there exist better algorithms...
10     y = sorted(x)
11     if not y:
12         raise ValueError('empty sequence!')
13     left = (len(y) - 1)//2
14     right = len(y)//2
15     sum = y[left] + y[right]
16     if use_float:
17         return sum/2
18     else:
19         return sum//2
20
21 def mean(x):
22     total = 0
23     count = 0
24     for y in x:
25         total += y
26         count += 1
27     return total/count
28
29 def shuffled(x):
30     x = list(x)
31     random.shuffle(x)
32     return x
33
34 def shift_left(n, m):
35     # python: :(
36     if m >= 0:
37         return n << m
38     return n >> -m
39
40 def clip(x, (low, high)):
41     if x < low:
42         return low
43     elif x > high:
44         return high
45     else:
46         return x
47
48 add_to_range = lambda x, (low, high): (min(low, x), max(high, x))
49
50 def nth(i, n=0):
51     i = iter(i)
52     for _ in xrange(n):
53         i.next()
54     return i.next()
55
56 def geometric(p):
57     if p <= 0 or p > 1:
58         raise ValueError('p must be in the interval (0.0, 1.0]')
59     if p == 1:
60         return 1
61     return int(math.log1p(-random.random()) / math.log1p(-p)) + 1
62
63 def add_dicts(*dicts):
64     res = {}
65     for d in dicts:
66         for k, v in d.iteritems():
67             res[k] = res.get(k, 0) + v
68     return dict((k, v) for k, v in res.iteritems() if v)
69
70 mult_dict = lambda c, x: dict((k, c*v) for k, v in x.iteritems())
71
72 def format(x):
73     prefixes = 'kMGTPEZY'
74     count = 0
75     while x >= 100000 and count < len(prefixes) - 2:
76         x = x//1000
77         count += 1
78     s = '' if count == 0 else prefixes[count - 1]
79     return '%i' % (x,) + s
80
81 def format_dt(dt):
82     for value, name in [(60*60*24, 'days'), (60*60, 'hours'), (60, 'minutes'), (1, 'seconds')]:
83         if dt > value:
84             break
85     return '%.01f %s' % (dt/value, name)
86
87 perfect_round = lambda x: int(x + random.random())
88
89 def erf(x):
90     # save the sign of x
91     sign = 1
92     if x < 0:
93         sign = -1
94     x = abs(x)
95     
96     # constants
97     a1 =  0.254829592
98     a2 = -0.284496736
99     a3 =  1.421413741
100     a4 = -1.453152027
101     a5 =  1.061405429
102     p  =  0.3275911
103     
104     # A&S formula 7.1.26
105     t = 1.0/(1.0 + p*x)
106     y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
107     return sign*y # erf(-x) = -erf(x)
108
109 def find_root(y_over_dy, start, steps=10, bounds=(None, None)):
110     guess = start
111     for i in xrange(steps):
112         prev, guess = guess, guess - y_over_dy(guess)
113         if bounds[0] is not None and guess < bounds[0]: guess = bounds[0]
114         if bounds[1] is not None and guess > bounds[1]: guess = bounds[1]
115         if guess == prev:
116             break
117     return guess
118
119 def ierf(z):
120     return find_root(lambda x: (erf(x) - z)/(2*math.e**(-x**2)/math.sqrt(math.pi)), 0)
121
122 try:
123     from scipy import special
124 except ImportError:
125     def binomial_conf_interval(x, n, conf=0.95):
126         assert 0 <= x <= n and 0 <= conf < 1
127         if n == 0:
128             left = random.random()*(1 - conf)
129             return left, left + conf
130         # approximate - Wilson score interval
131         z = math.sqrt(2)*ierf(conf)
132         p = x/n
133         topa = p + z**2/2/n
134         topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
135         bottom = 1 + z**2/n
136         return [clip(x, (0, 1)) for x in add_to_range(x/n, [(topa - topb)/bottom, (topa + topb)/bottom])]
137 else:
138     def binomial_conf_interval(x, n, conf=0.95):
139         assert 0 <= x <= n and 0 <= conf < 1
140         if n == 0:
141             left = random.random()*(1 - conf)
142             return left, left + conf
143         bl = float(special.betaln(x+1, n-x+1))
144         def f(left_a):
145             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)))
146             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)
147             bottom = (x - n*right)*left*(1-left) - (x - n*left)*right*(1-right)
148             return top/bottom
149         left_a = find_root(f, (1-conf)/2, bounds=(0, 1-conf))
150         return float(special.betaincinv(x+1, n-x+1, left_a)), float(special.betaincinv(x+1, n-x+1, left_a + conf))
151
152 minmax = lambda x: (min(x), max(x))
153
154 def format_binomial_conf(x, n, conf=0.95, f=lambda x: x):
155     if n == 0:
156         return '???'
157     left, right = minmax(map(f, binomial_conf_interval(x, n, conf)))
158     return '~%.1f%% (%.f-%.f%%)' % (100*f(x/n), math.floor(100*left), math.ceil(100*right))
159
160 def reversed(x):
161     try:
162         return __builtin__.reversed(x)
163     except TypeError:
164         return reversed(list(x))
165
166 class Object(object):
167     def __init__(self, **kwargs):
168         for k, v in kwargs.iteritems():
169             setattr(self, k, v)
170
171 def add_tuples(res, *tuples):
172     for t in tuples:
173         if len(t) != len(res):
174             raise ValueError('tuples must all be the same length')
175         res = tuple(a + b for a, b in zip(res, t))
176     return res
177
178 def flatten_linked_list(x):
179     while x is not None:
180         x, cur = x
181         yield cur
182
183 def weighted_choice(choices):
184     choices = list((item, weight) for item, weight in choices)
185     target = random.randrange(sum(weight for item, weight in choices))
186     for item, weight in choices:
187         if weight > target:
188             return item
189         target -= weight
190     raise AssertionError()
191
192 def natural_to_string(n, alphabet=None):
193     if n < 0:
194         raise TypeError('n must be a natural')
195     if alphabet is None:
196         s = '%x' % (n,)
197         if len(s) % 2:
198             s = '0' + s
199         return s.decode('hex')
200     else:
201         assert len(set(alphabet)) == len(alphabet)
202         res = []
203         while n:
204             n, x = divmod(n, len(alphabet))
205             res.append(alphabet[x])
206         res.reverse()
207         return ''.join(res)
208
209 def string_to_natural(s, alphabet=None):
210     if alphabet is None:
211         assert not s.startswith('\x00')
212         return int(s.encode('hex'), 16) if s else 0
213     else:
214         assert len(set(alphabet)) == len(alphabet)
215         assert not s.startswith(alphabet[0])
216         return sum(alphabet.index(char) * len(alphabet)**i for i, char in enumerate(reversed(s)))
217
218 class RateMonitor(object):
219     def __init__(self, max_lookback_time):
220         self.max_lookback_time = max_lookback_time
221         
222         self.datums = []
223         self.first_timestamp = None
224     
225     def _prune(self):
226         start_time = time.time() - self.max_lookback_time
227         for i, (ts, datum) in enumerate(self.datums):
228             if ts > start_time:
229                 self.datums[:] = self.datums[i:]
230                 return
231     
232     def get_datums_in_last(self, dt=None):
233         if dt is None:
234             dt = self.max_lookback_time
235         assert dt <= self.max_lookback_time
236         self._prune()
237         now = time.time()
238         return [datum for ts, datum in self.datums if ts > now - dt], min(dt, now - self.first_timestamp) if self.first_timestamp is not None else 0
239     
240     def add_datum(self, datum):
241         self._prune()
242         t = time.time()
243         self.datums.append((t, datum))
244         if self.first_timestamp is None:
245             self.first_timestamp = t
246
247 if __name__ == '__main__':
248     import random
249     a = 1
250     while True:
251         print a, format(a) + 'H/s'
252         a = a * random.randrange(2, 5)