X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Futil%2Fmath.py;h=04ed3ffa2ed2d2f0cf78c5292a68f5f3be702bc9;hb=62a65ed608004510a8207ba16a2acacfd3aaef24;hp=a484c040dbe9cd907cd7edef4d31018a58cd174c;hpb=59b23dbea8bef11a75170f9cf5b263fd6226c607;p=p2pool.git diff --git a/p2pool/util/math.py b/p2pool/util/math.py index a484c04..04ed3ff 100644 --- a/p2pool/util/math.py +++ b/p2pool/util/math.py @@ -45,6 +45,8 @@ def clip(x, (low, high)): else: return x +add_to_range = lambda x, (low, high): (min(low, x), max(high, x)) + def nth(i, n=0): i = iter(i) for _ in xrange(n): @@ -58,12 +60,17 @@ def geometric(p): return 1 return int(math.log1p(-random.random()) / math.log1p(-p)) + 1 -def add_dicts(*dicts): - res = {} - for d in dicts: - for k, v in d.iteritems(): - res[k] = res.get(k, 0) + v - return dict((k, v) for k, v in res.iteritems() if v) +def add_dicts_ext(add_func=lambda a, b: a+b, zero=0): + def add_dicts(*dicts): + res = {} + for d in dicts: + for k, v in d.iteritems(): + res[k] = add_func(res.get(k, zero), v) + return dict((k, v) for k, v in res.iteritems() if v != zero) + return add_dicts +add_dicts = add_dicts_ext() + +mult_dict = lambda c, x: dict((k, c*v) for k, v in x.iteritems()) def format(x): prefixes = 'kMGTPEZY' @@ -75,7 +82,7 @@ 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) @@ -115,35 +122,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 (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)) @@ -189,7 +179,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') @@ -236,13 +226,7 @@ class RateMonitor(object): def add_datum(self, datum): self._prune() t = time.time() - self.datums.append((t, datum)) if self.first_timestamp is None: self.first_timestamp = t - -if __name__ == '__main__': - import random - a = 1 - while True: - print a, format(a) + 'H/s' - a = a * random.randrange(2, 5) + else: + self.datums.append((t, datum))