Suppose we have a sequence of $n$ coin flips of a biased coin with probability $p$ of being heads. Let $X_n$ be the random variable that records the number of runs of consecutive heads, e.g. THHTHTHH has 3 runs of heads. By linearity of expectation, $E(X_n)$ is the sum of $n$ probabilities of getting a run of heads starting at coin flip $j$ for $1 \leq j \leq n$. For $j = 1$ this probability is $p$, and for $j > 1$ we must have flip $j-1$ is tails and flip $j$ is heads, which has probability $p(1-p)$. So $E(X_n) = p + (n-1)p(1-p)$. What about Var$(X_n)$? Is there any way to compute a formula for it? Or if not, at least get asymptotics for it?
[Math] Variance of number of runs of consecutive heads in $n$ biased coin flips
probability
Related Solutions
I’ll consider the case of an infinite string of coin tosses, as it’s easier.
Start with anon’s suggestion. Let $q$ be the probability that the coin comes up tails. Then the probability of getting two tails in a row must be $q^2$, and the probability of getting two heads in a row must be $(1-q)^2$, so you know that $p=q^2+(1-q)^2=2q^2-2q+1$, and therefore $$q=\frac14\left(2\pm\sqrt{4-8(1-p)}\right)=\frac12\left(1\pm\sqrt{2p-1}\right)\;.$$ One of the solutions is $q$; the other is $1-q$, the probability that the coin comes up heads. There’s no way to decide which is which without further information.
At any stage in the process the probability of a run of $n$ tails is $q^n(1-q)$, so the expected length of a run of tails is $\sum_{n\ge 0}nq^n(1-q)$. However, this includes runs of length $0$, which you don’t want to count, so an adjustment is needed. The probability of a run of length $0$ is $1-q\,$, so the probability of a run of positive length is $q$, and for $n>0$ the probability of a run of length $n$ given that the run has positive length is therefore $$\frac{q^n(1-q)}q=q^{n-1}(1-q)\;.$$ Thus, the expected length of a non-zero run of tails is $$\sum_{n\ge 1}nq^{n-1}(1-q)=(1-q)\sum_{n\ge 1}nq^{n-1}=\frac{1-q}{(1-q)^2}=\frac1{1-q}\;.$$
This is a fairly standard summation, but if you’re not familiar with it, just differentiate $$\sum_{n\ge 0}x^n=\frac1{1-x}\;.$$
For $k=1,\dots,n$ the probability that a given tail is the $k$-th of a run of $n$ tails is $q^{n-1}(1-q)^2$ (since we may ignore edge the effects at the beginning of the infinite string of tosses), so the probability that it belongs to a run of $n$ tails is $nq^{n-1}(1-q)^2$, and the probability that it belongs to a run of at least three tosses is $$\begin{align*}\sum_{n\ge 3}nq^{n-1}(1-q)^2&=(1-q)^2\sum_{n\ge 3}nq^{n-1}\\ &=(1-q)^2\left(\frac1{(1-q)^2}-(1+2q)\right)\\ &=q^2(3-2q)\;; \end{align*}$$
this is the fraction of tails belonging to runs of three or more.
I ask you for additional explanation. Meanwhile I'll post here another approach.
Denote by $\tau_i^5$ the random variable that counts the time required to get five heads starting from $i$ heads, ok?
What we want is exactly $E[\tau_0^5]$, right?
Now, you can evaluate $E[\tau_0^5]$ conditioning at the first step.
$$ E[\tau_0^5] = \frac{E[\tau_0^5]}{2} + \frac{E[\tau_1^5]}{2} +1 $$
Explaining the equation above. With probability $1/2$ you have a tail, so the time you will take to get five heads is the same, because you have any heads. On the other hand, with the same probability you get a head, and now, the number of flips needed to get 5 heads is $E[\tau_1^5]$, because now you that you have one head. The +1 it is because you have to count the first step.
Repeating the argument above you get
$$ E[\tau_1^5] = \frac{E[\tau_0^5]}{2} + \frac{E[\tau_2^5]}{2} +1 $$
Proceeding this way, and remembering $E[\tau_5^5]=0$, you get
$$ E[\tau_0^5] = 62 $$
This may seems more complicated at the first sight, but the idea of "to conditionate at what happens at the first time (or movement)" solve a big variety of problems.
Best Answer
Following the idea of your derivation: let $t_i$ be $1$ if a run of heads starts at coin number $i$ ($i=1 \cdots n$), $0$ otherwise. Then $X_n = \sum_{i=1}^n t_i$ and
$$ P(t_i=1)=E(t_i)= \begin{cases} p & \text{if $i=1$} \\ p(1-p) & \text{otherwise} \\ \end{cases} $$
The vars $t_i$ are not independent, but $E(X_n)=\sum E(t_i) = p + (n-1)p (1-p)$ as you already found. Now let's compute $$E(X^2_n)= E\left[\left(\sum t_i \right)^2\right]=\sum_{i=1}^n\sum_{j=1}^n E(t_i t_j)$$
The terms of the sum can be grouped in:
A) $i=j$: (with $n$ terms) $E(t_i t_j)=E(t_i^2)=E(t_i)$. The sum over these terms is $p + (n-1)p (1-p)$.
B) $|i-j|=1$: (with $2 \times (n-1)$ terms). Here one of the two must be zero,so $E(t_i t_j)=0$
In the remainder, the variables $t_i,t_j$ are independent. Hence
C) $|i-j|>1$ and $i=1$ or $j=1$ : (with $2\times(n-2)$ terms) $E(t_i t_j) = p^2(1-p)$
D) $|i-j|>1$ and $i>1$ and $j>1$ : (with $n^2-5n +6$ terms) $E(t_i t_j) = p^2(1-p)^2$
Finally
$$E(X_n^2)=p+\left( n-1\right) \,\left( p-1\right) \,p+ 2\,\left( n-2\right) \,\left( 1-p\right) \,{p}^{2}+\left( {n}^{2}-5\,n+6\right) \,{\left( 1-p\right) }^{2}\,{p}^{2}$$
$$Var(X_n) = E(X_n^2) - E(X_n)^2 = \left( 1-p\right) \,p\,\left( 3\,n\,{p}^{2}-5\,{p}^{2}- 3 \, n\,p+3 p+n\right)=\\ =\left( 1-p\right) \,p\,\left[ n\,\left( 3\,{p}^{2}-3\,p+1\right) -p\,\left( 5\,p-3\right) \right]$$
As pointed out by Did in a comment, this is valid for $n\ge 4$; if $n<4$ the group D vanishes; if $n\le 2$, only case A survives. Then
$$Var(X_1)=p(1-p) \hspace{9pt} Var(X_2)=p(1-p)^2(2-p)\hspace{9pt} Var(X_3)=p(1-p)^2(4p^2-6p+3)$$