Compute variance of "tree" random variable Let A be a random variable defined as:


*

*With probability $p[i]$, the random variable $B[i]$ is drawn

*$B[i] ~ N[mu[i],sigma[i]]$

*probabilities $p[i]$ sum up to one


I know how to compute the mean, which is given by:
$$E[A] = p[1]*mu[1] + .. +  p[N]*mu[N]$$
I would like to know how to compute the variance

 A: Let $D=i$ stand for the event that variable $B[i]$ is drawn.
For 'nice' function $f$:
$\mathbb{E}f\left(A\right)=\sum_{i=1}^{N}\mathbb{E}\left(f\left(A\right)\mid D=i\right)P\left(D=i\right)=\sum_{i=1}^{N}\mathbb{E}f\left(B\left[i\right]\right)p\left[i\right]$
where $B\left[i\right]\sim N\left(\mu_{i},\sigma_{i}\right)$.
This enables you to find $\mathbb{E}A$ where $f$ is prescribed by
$a\mapsto a$ but also $\mathbb{E}A^{2}$ where $f$ is the function
prescribed by $a\mapsto a^{2}$.
Then you are ready: $\text{Var}A=\mathbb{E}A^{2}-\left(\mathbb{E}A\right)^{2}$
A: Let $(I_i)_{1\leq i\leq n}$ be a partition of the interval $[0,1]$ into $n$ disjoint subsets such that $I_i$ has length $p_i$ and $\bigcup\limits_{i=1}^n I_i=[0,1]$. Then 
$$
A=\sum_{i=1}^n B_i\mathbf{1}_{U\in I_i},
$$
where $U$ is uniformly distributed on $[0,1]$ and independent of all $B_i$. The mean of $A$ is given by
$$
{\rm E}[A]=\sum_{i=1}^n {\rm E}[B_i]{\rm E}[\mathbf{1}_{U\in I_i}]=\sum_{i=1}^n {\rm E}[B_i]P(U\in I_i)=\sum_{i=1}^n {\rm E}[B_i]p_i.
$$
In a similar fashion you should be able to find the variance.
