# Difference of expectations is integral of certain probabilities

I'm trying to prove the equality $$\mathbb{E}(Y) - \mathbb{E}(X) = \int_{\mathbb{R}} (\mathbb{P}[X < t \leq Y] - \mathbb{P}[Y < t \leq X]) \, \mathrm{d} t$$ when $X,Y\in L^1$ (not necessarily independent). So far I've tried this:

Let $g(x,y,t) = \mathbf{1}_{\{ (u,v,w) \in \mathbb{R}^3 : u < w \leq v \}}(x,y,t)$. I'll work with the integral of the first probability in the RHS: $$\int_{\mathbb{R}} \mathbb{P}[X < t \leq Y] \, \mathrm{d} t = \int_{\mathbb{R}} \left( \int_{\mathbb{R}^2} \mathbf{1}_{\{ (u,v) \in \mathbb{R}^2 : u < t \leq v \}}(x,y) \, \mathrm{d} (F_X \times F_Y) \right)\, \mathrm{d} t.$$

We can apply Fubini to $\int g \, \mathrm{d}(F_X \times F_Y \times \lambda)$ ($\lambda$ is the Lebesgue measure) with domain $\mathbb{R}^3$ and we obtain \begin{align*} \int_{\mathbb{R}} \mathbb{P}[X < t \leq Y] \, \mathrm{d} t & = \int_{\mathbb{R}} \left( \int_{\mathbb{R}^2} \mathbf{1}_{\{ (u,v) \in \mathbb{R}^2 : u < t \leq v \}}(x,y) \, \mathrm{d} (F_X \times F_Y) \right)\, \mathrm{d} t\\ \text{Fubini } \leftarrow & = \int_{\mathbb{R}^2} \left( \int_{\mathbb{R}} \mathbf{1}_{\{ w \in \mathbb{R} : x < w \leq y \}}(t) \,\mathrm{d}t \right) \,\mathrm{d}(F_X\times F_Y)\\ &= \int_{\mathbb{R}^2} (y-x)\,\mathrm{d}(F_X\times F_Y)\\ \text{Fubini } \leftarrow &= \int_{\mathbb{R}} \left( \int_{\mathbb{R}} (y-x) F_X(\mathrm{d}x) \right) F_Y(\mathrm{d}y)\\ &= \int_{\mathbb{R}} \left( y-\mathbb{E}(X) \right) F_Y(\mathrm{d}y)\\ &= \mathbb{E}(Y) - \mathbb{E}(X) \end{align*}

Here I'm defining this probability on $(\mathbb{R}^2, \mathcal{B}(\mathbb{R}) \times \mathcal{B}(\mathbb{R}))$ (generated by the rectangles): $$\widetilde {\mathbb{P}}(A\times B) = \mathbb{P}\circ X^{-1}(A)\mathbb{P}\circ Y^{-1}(B) = \mathbb{P}[X\in A]\mathbb{P}[Y\in B] = F_X(A)F_Y(B),$$ and as such I denote $\widetilde{\mathbb{P}}$ as $F_X\times F_Y$.

I'm obviously doing something wrong, because by this argument the second integral on the RHS is $\mathbb{E}(X) - \mathbb{E}(Y)$ and the subtraction is $2\mathbb{E}(Y) - 2\mathbb{E}(X)$. Maybe I defined some product measure in a wrong way, ergo I applied Fubini wrong, but I cannot see it.

Can you please help me? Any hint would be appreciated.

Your error is in evaluating this integral: $$\int_{\mathbb{R}} \mathbf{1}_{\{ w \in \mathbb{R} : x < w \leq y \}}(t) \,\mathrm{d}t$$ The integral simplifies to $y-x$ provided $y\ge x$. If not, the integral is zero. So the proper simplification is: $$\int_{\mathbb{R}} \mathbf{1}_{\{ w \in \mathbb{R} : x < w \leq y \}}(t) \,\mathrm{d}t =(y-x) \mathbf{1}_{\{y\ge x\}},$$ which leads to $$\int_{\mathbb{R}} \mathbb{P}[X < t \leq Y] \, \mathrm{d} t = \mathbb{E}\left[(Y-X) \mathbf{1}_{(Y\ge X)}\right].$$ From here you can derive the desired result.