TITLE: Time until a consecutive sequence of ones in a random bit sequence
QUESTION [9 upvotes]: This a reformulation of a practical problem I encountered.
Say we have an infinite sequence of random, i.i.d bits. For each bit $X_i$, $P(X_i=1)=p$.
What is the expected time until we get a sequence of $n$ 1 bits?
Thanks!

REPLY [5 votes]: Here is a generating function approach to this problem.
Preliminaries
For $0\le k<n$, let an atom $a_k$ be a sequence of $k$ $1$ bits followed by a $0$ bit. Let a terminal atom $a_n$ be a sequence of $n$ $1$ bits. Let a complete $n$ sequence be a binary sequence which ends at the first subsequence of $n$ consecutive $1$ bits. Any complete $n$ sequence can be written as a unique sequence of atoms followed by a terminal atom. The probability of an atom occurring is $p^k(1-p)$ and the length of an atom is $k+1$. The probability of a terminal atom occurring is $p^n$ and the length of a terminal atom is $n$.
Therefore, for each atom $a_k$, define the monomial $\phi_x(a_k)$ as
$$
\phi_x(a_k)=\left\{\begin{array}{ll}p^k(1-p)x^{k+1}&\text{if }0\le k< n\\p^nx^n&\text{if }k=n\end{array}\right.\tag{1}
$$
and for the concatenation of two binary sequences, $s_1$ and $s_2$
$$
\phi_x(s_1s_2)=\phi_x(s_1)\phi_x(s_2)\tag{2}
$$
The probability of any sequence of atoms $s$ occurring is the coefficient of the monomial $\phi_x(s)$ and the length of $s$ is the exponent of $x$ in $\phi_x(s)$.
The Generating Function
Thus, the probability of a complete $n$ sequence with length $k$ coming from the concatenation of $j$ atoms and a terminal atom is the coefficient of $x^k$ in
$$
\begin{align}
&(\phi_x(a_0)+\phi_x(a_1)+\phi_x(a_2)+\dots+\phi_x(a_{n-1}))^j\phi(a_n)\\
&=p^nx^n((1-p)x)^j\left(1+px+p^2x^2+\dots+p^{n-1}x^{n-1}\right)^j\\
&=p^nx^n\left((1-p)x\frac{1-p^nx^n}{1-px}\right)^j\tag{3}
\end{align}
$$
Summing over all $j$, the probability of a complete $n$ sequence with length $k$ is the coefficient of $x^k$ in
$$
\begin{align}
f_n(x)
&=\sum_{j=0}^\infty p^nx^n\left((1-p)x\frac{1-p^nx^n}{1-px}\right)^j\\
&=\frac{p^nx^n}{1-(1-p)x\frac{1-p^nx^n}{1-px}}\tag{4}
\end{align}
$$
That is, $f_n(x)$ is the generating function for the probability of a complete $n$ sequence with a given length.
Since $f_n(1)=1$, the probability that a complete $n$ sequence occurs eventually is $1$.
Expected Length
$$
f_n(x)=\sum_{k=0}^\infty c_kx^k\tag{5}
$$
where $c_k$ is the probability of a complete $n$ sequence with length $k$. Therefore, $xf_n^\prime(x)$ evaluated at $x=1$ is
$$
\sum_{k=0}^\infty kc_k\tag{6}
$$
which is the expected length of a complete $n$ sequence.
$$
xf'(x)=p^nx^n\frac{n\frac{1-x}{1-px}+x\frac{1-p}{1-px}\frac{1-p^nx^n}{1-px}}{\left(1-(1-p)x\frac{1-p^nx^n}{1-px}\right)^2}\tag{7}
$$
Evaluating $(7)$ at $x=1$ yields that the expected length of a complete $n$ sequence is
$$
\mathrm{E}(k)=\frac{1-p^n}{p^n(1-p)}\tag{8}
$$
Expected Variance
Using the same idea that was used to get $(6)$, we "take the derivative and multiply by $x$" twice to see that $xf_n^\prime(x)+x^2f_n^{\prime\prime}(x)$ evaluated at $x=1$ is
$$
\sum_{k=0}^\infty k^2c_k\tag{9}
$$
which is the expected square of the length of a complete $n$ sequence.
$$
\begin{align}
xf_n^\prime(x)+x^2f_n^{\prime\prime}(x)
&=\frac{p^nx^n}{(1-px)^3\left(1-(1-p)x\left(\frac{1-p^nx^n}{1-px}\right)\right)^3}\\
&\times\left(
\begin{aligned}
&n^2(1-x)(1-px)\left(1-x-(1-p)xp^nx^n\right)\\
&+2n(1-p)x\left(1-x-(2-x-px)p^nx^n\right)\\
&+(1-p)x(1-p^nx^n)\left(1+x-(1-p)xp^nx^n\right)\\
\end{aligned}
\right)\tag{10}
\end{align}
$$
Evaluating $(10)$ at $x=1$ yields that the expected square of the length of a complete $n$ sequence is
$$
\mathrm{E}(k^2)=\frac{2(1-p^n)}{p^{2n}(1-p)^2}-\frac{1+2n-p^n}{p^n(1-p)}\tag{11}
$$
Combining $(8)$ and $(11)$, we get that the expected variance of the length of a complete $n$ sequence is 
$$
\begin{align}
\mathrm{Var}(k)
&=\mathrm{E}(k^2)-\mathrm{E}(k)^2\\
&=\frac{1-p^{2n+1}}{p^{2n}(1-p)^2}-\frac{2n+1}{p^n(1-p)}\tag{12}
\end{align}
$$