# Changing order of integration in tail expectation formula?

My textbook (Casella and Berger) proves that for a non-negative continuous random variable, $$X$$, we have

$$E[x] = \int_{0}^{\infty}(1-F(x)) \, dx$$ where $$F(x)$$ is the CDF and $$E[x]$$ is the expectation.

The proof is

\begin{aligned} \int_{0}^{\infty}\left(1-F_{X}(x)\right) d x &=\int_{0}^{\infty} P(X>x) d x \\ &=\int_{0}^{\infty} \int_{x}^{\infty} f_{X}(y) d y d x \\ &=\int_{0}^{\infty} \int_{0}^{y} d x f_{X}(y) d y \\ &=\int_{0}^{\infty} y f_{X}(y) d y=\mathrm{E} X \end{aligned}

Could someone explain how when changing the order of integration, the limits go from $$x$$ to infinity, to $$0$$ to $$y$$? I've seen a few other posts on this topic, but I don't see any that explain how the limits change.

The region of integration is an infinite triangular region in the first quadrant satisfying the inequalities $$y \ge x \ge 0.$$ If you switch the region of integration so that $$x$$ is integrated first, then for a given $$y \in [0,\infty)$$, the range of $$x$$-values of this region are $$x \in [0, y]$$. Hence $$\int_{x=0}^\infty \int_{y=x}^\infty f(x,y) \, dy \, dx = \int_{y=0}^\infty \int_{x=0}^y f(x,y) \, dx \, dy$$ by Fubini's theorem. This is analogous to the finite discrete double summation case $$\sum_{x = 1}^n \sum_{y=x}^n f(x,y) = \sum_{y=1}^n \sum_{x=1}^y f(x,y)$$ which you can see from the following table (rows are $$x$$, columns are $$y$$):

$$\begin{array}{c|ccccc} (x,y) & 1 & 2 & 3 & \cdots & n \\ \hline 1 & (1,1) & (1,2) & (1,3) & \cdots & (1,n) \\ 2 & & (2,2) & (2,3) & \cdots & (2,n) \\ 3 & & & (3,3) & \cdots & (3,n) \\ \vdots & & & & \ddots & \vdots \\ n & & & & & (n,n) \end{array}$$

You can reach the same result integrating by parts

$$\mathbb{E}[X]=\int_0^{\infty}\Big[1-F_X(x)\Big]dx$$

...and more, if X is an arbitraty rv, you can easily prove that

$$\mathbb{E}[X]=\int_0^{\infty}\Big[1-F_X(x)\Big]dx-\int_{-\infty}^0F_X(x)dx$$

...always integrating by parts

Rewrite things with indicators, it usually helps: \begin{align} \int_0^{\infty} \int_x^\infty f_X(y) dydx &= \int_0^{\infty} dx \int_0^\infty dy \mathbf{1}_{[x,\infty)}(y)f_X(y)\\ &= \int_0^{\infty} dy f_X(y) \int_0^\infty dx \mathbf{1}_{[x,\infty)}(y) \tag{Fubini}\\ &= \int_0^{\infty} dy f_X(y) \int_0^\infty dx \mathbf{1}_{[0,y]}(x)\\ &= \int_0^{\infty} dy f_X(y) \int_0^y dx\\ &= \int_0^{\infty} yf_X(y) dy \end{align} where the only tricky part is where we used that $$\mathbf{1}_{[x,\infty)}(y)=\mathbf{1}_{[0,y]}(x)$$ since both are the indicator of $$\{ x\leq y\}$$.