The condition proposed by Nate Eldredge is not only necessary, but also sufficient. This is classical Strassen's theorem:
Theorem For integrable random variables $X$ and $Y$ the following is equivalent:
- There exist $\hat X \overset{d}{=} X$ and $\hat Y \overset{d}{=} Y$ such that $E[\hat X | \hat Y] = \hat Y$.
There exist $\hat X \overset{d}{=} X$ and $\hat Y \overset{d}{=} Y$ such that $E[\hat X | \hat Y] = \hat Y$.
- $Y\prec X$ in convex stochastic order, i.e. $E[f(Y)]\le E[f(X)]$ for any convex function $f$.
$Y\prec X$ in convex stochastic order, i.e. $E[f(Y)]\le E[f(X)]$ for any convex function $f$.
Note that the formulation in Strassen's paper differs from this one (and Strassen attributed it himself to Hardy-Littlewood-Polya-Blackwell-Stein-Sherman-Cartier-Fell-Meyer). In the present form it is given e.g. in Ruschendorf (Theorem 6 for $i=2$).