Finding the expected value of a function of random variables I'm having troubles with finding marginal density functions and expected values in my probability theory class. I was hoping someone would be able to walk me through the solution to this question (I have the answer, I just don't understand how to get to it).
The question:
$$f(y_1, y_2)=\left\{\begin{array}{ccc}\frac{1}{y_1}&& 0\leq y_2 \leq y_1 \leq 1\\0&& \mathrm{elsewhere}\end{array}\right.$$
Find $\mathbf{E}(Y_1-Y_2)$. 
The answer is $1/4$ and I understand that I have to first find the marginal density functions and use them to find $\mathbf{E}(Y_1)$ and $\mathbf{E}(Y_2)$ but I can't even do that. Any help would be greatly appreciated.       
 A: 
I understand that I have to first find the marginal density functions and use them to find E(Y1) and E(Y2)

Not necessarily, the blindest application of the definition yields $$E(Y_1-Y_2)=\iint_{\mathbb R^2}(u-v)f_{(Y_1,Y_2)}(u,v)\mathrm du\mathrm dv.$$ At this point, it might help to write down $f_{(Y_1,Y_2)}$ correctly, that is, avoiding cases and conditions on the side, as a bona fide function defined on the whole space $\mathbb R^2$. 
In the present case, for every $(u,v)$ in $\mathbb R^2$, $$f_{Y_1,Y_2}(u,v)=\frac1u\,\mathbf 1_{0\lt v\lt u\lt1},$$ hence  $$E(Y_1-Y_2)=\iint(u-v)\frac1u\,\mathbf 1_{0\lt v\lt u\lt1}\mathrm du\mathrm dv=\int_0^1\left(\int_0^u(u-v)\,\frac1u\,\mathrm dv\right)\mathrm du.$$ And now, surely you can finish this... (The answer $E(Y_1-Y_2)=\frac14$ is correct.)
A: As pointed out by Did, you do not have to find the marginals to find $E(Y_1)$ and $E(Y_2)$. 
But we show how to  find the (marginal) distribution functions. We do the marginal distribution of $Y_2$, since in my experience students would find it a little harder. 
We want to "integrate out" $y_1$, so we want to find
$$\int_{-\infty}^\infty f(y_1,y_2)\,dy_1.$$
Now we must take into account of the fact that $f(y_1,y_2)$ is defined by different formulas on different parts of the world. The density function is $\frac{1}{y_1}$ if $0\le y_2\le y_1\le 1$ and $0$ elsewhere. 
Draw a picture, placing the $y_1$-axis is the position usually occupied by the $x$-axis, and the $y_2$-axis in the usual position of the $y$-axis.
Then our joint density function "lives" on the triangle $T$ with corners $(0,0)$, $(1,0)$, and $(1,1)$. This is the part of the unit square that is below the line $y_2=y_1$.
So when we integrate out $y_1$, the variable $y_1$ travels from $y_1=y_2$ to $y_1=1$. The (marginal) density of $Y_2$ is therefore 
$$\int_{y_1=y_2}^{y_1=1} \frac{1}{y_1}\,dy_1.$$
Integrate. We get that the density function of $Y_2$ is $-\ln y_2$ for $0\lt y_2\lt 1$ and $0$ elsewhere. 
Remark: Now we can find $E(Y_2)$ in the usual way. It can be done by integration by parts.
But this is not a good way of finding $E(Y_2)$. For if instead we find 
$$\int_T y_2 \frac{1}{y_1}dA,$$
we have our choice as to whether to integrate first with respect to $y_1$ or with respect to $y_2$. If we choose to integrate first with respect to  $y_2$, the result partly cancels the $\frac{1}{y_1}$, which makes life definitely easier. 
A: While @Did's answer is, as usual, perfectly correct and the most natural way of
solving the problem, an alternative approach can be used for this particular
density.
Let $Y_1$ be uniformly distributed on $[0,1]$ and let the conditional
distribution of $Y_2$ given that $Y_1 = u$ be a uniform distribution on
$[0, u]$.  Then,
$$f_{Y_1,Y_2}(u,v) = f_{Y_2\mid Y_1}(v \mid u)f_{Y_1}(u) 
= \left[\frac{1}{u}\mathbf 1_{0 \leq v\leq u}\right]\mathbf 1_{0 \leq u\leq 1}
= \frac{1}{u}\mathbf 1_{0 \leq v\leq u \leq 1}$$
which is the given density. Consequently, since the conditional mean of $Y_2$ given $Y_1 = u$ is just $\frac 12 u$ and the (unconditional) mean of
$Y_1$ is $\frac 12$,  we have that
$$E[Y_1-Y_2] = E\left[E[Y_1-Y_2\mid Y_1]\right]
= E\left[Y_1-\frac 12 Y_1\right]
= E\left[\frac 12 Y_1\right] = \frac 14.$$
Of course, this approach depends on knowing that this particular density
has the characterization shown above, and I freely confess that I did not 
dream up this characterization ab initio. It is a favorite (and
familiar) example
that begins with breaking a unit-length stick into two pieces
at point $Y_1$ from the left end where $Y_1$ is uniformly distributed
on the stick, and then breaking the left-hand piece at a
$Y_2$ where $Y_2$ is uniformly distributed on the length of the
left-hand piece.
