naman
naman

Reputation: 35

Monte-Carlo Simulation of expected tosses for two consecutive heads in python

The expected number of tosses to get two heads in a row is 6. However on executing the below simulation of multiple runs of this experiment, I get a different plot from that expected. Could you help me identify the error in the logic? Thanks

Code to simulate tosses and check at when two consecutive heads are encountered repeated 10000 times:

import random
def toss():
    return random.randint(0, 1)

expected_tosses=list()
for test in range(10000):
    a=toss()
    for i in range(2,100):
        b=toss()
        if a and b:
            expected_tosses.append(i)
            break
        a=b

import matplotlib.pyplot as plt
plt.hist(expected_tosses, range(1,10)) 
plt.show()

Histogram of number of tosses to get two consecutive heads

Upvotes: 1

Views: 478

Answers (2)

LIU ZHIWEN
LIU ZHIWEN

Reputation: 173

Here is a function to simulate N consecutive heads:

import numpy as np

def toss1():
    return np.random.randint(0, 2)

def headN(N):
    # expected number of tosses 
    # to get N consecutive heads
    i = 0
    temp = [0]*N
    while np.sum(temp)<N:
        temp[i%N] = toss1()
        i += 1
    return i

Upvotes: 0

sascha
sascha

Reputation: 33532

A: Some subtle error in the code

You are changing the statistics by limiting your upper-loop. Use an infinite loop instead.

The code then looks like the following which should be more precise:

import random
def toss():
    return random.randint(0, 1)

expected_tosses=list()
for test in range(10000):
    a=toss()

    i = 2
    while True:
        b=toss()
        if a and b:
            expected_tosses.append(i)
            break
        a=b
        i+=1

import matplotlib.pyplot as plt
print(sum(expected_tosses) / float(len(expected_tosses)))
plt.hist(expected_tosses, range(1,10))
plt.show()

B: Interpretation of the output

The results are okay!

I introduced a calculation of the mean of tosses needed (in the code above) and print it out. You will see, that the means looks like what you expected!

Some example output (we don't use a seed so results will differ each run):

5.9941

Just to be clear: The mean does not tell you that much about the shape of the histogram. Maybe this is the source of your confusion (having a near-minimum at 6 in your plot).

Upvotes: 3

Related Questions