Let $X,Y$ be two discrete random variables with finite expected value and variance,
we define: $Var(Y|X=x)=E[(Y-E(Y|X=x))^2|X=x]$ for $x$ such that $P_X(x)>0$.
The variance of $Y$ given $X$: $Var(Y|X)=g(X)$ such that $g(x)=Var(Y|X=x)$.
Prove:
$Var(Y|X)=E(Y^2|X)-(E(Y|X))^2$
$Var(Y)=E(Var(Y|X))+Var(E(Y|X))$
My Attempt:
The idea: Find $Var(Y|X=x)$ and substitute $X$.
$Var(Y|X=x)=E[Y^2-2YE(Y|X=x)+(E(Y|X=x))^2 | X=x]=E(Y^2|X=x)-2E(Y|X=x)E(Y|X=x)+
(E(Y|X=x))^2=E(Y^2|X=x)-(E(Y|X=x))^2$
I have reached what needs to be proven, I wanted to ask, if it wasn't given that $Var(Y|X)=g(X)$ such that $g(x)=Var(Y|X=x)$, I couldn't just jump from here substituting $X$, like my question is the idea to substitute $X$ a general one or it's specific for this question.
I got stuck in the second part, I'm not sure what to use right here to reach $Var(Y)$, I know that $E(E(Y|X))=E(Y)$, but I tried to use definition of variance, and I didn't know how to start.
$Var(Y)=E(Y^2)-E(Y)^2$
Any feedback is appreciated, thanks in advance.
For the second part note that you can write, \begin{align} \mathbb{E}[\text{Var}(Y|X)] &= \mathbb{E}\left[\mathbb{E}[Y^2|X] - \mathbb{E}[Y|X]^2\right] \\ &= \mathbb{E}\left[\mathbb{E}[Y^2|X]\right] - \mathbb{E}\left[\mathbb{E}[Y|X]^2\right] \\ &= \mathbb{E}[Y^2] - \left(\mathbb{E}\left[\mathbb{E}[Y|X]^2\right] - \mathbb{E}\left[\mathbb{E}[Y|X]\right]^2 + \mathbb{E}\left[\mathbb{E}[Y|X]\right]^2\right) \\ &= \mathbb{E}[Y^2] - \mathbb{E}\left[\mathbb{E}[Y|X]\right]^2 - (\text{Var}(\mathbb{E}[X|Y])) \\ &= \text{Var}(Y) - \text{Var}(\mathbb{E}[Y|X]) \end{align}
Now rearranging gives the desired result. Hope this helps.