added format_dt function and used it in status displays
[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 def format_dt(dt):
77     for value, name in [(60*60*24, 'days'), (60*60, 'hours'), (60, 'minutes'), (1, 'seconds')]:
78         if dt > value:
79             break
80     return '%.01f %s' % (dt/value, name)
81
82 perfect_round = lambda x: int(x + random.random())
83
84 def erf(x):
85     # save the sign of x
86     sign = 1
87     if x < 0:
88         sign = -1
89     x = abs(x)
90     
91     # constants
92     a1 =  0.254829592
93     a2 = -0.284496736
94     a3 =  1.421413741
95     a4 = -1.453152027
96     a5 =  1.061405429
97     p  =  0.3275911
98     
99     # A&S formula 7.1.26
100     t = 1.0/(1.0 + p*x)
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)
103
104 def find_root(y_over_dy, start, steps=10, bounds=(None, None)):
105     guess = start
106     for i in xrange(steps):
107         prev, guess = guess, guess - y_over_dy(guess)
108         if bounds[0] is not None and guess < bounds[0]: guess = bounds[0]
109         if bounds[1] is not None and guess > bounds[1]: guess = bounds[1]
110         if guess == prev:
111             break
112     return guess
113
114 def ierf(z):
115     return find_root(lambda x: (erf(x) - z)/(2*math.e**(-x**2)/math.sqrt(math.pi)), 0)
116
117 try:
118     from scipy import special
119 except ImportError:
120     print 'Install SciPy for more accurate confidence intervals!'
121     def binomial_conf_interval(x, n, conf=0.95):
122         assert 0 <= x <= n and 0 <= conf < 1
123         if n == 0:
124             left = random.random()*(1 - conf)
125         # approximate - Wilson score interval
126         z = math.sqrt(2)*ierf(conf)
127         p = x/n
128         topa = p + z**2/2/n
129         topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2)
130         bottom = 1 + z**2/n
131         return (topa - topb)/bottom, (topa + topb)/bottom
132 else:
133     def binomial_conf_interval(x, n, conf=0.95):
134         assert 0 <= x <= n and 0 <= conf < 1
135         if n == 0:
136             left = random.random()*(1 - conf)
137             return left, left + conf
138         bl = float(special.betaln(x+1, n-x+1))
139         def f(left_a):
140             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)))
141             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)
142             bottom = (x - n*right)*left*(1-left) - (x - n*left)*right*(1-right)
143             return top/bottom
144         left_a = find_root(f, (1-conf)/2, bounds=(0, 1-conf))
145         return float(special.betaincinv(x+1, n-x+1, left_a)), float(special.betaincinv(x+1, n-x+1, left_a + conf))
146
147 def binomial_conf_center_radius(x, n, conf=0.95):
148     assert 0 <= x <= n and 0 <= conf < 1
149     left, right = binomial_conf_interval(x, n, conf)
150     if n == 0:
151         return (left+right)/2, (right-left)/2
152     p = x/n
153     return p, max(p - left, right - p)
154
155 minmax = lambda x: (min(x), max(x))
156
157 def format_binomial_conf(x, n, conf=0.95, f=lambda x: x):
158     if n == 0:
159         return '???'
160     left, right = minmax(map(f, binomial_conf_interval(x, n, conf)))
161     return '~%.1f%% (%.f-%.f%%)' % (100*f(x/n), 100*left-1/2, 100*right+1/2)
162
163 def reversed(x):
164     try:
165         return __builtin__.reversed(x)
166     except TypeError:
167         return reversed(list(x))
168
169 class Object(object):
170     def __init__(self, **kwargs):
171         for k, v in kwargs.iteritems():
172             setattr(self, k, v)
173
174 def add_tuples(res, *tuples):
175     for t in tuples:
176         if len(t) != len(res):
177             raise ValueError('tuples must all be the same length')
178         res = tuple(a + b for a, b in zip(res, t))
179     return res
180
181 def flatten_linked_list(x):
182     while x is not None:
183         x, cur = x
184         yield cur
185
186 def weighted_choice(choices):
187     choices = list((item, weight) for item, weight in choices)
188     target = random.randrange(sum(weight for item, weight in choices))
189     for item, weight in choices:
190         if weight > target:
191             return item
192         target -= weight
193     raise AssertionError()
194
195 def natural_to_string(n, alphabet=None):
196     if n < 0:
197         raise TypeError('n must be a natural')
198     if alphabet is None:
199         s = '%x' % (n,)
200         if len(s) % 2:
201             s = '0' + s
202         return s.decode('hex')
203     else:
204         assert len(set(alphabet)) == len(alphabet)
205         res = []
206         while n:
207             n, x = divmod(n, len(alphabet))
208             res.append(alphabet[x])
209         res.reverse()
210         return ''.join(res)
211
212 def string_to_natural(s, alphabet=None):
213     if alphabet is None:
214         assert not s.startswith('\x00')
215         return int(s.encode('hex'), 16) if s else 0
216     else:
217         assert len(set(alphabet)) == len(alphabet)
218         assert not s.startswith(alphabet[0])
219         return sum(alphabet.index(char) * len(alphabet)**i for i, char in enumerate(reversed(s)))
220
221 if __name__ == '__main__':
222     import random
223     a = 1
224     while True:
225         print a, format(a) + 'H/s'
226         a = a * random.randrange(2, 5)