Expected values of squares Question A fair coin is tossed three times. Let Y be the random variable that denotes the square of the number of heads. For example, in the outcome HTH, there are two heads and Y = 4. What is E[Y]?
My answer: 
possible outcomes to toss a coin three times : 0, 1, 2, 3
possible outcomes of Y : 0, 1, 4, 9
E[Y] = (1/6 * 0) + (1/6 * 1) + (1/6 * 4) + (1/6 * 9)
Is it ok? Thanks! 
 A: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

$\ds{h:\ HEAD.\ t:\ TOSSES}$.

\begin{align}
&\bbox[10px,#ffd]{\sum_{h = 0}^{t}{t \choose h}
\pars{1 \over 2}^{t - h}\pars{1 \over 2}^{h}h^{2}} =
\left.{1 \over 2^{t}}\,\pars{x\,\partiald{}{x}}^{2}\sum_{h = 0}^{t}
{t \choose h}x^{h}\,\right\vert_{\ x\ =\ 1}
\\[5mm] = &\
\left.{1 \over 2^{t}}\,\pars{x\,\partiald{}{x}}^{2}\pars{1 + x}^{t}\,\right\vert_{\ x\ =\ 1} =
\left.{1 \over 2^{t}}\,x\,\partiald{}{x}tx\pars{1 + x}^{t - 1}
\,\right\vert_{\ x\ =\ 1}
\\[5mm] = &\
{t \over 2^{t}}\,x\bracks{%
\pars{1 + x}^{t - 1} + x\pars{t - 1}\pars{1 + x}^{t - 2}}
_{\ x\ =\ 1}
\\[5mm] = &\
{t \over 2^{t}}\bracks{2^{t - 1} + \pars{t - 1}2^{t - 2}}
=
{1 \over 2}\,t + {1 \over 4}\,t\pars{t - 1} =
\bbx{t\pars{t + 1} \over 4}
\end{align}
A: In general for $n$ tosses
$$
 \mathbf{E} = \sum_{i=0}^n{n \choose i}\left(\frac1{2}\right)^{\!\!n} i^2
$$
A: In this particular case the probabilities of 0, 1, 2, and 3 heads are 1/8, 3/8, 3/8, 1/8 and so 
$$ E(Y) = (1/8) \times 0^2 + (3/8) \times 1^2 + (3/8) \times 2^2 + (1/8) \times 3^2 = 3. $$
In general for $n$ tosses, you have $E(Y) = E(X^2)$ where $X$ is a binomial(n, 1/2) random variable.  Furthermore $E(X^2) = E(X)^2 + Var(X)$.  It's well-known that $E(X) = n/2, Var(X) = n/4$, and so you have $E(X)^2 = (n/2)^2 + n/4 = n(n+1)/4$.  Felix Marin already proved this by  a different route.
