How To Calculate Covariance in 6 Steps (With Examples)
Updated October 16, 2023
Determining which stocks to add to your portfolio is an important decision. Covariance is one measurement you can use to help you analyze risk. Knowing how to calculate covariance can give you insights into the relationship between two stocks.
In this article, we discuss what covariance is, how it differs from variance, how to calculate it in six steps, its applications and an example of the calculation.
Related: How To Calculate Safety Stock (With Examples)
What is covariance?
Covariance is a measurement used in statistics to determine if two variables are changing in the same direction. It is a measurement of the difference between the two variables, and the two variables used to determine covariance are unrelated.
You can measure covariance in terms of units related to the two variables in the data sets. For example, in finance, two data sets could be the cost of one company's stock, while the other could be an unrelated company's stock. Since they represent both values in terms of dollars, the units for the measurement would be dollars.
Covariance compares the two variables in terms of positive and negative. If the value for covariance is negative, then, the two variables move in opposite directions. If the value for covariance is positive, then the two variables move in the same direction.
Notably, this means that two variables could decrease in the same direction, and the covariance would still return as a positive value. For example, if two companies both have stocks that are becoming cheaper over time, then their covariance would be positive.
Related: How To Calculate Sample Covariance
Covariance versus variance
Variance is a measurement of the distance between a variable and the average value of a set of data. Unlike covariance, one data point or trend is the average, while the other is a point or trend of interest that you decide to measure.
Using the above example, if the first company has stock that is growing over time, but the general trend for all stock is falling, then, the variance between the average and the company's stock may increase. If the second company's stock is also increasing at a similar rate to the first, then the covariance would be positive.
Related: What Is Variance?
How to calculate covariance
To calculate covariance, you can use the formula:
Cov(X, Y) = Σ(Xi-µ)(Yj-v) / n
Where the parts of the equation are:
Cov(X, Y) represents the covariance of variables X and Y.
Σ represents the sum of other parts of the formula.
(Xi) represents all values of the X-variable.
µ represents the average value of the X-variable.
Yj represents all values of the Y-variable.
v represents the average value of the Y-variable.
Σ represents the sum of the values for both (Xi-µ) and (Yj-v).
n represents the total number of data points across both variables.
You can use the following steps and the covariance formula to find the covariance of your data:
1. Get the data
The first step in finding the covariance of two variables is to gather the data for both sets. For example, the table below shows the values of two new company stocks between 2015 and 2020:
Year | Company X Stock Value ($) | Company Y Stock Value ($) |
---|---|---|
2015 | 1,245 | 100 |
2016 | 1,415 | 123 |
2017 | 1,312 | 129 |
2018 | 1,427 | 143 |
2019 | 1,510 | 150 |
2020 | 1,590 | 197 |
2. Calculate the average value for each variable
To find the average value for each stock, add all the X-values together and divide by the total number of X-values. Then, do the same for the Y-values:
µ = 1,245 + 1,415 + 1,312 + 1,427 + 1,510 + 1,590 / 6
µ = 1,416.5
v = 100 + 123 + 129 + 143 + 150 + 197 / 6
v = 140.3
3. Find the difference between each value and the mean for both variables
Subtract the mean value for each set of variables from each variable within that set. For example:
Year | Company X (Xi-µ) | Company Y (Yj-v) |
---|---|---|
2015 | 1,245 - 1,416.5 = -171.5 | 100 - 140.3 = -40.3 |
2016 | 1,415 - 1,416.5 = -1.5 | 123 - 140.3 = -17.3 |
2017 | 1,312 - 1,416.5 = -104.5 | 129 - 140.2 = -11.2 |
2018 | 1,427 - 1,416.5 = 10.5 | 143 - 140.3 = 2.7 |
2019 | 1,510 - 1,416.5 = 93.5 | 150 - 140.3 = 9.7 |
2020 | 1,590 - 1,416.5 = 173.5 | 197 - 140.3 = 56.7 |
4. Multiply the values for the two variables
Once you have found the values for both variables in the previous step, you can multiply them together. For example:
Year | Company X (Xi-µ) | Company Y (Yj-v) | (Xi-µ)(Yj-v) |
---|---|---|---|
2015 | 1,245 - 1,416.5 = -171.5 | 100 - 140.3 = -40.3 | (-171.5)(-40.3) = 6,911.45 |
2016 | 1,415 - 1,416.5 = -1.5 | 123 - 140.3 = -17.3 | (-1.5)(-17.3) = 25.95 |
2017 | 1,312 - 1,416.5 = -104.5 | 129 - 140.2 = -11.2 | (-104.5)(-11.3) = 1,180.85 |
2018 | 1,427 - 1,416.5 = 10.5 | 143 - 140.3 = 2.7 | (10.5)(2.7) = 28.35 |
2019 | 1,510 - 1,416.5 = 93.5 | 150 - 140.3 = 9.7 | (93.5)(9.7) = 906.95 |
2020 | 1,590 - 1,416.5 = 173.5 | 197 - 140.3 = 56.7 | (173.5)(56.7) = 9,837.45 |
5. Add the values together
After you have calculated the product of the two variables together, you can add the values to get the second to last part of the equation. For example, you can add the product values from the companies above to get the summation of all values:
6,911.45 + 25.95 + 1,180.85 + 28.35 + 906.95 + 9,837.45 = 18,891
6. Use the values from previous steps to find the covariance of the data
Once you have calculated the parts of the equation, you can put your values into it. For example, you can put the stocks of the company from above into the equation as shown below:
Cov(X, Y) = 18,891 / 6
Where the values are:
18,891 = Σ(Xi-µ)(Yj-v)
6 = n
As calculated above, the covariance of company X's stock and company Y's stock is 3,148.5. The positive nature of the covariance value shows that the two companies' stocks move in the same direction.
Applications of covariance
One application of covariance is in finance. You can use covariance to evaluate the risk of particular stocks by comparing whether they move with or against each other. For example, if the value of two stocks increases and decreases opposite one another, then they would be complementary, with minimal risk because they minimize financial loss by having one growing while the other shrinks.
You can also use covariance with correlation to determine if and how variables move together, and investors often use both to determine whether to add stocks to a portfolio. While covariance can tell you how two or more sets of the data move, correlation can tell you what other factors influence that movement and if the two variables relate to each other.
Read More: What Is Correlation? (With Definition and Examples)
Example calculation
Below is an example of how to calculate the covariance of sales of two new toys sold by the same company:
1. Find your data
First, find the data that you are interested in. For this example, it is the number of two toys sold from January to April:
Month | Toy X | Toy Y |
---|---|---|
January | 12 | 67 |
February | 13 | 45 |
March | 25 | 32 |
April | 39 | 21 |
2. Find the number of toys sold
Next, find the number of each toy sold for the months above, and you can find the average number of toys sold for each one:
µ = 12 + 13 + 25 + 39 / 4
µ = 22.25
v = 67 + 45 + 32 + 21 / 4
v = 41.25
3. Find the difference in values
Third, calculate the difference between each value of X and µ. Then calculate the difference between each value Y and v:
Month | Toy X (Xi-µ) | Toy Y (Yj-v) |
---|---|---|
January | 12 - 22.25 = -10.25 | 67 - 41.25 = 25.75 |
February | 13 - 22.25 = -9.25 | 45 - 41.25 = 3.75 |
March | 25 - 22.25 = 2.75 | 32 - 41.25 = -9.25 |
April | 39 - 22.25 = 16.75 | 21 - 41.25 = -20.25 |
4. Calculate the product
Fourth, you can calculate the product of (Xi-µ) and (Yj-v):
(-10.25)(25.75) = -263.94
(-9.25)(3.75) = -34.69
(2.75)(-9.25) = -25.44
(16.75)(-20.25) = -339.19
5. Add products together
Fifth, you can add the products of the precious calculations together to get a sum of -663.26:
Σ = (-263.94) + (-34.69) + (-25.44) + (-339.19) = -663.26
6. Substitute values
Finally, you can substitute the values into the equation from before:
Cov(X, Y) = -663.26 / 4
Cov(X, Y) = -165.82
Using this covariance, you can determine that when the number of toys sold increases for one toy, it decreases for the other. This is because the value for the covariance is negative.
Explore more articles
- How To Become a Travel Agent in 4 Steps (With Helpful Tips)
- Debt-to-Equity Ratio: Definition and Calculation Formula
- Common Communication Barriers (With Examples and Tips)
- How To Open a Word Document in Excel (With 2 Methods)
- How To Become a US Diplomat in 6 Steps (With Duties)
- 10 Ways To Demonstrate Professional Behavior at Work
- 11 Types of Economic Theories Aspiring Economists Should Know
- 10 Different Types of Stores
- Writing About Your Career Goals in a Scholarship Essay (With Examples)
- How To Format a Proper Business Letter (With Examples)
- The Top 25 Qualities of a Good Coworker
- How To Write a Letter Requesting for a Company Quotation