using http://hundtmath.com/probabilities-of-consecutive-coin-flipse
I get 26.6%, but the answer in a book (no explanation) is 25%.
I get 26.6%, but the answer in a book (no explanation) is 25%.
To get 26.6% I assume you're doing 1/2^5 + 15*(1/2)^6. The problem with that is it double counts sequences that have two separate 5+ runs.
I have a non brute force approach that agrees with GAT's (though there are 262008 sequences that contain 5+ runs, not 524105). I can post it this evening if you want it.
To get 26.6% I assume you're doing 1/2^5 + 15*(1/2)^6. The problem with that is it double counts sequences that have two separate 5+ runs.
import numpy as np
flips = 20 # total number of coin flips
run_length = 5 # minimum number of heads in a row
# comments below assume run_length == 5
# a[i] is the number of sequences of length i that have a run of 5 heads
a = np.zeros(flips + 1)
# N[i] is the total number of sequences of length i
N = 2**np.arange(flips + 1)
# b[i] is the number of sequences of length i that do not have a full run
# but end in 4 heads
b = np.zeros(flips + 1)
b[run_length - 1] = 1
# fill in the arrays iteratively
for i in range(run_length, flips + 1):
# Each sequence of length i - 1 with a valid run generates two sequences with runs on the ith flip
# Also, each sequence of length i - 1 without a run but ending in 4 heads
# generates one sequence with a run of 5 heads on the ith flip
a[i] = a[i - 1]*2 + b[i - 1]
# The number of sequences of length i not containing a run of 5 but ending in 4 heads
# is just the number of sequences of length (i - 5) that don't contain a run,
# because each of these can be followed by one tails and 4 heads
b[i] = N[i - run_length] - a[i - run_length]
print("%f%% of sequences of length %d have runs of at least %d heads (%d out of %d)" % (
100.*a[-1]/N[-1],
flips,
run_length,
a[-1],
N[-1]
))
That double counts runs of 6+ heads, or sequences with two separate 5+ runs. Following that logic, a sequence of 100 flips would have a 300% chance.1 in 32 rolling 5 in a row, but it's got to start before / at 16, which means 50% chance of it happening.