X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Futil%2Fmath.py;h=71b5dec584657c10da92e366c2f8b429d3dc0981;hb=4e7adf06f15f9bca409a5a5fe3ef03deac7d2268;hp=0e2866e21b337f35a89a0d46e592a5a4fec7d817;hpb=055844fcd2b845bfd4ac68e2e29ae97eccbc535c;p=p2pool.git diff --git a/p2pool/util/math.py b/p2pool/util/math.py index 0e2866e..71b5dec 100644 --- a/p2pool/util/math.py +++ b/p2pool/util/math.py @@ -82,7 +82,13 @@ def format(x): return '%i' % (x,) + s def format_dt(dt): - for value, name in [(60*60*24, 'days'), (60*60, 'hours'), (60, 'minutes'), (1, 'seconds')]: + for value, name in [ + (365.2425*60*60*24, 'years'), + (60*60*24, 'days'), + (60*60, 'hours'), + (60, 'minutes'), + (1, 'seconds'), + ]: if dt > value: break return '%.01f %s' % (dt/value, name) @@ -122,35 +128,18 @@ def find_root(y_over_dy, start, steps=10, bounds=(None, None)): def ierf(z): return find_root(lambda x: (erf(x) - z)/(2*math.e**(-x**2)/math.sqrt(math.pi)), 0) -try: - from scipy import special -except ImportError: - def binomial_conf_interval(x, n, conf=0.95): - assert 0 <= x <= n and 0 <= conf < 1 - if n == 0: - left = random.random()*(1 - conf) - return left, left + conf - # approximate - Wilson score interval - z = math.sqrt(2)*ierf(conf) - p = x/n - topa = p + z**2/2/n - topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2) - bottom = 1 + z**2/n - return [clip(x, (0, 1)) for x in add_to_range(x/n, [(topa - topb)/bottom, (topa + topb)/bottom])] -else: - def binomial_conf_interval(x, n, conf=0.95): - assert 0 <= x <= n and 0 <= conf < 1 - if n == 0: - left = random.random()*(1 - conf) - return left, left + conf - bl = float(special.betaln(x+1, n-x+1)) - def f(left_a): - 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))) - 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) - bottom = (x - n*right)*left*(1-left) - (x - n*left)*right*(1-right) - return top/bottom - left_a = find_root(f, (1-conf)/2, bounds=(0, 1-conf)) - return float(special.betaincinv(x+1, n-x+1, left_a)), float(special.betaincinv(x+1, n-x+1, left_a + conf)) +def binomial_conf_interval(x, n, conf=0.95): + assert 0 <= x <= n and 0 <= conf < 1 + if n == 0: + left = random.random()*(1 - conf) + return left, left + conf + # approximate - Wilson score interval + z = math.sqrt(2)*ierf(conf) + p = x/n + topa = p + z**2/2/n + topb = z * math.sqrt(p*(1-p)/n + z**2/4/n**2) + bottom = 1 + z**2/n + return [clip(x, (0, 1)) for x in add_to_range(x/n, [(topa - topb)/bottom, (topa + topb)/bottom])] minmax = lambda x: (min(x), max(x)) @@ -196,7 +185,7 @@ def natural_to_string(n, alphabet=None): if n < 0: raise TypeError('n must be a natural') if alphabet is None: - s = '%x' % (n,) + s = ('%x' % (n,)).lstrip('0') if len(s) % 2: s = '0' + s return s.decode('hex') @@ -248,9 +237,7 @@ class RateMonitor(object): else: self.datums.append((t, datum)) -if __name__ == '__main__': - import random - a = 1 - while True: - print a, format(a) + 'H/s' - a = a * random.randrange(2, 5) +def merge_dicts(*dicts): + res = {} + for d in dicts: res.update(d) + return res