Paul's Python CoursePPC

Correlation, and what it doesn't say

Not met yet

The correlation r measures how closely two things rise and fall together along a straight line, from 1 through 0 to -1. Take each mean off, multiply the pairs and add them up, then divide by the square root of the two sums of squares; np.corrcoef(x, y)[0, 1] does the same. r is symmetric, has no units, and isn't a slope. It only sees straight lines, so a hump can give 0 when one thing decides the other completely. And a correlation never shows a cause on its own: two things can both follow a third, like the year or the season, which is how the price of coffee correlates with the warming.

Try it

main.py
import numpy as np

years = np.arange(1925, 2025)
moss_age = years - 1890
rng = np.random.default_rng(0)
warmth = 11 + 0.01 * (years - 1925) + rng.normal(0, 0.4, size=100)
print(round(np.corrcoef(years, warmth)[0, 1], 2), round(np.corrcoef(moss_age, warmth)[0, 1], 2))

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks correlation measures any relationship. It measures how well the points fit a straight sloping line, so a hump or a U can give r near 0.
  • Thinks a strong correlation shows that one thing causes the other, when both may follow a third thing, like time or the season, or the link may be luck.
  • Thinks r is the slope, how much y rises for each step in x. r has no units and stays the same if either set of numbers is stretched.
  • Thinks correlation(x, y) and correlation(y, x) are different numbers. Correlation is symmetric.

Where it comes up