Question about N iid exponentially distributed r.v's Xi (where N is also a r.v.) I took screenshots of both the question and answers provided, however there are a couple of steps I did not understand and i put red arrows over equal signs i didn't get. (normally there's an asterisk above the equals sign and an explanation, but none for this question).
I'm asking if someone can explain both where the formulae come from and also what's going on in the transformations.


 A: We note that $S = X_1 + \cdots + X_N$ is the sum of $N$ IID exponential random variables, where $N$ is itself random (Poisson).  The law of total expectation states that $$\operatorname{E}[S] = \operatorname{E}[\operatorname{E}[S \mid N]],$$ where the inner expectation is taken with respect to $X$ over the conditional random variable $S \mid N$, and the outer expectation is taken with respect to $N$.  The application is simple.  If $S$ is a sum of $N$ random variables, then $S \mid N$ is the same sum, but presuming that $N$ is fixed and known--that is to say, $S \mid N$ is the random value of $S$ given that $N$ is known.  In such a case, the expectation of $S \mid N$ (i.e. ignoring for the time being the randomness of $N$) is simply $$\operatorname{E}[S \mid N] = \operatorname{E}[X_1 + X_2 + \cdots + X_N \mid N] = N \operatorname{E}[X] = N/\theta,$$ since each of the $X_1, X_2, \ldots $ are IID. Consequently, $$\operatorname{E}[S] = \operatorname{E}[N/\theta] = \operatorname{E}[N]/\theta,$$ since $\theta$ is a fixed (but unknown) parameter, not a random variable.  And since the mean of $N$ is $\lambda$, we get the claimed result.
A similar principle applies to the law of total variance.  Here, $\operatorname{Var}[S]$ requires us to calculate the conditional variance $\operatorname{Var}[S \mid N]$, which is similar to our previous calculation above in that we assume momentarily that $N$ is fixed.  Again, since the individual $X_k$s are IID, the variance of the sum is equal to the sum of their variances; i.e., $$\operatorname{Var}[S \mid N] = N \operatorname{Var}[X] = N/\theta^2.$$  Now we combine all the results together:  $$\operatorname{Var}[S] = \operatorname{E}[\operatorname{Var}[S \mid N]] + \operatorname{Var}[\operatorname{E}[S \mid N]] = \operatorname{E}[N/\theta^2] + \operatorname{Var}[N/\theta] = \lambda/\theta^2 + \lambda/\theta^2 = 2\lambda/\theta^2.$$
Finally for the MGF, the thing to remember is the definition of the MGF in terms of the expectation:  $$M_S(t) = \operatorname{E}[e^{tS}].$$  But this expectation can be written as $$\operatorname{E}[\operatorname{E}[e^{tS} \mid N]]$$ using the law of total expectation in reverse.  And since $$e^{tS} = e^{t(X_1 + \cdots + X_N)} = e^{tX_1} e^{tX_2} \cdots e^{tX_N},$$ we see that $$\operatorname{E}[e^{tS}\mid N] = \prod_{k=1}^N \operatorname{E}[e^{tX}] = (M_X(t))^N = \exp \left(N \log M_X(t)\right),$$ again because the $X_k$s are IID.  Now we write $$M_S(t) = \operatorname{E}[e^{N \log M_X(t)}] = M_N(\log M_X(t)),$$ and this proves the claim.
