# Sum of a random number of independent random variables

Consider the sum $Y = X_1 + \cdots + X_N$ where $N$ is a random variable that takes nonnegative integer values, and $X_1, X_2, \cdots$, are identically distributed random variables. Assume that $N, X_1, X_2, \cdots$ are independent. Find the moment generating function for $Y$, $M_Y(s) = \displaystyle \mathbb{E}[e^{sY}]$.

Working: Using the law of total expectation we have:

$\displaystyle \mathbb{E}[e^{sY}] = \mathbb{E}[\mathbb{E}[e^{sY}|N]]$

$\mathbb{E}[e^{sY}|N=n] = \mathbb{E}[e^{sX_1} \cdots e^{sX_N}|N=n] = \mathbb{E}[e^{sX_1} \cdots e^{sX_n}] = \mathbb{E}[e^{sX_1}]\cdots \mathbb{E}[e^{sX_n}] = [M_X(s)]^n$

where $M_X(s)$ denotes the common moment generating function of each of the $X_i$'s.

Thus, $M_Y(s) = \mathbb{E}[\mathbb{E}[e^{sY}|N]] = \mathbb{E}[[M_X(s)]^N]$.

Query: My book says to actually compute $M_Y(s) = \mathbb{E}[[M_X(s)]^N]$, one can just simply start with the transform $M_N(s) = \mathbb{E}[e^{sN}]$ and then replace each instance of $e^s$ with $M_X(s)$. They give the following example (amongst others): What I don't understand is that how can we simply just replace $e^s$ with $M_X(s)$? Because in general, if we know the expression for, say, $\mathbb{E}[e^{sQ}]$ (for some generic random variable $Q$), then to find $\mathbb{E}[g(s)^Q]$ (for some generic function $g(s)$), we CANNOT just simply replace every instance of $e^s$ (in the expression for $\mathbb{E}[e^{sQ}]$) with $g(s)$. How come my book says we can?

You can write $M_X(s)^N$ as $\exp(N \log(M_X(s))$. So we have $$M_Y(s)=\sum_n \exp(N \log(M_X(s)) P[N=n] = M_N(\log M_X(s)).$$
The replacing of $e^s$ with $M_X(s)$ is a cumbersome (and somewhat confusing) way of saying replace $s$ with $\log M_X(s)$.