I have trouble with determining the domain for integration in the case of having a joint pdf when one variable depends on the other. There are two examples I don't quite understand, and hopefully, you can help me out.
Example 1: $ f(x,y)=2, 0\le x\le y \le 1$ In the solution, the expected values for $X$ and $Y$ $E[X]$ and $E[Y]$ are given as: $$E[X]=\int_{0}^{1} \int_{x}^{1} 2x \,dy \,dx$$ $$E[Y]=\int_{0}^{1} \int_{y}^{0} 2y \,dx \,dy$$ In the first case, we are using $1$ and $x$ for the inner integral. In the second case, I believe we are supposed to use $x$ and $1$ rather than $0$ and $1$(based on inequality). Am I wrong? I don't see why in the first case we chose $x$ rather than $0$ for the initial value while in the second case we chose $0$ over $x$ as an initial value.
Example 2:(similar concept but applied to marginal pdf):
Let $X$ and $Y$ have a joint pdf $f(x,y)=1, x\le y\le x+1, 0\le x\le1$ Then, $$f_{X}(x)=\int_{x}^{x+1}1dy,0\le x\le1$$ $$\begin{equation} f_{Y}(y)=\begin{cases} \int_{0}^{y}1dx=y, & \text{$0\le y\le1$}.\\ \int_{y-1}^{1}1dx=2-y, & \text{$1\le y\le2$}. \end{cases} \end{equation}$$ Why does here $f_{Y}(y)$ branches into 2 parts and why can't we simply say that $f_{Y}(y)=\int_{x}^{x+1}1dx$ ? (simillar to $f_{X}(x)$) . I do get that we need to get a function in terms of $y$ but all I am doing is just looking at the given inequality.
I ask these 2 questions together because I believe the issue comes from the same misunderstanding on my part.