Covariance measures whether two variables tend to move in the same direction. For each pair of values the calculator multiplies the deviation of x from its mean by the deviation of y from its mean. When both are above or both are below their means the product is positive; when one is above and the other below it is negative. The average of these products is the covariance.
A positive covariance means the variables rise together, a negative one means one tends to fall as the other rises, and a value near zero means there is no linear tendency either way. Investors use covariance between asset returns to build diversified portfolios: assets with low or negative covariance smooth each other’s swings. Covariance is also the building block of regression slopes, principal component analysis and the variance of a sum, Var(X + Y) = Var(X) + Var(Y) + 2Cov(X, Y).
The size of a covariance depends on the units – measuring heights in centimetres instead of metres multiplies it by 100 – so it is hard to read on its own. Dividing it by both standard deviations gives the unit-free correlation coefficient r, which the calculator shows too.
How to use the Covariance Calculator
- Data set X
- Data set Y (same order as X)
Results update as you type, and everything is calculated in your browser.
How it works
Enter the two data sets in the same order. The calculator finds both means, multiplies the paired deviations, adds them up and divides by n − 1 for a sample or by N for a whole population.
Worked example
Take X = 2.1, 2.5, 3.6, 4.0 and Y = 8, 10, 12, 14. The means are 3.05 and 11. The deviations of X are −0.95, −0.55, 0.55 and 0.95, and of Y −3, −1, 1 and 3, so the cross-products are 2.85, 0.55, 0.55 and 2.85, adding to 6.8. The sample covariance is 6.8 ÷ 3 = 2.2667 and the population covariance 6.8 ÷ 4 = 1.7. With sx = 0.8963 and sy = 2.582, the correlation is 2.2667 ÷ (0.8963 × 2.582) = 0.9795 – a very strong positive relationship.
Covariance and portfolio risk
For two assets held in proportions w₁ and w₂, the variance of the portfolio return is w₁²σ₁² + w₂²σ₂² + 2w₁w₂·Cov(R₁, R₂). A negative covariance makes the last term negative, which is why mixing assets that do not move together lowers risk without necessarily lowering the expected return.
Inputs explained
| Field | What to enter |
|---|---|
| Data set X | A list of numbers separated by commas, spaces or new lines |
| Data set Y (same order as X) | A list of numbers separated by commas, spaces or new lines |
What each result means
- Sample covariance = cov([Data set X], [Data set Y (same order as X)])
- Population covariance = pcov([Data set X], [Data set Y (same order as X)])
- Sum of cross-products Σ(x − x̄)(y − ȳ) = cov([Data set X], [Data set Y (same order as X)])×(count([Data set X]) – 1)
- Mean of X = mean([Data set X])
- Mean of Y = mean([Data set Y (same order as X)])
- Standard deviation of X (sample) = stdev([Data set X])
- Standard deviation of Y (sample) = stdev([Data set Y (same order as X)])
- Correlation coefficient r = corr([Data set X], [Data set Y (same order as X)])
- Number of pairs = count([Data set X])
Formula
Sample cov(X, Y) = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ (n − 1) · Population cov = Σ(xᵢ − μx)(yᵢ − μy) ÷ N · r = cov ÷ (sx · sy)
Worked example
Inputs — Data set X: 2.1, 2.5, 3.6, 4.0; Data set Y (same order as X): 8, 10, 12, 14.
Results — Sample covariance = 2.266667; Population covariance = 1.7; Sum of cross-products Σ(x − x̄)(y − ȳ) = 6.8; Mean of X = 3.05; Mean of Y = 11; Standard deviation of X (sample) = 0.896289; Standard deviation of Y (sample) = 2.581989; Correlation coefficient r = 0.979457; Direction = Positive – X and Y tend to rise together; Number of pairs = 4.
Key terms
- Covariance
- The average product of paired deviations from the means.
- Cross-product
- (xᵢ − x̄)(yᵢ − ȳ) for one pair of observations.
- Covariance matrix
- A table of the variances (diagonal) and covariances (off-diagonal) of several variables.
Frequently asked questions
What does a positive covariance mean?
That above-average values of X tend to go with above-average values of Y. Its size alone does not say how strong the link is – use the correlation coefficient for that.
What is the difference between sample and population covariance?
The sample version divides by n − 1 and is an unbiased estimate when your data are a sample; the population version divides by N. With 4 pairs and Σ = 6.8 they are 2.267 and 1.7.
Can covariance be negative?
Yes. Negative covariance means the variables move in opposite directions, like the price of a good and the quantity bought.
How is covariance related to variance?
The covariance of a variable with itself is its variance: Cov(X, X) = Var(X).
How is covariance related to correlation?
r = Cov(X, Y) ÷ (sx · sy). Correlation is covariance rescaled to lie between −1 and +1.
Why do the data sets need the same length?
Covariance pairs each x with one y, so every observation must have both values, in the same order.
How is covariance used in finance?
The covariance of two stocks' returns enters the portfolio variance; combining assets with low covariance lowers the overall risk.