In [1]:
import scipy
In [23]:
#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
Out[23]:
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])
In [27]:
#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
In [29]:
#we got one sequence, lets have a look
s
Out[29]:
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])
In [33]:
prior = s[:20] #prior observation: 20 heads
future = s[20:]#future observations: 20 more random coin tosses
In [35]:
prior #as expected the prior observation is 20 tails in a row
Out[35]:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In [36]:
future #the future observation still has a 50/50 distribution of heads and tails, it doesn't 'compensate' for prior in any way
Out[36]:
array([0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0])
In [46]:
#histogram of the prior observations.. 100% tails
ax = plt.hist(prior, bins = 4)
In [45]:
#histogram of the future observations: roughly 50/50. A
ax = plt.hist(future, bins = 4) 
In [44]:
#histogram of the complete trial: highly skewed on tails
ax = plt.hist(s, bins = 4)
In []:
#remember this is a series of completely random coin tosses