Solution (source code)

= Solution

Write $A=g(X)$ and $B=g(Y)$. The two <conditional expectation> identities supply a $\sigma(Y)$-<measurable> version of $B$ and a $\sigma(X)$-<measurable> version of $A$. Work with those versions, which do not change any <almost sure equality>. Both lie in <L2 space>, and the <Cauchy-Schwarz inequality> makes their product an <integrable random variable>.

Testing the first <conditional expectation> identity against $B$, and the second against $A$, gives
$$
\mathbb E[AB]=\mathbb E[B^2],\qquad
\mathbb E[AB]=\mathbb E[A^2].
$$
For completeness, these tests with unbounded multipliers are valid by first using the bounded truncations $B_m=(-m)\vee(B\wedge m)$ and $A_m=(-m)\vee(A\wedge m)$, then passing to the limit with the <Cauchy-Schwarz inequality> and <convergence in L2>. Consequently,
$$
\mathbb E[(A-B)^2]=\mathbb E[A^2]+\mathbb E[B^2]-2\mathbb E[AB]=0.
$$
A nonnegative <random variable> with zero <expected value> vanishes <almost surely>. Therefore \b[$g(X)=g(Y)$ almost surely]. This is <mutual conditional expectation rigidity in L2>; it also follows from the <equality case for conditional second moments>.

For a counterexample to equality of the original <random variables>, let $X$ give <probability> $1/4$ to each of $-2,-1,1,2$, set $Y=-X$, and use the nonconstant <function> $g(t)=t^2$. Then $g(X)=g(Y)=X^2$ takes both values $1$ and $4$. It is <measurable> with respect to both generated <sigma-algebras>, so both required <conditional expectation> identities hold. Symmetry gives $\mathbb EX=\mathbb EY=0$, while neither <random variable> can be zero. Thus
$$
\boxed{\mathbb P(X=\mathbb EX)=\mathbb P(Y=\mathbb EY)=0,\qquad\mathbb P(X=Y)=0.}
$$