<s>[INST] Iterated expectations and variances examples Suppose we generate a random variable $X$ in the following way. First we flip a fair coin. If the coin is heads, take $X$ to have a $Unif(0,1)$ distribution. If the coin is tails, take $X$ to have a $Unif(3,4)$ distribution.
Find the mean and standard deviation of $X$.
This is my solution.  I wanted to check if it's correct or if there's a better approach. 
Let $Y$ denote the random variable that is $1$ if the coin lands on a head and $0$ otherwise 
Firstly $\mathbb{E}(\mathbb{E}(X|Y)) = \mathbb{E}(X)$
Thus $\mathbb{E}(\mathbb{E}(X|Y)) = \frac{1}{2} \cdot \mathbb{E}(X|Y=0) + \frac{1}{2} \cdot \mathbb{E}(X|Y=1) = \frac{1}{2} \cdot \frac{1}{2} + \frac{1}{2} \cdot \frac{7}{2}=2$
Secondly $\mathbb{V}(X) = \mathbb{E}(\mathbb{V}(X|Y))+\mathbb{V}(\mathbb{E}(X|Y))$
Now $\mathbb{V}(X|Y = 0) = \mathbb{V}(X|Y=1) = \frac{1}{12}$. Thus $\mathbb{E}(\mathbb{V}(X|Y)) = \frac{1}{12}$. Next calculating $\mathbb{V}(\mathbb{E}(X|Y)) = \mathbb{E}(\mathbb{E}(X^2|Y)) - (\mathbb{E}(\mathbb{E}(X|Y)))^2 = (\frac{1}{2} \cdot \frac{1}{4} + \frac{49}{4} \cdot \frac{1}{2}) - (2)^2 = \frac{50}{8} - 4.$ 
 [/INST] There are generally two ways to approach these types of problems: by (1) Finding the second stage expectation $E(X)$ with the theorem
of total expectation; or by (2) Finding the second stage expectation
$E(X)$, using $f_{X}(x)$. These are equivalent methods, but you
might find one easier to comprehend, so I present them both in detail
below for $E(X)$. The approach is similar for $Var(X)$, so I exclude
its presentation, but can update my answer if you really need it.
Method (1) Finding the second stage expectation $E(X)$ with the theorem of total expectation
In this case, the Theorem of Total Expectation states that:
\begin{eqnarray*}
E(X) & = & \sum_{y=0}^{1}E(X|Y=y)P(Y=y)\\
 & = & \sum_{y=0}^{1}E(X|Y=y)f_{Y}(y)
\end{eqnarray*}
So, we simply need to find the corresponding terms in the line above
for $y=0$ and $y=1$. We are given the following:
\begin{eqnarray*}
f_{Y}(y) & = & \begin{cases}
\frac{1}{2} & \text{for}\,y=0\,(heads),\,1\,(tails)\\
0 & \text{otherwise}
\end{cases}
\end{eqnarray*}
and
\begin{eqnarray*}
f_{X|Y}(x|y) & = & \begin{cases}
1 & \text{for}\,3<x<4;\,y=0\\
1 & \text{for}\,0<x<1;\,y=1
\end{cases}
\end{eqnarray*}
Now, we simply need to obtain $E(X|Y=y)$ for each realization of $y$:
\begin{eqnarray*}
E(X|Y=y) & = & \int_{-\infty}^{\infty}xf_{X|Y}(x|y)dx\\
 & = & \begin{cases}
\int_{3}^{4}x(1)dx & \text{for}\,y=0\\
\int_{0}^{1}x(1)dx & \text{for}\,y=1
\end{cases}\\
 & = & \begin{cases}
\left.\frac{x^{2}}{2}\right|_{x=3}^{x=4} & \text{for}\,y=0\\
\left.\frac{x^{2}}{2}\right|_{x=0}^{x=1} & \text{for}\,y=1
\end{cases}\\
 & = & \begin{cases}
\frac{7}{2} & \text{for}\,y=0\\
\frac{1}{2} & \text{for}\,y=1
\end{cases}
\end{eqnarray*}
So, substituting each term into the Theorem of Total Expectation above
yields:
\begin{eqnarray*}
E(X) & = & \sum_{y=0}^{1}E(X|Y=y)f_{Y}(y)\\
 & = & E(X|Y=0)f_{Y}(0)+E(X|Y=1)f_{Y}(1)\\
 & = & \left(\frac{7}{2}\right)\left(\frac{1}{2}\right)+\left(\frac{1}{2}\right)\left(\frac{1}{2}\right)\\
 & = & 2
\end{eqnarray*}
Method (2) Finding the second stage expectation $E(X)$, using $f_{X}(x)$
To use this method, we first find the $f_{X,Y}(x,y)$ and $f_{X}(X)$.
To begin, recall that $f_{X,Y}(x,y)$ is given by:
\begin{eqnarray*}
f_{X,Y}(x,y) & = & f_{X|Y}(x|y)f_{Y}(y)\\
 & = & \begin{cases}
\left(1\right)\left(\frac{1}{2}\right) & \text{for}\,3<x<4;\,y=0\\
\left(1\right)\left(\frac{1}{2}\right) & \text{for}\,0<x<1;\,y=1
\end{cases}\\
\end{eqnarray*}
and we can find $f_{X}(x)$ by summing out the $y$ component:
\begin{eqnarray*}
f_{X}(x) & = & \sum_{y=0}^{1}f_{X,Y}(x,y)\\
 & = & f_{X,Y}(x,0)+f_{X,Y}(x,1)\\
 & = & \frac{1}{2}I(3\le x\le4)+\frac{1}{2}I(0\le x\le1)
\end{eqnarray*}
And now, we can just find $E(X)$ using the probability density function of $f_{X}(x)$ as
usual:
\begin{eqnarray*}
E(X) & = & \int_{-\infty}^{\infty}xf_{X}(x)dx\\
 & = & \int_{-\infty}^{\infty}x\left[\frac{1}{2}I(3\le x\le4)+\frac{1}{2}I(0\le x\le1)\right]dx\\
 & = & \frac{1}{2}\int_{-\infty}^{\infty}xI(3\le x\le4)dx+\frac{1}{2}\int_{-\infty}^{\infty}xI(0\le x\le1)dx\\
 & = & \frac{1}{2}\int_{3}^{4}xdx+\frac{1}{2}\int_{0}^{1}xdx\\
 & = & \left(\frac{1}{2}\right)\left.\left(\frac{x^{2}}{2}\right)\right|_{x=3}^{x=4}+\left(\frac{1}{2}\right)\left.\left(\frac{x^{2}}{2}\right)\right|_{x=0}^{x=1}\\
 & = & \left(\frac{1}{2}\right)\left(\frac{7}{2}\right)+\left(\frac{1}{2}\right)\left(\frac{1}{2}\right)\\
 & = & 2
\end{eqnarray*}
the same two approaches can be used to compute $Var(X)$.
</s>