TITLE: Finding the probability that at least 8 houses in a row are of same colour.
QUESTION [7 upvotes]: There are 400 houses in a row. Each house is to be coloured with any of the following five colours: red, blue, green, yellow, and white. The colour of each house is to be chosen randomly from among these five colours and independent of any other selection. Now, what is the probability that at least 8 houses in a row will have the same colour?
My idea is to first calculate the number of ways in which there are at least 8 red-coloured houses in a row. Then do the same for green, then blue, yellow and white (${R}\to{G}\to{B}\to{Y}\to{W}$). Suppose, $ n(R) $ denotes the number of ways that there are 8 red-coloured houses in a row.
So, $$n(R) = (number\ of\ ways\ to\ choose\ one\ row\ of\ 8\ houses\ from\ 400\ houses)\times(number\ of\ ways\ rest\ of\ the\ houses\ can\ be\ painted) = \mathrm{393}\!\cdot\!\mathrm{5^{392}} $$
Now, $$ n(R) = n(G) = n(B) = n(Y) = n(W)$$
Initially I was careless to conclude that the required probility will be,
$$ p = \frac{n(R) + n(G) + n(B) + n(Y) + n(W)}{5^{400}}$$
But a few moments later I realised that there is a lot of double-counting in this solution. (For example, when calculating $n(R)$, you can take 1st 8 houses to be red, and the rest the 392 houses are randomly coloured. This will include the case that last 8 houses in a row are red. So, when you come to choose last 8 houses to be red, there will be an occasion when first 8 houses become red-coloured. Similarly there are so many ways that double counting is happening.) What should be done?

REPLY [3 votes]: We interpret $8$ houses in a row as runs of $8$ consecutive houses. The following answer is based upon the Goulden-Jackson Cluster Method. We consider the set of words of length $n\geq 0$ built from an alphabet $$\mathcal{V}=\{R,G,B,Y,E\}$$ and the set $B=\{R^8,G^8,B^8,Y^8,E^8\}$ of bad words, which are not allowed to be part of the words we are looking for. We derive a generating function $A(z)=\sum_{n=0}^\infty a_nz^n$ with the coefficient of $z^n$ being  the number of words of length $n$. Since we want to count the number of words which do have runs of length $8$, the resulting generating function is
\begin{align*}
\frac{1}{1-5z}-A(z)&=1+5z+5^2z+5^3z+\cdots-A(z)\\
&=\sum_{n=0}^\infty \left(5^n-a_n\right)z^n\tag{1}
\end{align*}
The wanted number of $400$ houses is the coefficient $[z^{400}]$ of the series in (1).
According to the paper (p.7) the generating function $A(z)$  is
\begin{align*}
A(z)=\frac{1}{1-dz-\text{weight}(\mathcal{C})}\tag{2.1}
\end{align*}
with $d=|\mathcal{V}|=5$, the size of the alphabet and $\mathcal{C}$ is the weight-numerator of bad words with
\begin{align*}
\text{weight}(\mathcal{C})=\sum_{t\in\{R,G,B,Y,E\}}\text{weight}(\mathcal{C}[t^8])\tag{2.2}
\end{align*}

We calculate according to the paper
\begin{align*}
\text{weight}(\mathcal{C}[t^8])&=-z^8-\left(z+z^2+\cdots+z^7\right)\text{weight}\mathcal{C}([t^8])\tag{2.3}\\
\text{weight}(\mathcal{C}[t^8])&=-\frac{z^8(1-z)}{1-z^8}\qquad\qquad t\in\{R,G,B,Y,E\}\tag{2.4}
\end{align*}
so  that according (2.2) and (2.4)
\begin{align*}
\text{weight}(\mathcal{C})&=\sum_{t\in\{R,G,B,Y,E\}}\text{weight}(\mathcal{C}[t^8])\\
&=\sum_{t\in\{R,G,B,Y,E\}}(-1)\frac{z^8(1-z)}{1-z^8}\\
&=-\frac{5z^8(1-z)}{1-z^8}\tag{2.5}\\
\end{align*}
The additional term on the right-hand side of (2.3) takes account of the overlapping of $t^8, t\in\{R,G,B,Y,E\}$ with parts of it.
We obtain according to (2.1) and (2.5)
\begin{align*}
\color{blue}{A(z)}=\frac{1}{1-5z+\frac{5z^8(1-z)}{1-z^8}}
&\;\;\color{blue}{=\frac{1-z^8}{1-5z+4z^8}}
\end{align*}
We finally get
\begin{align*}
\frac{1}{1-5z}-A(z)&=\frac{1}{1-5z}-\frac{1-z^8}{1-5z+4z^8}\\
&=5z^8+\color{blue}{45}z^9+325z^{10}+2\,125z^{11}\\
&\qquad+\cdots+\color{blue}{\left(5^{400}-a_{400}\right)}z^{400}+\cdots\tag{3.1}
\end{align*}
where the last line was calculated with some help of Wolfram Alpha.

A small example: The blue marked coefficient of $z^{9}$  shows there are $\color{blue}{45}$ words of length $9$ over the alphabet $\mathcal{V}=\{R,G,B,Y,E\}$ which do contain runs of length $8$. These $45$ words are
\begin{align*}
\begin{array}{ccccccccc}
R\color{blue}{RRRRRRRR}&R\color{blue}{GGGGGGGG}&R\color{blue}{BBBBBBBB}\\
G\color{blue}{RRRRRRRR}&G\color{blue}{GGGGGGGG}&G\color{blue}{BBBBBBBB}\\
B\color{blue}{RRRRRRRR}&B\color{blue}{GGGGGGGG}&B\color{blue}{BBBBBBBB}\\
Y\color{blue}{RRRRRRRR}&Y\color{blue}{GGGGGGGG}&Y\color{blue}{BBBBBBBB}\\
E\color{blue}{RRRRRRRR}&E\color{blue}{GGGGGGGG}&E\color{blue}{BBBBBBBB}\\
\color{blue}{RRRRRRRR}G&\color{blue}{GGGGGGGG}R&\color{blue}{BBBBBBBB}R\\
\color{blue}{RRRRRRRR}B&\color{blue}{GGGGGGGG}B&\color{blue}{BBBBBBBB}G\\
\color{blue}{RRRRRRRR}Y&\color{blue}{GGGGGGGG}Y&\color{blue}{BBBBBBBB}Y\\
\color{blue}{RRRRRRRR}E&\color{blue}{GGGGGGGG}E&\color{blue}{BBBBBBBB}E\\
\\
R\color{blue}{YYYYYYYY}&R\color{blue}{EEEEEEEE}\\
G\color{blue}{YYYYYYYY}&G\color{blue}{EEEEEEEE}\\
B\color{blue}{YYYYYYYY}&B\color{blue}{EEEEEEEE}\\
Y\color{blue}{YYYYYYYY}&Y\color{blue}{EEEEEEEE}\\
E\color{blue}{YYYYYYYY}&E\color{blue}{EEEEEEEE}\\
\color{blue}{YYYYYYYY}R&\color{blue}{EEEEEEEE}R\\
\color{blue}{YYYYYYYY}G&\color{blue}{EEEEEEEE}G\\
\color{blue}{YYYYYYYY}B&\color{blue}{EEEEEEEE}B\\
\color{blue}{YYYYYYYY}E&\color{blue}{EEEEEEEE}Y\\
\end{array}
\end{align*}

Result: We find according to (3.1) and Wolfram Alpha the wanted number of words is
\begin{align*}
\color{blue}{5^{400}-a_{400}}&=[z^{400}]\left(\frac{1}{1-5z}-\frac{1-z^8}{1-5z+4z^8}\right)\\
&\,\,\color{blue}{=1.556\,428\,\ldots\cdot 10^{277}}
\end{align*}
and the wanted probability is
\begin{align*}
\color{blue}{\frac{5^{400}-a_{400}}{5^{400}}=4.019\,088\ldots\cdot 10^{-3}}
\end{align*}