STAT 200 · Statistics for Economics
Covariance and Correlation
Measure how two variables move together, compute a correlation by hand, and learn why it does not prove cause and effect.
Key takeaways
- Covariance is positive when two variables tend to be above or below their means together, and negative when one tends to be high as the other is low.
- The size of a covariance depends on the units, so it is hard to interpret on its own.
- The correlation coefficient rescales covariance to lie between −1 and +1 and has no units.
- Correlation measures only straight-line association and can be badly distorted by curves or outliers.
- A correlation alone cannot show that one variable causes another.
In many towns, the weeks with the most ice cream sales are also the weeks with the most sunburns. Should the health department ban rocky road?
Of course not. Hot, sunny weather pushes both up. But the pattern is real: when one is high, the other tends to be high too. Describing that "tend to move together" idea with a number is useful, and misreading the number is one of the most popular mistakes on the planet.
Our companion is Leo, who runs a car wash. Over five weekends he handed out different numbers of flyers and recorded his sales. He wants to know whether weekends with more flyers were weekends with more sales, and how tight that link is.
| Weekend | Flyers (hundreds), x | Sales (hundreds of $), y |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 5 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
The idea: moving together
Start with a picture. A scatter plot puts flyers on the horizontal axis and sales on the vertical. Then draw two imaginary lines: a vertical one at the mean number of flyers, 3, and a horizontal one at mean sales, 4. They cut the picture into four corners.
A dot in the upper right is above average on both. A dot in the lower left is below average on both. Both kinds say, "these two travel together." A dot in the upper left or lower right says the opposite. Covariance simply tallies those votes, giving louder voices to dots far from both lines.
Covariance
For each case, find the deviation of x from its mean and the deviation of y from its mean. Multiply the two. A matching pair of signs gives a positive product, and mismatched signs give a negative one. Add the products and divide by n − 1 for a sample.
The recipe is a close cousin of the variance. In fact, the covariance of a variable with itself is its variance, since each deviation gets multiplied by itself.
A worked example: flyers and sales
The means are x̄ = (1 + 2 + 3 + 4 + 5) ÷ 5 = 3 and ȳ = (2 + 4 + 5 + 4 + 5) ÷ 5 = 4.
| x − 3 | y − 4 | Product | (x − 3)² | (y − 4)² |
|---|---|---|---|---|
| −2 | −2 | 4 | 4 | 4 |
| −1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 2 | 1 | 2 | 4 | 1 |
| Total 0 | Total 0 | 6 | 10 | 6 |
Covariance = 6 ÷ 4 = 1.5
Variance x = 10 ÷ 4 = 2.5 s_x = √2.5 ≈ 1.581
Variance y = 6 ÷ 4 = 1.5 s_y = √1.5 ≈ 1.225
Correlation = 1.5 ÷ (1.581 × 1.225) ≈ 0.775 So the covariance is positive: flyers and sales tended to rise together. But is 1.5 big? Suppose Leo records sales in dollars instead of hundreds of dollars. Every y deviation becomes 100 times larger, and the covariance leaps to 150. Nothing about his car wash changed. A number that swings with your choice of units cannot tell you how strong a relationship is.
The correlation coefficient
The fix is to divide the covariance by both standard deviations:
r = cov(x, y) ÷ (s_x × s_y) The result, called the correlation coefficient and written r, has no units. If you switch to dollars, the covariance and s_y both grow 100 times, and the factor cancels. The value always lies between −1 and +1.
- r = +1: every point lies exactly on an upward-sloping straight line.
- r = −1: every point lies exactly on a downward-sloping straight line.
- r = 0: no straight-line tendency either way.
Values closer to −1 or +1 mean the points hug a line more tightly. Leo's r of about 0.77 shows a fairly strong upward tendency. Words such as "strong" and "weak" have no universal cutoffs, since what counts as impressive differs between fields.
Note what r does not say. It is not the slope. A line can be nearly flat or very steep and still give r = 1, provided the points sit exactly on it. An r of 0.77 also does not mean 77% of points fit, or that sales rise 77 cents per anything.
The square of r has a useful reading when you fit a straight line to the data. Here r² = 0.775² ≈ 0.60, meaning about 60% of the variation in sales is accounted for by the straight-line relationship with flyers. "Accounted for" is a statement about the arithmetic of the fit, not proof of cause.
A second example: a relationship that runs downhill
Leo also sells lemonade by the vacuum station. Over five days he tried different prices and counted cups.
| Price ($), x | Cups sold, y | x − 4 | y − 40 | Product |
|---|---|---|---|---|
| 2 | 50 | −2 | 10 | −20 |
| 3 | 46 | −1 | 6 | −6 |
| 4 | 40 | 0 | 0 | 0 |
| 5 | 36 | 1 | −4 | −4 |
| 6 | 28 | 2 | −12 | −24 |
The products total −20 − 6 + 0 − 4 − 24 = −54. Dividing by 4 gives a covariance of −13.5. For the standard deviations, the squared price deviations total 10, so the variance is 2.5. The squared cup deviations are 100, 36, 0, 16 and 144, totaling 296, so the variance is 74.
r = −13.5 ÷ √(2.5 × 74) = −13.5 ÷ √185 ≈ −13.5 ÷ 13.60 ≈ −0.99 The minus sign says higher prices went with fewer cups. The size, close to 1, says the five points sit very near a straight line. This is what you would expect from a demand relationship. Still, if Leo had raised prices only on cool, quiet days, the weather would be tangled into the answer.
Notice also that r does not care which variable you call x. The correlation of price with cups equals the correlation of cups with price. That symmetry is one more reason it cannot tell you which way any causal arrow points.
Curves and lone points
Correlation looks only for straight lines. Consider the points x = −2, −1, 0, 1, 2 with y = x², so y = 4, 1, 0, 1, 4. Here y is completely determined by x. Yet the mean of x is 0 and the mean of y is 2, and the products of deviations are −4, 1, 0, −1, 4. They total zero, so r = 0.
The falling half and the rising half cancel, so r = 0 although y depends entirely on x.
Zero correlation does not mean "no relationship." It means "no straight-line relationship."
A single odd point can also do enormous mischief. A shapeless blob of dots plus one point far up and to the right can produce a large positive r all by itself. An outlier off the line can also drag a strong correlation toward zero. Always plot the data before trusting r.
A common mistake: correlation as cause
Leo is thrilled. "Flyers raise sales!" Maybe. The same pattern could appear for several reasons.
A third factor that influences both variables is a confounder. Maybe Leo prints more flyers when the forecast looks sunny, and sunny weekends are great for car washes regardless. The flyers could be doing nothing at all.
How could he find out? One strong approach is an experiment: decide the number of flyers by drawing slips from a hat, so weather and mood cannot influence the choice. Then a persistent link is much harder to explain away. When experiments are impossible, economists use careful comparisons that try to imitate one.
Trends cause a special version of the trap. Many things grow over the years, so two unrelated rising series can show a high correlation just because both climb. Looking at changes from period to period is often a better check.
When two unsteady things are added
Covariance has a practical job beyond describing pairs. Suppose Leo's weekly washing income has a standard deviation of $3 hundred and his weekly snack income has a standard deviation of $4 hundred. How unsteady is the total? The rule is:
variance of (x + y) = variance of x + variance of y + 2 × cov(x, y) Say the correlation between them is −0.5, perhaps because customers snack more on slow days while waiting around. Then the covariance is r × s_x × s_y = −0.5 × 3 × 4 = −6.
variance of total = 9 + 16 + 2 × (−6) = 13
standard deviation = √13 ≈ 3.6 hundred dollars The total swings less than the snack income alone. With a correlation of +0.5 instead, the variance would be 9 + 16 + 12 = 37, for a standard deviation of about 6.1. Same two businesses, very different ride. That is the arithmetic behind the advice not to keep all your eggs in one basket, especially baskets that tend to fall together.
Where economists use it
Correlation helps describe whether spending moves with income, whether prices of two goods move together, or which indicators tend to rise before a recession. Investors care about it too. Holding two assets that do not move in lockstep can make a combined portfolio less bumpy than either alone might suggest, though correlations can shift, especially in turbulent times.
Always report the number of pairs along with r. Leo has five. With so few, an r of 0.77 could easily arise by luck, a worry you will learn to quantify in the lessons on inference. Later, hypothesis testing gives you a way to ask whether a pattern is more than noise.
Words to know
Covariance a measure of the direction in which two variables vary together, in combined units.
Correlation coefficient covariance divided by both standard deviations, a unitless number from −1 to +1.
Positive association a tendency for higher values of one variable to go with higher values of the other.
Negative association a tendency for higher values of one variable to go with lower values of the other.
Confounder a third factor that influences both variables and can create a misleading link.
Reverse causation the case where the supposed effect is actually driving the supposed cause.
Check yourself
- What does a negative covariance tell you about two variables?
- Three pairs are (1, 3), (2, 5) and (3, 7). Without long calculations, what is r, and why?
- A sample of 6 pairs has a sum of products of deviations equal to 20. What is the covariance?
- If cov = 12, s_x = 4 and s_y = 5, what is r?
- Why does changing sales from dollars to cents change the covariance but not the correlation?
- Towns with more firefighters at a fire tend to report more damage. Explain why this need not mean firefighters cause damage.
Show the answers
- They tend to move in opposite directions, with one above its mean when the other is below.
- r = +1, because every point lies exactly on the upward-sloping line y = 2x + 1.
- It is 20 ÷ (6 − 1) = 4.
- r = 12 ÷ (4 × 5) = 0.6.
- The covariance and the standard deviation of sales are both multiplied by 100, and the factor cancels in r.
- The size of the fire is a likely confounder, since bigger fires draw more firefighters and also cause more damage.