Skip to content

Instantly share code, notes, and snippets.

@takada-at
Created May 26, 2015 17:03
Show Gist options
  • Save takada-at/9eb08262311324aab329 to your computer and use it in GitHub Desktop.
Save takada-at/9eb08262311324aab329 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import logging
import random
logger = logging.getLogger(__name__)
def matters_and_notmatters(lovals, hivals, threshold):
"""
>>> matters_and_notmatters([1, 2, 3], [4, 5, 6], 4)
([1, 2, 5, 6], [3, 4])
>>> matters_and_notmatters([1, 2, 3], [4, 5, 6], 2)
([1, 2, 3, 4, 5, 6], [])
>>> matters_and_notmatters([1, 2, 3], [4, 5, 6], 10)
([], [1, 2, 3, 4, 5, 6])
"""
notmatters = []
matters = []
for i, l in enumerate(lovals):
if abs(l - hivals[-1]) < threshold:
notmatters += lovals[i:]
break
else:
matters.append(l)
for i, h in enumerate(hivals):
if abs(h - lovals[0]) < threshold:
notmatters.append(h)
else:
matters += hivals[i:]
break
return matters, notmatters
def simulate(low, high):
assert high > low
threshold = 10.0
sig = 1.0
dz = 0.1
times = 10
diff = abs(high - low)
while abs(diff) > 1.0:
logger.debug("high: %s low: %s" % (high, low))
hivals = [random.normalvariate(high, sig) for i in range(times)]
lovals = [random.normalvariate(low, sig) for i in range(times)]
hivals.sort()
lovals.sort()
hiavg = sum(hivals) / times
loavg = sum(lovals) / times
matters, notmatters = matters_and_notmatters(lovals, hivals, threshold)
if abs(hiavg - loavg) >= threshold:
logger.debug("whether p matters! hi:%s lo:%s" % (hiavg, loavg))
else:
logger.debug("whether p does not matters! hi:%s lo:%s" % (hiavg, loavg))
if notmatters and matters:
mattersavg = sum(matters) / len(matters)
notmattersavg = sum(notmatters) / len(notmatters)
if abs(mattersavg - notmattersavg) >= threshold:
logger.debug("whether whether p matters matters ! hi:%s lo:%s" % (mattersavg, notmattersavg))
else:
logger.debug("whether whether p matters does not matters ! hi:%s lo:%s" % (mattersavg, notmattersavg))
if abs(hiavg - loavg) >= threshold and abs(mattersavg - notmattersavg) < threshold:
logger.info("whether p matters and whether whether p does not matters")
logger.info("hiavg: %s loavg: %s matters avg: %s not matters avg %s" % (hiavg, loavg,
mattersavg, notmattersavg))
print(hivals, lovals, high, low)
print(hiavg, loavg)
print(matters, notmatters)
print(mattersavg, notmattersavg)
if abs(hiavg - loavg) < threshold and abs(mattersavg - notmattersavg) >= threshold:
logger.info("whether p does not matters and whether whether p matters")
logger.info("hiavg: %s loavg: %s matters avg: %s not matters avg %s" % (hiavg, loavg,
mattersavg, notmattersavg))
high -= dz
low += dz
diff = abs(high - low)
def main():
logging.basicConfig(level=logging.INFO, format="%(message)s")
simulate(10, 100)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment