If we define the geometric pmf as $\displaystyle P(X = x) = (1-p)^{x-1} p$, the second moment of a geometric random variable should be $\displaystyle (2-p)/p^2$. However, when I try to derive it, I'm getting $\displaystyle 2(1-p)/p^2$. Can somebody help me find the error in my derivation?

Let q = 1-p.

$\displaystyle E(X^2) = \sum_{x=1}^\infty x^2 (1-p)^{x-1} p = \sum_{x=1}^\infty x^2 q^{x-1} p =\sum_{x=1}^\infty x \large( \dfrac{d}{dq} q^x \large) p = p \dfrac{d}{dq} \sum_{x=1}^\infty x q^x$

$\displaystyle = pq \dfrac{d}{dq} \sum_{x=1}^\infty x q^{x-1} = pq \dfrac{d}{dq} \sum_{x=1}^\infty \dfrac{d}{dq} (q^x) = pq \dfrac{d^2}{dq^2} \sum_{x=1}^\infty q^x = pq \dfrac{d^2}{dq^2} \large( \dfrac{q}{1-q} \large)$

$\displaystyle = pq \dfrac{d}{dq} [(1-q)^{-2}] = 2pq(1-q)^{-3} = \dfrac{2pq}{p^3} = \dfrac{2(1-p)}{p^2}$