Feeds:
Posts

## Test your intuition: consecutive tails

Something very unfortunate has happened: several things I have recently written that could have been blog entries are instead answers on math.SE! In the interest of exposition beyond the Q&A format I am going to “rescue” one of these answers. It is an answer to the following question, which I would like you to test your intuition about:

Flip $150$ coins. What is the probability that, at some point, you flipped at least $7$ consecutive tails?

Jot down a quick estimate; see if you can get within a factor of $2$ or so of the actual answer, which is below the fold.

The actual answer, as estimated largely by hand

The actual answer is about $0.44$. As Tanya Khovanova observed, when people try to write down sequences of random coin flips, they make certain biased decisions to make the sequences “look” more random. One of the decisions that people make is to underestimate the number and frequency of consecutive heads and tails, which are surprisingly likely. (The original questioner admitted that he expected the answer to be around $0.1$ and was not surprised when he misinterpreted my answer and thought the answer was around $0.0044$!)

One way to correct this tendency is think about the expected number of such runs. Our saving grace here is that linearity of expectation holds regardless of whether the random variables involved are independent or not. So the expected number of times you get $7$ consecutive tails in $150$ coins is just the expected number of times the first $7$ coins all come up tails, plus the expected number of times the next $7$ coins (as in coins $2$ through $8$) all come up tails, and so forth – or $\frac{144}{128}$. So we expect, on average, a little more than one such run of tails, which means they should actually be fairly likely. If I knew more statistics I might even tell you what we expect the distribution of runs of tails to look like in the limit and give an estimate of the probability from there, but I don’t, so I’ll estimate the probability the hard way.

First, some generalities. The set of sequences of coin flips in which no more than $7$ tails are consecutively flipped is an example of a regular language. Regular languages have a very well-understood enumerative and asymptotic theory; one can either construct a rational generating function from an unambiguous regular expression, or if one is not available, take powers of the adjacency matrix of a finite state machine recognizing the language. In this case the Perron-Frobenius theorem, when applicable, implies that the eigenvalue of the matrix of largest absolute value is positive, real, unique, and has multiplicity $1$, so that asymptotically the number of words of length $n$ in such a language grows as $cr^n + O(s^n)$ where $c$ is some constant and $s < r$.

In this case we can bypass the matrix machinery because the generating function is easy to describe directly. Consider the sequence $a_n$ describing the number of ways to flip $n$ coins such that at no point are there $7$ or more tails in a row. Then the number we want to compute is $1 - \frac{a_{150}}{2^{150}}$. By appending heads to the beginning of such a sequence, we can decompose any such sequence into the blocks $\displaystyle \{ H, HT, HTT, HTTT, HTTTT, HTTTTT, HTTTTTT \}$

in a unique manner. This implies the generating function identity $\displaystyle A(x) = 1 + \sum_{n \ge 1} a_{n-1} x^n = \frac{1}{1 - x - x^2 - x^3 - x^4 - x^5 - x^6 - x^7}$

where we need to shift the index to account for the missing heads at the beginning. (Note that when $7$ is replaced by $2$ we get a generating function for the Fibonacci numbers.) The partial fraction decomposition of the RHS then determines a closed form for $a_{n-1}$ in which the leading term is given by the pole of the RHS closest to the origin. General theorems assert, although we can prove directly, that this pole is positive, real, unique, and has multiplicity $1$. So write $\displaystyle A(x) = \frac{\lambda}{1 - rx} + \text{other terms}$

which implies that $a_{n-1} \approx \lambda r^n$. Here $r$ is the largest positive root of the characteristic polynomial $\displaystyle x^7 = x^6 + x^5 + x^4 + x^3 + x^2 + x + 1$

and since the two sides are approximately equal when $x = 2$ (and since we expect that, for small $n$, $a_n$ is close to $2^n$) we conclude that $r$ should be a little less than $2$. In fact, multiplying the above by $x - 1$ gives $\displaystyle x^8 - x^7 = x^7 - 1 \Leftrightarrow x = 2 - \frac{1}{x^7}$.

The iteration $x_n = 2 - \frac{1}{x_{n-1}^7}$ should converge rapidly to $r$. In fact with $x_0 = 2$ we get $x_1 = 2 - \frac{1}{128}$, and the difference between $x_2$ and $x_1$ is negligible. So $r \approx 2 - \frac{1}{128}$ should be quite a good approximation.

To estimate $a_{150}$ it now remains to estimate $\lambda$. We can compute $\lambda$ directly in terms of $r$ using l’Hopital’s rule and the observation that $\lambda$ is equal to $\displaystyle \lim_{x \to \frac{1}{r}} A(x) (1 - rx)$. We compute that this is equal to $\displaystyle \lim_{x \to \frac{1}{r}} \frac{(1 - rx)(1 - x)}{1 - 2x + x^8} = \lim_{x \to \frac{1}{r} } \frac{-r-1 + 2rx}{-2 + 8x^7} = \frac{r-1}{2 - \frac{8}{r^7}} \approx \frac{1}{2}$.

This gives $a_n \approx r^n$, so $\displaystyle 1 - \frac{a_{150}}{2^{150}} \approx 1 - \left( 1 - \frac{1}{256} \right)^{150} \approx 1 - e^{- \frac{150}{256}} \approx 0.44$.

(If I estimated the error in my approximation of $\lambda$ I might be compelled to give more digits, but this is fine for now.) So we see that, in agreement with the expected value calculation, the probability that at least one run of $7$ consecutive tails appears is quite good if the number of coins flipped is in the neighborhood of $2^7$ (and the more general statement with $7$ replaced by another positive integer).

The obvious generalization of the above computation shows that the probability that at least one run of $k$ consecutive tails appears when $n$ coins are flipped are, if $k$ is not too small and $n$ is less than or around $2^k$, reasonably close to $1 - e^{-\frac{n}{2^{k+1}}}$. If $n$ is significantly less than $2^k$, the above is approximately $\frac{n}{2^{k+1}}$, which is within a factor of $2$ of the answer we would get if $n$ were significantly larger than $k$ and, in addition, we assumed that the events of the first $k$ coins, the next $k$ coins (again, coins $2$ through $k+1$), and so forth all turning up tails were independent (which they aren’t, but again, this is an approximation). If $n$ is significantly larger than $2^k$ then, of course, the probability approaches $1$ rather rapidly.

### 5 Responses

1. on July 22, 2011 at 6:39 pm | Reply W. Volterman

I know this is quite old, but it can’t hurt to respond.

You can treat this as a markov chain, with transition matrix

[ p (1-p) 0 0 0 0 0 0; p 0 (1-p) 0 0 0 0 0; … ; p 0 0 0 0 0 0 (1-p); 0 0 0 0 0 0 0 1]

For the fair coin case, p = 0.5

Start with initial vector pi_0 = [p (1-p) 0 0 0 0 0 0] and iterate 149 times, either with matrix multiplication or perhaps diagonalization.

You get 0.441958742802… or so

Not as elegant, but direct. Probably much more accurate for small sizes.

2. on September 29, 2010 at 9:00 am | Reply hernan

A quick way of calcuting the asympotical result (generalizable to weighed coins):

Instead of throwing N coins, lets consider the experiment of throwing M alternate runs (a ‘run’ is a sequence of consecutive tails/heads). That is, instead of throwing N iid Bernoulli random variables, we throw M iid geometric random variables.

If we choose M=N/2, the expected total number of coins will be N, and so we can expect (informally) that the two experiments are asymtotically equivalent (in one experiment the number of coins is fixed, the number of runs is a random variable; in the other, the reverse; this could be related to the use of different ensembles in statistical physics).

In the modified experiment it’s easy to compute the probability that no run exceeds a length L: it’s just (1-(1/2)^(L-1))^(N/2)
If we consider just tails, the number of trials would be N/4 instead. This, in our case, gives 1-0.44160.

3. on September 29, 2010 at 7:03 am | Reply hernan

I had looked at this problem a while ago, and ended rediscovering the generalized Fibonacci numbers http://mathworld.wolfram.com/Fibonaccin-StepNumber.html

And I got (rather informally) the asymptotical result:
k = log_2(n) – G(p) + o(1) with
G(p) = 1 + log_2( – ln(1-p) )

It agrees with your last paragrpah, if I’m not mistaken.

4. on September 21, 2010 at 11:02 am | Reply D. Eppstein

Linearity of expectation of the number of patterns of exactly seven tails didn’t seem like the right tool to use because the probabilities of getting these patterns are so highly non-independent (if you get one, you have a much higher probability of getting a second one).

Instead, I used the following reasoning: each sequence of zero or more tails is started by a head, and you should have about 75 heads. The last three or so heads are likely too near the end to count, but otherwise each head has probability 1/128 of being followed by seven or more tails. So the expected number of consecutive sequences of seven OR MORE tails should be around 72/128, and I rounded that down to 1/2 to give my guess at the probability of at least one such sequence.

• on September 21, 2010 at 11:08 am | Reply Qiaochu Yuan

Fair enough. I was going to mention that the expected value computation was something of a copout for precisely the reason you describe, but it was just an easy computation for motivating the hard computation.