Let $K$ be a random variable with mean $\mathbf E[K]=e$, and $\textrm{var}(K)=v$. Let $X_1,X_2\ldots$ be a sequence of independent identically distributed random variables, all independent of $K$, with mean $1$ and variance $1$. Let $X=\sum_{i=1}^N X_i$. Calculate $\textrm{var}(X)$.
My calculation:
$\mathbf E[X]=\mathbf E[X_1+\ldots +X_K]$
$=\mathbf E\Bigl[\mathbf E\bigl[(X_1+\ldots +X_K)\mid K\bigr]\Bigr]$
$=\mathbf E\bigl[K\mathbf E[X_1]\bigr]$
$=\mathbf E[K\cdot 1]$
$=e$
$\mathbf E[X^2]=\mathbf E\bigl[(X_1+\ldots +X_K)(X_1+\ldots +X_K)\bigr]$
$=\mathbf E\Bigl[\mathbf E\bigl[(X_1+\ldots +X_K)(X_1+\ldots +X_K)\mid K\bigr]\Bigr]$
$=\mathbf E\Bigl[\mathbf E\bigl[(X_1+\ldots +X_K)\mid K\bigr]\mathbf E\bigl[(X_1+\ldots +X_K)\mid K\bigr]\Bigr]$
$=\mathbf E\bigl[K\mathbf E[X_1]K\mathbf E[X_1]\bigr]$
$=\mathbf E[K^2]$
$=\textrm{var}(K) + \bigl(\mathbf E[K]\bigr)^2$
$=v+e^2$
$\textrm{var}(X)=\mathbf E[X^2]-\bigl(\mathbf E[X]\bigr)^2=v+e^2-e^2=v$
Given solution:
$\textrm{var}(X)=\textrm{var}(X_i)\mathbf E[K]+\bigl(\mathbf E[X_i]\bigr)^2\textrm{var}(K)=e+v$
Can someone point out what I did wrong? I don't quite follow the given solution.
In your computation of the second moment, you did
$$E\left[ \left(\sum_{i=1}^KX_i\sum_{j=1}^KX_j|K\right)\right]=E\left[ \left(\sum_{i=1}^KX_i|K\right)\right]E\left[ \left(\sum_{i=1}^KX_i|K\right)\right]$$
This step is not correct.
Also, I believe it's a typo, in the computation of $E[X]$, the final solution is $e$ and not $v$.
Edit:
We use the Law of total Variance,
\begin{align}Var(X)&=E[Var(X|K)]+Var(E(X|K)) \\ &=E\left[Var\left(\sum_{i=1}^K X_i|K\right)\right]+ Var(KE(X_1))\\ &= E[KVar(X_1)]+(E(X_1))^2Var(K)\\ &= Var(X_1)E[K] + (E(X_1))^2Var(K)\end{align}