Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
"""Attempt to confirm results in paper by Miller and Sanjurjo
["Confused by the Gambler's and Hot Hand Falacy?..."](
Calculate the probability of a streak of length 2 in a coin flip
for a small sequence of attempts ("finite sequence of iid Bernoulli trials").
The fencepost counting issue means you have to ignore one of the flips.
Perhaps this is what causes Miller and Sanjurjo to get 0.4 instead of 0.5.
"the conditional relative frequency... is 0.4 not 0.5"
from random import randint
h, t = 0, 0
# L is the streak length but is unused (2 is hard coded into the logic)
L, M, N = 2, 1000, 4
for i in range(M):
flip0 = randint(0, 1)
for j in range(N-1):
flip1 = randint(0, 1)
if flip0:
h += flip1
t += not flip1
flip0 = flip1
print float(h) / float(h + t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment