in this lesson we're going to talk about how to calculate the co-variance between two variables so we have the values of X and the corresponding values of Y the equation that you need in order to calculate the co-variance is this equation the co-variance between the variables X and Y is the sum of the products of the differences in the X values with the mean of X or the average x value times differences of all the Y values with the average yvalue and this is going to be divided by now when calculating the sample covariance it's n minus one when calculating the population variance it's simply n but for this video we're going to calculate the sample covariance so that's the formula that you need in order to calculate the co-variance between X and Y now let's talk about how to use this formula with the use of a table so in the First Column I'm going to put all of the X values in the second column I'm going to place the Y values and then the difference between the X values and the mean here I'm going to have the difference between the Y values and the mean and then in the last column I'm going to put the products of the difference between the X values in the mean and the Y values and the corresponding mean so first let's write out the X values we have 2 4 6 8 and 10 now for the Y values it's 3 7 10 14 and 17 now before we can move on to the next column we need to calculate xar and Y Bar so let's sum up the X values 2 + 4 + 6 + 8 + 10 so the sum of all the X values is 30 now let's do the same thing for the next column the sum of all the Y values is 51 now to calculate xar it's going to be the sum of all of the X values / n so here's the sum of the X values and we can see that n is five because we have five x values and five y values so xar the average of all the X values is six now let's calculate Y Bar so let's take the sum of all the Y values and divide by n so the sum of the Y values is 51 n is equal to 5 51 / 5 is 10.2 so let's save these values now for the next column we're going to subtract every x value by xar so this is going to be 2 - 6 which is -4 and then 4 - 6 which is -2 and 6 - 6 that's zero and then 8 - 6 is two and then finally 10 - 6 is 4 now there's no need for us to take the sum of this column because if we add it we're just going to get zero and that value we're not going to use so we don't need to worry about that now let's do the same thing for y so let's take Y and subtract it by Y Bar so 3 - 10.2 is -7.2 and then 7 - 10 10.2 that's going to be 3.2 10 - 10.2 is.2 now 14 - 10.2 that's going to be 3.8 but it's going to be a positive number and 17 - 10.2 is 6.8 if you were to take the sum of this column you're going to get zero just like if you take the sum of these numbers you'll get zero as well now for the next part we need to take the product of these two columns so -4 * -7.2 that's going to give us a positive number so postive 28.8 and then -2 * -3.2 that's going to be postive 6.4 0 * .2 is z and then 2 * 3.8 is 7.6 and 4 * 6.8 is 27.2 next we need to take the sum of that column so 28.8 + 6.4 + 7.6 + 27.2 we get a total value of 70 all right so at this point I'm going to get rid of this part part of the table just to make some space so the co variance is going to be the sum of x - xar * y - Y Bar over nus1 this part here the sum of the products of the differences of X and xar time Y and Y Bar that's this number over here that's 70 because we summed each individual iteration of this product so we already have the numerator for that equation n is 5 so this becomes 70 ID 4 70 ID 4 is 17.5 so this is the co-variance between X and Y for this problem it's positive 17.5 so make a note of that and we're going to talk about what that number represents later in this video now let's work on another example problem feel free to pause the video if you want to try it yourself so our X values is going to be 3 6 9 12 15 and for y let's say it's 20 17 13 9 and four so go ahead and take a minute to work on this example problem so let's begin with a table so first we have X and then then Y and then x - xar and then y - Y Bar and then the product of x - xar * y - Y Bar so the X values that we have are 3 6 9 12 and 15 for y it's 20 17 and then 13 9 and 4 so let's take the sum of the first column 3 + 6 + 9 + 12 + 15 is 45 and then let's do the same for the Y values 20 + 17 + 13 + 9 + 4 is 63 now let's calculate xar so X part is going to be the sum of all the X values divided by n so the sum of the X values is 45 and we have five data points so we're going to get nine for the value of xar now let's calculate Y Bar it's going to be the sum of all of the Y values divid by n so that's 63 / 5 63 ID 5 is 12.6 so now that we have that let's go ahead and calculate x - xar so 3 - 9 is-6 6 and then 6 - 9 is -3 9 - 9 is 0 12 - 9 is 3 and then 15 - 9 is 6 if we take the sum of this column we're going to get zero now moving on to next column we have y - Y Bar so 20 - 12.6 that's going to be 7 .4 and then 17 - 12.6 is 4.4 13 - 12.6 is positive4 9 - 12.6 is -3.6 and 4 - 12.6 is 8.6 if you add up this column you're going to get zero as well now for the next one we need to take the product of the third and the fourth column to get the values for the fifth column so x - xar that's -6 * y - Y Bar that's 7.4 so -6 * 7.4 is 44.4 next we'll multiply -3 by 4.4 which is 13.2 0 * point4 is 0 3 * -3.6 is -1.8 and then 6 * 8.6 is 51.6 now the next thing we need to do is take the sum of this column so if we add those five numbers we're going to get -20 so now we can calculate the co-variance between the variables X and Y so it's going to be the sum of the product of x - xar and Y - Y Bar / n minus one so this part the sum of the products of those differences we already have I'm going to highlight that in red it's -20 n is 5 in this problem so this becomes -10 / 4 which is -30 so that's the answer for this problem so notice that the co-variance is negative for that situation now what I'm going to do is I'm going to create a graph I'm actually going to create two graphs we're going to place the X variables on the horizontal axis and the Y variables on the vertical axis now for the first problem that we had I'm going to plot these points so for the X values it was 2 4 6 8 10 and then for the Y values it was 3 7 10 14 and 17 this is not drawn a scale but we're going to make the best of this so when X is 2 Y is 3 when X is 4 Y is about 7even when X is 6 Y is 10 x is 8 Y is going to be 14 when X is 10 Y is 17 so it's not a perfectly straight line but we could clearly see a linear a linear relationship now I'm going to plot the second data set that we had so for the X values it was three 6 9 12 and 15 now for the Y values we had 4 9 13 17 and 20 when X is 3 Y is 20 when X is 6 it's 17 when X is 9 it's 13 and when X is 12 it's 9 and then we have the 154 so we could see that for the second data set there was a negative uh relationship so for the first problem we calculate the co-variance between X and Y to be positive 17.5 and for the second problem the co-variance between these two variables was calculated to be negative 30 so notice that as X increases y increases for the first graph and the covariance is positive so if you have a linear relationship with a positive slope the covariance is going to be above zero it's going to be positive if the slope is negative the co-variance will be less than zero so for the second one as X increases y decreases so we can see the negative relationship there so that's what you can learn from co-variance if the co-variance is positive well that describes the positive relationship between X and Y as one variable increases the other increases if the covariance is negative or less than zero it shows a negative relationship between the two when one goes up the other goes down if the covariance is equal to zero then there's no relationship between X and Y