# Probability of Runs

I hope the title accurately reflects my question.

I have an independent event, with a 98% chance of occurring.

Now, I observe and record the outcome of this event 100 times.

What is the probability that there is a single run of 17 consecutive occurrences?

Put another way that I don't think changes the question, given an unfair coin with a 98% chance of landing on heads, after a hundred flips, what is the probability that the coin landed on heads 17 consecutive times?

EDIT:

Per whuber's questions, use the following clarifications:

The desired probability is a trial with a run of at least 17 heads. Furthermore, the desired probability is a trial with at least one such run

• Do you mean given an unfair coin 98% to 2% chance of heads to tails and that you flip it 100 times in a row, there will be a run of exactly 17 consecutive heads but a run of 18, 19, etc. would not count? What if there were two runs of 17 heads (e.g., ..TTHHHHHHHHHHHHHHHHHTHHHHHHHHHHHHHHHHH...)? – Daniel Mar 9 '18 at 18:47
• I am only interested in the probability of one run of 17 occurring in the trial. So runs of 18+ contain one(or more) run of 17, and are probably superfluous? This is beyond my comfort level with statistics. – DocML Mar 9 '18 at 18:51
• The event you ask about isn't clearly defined. Do you intend to describe (a) a run of at least 17 heads; (b) a run of exactly 17 heads; and (c) regardless of the former, do you mean it to consist of exactly one such run or of at least one such run? – whuber Mar 9 '18 at 18:56
• Ah, I see. For (a/b) let's go with a run of at least 17 heads. For (c), lets also go with at least one such run – DocML Mar 9 '18 at 18:58

Do you need an analytical expression or will a simple simulation suffice ?

get_run <- function(...){
max_run <- 0
cur_run <- 0
x = rbinom(100 , 1 , 0.98)

for ( i in x){
if(i == 1) {
cur_run = cur_run + 1
} else {
max_run = max( cur_run , max_run)
cur_run = 0
}
}
max_run = max( cur_run , max_run)
return(max_run)
}

x <- replicate(300000,  get_run())
hist(x)
sum(x <= 17) / length(x)


Histogram of the distribution of max run length is:

As you can see the probability of the maximum run length being <= 17 is incredibly low with our simulation probability being 3.666667e-05 (though that massive uneven spike of probability at max_run = 100 makes me feel like I've probably got a bug in my code)

• I don't think it's a bug, because the chance that all 100 flips are heads is $0.98^{100}\approx 13.26\%.$ To 16 significant figures the chance of not observing a run of at least 17 in 100 flips is $1.624379427379659\times 10^{-5},$ which is consistent with your result. However, simulations are not good methods to learn about rare events. Some initial analysis can be extremely helpful. Here's R code to check: f <- function(n, p, r) { q <- c(rep(0,r), 1); for (j in 2:(n+1)) q[-(r+1)] <- p * q[-1] + (1-p) * q[1]; q[1] }; 1 - f(100, .98, 17) – whuber Mar 9 '18 at 19:15