How To Calculate Covariance
How To Calculate Covariance
Covariance tells you how closely related two variables are. Here, we will explain what covariance is, as well as showing you step by step everything you need to know to calculate it!
Hi, I'm Dr. Shah. I was the national lecture competition winner in 1999, and I'm the maths master at Mathschool.
Now, ready for a new way of doing maths? How to calculate covariance? Covariance is used when we have bivariate data. That is, we have x's and y's, and we want to see if those two are varying together, or if they just quite randomly, they're not connected to each other. So, covariance is a measure of how much x and y vary together.
So in order to calculate covariance, first we're going to have to learn a few simpler things. First thing we need to be able to do is find the mean of x, and the variance of x. And we need to find the mean of y, and the variance of y.
So to find the mean of x, which I'm going to call x-bar, I add up all the x values. So, add them all up, and that should give me 45. Count how many there are, well I can see there's 1, 2, 3, 4, 5, 6, 7, 8, 9.
And so my mean of x, is 5. So, first thing we're going to do is find the mean of x, done that. Next thing we're going to do is find the variance of x.
Now, the variance of x is sigma x-squared over n, minus the mean squared. Well, the mean we've just worked out is 5. Sigma x-squared means I've got to square all of these values and add them up.
So 1 squared plus 2 squared plus 3 squared plus 4 squared plus 5 squared 6 squared 7 squared 8 squared 9 squared, all added up together. And you do that on the calculator, and you should get two eight five as your answer for that. N is still 9, there are still 9 values.
And so, n we're going to replace with 9. And we said before that the mean of x is 5, so 5 squared. And you work that out again on the calculator, and you get 20 over 3.
So there's my variance of x. So we now know the mean of x and the variance of x. So I'm going to put those aside, and then carry on to work out the next thing.
So just in the same way as we found the mean of x and the variance of x, we're going to do the same for the y values. So, the mean of y is found by adding up all of the y values. Again, sticking them in the calculator, you should get 72 as your answer.
Divide it by how many there are: 1, 2, 3, 4, 5, 6, 7, 8, 9, and so the mean of y is 8. And we also need the variance of y. Variance of y is found by sigma y squared over n minus y-bar squared.
So, same formula as the variance of x, except we're just using the letters y here. We know our mean of y, which we worked out before was 8. So that's 8 squared.
Our sigma y squared is going to be 5 squared and 4 squared and 5 squared and 6 squared and 8 squared and 9 squared and 10 squared and 13 squared and 12 squared all added together. Again, you do that on your calculator, and that will give you 660. And divided by n, 1, 2, 3, 4, 5, 6, 7, 8, 9 as in every case.
And so that gives you an answer of 28 over 3. And again, I'll store these numbers. So my mean of y was 8, and my variance of y was 28 over 3.
So now, to work out the covariance, we're going to add another row onto this table, which is going to be called xy, and to work that out, we're going to multiply the x values by the y values. So, 1 times 5, 5. 2 times 4, 8.
3 times 5, 15. 4 times 6, 24. 5 times 8 is 40.
6 times 9 is 54. 70, 8 times 13 is 104, and 9 times 12 is 108. Now that we've done that, we're going to work out the covariance in the same sort of way.
Covariance xy is sigma xy over n minus mean of x, mean of y. You'll notice how it's similar to the equation we used for variance of x and variance of y. Variance of x was sigma x squared, or xx, this is xy.
And here you'd have x bar times x bar, and here we've got x bar times y bar. So to notify the fact that we're not just using x, we're using x and y together this time. Our sigma xy is found by adding all of these together, so we add all of these together.
Again, sticking that into the calculator, that gives you 428. Our n is still 9, there's 9 values. Our mean of x, we worked out before, is 5.