import scipy
#this is how we make a sequence of coin tosses
#each trial makes 40 choices between either 0 (heads) or 1 (tails) with 50/50 probability
s = scipy.random.choice([0,1], size = 40)
s
array([0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1])
#here we keep trying many many times until we get a random sequence where the sum of the first 20 attempts is 20. That is, we got 20 heads in a row
s = []
for i in range(10000000):
s = scipy.random.choice([0,1], size = 40)
prior = s[0:20]
if sum(prior) == 20:
print "found"
break
found
#we got one sequence, lets have a look
s
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0])
prior = s[:20] #prior observation: 20 heads
future = s[20:]#future observations: 20 more random coin tosses
prior #as expected the prior observation is 20 tails in a row
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
future #the future observation still has a 50/50 distribution of heads and tails, it doesn't 'compensate' for prior in any way
array([0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0])
#histogram of the prior observations.. 100% tails
ax = plt.hist(prior, bins = 4)
#histogram of the future observations: roughly 50/50. A
ax = plt.hist(future, bins = 4)
#histogram of the complete trial: highly skewed on tails
ax = plt.hist(s, bins = 4)
#remember this is a series of completely random coin tosses