Variance of number of 'HT' in n tosses of a coin Suppose I am tossing a coin $n$ times and $Y=$ number of times the sequence $HT$ appears. I wish to find $\textrm{Var}(Y)$. During each toss let $p=\mathbb{P}(\text{Head})$.
What I tried :
Let the sequence of outcomes be denoted by $X_1,X_2,...,X_n$. Define $$f(X_i,X_{i+1})=\begin{cases} 1, X_i=H,X_{i+1}=T\\
0 \text{ otherwise} \end{cases}$$
I found out
$$\mathbb{E}(Y)=\sum\limits_{i=1}^{n-1}\mathbb{E}[f(X_i,X_{i+1})]=(n-1)p(1-p)$$
And $$\mathbb{E}(Y^2)=\sum\limits_{i=1}^{n-1}\mathbb{E}[f(X_i,X_{i+1})^2]+\sum\limits_{i\neq j}\mathbb{E}[f(X_i,X_{i+1})f(X_j,X_{j+1})]$$
What I figured was that $$f(X_i,X_{i+1})f(X_{i+1},X_{i+2})=0$$ and $$f(X_i,X_{i+1})f(X_j,X_{j+1})=1$$ with probability $p^2(1-p)^2$ otherwise. So that $2(n-2)$ terms in the second summand will be zero.
Then $$\mathbb{E}(Y^2)= (n-1)p(1-p)+\{(n-1)^2-(n-1)-2(n-2)\}p^2(1-p)^2$$
Is there anything wrong with this?
 A: Looking at your method and comparing your outcome with mine (see below) I conclude that there is nothing wrong with that. My method has the same underlying thinking, is a bit more direct and goes immediately for finding the variance. Also I use the abbreviation $q:=1-p$.

Let $Y_{1},\dots,Y_{n-1}$ where $Y_{i}=1$ if $H$ on spot $i$ and $T$
on spot $i+1$, and $Y_{i}=0$ otherwise.
Then $Y=\sum_{i=1}^{n-1}Y_{i}$ so that by linearity of expectation
and symmetry: $$\mathbb{E}Y=\left(n-1\right)\mathbb{E}Y_{1}=\left(n-1\right)pq$$
(Actually we do not need this result, but it comes at hand later by checking your outcome)
Further:
$$\mathsf{Var}\left(Y\right)=\mathsf{Cov}\left(\sum_{i=1}^{n-1}Y_{i},\sum_{i=1}^{n-1}Y_{i}\right)=\sum_{i=1}^{n-1}\sum_{j=1}^{n-1}\mathsf{Cov}\left(Y_{i},Y_{j}\right)$$and we discern:


*

*If $\left|i-j\right|\geq2$ then $Y_{i}$ and $Y_{j}$ are independent
so that in that case $\mathsf{Cov}\left(Y_{i},Y_{j}\right)=0$.

*$\mathsf{Cov}\left(Y_{i},Y_{i}\right)=\mathsf{Var}\left(Y_{1}\right)=pq\left(1-pq\right)$.

*If $\left|i-j\right|=1$ then $\mathsf{Cov}\left(Y_{i},Y_{j}\right)=\mathsf{Cov}\left(Y_{1},Y_{2}\right)=\mathbb{E}Y_{1}Y_{2}-\mathbb{E}Y_{1}\mathbb{E}Y_{2}=0-p^{2}q^{2}=-p^{2}q^{2}$
So we end up with: $$\mathsf{Var}\left(Y\right)=\left(n-1\right)pq\left(1-pq\right)-2\left(n-2\right)p^{2}q^{2}=$$$$\left(n-1\right)pq-\left(n-1\right)p^{2}q^{2}-2\left(n-2\right)p^{2}q^{2}=$$$$\left(n-1\right)pq-\left(3n-5\right)p^{2}q^{2}$$
To enable ourselves to check your outcome we go on with:
$$\mathbb{E}Y^{2}=\mathsf{Var}\left(Y\right)+\left(\mathbb{E}Y\right)^{2}=\left(n-1\right)pq-\left(3n-5\right)p^{2}q^{2}+\left(n-1\right)^{2}p^{2}q^{2}=$$$$\left(n-1\right)pq+\left[\left(n-1\right)^{2}-3n+5\right]p^2q^2$$so the same as yours.
