Calculating expected value for a Binomial random variable How do you calculate $E(X^2)$ given the the number of trials and the probability of success?
$E(X) = np$, then $E(X^2) = $?
Do we have to draw up a table for $n=0,1,2,\ldots,n$ and then use the probability of success for each. 
$$E(X) = x \ P(X=x) \ldots$$ this would take forever, is there a shortcut? 
 A: (Here, I assume that you do not know the variance of $X$; otherwise, $\mathbb{E}(X^2)$ can be easily calculated using $(1)$.)
If $X \sim Bin(n,p)$, then we can write $$X = \sum_{j=1}^n Y_j$$ where $Y_j$ are independent identically distributed random variables $Y_j \sim p \delta_1+ (1-p) \delta_0$ (i.e. $\mathbb{P}(Y_j = 1) = p$, $\mathbb{P}(Y_j = 0) = 1-p$). Since the random variables are independent, it is not difficult to see that
$$\DeclareMathOperator \var{var} \var X = \var\left( \sum_{j=1}^n Y_j \right) = \sum_{j=1}^n \var Y_j = n \var Y_1.$$
Moreover,
$$\var X = \mathbb{E}(X^2)-(\mathbb{E}X)^2. \tag{1}$$
Consequently,
$$\mathbb{E}(X^2) = (\mathbb{E}X)^2 + n \var \, Y_1 = (np)^2 + n \var Y_1.$$
It remains to calculate $\var Y_1$; since this is a standard exercise I leave it to you.
A: From the Newton binomial expansion
$$
(a+b)^{n}=\sum_{k=0}^n\binom{n}{k}a^kb^{n-k} \tag1
$$
you may deduce, by differentiating twice with respect to $a$, that
$$
n(a+b)^{n-1}=\sum_{k=1}^nk\binom{n}{k}a^{k-1}b^{n-k} \tag2
$$
$$
n(n-1)(a+b)^{n-2}=\sum_{k=2}^n k(k-1)\binom{n}{k}a^{k-2}b^{n-k} \tag3
$$
Now, we have
$$
E(X^2)=\sum_{k=0}^nk^2\binom{n}{k}p^{k}(1-p)^{n-k},
$$ observing that $k^2=k(k-1)+k$ and using $(1),(2)$ and $(3)$, should do it.
A: Well, $\mathrm{var}(X) = np(1-p)$ and $\mathrm{var}(X) = E(X²) - (E(X))^2$, so: $$E(X²) = \mathrm{var}(X) + (E(X))² = np(1-p) + n^2p^2 = np(1-p+np)$$
