The formal way of computing conditional expectation The question is: there are two random variables $X$ and $Y$ whose joint density is:
$$
f(x,y)=\begin{cases}
2 &\quad 0<x<1;0<y<x\\
0 &\quad otherwise
\end{cases}
$$
Find $E[X|Y]$.
I just intuitively compute in this way but don't know whether it is legal:
$$
f_Y(y)=\int_{y}^{1}2 \mathrm{d}x=2-2y
$$
$$
E[X|Y]=\frac{\int_{Y}^{1}2x\ \mathrm{d}x}{2(1-Y)}=\frac{1}{2}(1+Y)
$$
Do we need to care the range of $Y$ maybe? I am a little confused, on $\{Y<0\}$ and $\{Y>1\}$, is  $E[X|Y]$ defined as $0$ or undefined or can be defined as anything? I myself think according to definition, it can be anything. Furthermore, how to check $\forall A\in\sigma(Y)$, we have $\int_{A}E[X|Y]\mathrm{d}P=\int_{A}X\mathrm{d}P$?
And I have another concern: for an event $A$ and $P(A)$ is neither $0$ nor $1$, $E[X|A]$ should be defined as:
$$
E[X|A]=E[X|\sigma(A)]=\begin{cases}\frac{1}{P(A)}\int_{A}X\ \mathrm{d}P &\quad \omega\in A\\
\frac{1}{1-P(A)}\int_{\Omega-A}X\ \mathrm{d}P &\quad \omega\in \Omega-A
\end{cases}
$$
However, we cannot write $E[X|Y=y]=\frac{1}{P(Y=y)}\int_{Y=y}X\ \mathrm{d}P$ since $P(Y=y)=0$. But $\int_{Y=y}X\ \mathrm{d}P$ is also $0$ so I guess there is some way to deal with this. Is there a formal and rigorous way to deal with this? Thanks for any help!
 A: Here it what you need to justify it.
Theorem. Consider two integrable random vectors $X$ and $Y$ on $\mathbf{R}^p$ and $\mathbf{R}^q,$ respectively, defined on some probability space $(\Omega, \mathscr{X}, \mathbf{P})$ and with joint density $(x, y) \mapsto f_{(X, Y)}(x, y)$ defined on $\mathbf{R}^{p + q}$ (where canonical identifications have been done). Consider a Borel measurable function $h:\mathbf{R}^p \to \mathbf{R}$ such that $h(X)$ is integrable. Then, a version of $\mathbb{E}(h(X) \mid Y)$ is given by $\varphi(Y),$ where $\varphi$ is the Borel function $\mathbf{R}^d \to \mathbf{R}$ given by the rule
$$
\varphi:y \mapsto \int\limits_{\mathbf{R}^p} h(x) f_{X \mid Y}(x, y)\ d\lambda_p(x),
$$
where
$$
f_{X \mid Y}(x, y) = \begin{cases}
\dfrac{f_{(X, Y)}(x, y)}{f_Y(x)} &\text{ if } f_Y(x) > 0,\\
0 &\text{ otherwise},
\end{cases}
$$
(here $\lambda_p$ is Lebesgue measure on $\mathbf{R}^p$ and similar notation for $\mathbf{R}^q$ will be employed).
Proof. Lesbesgue-Fubini theorem shows at once that $\varphi$ is Borel measurable (since $f$ is assumed Borel measurable). Hence, $\varphi$ will be a version of the conditional expectation of $X$ given $Y$ if $\varphi$ satisfies the defining property:
$$
\int\limits_{\Omega} h(X) \chi_{\{Y \in \mathrm{B}\}}\ d \mathbf{P} = \int\limits_{\{Y \in \mathrm{B}\}} h(X)\ d\mathbf{P} = \int\limits_{\{Y \in \mathrm{B}\}} \varphi(Y)\ d\mathbf{P} = \int\limits_{\mathrm{B}} \varphi(y) f_Y(y)d\lambda_q(y).
$$
Repeated applications of Lebesgue-Fubini theorem allows deducing at once
$$
\begin{align*}
\int\limits_{\mathrm{B}} \varphi(y) f_Y(y)d\lambda_q(y) &= \int\limits_{\mathbf{R}^p} \int\limits_{\mathrm{B}} h(x) f_{X\mid Y}(x, y) f_Y(y) d\lambda_q(y) \ d\lambda_p(x) \\
&= \int\limits_{\mathbf{R}^p} \int\limits_{\mathrm{B}} h(x) f_{(X, Y)}(x, y)\ d\lambda_q(y) d\lambda_p(x) \\
&= \int\limits_{\mathbf{R}^p} \int\limits_{\mathbf{R}^q} h(x) \chi_{\mathrm{B}}(y) f_{(X, Y)}(x, y) d\lambda_q(y) d\lambda_p(x) \\
&= \int\limits_{\Omega} h(X) \chi_{\{Y \in \mathrm{B}\}}\ d\mathbf{P}.
\end{align*}
$$
The proof is terminated. Q.E.D.
