Correlation
Last updated
On busy days at the Batangas store, more rice sells, and more sugar too. How closely do the two move together? A correlation puts that in one number, from −1 to +1. In this lesson you'll compute Pearson's r by hand and with SciPy, train your eye to guess it, use Spearman's rank correlation when the pattern curves, and see why r says nothing about how steep a relationship is, and how a single typo can make it say almost anything.
By the end of this lesson you can
- Compute Pearson's r and read what it says, with an interval
- Use Spearman's rank correlation for curved or skewed data
- Explain why correlation doesn't measure slope
- Spot an outlier that makes a correlation
Pearson's r
The first cell makes a table of the Batangas store's 60 days: kilos of oil, rice and sugar sold each day. Run the cells.
Pearson's correlation coefficient, , measures how closely two columns follow a straight line. For each day, take how far rice was above or below its mean, and the same for sugar, and multiply. On days when both are above their means, or both below, the product is positive; when one is up and the other down, it's negative. Add the products, and scale so the answer is between −1 and +1:
- By hand, with NumPy's
np.corrcoefand with SciPy, r = 0.81 for rice and sugar. stats.pearsonralso tests whether the true correlation could be 0 (: no), and gives a 95% interval, 0.70 to 0.88. With only 60 days, r itself is uncertain.bat.corr()gives every pair at once: all three items correlate at about 0.8.
How to read : +1 means the points lie on a rising straight line, −1 on a falling one, and 0 means no straight-line pattern. Between, the closer to ±1, the tighter the cloud around a line.
Why do the three items move together? Mostly because some days are busier than others: Saturdays sell more of everything (7.P). A correlation says two things move together, not why. Lesson 8.11 is about that.
Train your eye
Most people misjudge correlation from a picture: a cloud with looks much weaker than "halfway to a perfect line". Guess a few, and watch your average miss shrink:
Drag the slider, then check.
A rough guide for scatters like these: below about 0.3 you can barely see a pattern; around 0.7 it's clear but loose; above 0.9 the points hug a line.
Correlation isn't slope
Rice and sugar have ; rice and oil have . Does rice "matter" as much to both? Run the cells.
Each extra kilo of rice comes with 0.19 kg more sugar, but only 0.06 kg more oil. The correlations are almost the same; the slopes are three times apart. On the shared axes, the oil cloud is just as tight around its line, but the line is much flatter.
measures how tightly the points follow a line, not how steep the line is. A tiny effect that's very consistent has a high ; a big effect with lots of noise around it has a low one. To say how much changes with , you need the slope, from a regression: lesson 8.10.
Spearman: correlation of ranks
Pearson's looks for a straight line. Spearman's rank correlation, (the Greek letter rho), asks a looser question: when goes up, does tend to go up too, in a straight line or not? It replaces each column by its ranks (1 for the smallest, 2 for the next, and so on) and works out Pearson's on the ranks. Run the cells.
- Receipts: more items means more pesos, but not in a straight line (a pack of cigarettes and a sack of rice are both one item). Pearson gives 0.724, Spearman 0.756.
- A curve that always rises: Spearman is exactly 1.0, because every step up in is a step up in . Pearson gives 0.789, because the points don't lie on a straight line.
Use Spearman when the data are skewed, have outliers, or follow a curve that only goes one way. It's also the right choice for ranked answers, such as a survey's 1 to 5.
Wrong way: one typo, r = 0.97
An analyst checks whether rainy days sell more cooking oil. Twenty days of data; run the cells.
- The twenty real days: r = 0.10. Rain and oil have nothing to do with each other here (the cell made them that way).
- One more row, a day typed as 210 mm and 35 kg instead of 21.0 and 3.5: r = 0.97.
One point far from the rest, in both directions at once, drags the line through itself and makes almost 1. It works the other way too: one far-off point can hide a real correlation. Spearman's barely notices (0.10), because to the ranks the typo is just "the biggest" on both scales.
The check: never report without looking at the scatter plot. If one or two points sit far from the cloud, find out what they are. If they're mistakes, fix them (5.7); if they're real, report with and without them, and Spearman's beside it.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- SciPy: scipy.stats.pearsonr: Pearson's r, its p-value and confidence interval
- SciPy: scipy.stats.spearmanr: Spearman's rank correlation
- pandas: DataFrame.corr: correlations of every pair of columns, Pearson or Spearman
- Diez, Çetinkaya-Rundel and Barr, OpenIntro Statistics, 4th ed., section 8.1: what correlation measures, and what it doesn't
- Anscombe, Graphs in Statistical Analysis, The American Statistician 27(1), 1973: four datasets with the same r and very different pictures