Covariance
Covariance measures how two variables move together: the average product of their deviations from their means, cov(x, y) = sum((x - x-bar)(y - y-bar)) / (n - 1) for a sample. Positive means they rise together, negative means one rises as the other falls, but its size depends on the units.
Syntax
cov(x, y) = sum( (x - x-bar) * (y - y-bar) ) / (n - 1)| Part | What it is |
|---|---|
x - x-bar, y - y-bar | the deviations of each pair from their means |
n - 1 | for a sample; a population divides by n |
Examples
x: 1, 2, 3, 4, 5 (x-bar = 3); y: 2, 4, 5, 4, 5 (y-bar = 4)
products of deviations: 4, 0, 0, 0, 2 (sum = 6)
sample cov = 6 / 4Result: 1.5
Excel: =COVARIANCE.S(A1:A5, B1:B5)
Same data as a population
6 / 5Result: 1.2
Excel: =COVARIANCE.P(A1:A5, B1:B5)
Correlation from covariance
r = cov / (sx * sy) = 1.5 / (1.581 * 1.225)Result: 0.775
Covariance of a variable with itselfResult: its variance: cov(x, x) = 10 / 4 = 2.5
Common mistakes
- Comparing covariances across data in different units; a covariance of 1.5 in meters becomes 15,000 in centimeters, use correlation to compare.
- Dividing by n for a sample and n - 1 for the matching variances; the mismatch gives a correlation that can exceed 1.
- Reading covariance 0 as independence; it only rules out a linear relationship.
Practice Covariance
These drills check your answer on live data, free, no account needed.
- Sample covarianceData on monthly ad spend and sales at regional stores (x = Ad spend ($k), y = Sales ($k)): (9, 87.8), (10, 98.7), (6, 81.3), (12, 99.1), (15, 111.9), (6, 78.6). What is the sample covariance of x and y? Round to 2 decimal places.
- Correlation coefficientData on monthly ad spend and sales at regional stores (x = Ad spend ($k), y = Sales ($k)): (9, 87.8), (10, 98.7), (6, 81.3), (12, 99.1), (15, 111.9), (6, 78.6). What is the correlation coefficient r? Round to 3 decimal places.
Questions
What is the difference between covariance and correlation?
Correlation is covariance standardized by the two standard deviations: r = cov(x, y) / (sx * sy). Covariance has units and no fixed range; correlation is unitless and runs from -1 to 1.
Where is covariance used?
In portfolio theory (the variance of a portfolio depends on the covariances between assets), in the regression slope (b = cov(x, y) / var(x)) and in multivariate methods such as PCA.
Which Excel function should I use, COVARIANCE.S or COVARIANCE.P?
COVARIANCE.S for a sample (divides by n - 1), COVARIANCE.P for a complete population. The legacy COVAR equals COVARIANCE.P.
Related
- Correlation coefficient (Pearson r)How to calculate the Pearson correlation coefficient r by hand and with Excel CORREL, a worked example with exact numbers and what values of r mean.
- VarianceHow to calculate variance step by step, why the sample version divides by n - 1, how it relates to standard deviation, and Excel VAR.S and VAR.P.
- Linear regressionHow to fit a regression line y = a + bx: slope b = Sxy / Sxx, intercept a = y-bar - b x-bar, a worked example, prediction and Excel SLOPE and INTERCEPT.
- Standard deviationThe standard deviation formula, sample versus population versions, a worked example with exact numbers and the Excel functions STDEV.S and STDEV.P.