Omni Calculator logo

If you have two samples and you want to determine if they vary in a similar way, this covariance calculator is the tool you need. Here, you will learn how the covariance formula works, how to calculate covariance, and understand the covariance vs correlation relationship.

First, to answer the question: What is covariance?

What is covariance?

If X and Y are two random variables with expected values, E(X)E(X) and E(Y)E(Y) respectively, their covariance is:

Cov(X,Y)=E([XE(X)][YE(Y)])\mathrm{Cov}(X,Y) \\ =E\left([X-E(X)][Y-E(Y)]\right)

This covariance formula can further be simplified to:

Cov(X,Y)=E(XY)E(X)E(Y)\mathrm{Cov}(X,Y) \\ =E(XY) - E(X)E(Y)

The above formula is not practical in real-life situations, as it relies on us knowing the expected values for X, Y, and XY. The only way to discover these numbers is by knowing how the random variables X, Y, and X*Y are distributed. But you usually don't know that. On the contrary, this is something you want to discover!

Instead, we use sample observations of x and y over a finite size, n. We will see that this is actually enough to perform one of the following two tasks:

  1. Find the covariance of X and Y when we have access to the whole population data.

  2. Find an estimate of the population covariance for X and Y when we only have access to a sample.

Covariance formula explained

So, each of the two samples, x and y, consists of n randomly observed values, X and Y, respectively. The elements of the first sample are denoted by x1,x2,...,xnx_1, x_2, ..., x_n, and their average by xmeanx_{\text{mean}}. Similarly, the elements of the second sample are y1,y2,...,yny_1, y_2, ..., y_n, with an average of ymeany_{\text{mean}}.

The following formula is the population covariance formula for two equally sized samples:

Covpop(x,y)=i=1n(xixmean)(yiymean)n\mathrm{Cov_{pop}}(x,y) \\ = \frac{\displaystyle\sum_{i=1}^n(x_i - x_{\text{mean}})(y_i - y_\text{mean})}{n}

The name here comes from the fact that we regard our two samples are all there is, i.e., they constitute our populations. We do not care here what happens outside of these samples.

The usefulness of the covariance formula is not immediately clear. Let's break it down to parts to understand it better.

Remember that n represents the sample size of each of the two samples. For each i=1,2,...,ni = 1, 2, ..., n, the terms xixmeanx_i - x_{\text{mean}} and yiymeany_i - y_{\text{mean}} calculate the differences between the sample elements and the sample average.

In the covariance formula, these two terms are multiplied, then summed over all sample elements, and finally averaged by dividing by nn, the size of each of the samples. Phew!

Don't be scared, though! The idea behind the covariance formula is actually pretty simple: we want to measure how much the data from the two samples vary together.

Don't worry, we've prepared an example to make things much easier to understand.

Example on how to calculate covariance

We will now dig a bit deeper into the calculation's details by looking at how the covariance formula works in a real-life example.

John is an investor who just bought his first few stocks in "Cool Places", a company specialising in polar vacations. But, as every smart investor knows, John should diversify his portfolio, so he decided to buy some stocks in either "Star Dust" and "Time Travel Vacations", two more travel agencies. But he can't decide which stocks to buy. What should he do?

Well, the covariance formula may have the answer! John randomly selects five daily closing prices, not necessarily in chronological order, for the stocks of "Cool Places" and "Star Dust", denoted by xix_i and yiy_i, respectively:

i

xi

yi

xdiff

ydiff

xdiff * ydiff

1

12.76

7.06

0.094

0.044

0.004

2

12.35

6.81

-0.316

-0.206

0.065

3

12.43

6.88

-0.236

-0.136

0.032

4

12.70

6.98

0.034

-0.036

-0.001

5

13.09

7.35

0.424

0.334

0.142

mean

12.666

7.016

Here is a step-by-step calculation:

  1. Calculate the average of the "Cool Places" stock closing prices by adding up the values in the second column and by dividing the sum by the sample size of n=5n = 5. Do the same for "Star Dust" stock prices in the third column:

xmean=12.666x_{\text{mean}} = 12.666 and ymean=7.016y_{\text{mean}} = 7.016.

  1. Complete the fourth and fifth columns by subtracting the corresponding averages xmeanx_{\text{mean}} and ymeany_{\text{mean}} from the stock prices listed in the rows: xdiff=xixmeanx_{\text{diff}} = x_i - x_{\text{mean}} and ydiff=yiymeany_{\text{diff}} = y_i - y_{\text{mean}}.

  2. Fill out the sixth column by simply multiplying the corresponding numbers from the fourth and fifth columns.

  3. Finally, when you sum up all the numbers in the sixth column and divide the sum by the sample size, in this case, n=5n = 5, you get the sample covariance

Covsample(x,y)=0.0483\mathrm{Cov_{sample}}(x,y) = 0.0483!

Check this result by using the covariance calculator and read on to find out how to interpret this number.

How to interpret the covariance?

Note that the covariance value does not have a particular significance by itself, although there are some essential comments we can still make.

If the covariance is positive, the observation of the two samples tends to exhibit similar behavior with respect to their averages. Either they are both higher than their corresponding means, or they are both lower. So, the way they vary from their averages is similar.

In the case of negative covariance, the samples behave in the generally opposite manners. When one observation is lower than its sample's average, the corresponding observation of the other sample is higher than its average, and vice versa. But, we can say the samples are still related, although in a different way than in the case of the positive covariance.

Also, the further the covariance is from zero, the more related the samples are, while a covariance value closer to zero suggests there isn't a strong relationship between sample variations:

Covariance values
Covariance values - negative, zero, positive. Source: GeeksforGeeks

What does this mean for John's diversification strategy? He will probably be better off if he buys stocks whose prices have a covariance close to zero when compared to the stocks he already has. Why? Because then he'll know that the second stock is less likely to vary simultaneously with the first one.

However, to make the right decision, he still needs to calculate the covariance of the observed closing prices for the stocks of "Cool Places" and "Time Travel Vacations".

Use the sample covariance calculator on the following data, where xix_i still represents "Cool Places", but this time yiy_i is the "Time Travel Vacations" closing prices:

i

xi

yi

1

12.76

17.12

2

12.35

16.35

3

12.43

19.21

4

12.70

19.75

5

13.09

17.52

Now we get a sample covariance equal to 0.00180.0018, which is a much lower covariance than the one for "Cool Places" and "Star Dust". John concludes that he should buy the "Time Travel Vacations" stocks in order to diversify his portfolio effectively.

How to estimate the population covariance from samples?

In practical situations, the sample to which we have access represents a larger population. Thankfully, even from limited samples, we can estimate the covariance in the whole population via the following formula:

Covsample(X,Y)=(xixmean)(yiymean)n1\mathrm{Cov_{sample}}(X,Y) \\ =\frac{\sum (x_i - x_{\text{mean}}) (y_i - y_{\text{mean}})}{n-1}

The name here comes from the fact that we regard our two samples are parts of larger populations. Based on the samples we want to describe what happens in the bigger world.

You can see that the denominator here is n-1, which will give a somewhat higher result than the original covariance formula. We intuitively do expect this to happen, as limited samples generally don't reflect the total variability between entire populations and it can be shown rigorously that the denominator n-1 is the right corrective factor.

The relationship between sample and population covariances is given by the formula:

Covsample(X,Y)=nn1Covpop(x,y)\mathrm{Cov_{sample}}(X,Y) \\ = \frac{n}{n-1} \mathrm{Cov_{pop}}(x,y)

But, bear in mind that, as the size of the samples gets bigger, the difference between n and n-1 gets smaller. So the original sample covariance formula and population covariance estimate give similar results for large samples.

You can check out what would be the results for John's stocks if we have, instead of the original formula, used the estimation formula for sample covariance.

Variance and correlation vs covariance

Covariance is a measure of variability between two random variables, X and Y, while variance measures how much a particular random variable varies by itself. If you'd like to learn more details, visit our variance calculator.

Here is the relationship between these two quantities:

Var(X)=Cov(X,X)\mathrm{Var}(X) = \mathrm{Cov}(X,X)

So, the variance of X is precisely the covariance between X and itself!

Another way to express the variability between two random variables is by the correlation between random variables X and Y. The relation between correlation and covariance is:

Corr(X,Y)=Cov(X,Y)σXσY\mathrm{Corr}(X,Y) = \frac{\mathrm{Cov}(X,Y)}{\sigma_X \sigma_Y}

where σX\sigma_X and σY\sigma_Y are the standard deviations of the random variables X and Y.

You can think of correlation as the normalized version of covariance: the above formula guarantees that correlation must be somewhere between the numbers -1 and 1. This property is the reason why correlation is more often and more readily used than covariance, although they do similar jobs.

Remember that in the example with John, we had the problem of interpreting how large or small the covariance is? Well, with correlation, you wouldn't have that issue.

It may be useful to note that there are various types of correlation. If you're interested in them, check out our dedicated calculators:

References

What is an intuitive explanation of covariance?

Calculating Covariance for Stocks

Baffled by Covariance and Correlation???

Beginners Guide to Correlation

A Gentle Introduction to Expected Value, Variance, and Covariance with NumPy

Miroslav Jerkovic, PhD
FIRST SAMPLE (enter up to 30 values)
Observation 1
Observation 2
Observation 3
SECOND SAMPLE (enter up to 30 values)
Observation 1
Observation 2
Observation 3
Check out 44 similar descriptive statistics calculators 📊
5 number summary5★ rating averageCoefficient of variation… 41 more
People also viewed…

BMR - Harris-Benedict equation

Harris-Benedict calculator uses one of the three most popular BMR formulas. Knowing your BMR (basal metabolic weight) may help you make important decisions about your diet and lifestyle.

Helium balloons

Wondering how many helium balloons it would take to lift you up in the air? Try this helium balloons calculator! 🎈

Inverse normal distribution

The inverse normal distribution calculator finds the x-value and Z-score if you input the area, mean, and standard deviation of the normal distribution.

Polynomial regression

The polynomial regression calculator can help you find a polynomial curve that best fits your data set.
Copyright by Omni Calculator sp. z o.o.
Privacy, Cookies & Terms of Service