# calculate $\operatorname{cov}(X,Y)$ from $f_{x,y}(x,y)$

I have the following density function: $$f_{x, y}(x, y) = \begin{cases}2 & 0\leq x\leq y \leq 1\\ 0 & \text{otherwise}\end{cases}$$

We know that $\operatorname{cov}(X,Y) = E[(Y - EY)(X - EX)]$, therefore we need to calculate E[X] and E[Y].

$$f_x(x)=\int_x^1 2\,\mathrm dy = \big[2y\big]_x^1 = 2-x, \forall x\in[0, 1]$$

$$E[X] = \int_0^1 x (2-x)\,\mathrm dx = \int_0^1 2x - x^2\,\mathrm dx= \left[\frac{2x^2}{2}-\frac{x^3}{3}\right]_0^1 = 1 - \frac{1}{3} = \frac23$$

$$f_y(y) = \int_0^y\,\mathrm dx = \big[2x\big]_0^y = 2y, \forall y\in [0, 1]$$

$$E[Y] = \int_0^1 y\cdot2y\,\mathrm dy= \int_0^1 2y^2\,\mathrm dy= \left[\frac{2y^3}{3}\right]_0^1 = \frac23$$

However, the provided solution states that $E[X]=\dfrac13$. Have I done a mistake or is the solution wrong?

The continuation of the solution is:

$$\mathrm{cov}(X,Y) = \int_0^1\int_x^1(x-\frac 13)(y- \frac 23) \times 2\,\mathrm dy\,\mathrm dx$$

Where does the $\underline{2\,\mathrm dy\,\mathrm dx}$ come from?

However, the provided solution states that $E[X]=1/3$. Have I done a mistake or is the solution wrong?

Yes. $f_X(x)=\text{correct stuff}={[2y]}_{y=x}^{y=1}\mathbf 1_{x\in(0;1)}=(2-\color{crimson}2x)\mathbf 1_{x\in(0;1)}$ $$\mathsf E(X)=\int_0^1 x(2-\color{crimson}2x)\mathsf d x = \tfrac 13$$

Where does the $\underline{2\,\mathrm dy\,\mathrm dx}$ come from?

It is from the joint probability density function. $$\mathsf {Cov}(X,Y)~{=~\iint_{\Bbb R^2} (x-\mathsf E(X))~(y-\mathsf E(Y))~f_{X,Y}(x,y)~\mathsf d(x,y)\\=\int_0^1\int_x^1 (x-\tfrac 13)~(y-\tfrac 23)~2~\mathsf dy~\mathsf d x\\=\tfrac 1{36}}$$

In general

$$\mathbb{E}[g(X,Y)] = \iint_{\mathbb{R}^2}{\rm d}x{\rm d}y~ g(x,y)\color{blue}{f_{X,Y}(x,y)} = \color{blue}{2}\int_0^1 {\rm d}x\int_x^1{\rm d}y~ g(x,y)$$

so that

\begin{eqnarray} \mathbb{E}[X] = 2\int_0^1 {\rm d}x\int_x^1{\rm d}y~ x = 2\int_0^1{\rm d}x~x(1-x) = \frac{1}{3} \end{eqnarray}

and

\begin{eqnarray} \mathbb{E}[Y] = 2\int_0^1 {\rm d}x\int_x^1{\rm d}y~ y = 2\int_0^1{\rm d}x~\frac{1}{2}(1-x^2) = \frac{2}{3} \end{eqnarray}

The covariance is then

\begin{eqnarray} {\rm Cov}[X,Y] &=& \mathbb{E}[(X-1/3)(Y-2/3)] = 2\int_0^1 {\rm d}x\int_x^1{\rm d}y~ (x-1/3)(y-2/3) = \frac{1}{36} \end{eqnarray}

• Thanks for the detailed answer. what does the g(x,y) stand for ? So, my calculation of E[X] and E[Y] are not correct? – user1607 May 22 '18 at 7:17
• @user1607 $g(x,y)$ is any function of the variables, it could be $x$, $y$, $(x-1/3)(y-2/3)$, $x^2$, ... The problem in your calculation is that you're violating Fubini's Theorem when you integrate using the marginal distributions, in my humble opinion it is easier to write the double integral, and then evaluate it – caverac May 22 '18 at 7:42
• @caverac No, the problem in the calculation was just that $f_X(x)=(2-\color{crimson}2x)\mathbf 1_{x\in(0;1)}$ . – Graham Kemp May 22 '18 at 8:06
• @GrahamKemp You're right, thanks for pointing that out – caverac May 22 '18 at 8:10