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

我的未来我决定 提交于 2019-12-12 03:03:56

问题


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()


回答1:


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).



来源:https://stackoverflow.com/questions/40556462/monte-carlo-simulation-of-expected-tosses-for-two-consecutive-heads-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!