In a sequence of Bernoulli trials let X be the length of the run 
In a sequence of Bernoulli trials let X be the length of the run (of either successes or failures) started by the first trial. Find the distribution of X, E(X) and Var(X)

The answers to this question (from the back of the book) are:
$p_k=p^kq+q^kp$
$E(X)=pq^{-1}+qp^{-1}$
$Var(X)=pq^{-2}+qp^{-2}-2$
I don't understand how to get these answers though. Can anyone explain in detail?
 A: Calling the possible trial outcomes Heads and Tails, the event $\{X=k\}$ occurs if and only if the sequence is either $k$ Heads followed by a Tail, or $k$ Tails followed by a Head, so $$P(X=k)=P(H^kT\cup T^kH)=P(H^k)P(T)+P(T^k)P(H)=p^kq+q^kp$$
where $p=P(H)$ and $q=1-p=P(T)$.
Now 
$$\begin{align}E(X)&=\sum_{k=1}^\infty k P(X=k)\\
&=\sum_{k=1}^\infty k (p^kq+q^kp)\\
&=q\sum_{k=1}^\infty k p^{k} + p\sum_{k=1}^\infty k q^{k}\\
\end{align}$$
$$\begin{align}E(X^2)&=\sum_{k=1}^\infty k^2 P(X=k)\\
&=\sum_{k=1}^\infty k^2 (p^kq+q^kp)\\
&=q\sum_{k=1}^\infty k^2 p^{k} + p\sum_{k=1}^\infty k^2 q^{k}\\
\end{align}$$
so we have $E(X)$ and $V(X)=E(X^2)-(E(X))^2$ once the above summations are calculated.
Letting $a$ stand for either $p$ or $q$, we have
$$\begin{align}\sum_{k=1}^\infty k a^{k}&=a\sum_{k=1}^\infty k a^{k-1}\\
&=a\frac{d}{da}\sum_{k=1}^\infty a^{k}\\
&=a\frac{d}{da}((1-a)^{-1}-1)\\
&=a(1-a)^{-2}\\
\end{align}$$ 
$$\begin{align}\sum_{k=1}^\infty k^2 a^{k}&=a\sum_{k=1}^\infty k^2 a^{k-1}\\
&=a\frac{d}{da}\sum_{k=1}^\infty k\,a^{k}\\
&=a\frac{d}{da}(a(1-a)^{-2})\\
&=a\{2a(1-a)^{-3}+(1-a)^{-2}\}\\
&=a(1+a)(1-a)^{-3}
\end{align}$$
so all four of the required summations are covered by the latter two results, giving the posted answers upon substitution.
