Created
July 28, 2017 11:37
-
-
Save awemany/57b95a24b09b648bda1db9509f5dcba9 to your computer and use it in GitHub Desktop.
Peter Rizun's memorylessness bet against CSW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# CSW vs. Peter Rizun bet demo by awemany | |
# Note this discretizes time into seconds for easier understanding. This will | |
# however-so-slightly bias the calculations in here, due to coincidences etc. | |
import numpy as np | |
MEAN_BLOCK_TIME = 600 | |
minute = 60 | |
T0 = 10000 | |
Tmax = 20000 | |
def blockTimes(tmax, hpf): | |
""" Create a boolean array indexed by time (in seconds) where, | |
'block found' is marked with 'true' and 'no block found' is marked with false. | |
hpf is hash power fraction. """ | |
r = np.zeros(tmax, bool) | |
nblocks = int(hpf * tmax / MEAN_BLOCK_TIME) # number of blocks expected | |
n=0 | |
while n < nblocks: | |
time = np.random.randint(low = 0, | |
high = tmax) | |
r[time] = True | |
n+=1 | |
return r | |
def blockTimesButNotIn(tmax, tleft, tright, hpf): | |
""" Like blockTimes(..) above, but will exclude blocks from being produced in the interval [tleft, tright). """ | |
r = np.zeros(tmax, bool) | |
nblocks = int(hpf * (tmax-(tright-tleft)) / MEAN_BLOCK_TIME) # number of blocks expected | |
n = 0 | |
while n < nblocks: | |
time = np.random.randint(low = 0, | |
high = tmax) | |
if time < tleft or time > tright: | |
r[time] = True | |
n+=1 | |
return r | |
def nextBlockAfter(B, t): | |
""" Returns number of seconds block comes in after t. Assumes a block comes | |
after time t, might fail (very unlikely for large number of blocks). """ | |
return np.nonzero(B[t:])[0][0] | |
# Scenario: | |
# Normal operation, generate blocks every 10min and look for the delta time | |
# Calculate delta time of next block after T0 for 10000 runs | |
delta_times = [nextBlockAfter(blockTimes(Tmax, 1.0), T0) for _ in range(10000)] | |
# Scenario: | |
# Hashing with 2/3rds of hash power. A priori condition that no block happened | |
# in interval [T0, T0 + 10minutes). | |
# Calculate delta time of next block after T0 for 10000 runs | |
x_delta_times = [nextBlockAfter(blockTimesButNotIn(Tmax, T0, T0+10*minute, 2/3.), T0) for _ in range(10000)] | |
print "Mean delta time for block after T0:", np.mean(delta_times) | |
print "Mean delta time for block after T0, with T0..T0+600s excluded:", np.mean(x_delta_times) | |
# Results: CSW is wrong and PeterR is right. 600s and 1500s (10min and 25min from T0) | |
# optional part: histograms (change if to 0 if you don't care) | |
if 1: | |
from matplotlib import pyplot as plt | |
plt.hist(x_delta_times, bins=100) | |
plt.hist(delta_times, bins=100) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment