# Covariance of two random variables

I am trying to find the covariance of two random variables but I am not having any lucky. Just for simplicity lets say that my random variables are :

X = value rolled by a die

Y = 1 if even 0 if odd

Just from intuiting $E(Y) = .5$ and $E(X) = 3.5$

Now how do I find the covariance? I can't find an explanation of how this works anywhere. I see that the formula is

$cov(XY) = E(XY) - E(X)E(Y)$

but no one anywhere thought it would be a good idea and explain what E(XY) is or how to compute it.

Anyways I found some hints and they said to multiply each pair by each other, but then what do I do with them? Add them? I wasn't sure I tried everything I could and never got the correct answer.

Going with what I think is the most correct thing to do I find the E(XY) by taking the value at each point and multiplying it by the probability of each at each point and then summing them all up.

So for $E(X_1, Y_1)$ I get an X value of 1 with probability $1/6$ and a Y value of 0 with probability $1/6$ So my value for that pair is $1/6$ From here would I then do 2, 3, 4, 5, 6 and sum up each one to determine the cross product expected value?

I have tried this a dozen times and it results in not the correct answer for my other problems. Is this simple example correct? Where did I go wrong?

First of all, a bit of intuition. The covariance of two random variables is a statistic that tells you how "correlated" two random variables are. If two random variables are independent, then their covariance is zero. If their covariance is nonzero, then the value gives you an indication of "how dependent they are".

I think you might find some use in the general formula for calculating expectations. For any random variable $X$ taking discrete values in $\mathbb{N}$, and any nonnegative measurable function $f$, you have

$$\mathbb{E}[f(X)]=\sum_{k\in\mathbb{N}}f(k)\mathbb{P}(X=k).$$

In your specific example, $Z=XY$ is a new random variable, and not a couple $(X,Y)$ as you seem to write in your example.

So, $Z$ is a random variable. What values does it take? Let us look at all the possibilities:

$$\mathbb{P}(Z=1)=\mathbb{P}(X=1,Y=1)=0,$$

because if $X=1$ then $Y=0$. Similarly,

$$\mathbb{P}(Z=2)=\mathbb{P}(X=2,Y=1)=\mathbb{P}(X=2)=\frac16,$$ because if $X=2$ then $Y=1$. Continuing the process, you find

$$\mathbb{P}(Z=3)=\mathbb{P}(X=5)=0,\text{ and }\mathbb{P}(Z=4)=\mathbb{P}(Z=6)=\frac16.$$

It remains to note that $Z$ can also take the value 0, and this happens whenever $Y=0$:

$$\mathbb{P}(Z=0)=\mathbb{P}(X=1)+\mathbb{P}(X=3)+\mathbb{P}(X=5)=\frac16+\frac16+\frac16=\frac12.$$

Once you know all the probabilities characterizing the distribution of $Z$, you can compute its expectation according to the previous formula:

$$\mathbb{E}[Z]=2\cdot\frac16+4\cdot\frac16+6\cdot\frac16+0\cdot\frac12=\frac{12}6=2.$$

• What happen if I get a negative covariance? I have been trying this problem all day and I can't get it. I did it just like you did. – Paul the Pirate May 9 '14 at 0:00
• The covariance can be negative, nothing says that it must be nonnegative. However, in this case $\mathrm{Cov}(X,Y)=2-\frac12\frac72=\frac14$. – Ian May 9 '14 at 0:13

The key point in computing the covariance is to determine whether a pair of random variables are independent of each other. If they are, the covariance would be zero. In your example, there are NOT independent events. The six possible values of $(X,Y)$ are: $$\left\{(1,1), (2,0),(3,1), (4,0),(5,1),(6,0)\right\}\ .$$ Each event occurs with probability $1/6$. Thus, $E(XY)=(0+2+0+4+6+0)/6=2$. It is easy to show that $E(X)=7/2$ and $E(Y)=1/2$. Thus $Cov(XY)=1/4$.

Edit: Corrected my formula. The earlier solution was for the random variable $Y$ being 1 for odd numbers (instead of 1 for even). Sorry, I misread the question.

• Is your method for expected variable correct? It does not seem so. – Paul the Pirate May 9 '14 at 0:05
• What is wrong? Be more explicit. – suresh May 9 '14 at 0:10
• You simply take an addition of everything and then divide by 6, but what if the probability of one of the events was not 1/6? You would have to do each individually correct? – Paul the Pirate May 9 '14 at 0:10
• There are six possible events when you toss a die and they occur with equal probability. Each outcome simultaneously determines both $X$ and $Y$. So it is $1/6$ and I don't agree with you. – suresh May 9 '14 at 0:17