Unfortunately I do not understand the argument given by robjohn in What are the odds of getting heads 7 times in a row in 40 tries of flipping a coin? (I also cannot comment on that post.) The explanation is informal and unclear. It is said there that: "To count the number of sequences of $40$ flips that do not contain $7$ consecutive heads, we look at the coefficient of $x^{41}$ in $$ \begin{align} \sum_{k=0}^\infty(x+x^2+x^3+x^4+x^5+x^6+x^7)^k &=\frac1{1-x\frac{x^7-1}{x-1}}\\ &=\frac{1-x}{1-2x+x^8} \end{align} $$ Why does this work? Why would we have to look at the coefficient of $x^{41}$ and not $x^{40}$? Could someone explain this in more detail rather than just stating "this and this is true" without further argument?
-
I'm not following the author's account...not every good word is built out of the atoms $H^iT$ where $i\in {0, \cdots, 6}$. For instance, the good word $H$ isn't built that way. Of course, you can modify that by allowing the final atom to shed it's $T$....but I don't see where that count is taking place. – lulu Dec 19 '23 at 13:02
-
Personally, I'd do it recursively. Say a good string is one without $H^7$. Call the number of good strings of length $n$, $a_n$. Then $a_n=\sum_{k=1}^7 a_{n-k}$. – lulu Dec 19 '23 at 13:04
-
I note that my answer yields the same result as the answer you linked to, so I'm missing something here... – lulu Dec 19 '23 at 13:09
-
Oh, I see: he's is defining a good string to be one of my "good" strings with a $T$ appended. That's why he needs $41$ instead of $40$...that last $T$ has to be removed. – lulu Dec 19 '23 at 13:10
2 Answers
We define a "good" word, to be one in which the string $H^7$ does not appear. We seek to count the good words of length $n$, call that count $a_n$.
the author looks instead at the collection of good words to which a $T$ is appended. If we call the number of these $b_n$, then $b_n=a_{n-1}$ since all we are doing is attaching a $T$ to the collection of good words. The point of doing this, is that all the words in the new collection can be built out of the atoms $$\{T, HT, H^2T, \cdots, H^6T\}$$
The author's generating function counts the words of length $n$ that can be built out of those six atoms. He needs the coefficient of $x^{41}$ because that's $b_{41}$ which is equal to $a_{40}$, which is what we are really interested in. We remark that the length of the atom $H^iT$ is just $i+1$, so the generating function works as advertised.
Perhaps an explicit example will help: Consider the term $$x^2\times x\times x^4\times x^4\times x^6\times x$$
That term has degree $2+1+4+4+6+1=18$ so it contributes to $b_{18}$, hence to $a_{17}$. Which good word of length $17$ does it count? Well, we follow the prescription and replace $x^i$ with $H^{i-1}T$. We get $$HTTH^3TH^3TH^5TT=HTTHHHTHHHTHHHHHTT$$ Which counts a word in $b_{18}$. To get our good word of length $17$ we now simply delete the final $T$ to get $$HTTHHHTHHHTHHHHHT$$
As mentioned in the comments, I (personally) find it clearer to express this recursively. Since a good word must start with one of those six atoms, we see that $$a_n=\sum_{k=1}^7 a_{n-k}$$
It is readily seen that this yields the same result as the generating function, as of course it should.

- 70,402
User lulu has provided a great explanation. I'd just like to share my school of thought that's different from the original author but gives the same result.
Until the first tails or end of string, possible strings are $A = \{*, H, H^2, \dots, H^6\}$, where $*$ is the empty string. This part's generating function is $1 + x + x^2 + \dots + x^6$.
After this, all valid strings are built from a sequence of strings in $B = \{T, T H, T H^2, \dots, T H^6\}$, with generating function $x + x^2 + x^3 + \dots + x^7$. The generating function of all possible sequences from them is given by $\sum_{k = 0}^\infty (x + x^2 + x^3 + \dots x^7)^k$. Note the empty sequence is included.
Take $HHTTHT$ as examples. It starts with $HH \in A$, followed by $T \in B$, then $TH \in B$, and finally another $T \in B$. A string can starts with an empty string or be followed by an empty sequence from $B$. For example, the empty string $*$ itself starts with an empty string, followed by an empty sequence of $TH^i$'s.
Combining these two parts, we have the generating function $$G(x) = (1 + x + x^2 + \dots + x^6) \sum_{k = 0}^\infty (x + x^2 + x^3 + \dots x^7)^k.$$ This generating function's coefficient of $x^n$ gives the number of sequences of $n$ flips that do not contain 7 consecutive heads.
Let's call the original genarating function by robjohn $F$; that is $F(x) = \sum_{k = 0}^\infty (x + x^2 + x^3 + \dots x^7)^k$. Comparing $G$ and $F$, we see $$\begin{align*} xG(x) &= (x + x^2 + x^3 + \dots x^7) \sum_{k = 0}^\infty (x + x^2 + x^3 + \dots x^7)^k \\ &= \sum_{k = 1}^\infty (x + x^2 + x^3 + \dots x^7)^k \\ &= F(x) - 1. \end{align*}$$ In other words, $F(x) = xG(x) + 1$. Hence, the $(n+1)$-th coefficient of $F$ is the $n$-th coefficient of $G$. In particular, to count the number of sequences of 40 flips that do not contain 7 consecutive heads, we look at the coefficient of $x^{40}$ in $G(x)$, which is the coefficient of $x^{41}$ in $F(x)$.

- 1,299