If $E[X|Y]=Y$ almost surely and $E[Y|X]=X$ almost surely then $X=Y$ almost surely 
Assume that $X$ and $Y$ are two random variables such that $Y=E[X|Y]$ almost surely and $X= E[Y|X]$ almost surely. Prove that $X=Y$ almost surely.

The hint I was given is to evaluate:
$$E[X-Y;X>a,Y\leq a] + E[X-Y;X\leq a,Y\leq a]$$
which I can write as: $$\int_A(X-Y)dP +\int_B(X-Y)dP$$ where $A=\{X>a, Y\leq a\}$ and $B=\{X\leq a,Y\leq a\}$.
But I need some more hints.
 A: Simply follow the hint... First note that, since $E(X\mid Y)=Y$ almost surely, for every $c$, $$E(X-Y;Y\leqslant c)=E(E(X\mid Y)-Y;Y\leqslant c)=0,$$ and that, decomposing the event $[Y\leqslant c]$ into the disjoint union of the events $[X>c,Y\leqslant c]$ and $[X\leqslant c,Y\leqslant c]$, one has $$E(X-Y;Y\leqslant c)=U_c+E(X-Y;X\leqslant c,Y\leqslant c),$$ with $$U_c=E(X-Y;X>c,Y\leqslant c).$$ Since $U_c\geqslant0$, this shows that $$E(X-Y;X\leqslant c,Y\leqslant c)\leqslant 0.$$ Exchanging $X$ and $Y$ and following the same steps, using the hypothesis that $E(Y\mid X)=X$ almost surely instead of $E(X\mid Y)=Y$ almost surely, one gets $$E(Y-X;X\leqslant c,Y\leqslant c)\leqslant0,$$ that is $$E(Y-X;X\leqslant c,Y\leqslant c)=0,$$  which, coming back to the first decomposition of an expectation above, yields $U_c=0$, that is, $$E(X-Y;X>c\geqslant Y)=0.$$ This is the expectation of a nonnegative random variable hence $(X-Y)\mathbf 1_{X>c\geqslant Y}=0$ almost surely, which can only happen if the event $[X>c\geqslant Y]$ has probability zero. Now, $$[X>Y]=\bigcup_{c\in\mathbb Q}[X>c\geqslant Y],$$ hence all this proves that $P(X>Y)=0$. By symmetry, $P(Y>X)=0$ and we are done.
A: If $X,Y$ are square-integrable we can give a quick proof.
Consider the random variable $(X-Y)^2 = X^2 - 2XY + Y^2$.  Let's compute its expectation by conditioning on $X$.  We have
$$\begin{align*} E[(X-Y)^2] &= E[E[(X-Y)^2 \mid X]] \\
&= E[E[X^2 - 2XY + Y^2 \mid X]] \\
&= E[X^2 - 2 X E[Y \mid X] + E[Y^2 \mid X]] \\
&= E[X^2 - 2 X^2 + E[Y^2 \mid X]] \\
&= E[-X^2 + Y^2]\end{align*}$$
If we condition on $Y$ instead we get $E[(X-Y)^2] = E[X^2 - Y^2]$.  Comparing these, we see that we have $E[(X-Y)^2] = -E[(X-Y)^2]$, i.e. $E[(X-Y)^2]=0$.  This means $X=Y$ almost surely.
Unfortunately I don't quite see a way to handle the case where $X,Y$ are merely integrable, since in that case some of the expectations used above may be undefined.
Acknowledgement of priority. After writing this I found (thanks to Did) that essentially the same proof was given by Michael Hardy in Conditional expectation and almost sure equality.
A: Let $h:{\mathbb R}\to{\mathbb R}$ be bounded and strictly increasing. (For example, $h(x)=1/(1+e^{-x})$.) Since $X$ and $Y$ are integrable and $h$ is bounded, the random variable $Z:=(X-Y)(h(X)-h(Y))$ is integrable, with expectation
$$
E[Xh(X) -Yh(X)-Xh(Y) +Yh(Y)].\tag1
$$
But $E[Yh(X)]=E\left[E(Yh(X)\mid X)\right]=E[h(X)E(Y\mid X)]=E[Xh(X)]$ and similarly $E[Xh(Y)]=E[Yh(Y)]$. Plugging into (1), we find the expectation of $Z$ is zero. But $Z$ is non-negative, since $h$ is increasing. It follows that $Z$ is zero almost surely. To finish the proof, the fact that $h$ is one-to-one implies the set inclusion
$$\left\{(X-Y)(h(X)-h(Y))=0\right\}\subset\left\{X=Y\right\}.
$$
A: we can prove that $\forall k \in \mathbb{N},E[\min(X,k)|\min(Y,k)]=\min(Y,k)$ and $E[\min(Y,k)|\min(X,k)]=\min(X,k)$ by noticing that $E[\min(X,k)|\min(Y,k)] \leq \min(E[X|\min(Y,k)],k)$ and that $E[X|\min(Y,k)]=E[E[X|Y]|\min(Y,k)]=E[Y|\min(Y,k)]$ also we have $\min(E[Y|\min(Y,k)],k)=\min(Y,k)$
This method, works for integrable functions and with functions taking values in $[0, \infty]$ by truncating the random variables, and then we can consider the $L^2$ case. 
