Statistics for analysis · lesson 9 of 13
Parts in this lesson
Astats.pearsonr(x, y)Pearson's r, with a p-value and an interval
Bdf.corr()every pair of columns at once
Cx.corr(y, method="spearman")Spearman's rank correlation

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.

together.ipynb

Pearson's correlation coefficient, r, 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:

r=∑i(xi−x¯)(yi−y¯)∑i(xi−x¯)2∑i(yi−y¯)2
  • By hand, with NumPy's np.corrcoef and with SciPy, r = 0.81 for rice and sugar.
  • stats.pearsonr also tests whether the true correlation could be 0 (p=5×10−15: 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 r: +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 r=0.5 looks much weaker than "halfway to a perfect line". Guess a few, and watch your average miss shrink:

Guess the correlation0 guessed
−10+1

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 r=0.81; rice and oil have r=0.80. Does rice "matter" as much to both? Run the cells.

together.ipynb

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.

r measures how tightly the points follow a line, not how steep the line is. A tiny effect that's very consistent has a high r; a big effect with lots of noise around it has a low one. To say how much y changes with x, you need the slope, from a regression: lesson 8.10.

Spearman: correlation of ranks

Pearson's r looks for a straight line. Spearman's rank correlation, ρ (the Greek letter rho), asks a looser question: when x goes up, does y 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 r on the ranks. Run the cells.

ranks.ipynb
  • 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 x is a step up in y. 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.

rain_oil.ipynb
  • 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 r 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 r 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 r with and without them, and Spearman's ρ beside it.

Check what you learned

Two columns have r = −0.9. What does that mean?
Predict: x is 1, 2, 3, 4, 5 and y is 1, 4, 9, 16, 25. What is Spearman’s ρ?
Sugar and oil both have r = 0.8 with rice. What can you say about their slopes?

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.

All sources for Simple Data Analysis