Covariance Calculator
Calculate sample covariance for two variables to see whether they move together or in opposite directions, dividing by n-1 for an unbiased estimate.
Covariance Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser — no data is sent to any server.
Formula: Cov(X,Y) = Σ(xᵢ-x̄)(yᵢ-ȳ) / (n-1)
Worked example — Cov = 1.5 (positive, X and Y move together) | r = 0.7746
Formula
Cov(X,Y) = Σ(xᵢ-x̄)(yᵢ-ȳ) / (n-1)
Measures how two variables change together. Positive = move together, negative = move opposite.
Worked Examples
Example 1: Sample covariance step by step
Problem:X: 1, 2, 3, 4, 5 and Y: 2, 4, 5, 4, 5
Solution:Means: x-bar = 3, y-bar = 4. Deviation products (x - 3)(y - 4): (-2)(-2)=4, (-1)(0)=0, (0)(1)=0, (1)(0)=0, (2)(1)=2. Sum = 6, and n - 1 = 4, so Cov = 6/4 = 1.5. For the correlation: sum of (x-3)^2 = 10 and sum of (y-4)^2 = 6. r = 6 / sqrt(10 x 6) = 6 / 7.746 = 0.7746.
Result:Cov = 1.5 (positive, X and Y move together) | r = 0.7746
Example 2: Why the sign matters more than the size
Problem:X: 10, 20, 30 and Y: 9, 6, 3
Solution:Means: x-bar = 20, y-bar = 6. Products: (-10)(3) = -30, (0)(0) = 0, (10)(-3) = -30. Sum = -60. Cov = -60 / 2 = -30, a negative covariance: Y falls as X rises. Rescaling X to metres instead of centimetres would change -30 to -0.3 without changing the relationship at all, which is why correlation (here r = -1 exactly) is the comparable measure.
Result:Cov = -30 (perfect inverse relationship) | r = -1.0
Frequently Asked Questions
What does covariance measure?
Covariance measures the directional relationship between two variables. A positive value means both variables tend to move in the same direction, while a negative value means they move in opposite directions. The magnitude depends on the scale of the data.
How do you interpret the Pearson correlation r from covariance?
The correlation r is covariance divided by the product of standard deviations, standardising it to a range of -1 to +1. Values near ±1 indicate a strong linear relationship; values near 0 indicate little or no linear association regardless of the raw covariance magnitude.
When should I use covariance instead of correlation?
Use raw covariance when you need to retain the original units and scale — for example, in portfolio variance calculations in finance where dollar magnitudes matter. Use correlation when you need a scale-free measure to compare relationships across different datasets or variable units.
What are the key assumptions of sample covariance?
Sample covariance assumes paired observations (each xᵢ corresponds to a yᵢ), that observations are independent of each other, and that the relationship between variables is linear. Outliers can heavily distort the covariance, so checking scatter plots before interpreting results is strongly recommended.
Why is the sample covariance divided by n - 1 rather than n?
Because the two means used in the formula are themselves estimated from the same data, one degree of freedom is already spent. Dividing by n would systematically underestimate the true population covariance, so Bessel's correction replaces n with n - 1 to make the estimator unbiased. The adjustment matters most for small samples: with n = 5 it inflates the result by 25 percent, while with n = 500 the difference is negligible. Use n only when your data really is the entire population.
What units does covariance have, and why does that limit interpretation?
Covariance carries the product of the two variables' units, so height in centimetres against weight in kilograms yields a value in centimetre-kilograms. Switching one variable from metres to centimetres multiplies the covariance by 100 without changing the underlying relationship at all. That is why the raw number cannot be compared between datasets and why the correlation r shown alongside it, being dimensionless, is the figure to quote when you want to describe strength.
What is the relationship between covariance and variance?
Variance is simply the covariance of a variable with itself: Cov(X, X) = Var(X). That is why both use the same sum-of-squared-deviations structure and the same n - 1 divisor. In multivariate work the connection becomes visible in the covariance matrix, where the diagonal holds each variable's variance and the off-diagonal entries hold the pairwise covariances, with the matrix symmetric because Cov(X, Y) = Cov(Y, X).
How does covariance change if I rescale or shift the variables?
Adding a constant to either variable leaves covariance untouched, because the formula uses deviations from the mean and the mean shifts by the same amount. Multiplying does change it: Cov(aX + b, cY + d) = ac x Cov(X, Y). So converting a variable from dollars to thousands of dollars divides the covariance by 1000. Correlation is immune to both operations as long as the scale factors are positive, which is exactly what makes it comparable across datasets.
Can covariance be zero when the two variables are strongly related?
Yes, because it only detects linear association. Take X = -2, -1, 0, 1, 2 with Y = X^2, giving Y = 4, 1, 0, 1, 4. The mean of X is 0 and the mean of Y is 2, and the deviation products sum to (-2)(2) + (-1)(-1) + (0)(-2) + (1)(-1) + (2)(2) = 0, so the covariance is exactly zero even though Y is completely determined by X. Always plot the data before concluding that no relationship exists.
How is the covariance matrix used when there are more than two variables?
With k variables you build a k by k symmetric matrix holding every pairwise covariance, with the variances on the diagonal. Portfolio theory uses it to compute the variance of a weighted combination, since risk depends on how the holdings co-move rather than on their individual volatilities alone. Principal component analysis takes the eigenvectors of the same matrix to find the directions of greatest variation, which is the basis of most dimension reduction.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator · Editorial policy
Related Calculators
🧮Correlation and Covariance Calculator
Calculate correlation and covariance with inputs, formulas, and instant results.
🧮Annulus Area Calculator
Calculate annulus area with inputs, formulas, and instant results.
🧮Area Calculator
Calculate area with inputs, formulas, and instant results.
🧮Area of a Rectangle Calculator
Calculate the area, perimeter, and diagonal of a rectangle. Find missing sides from known area. Convert between metric and imperial area units.
🧮Area of Crescent Calculator
Calculate area of crescent with inputs, formulas, and instant results.
🧮Center of Mass Calculator
Calculate center of mass with inputs, formulas, and instant results.
🧮Centroid Calculator
Calculate centroid with inputs, formulas, and instant results.
🧮Chord Length Calculator
Calculate chord length with inputs, formulas, and instant results.