Given
- $g(x)$ defined for all real numbers $x$.
- $p(x)$ defined for $a\le x\le b$, which is the probability distribution of $x$
I have trouble calculating variance of $Var(g(x))$ because the two approaches below yield different results:
Calculate the expectation of $g(x)$ using $\mu =\int_a^b g(x)p(x) dx$. Then, calculate variance $Var=\int_a^b(g(x)-\mu)^2p(x)dx$. Note that when calculating variance, I plugged in $\mu$
Calculate variance directly using $$Var =\int_a^b\Big[g(x)-Exp(g(x))\Big]^2p(x)dx = \int_a^b\left[g(x)-\left(\int g(x)p(x)dx\right)\right]^2p(x)dx$$
Which of my approaches above (if any) is correct, if any, and why?
Both approaches are doing the same workload. The second approach calculates the expectation inline, rather than calculating first then substituting.
$$\int_a^b \left(g(x)-\int_a^b g(x)~p(x)\mathsf d x\right)^2~p(x)~\mathsf d x ~=~\int_a^b\left(g(x)-\mu\right)^2~p(x)~\mathsf d x$$