In general the [[Expectation]] of a function is NOT equal to a function of the expectation. An example is the quadratic function, where $g(X)=x^2$.
$
\begin{align}
\mathbb E[g(X)] &\not = g(\mathbb E[X]) \\[4pt]
\mathbb E[X^2] &\not = (\mathbb E[X])^2
\end{align} $
## Functions with Constants
However, if the function $g(X)$ is linear, then the equality actually holds. A function is linear, if it only adds or multiplies constants to the input $x$.
$
\begin{rcases}
Y&=g(X) \\
g(X) &= ax+b
\end{rcases} \,
\mathbb E[ax+b]=a\mathbb E[x]+b
$
**Derivation:**
$
\begin{align}
\mathbb E[Y] &= \sum_x g(X) * p_X(x) \tag{1}\\
&= \sum_x ax+b * p_X(x) \tag{2}\\
&= \sum_x ax * p_X(x) + \sum_x b * p_X(x) \tag{3}\\
&= a \underbrace{\sum_x x p_X(x)}_{\mathbb =E[X]} + b \underbrace{\sum_x p_X(x)}_{=1} \implies a\mathbb E[X] +b \tag{4}
\end{align}
$
where:
- (2) Insert actual function for $g(X)$.
- (3) Split up the sums.
- (4) Pull out constants from the summation.
## Functions with Random Variables
Now we extend the concept of linearity by linear functions of random variables.
$ \mathbb E[X+Y]= \mathbb E[X] + \mathbb E[Y] $
**Derivation:** Let us take a function $g(x,y)=x+y$, and calculate the expectation of that function.
$
\begin{align}
\mathbb E[X+Y] &=\mathbb E[g(x,y)] \tag{1}\\[8pt]
&=\sum_x \sum_y (x+y)*p_{XY}(x,y) \tag{2}\\
&=\sum_x \sum_y x*p_{XY}(x,y) + \sum_y \sum_x y*p_{XY}(x,y) \tag{3}\\
&=\sum_x x \sum_y p_{XY}(x,y) + \sum_y y \sum_x p_{XY}(x,y) \tag{4}\\
&=\sum_x x*p_X(x) + \sum_y y* p_Y(y) \tag{5}\\
&=\mathbb E[X] + \mathbb E[Y] \tag{6}
\end{align}
$
(2) Insert the function and sum over all $(x,y)$ pairs.
(4) Pull $x$ out of the sum that is summing over all $y$, because it is just a constant in there. Vice versa for $y$ in the second double summation.
(5) The summing over one of the 2-dimensional joint PMFs, makes it a 1-dimensional PMF across the axis not summed over.