Correlation quantifies how strongly two variables move together — the number behind "does more experience mean a higher salary?". It's one of the most used (and most misused) statistics, so understanding what it does and doesn't tell you is essential.
Covariance
Covariance measures whether two variables tend to move in the same direction (positive) or opposite directions (negative). Its problem: its size depends on the units, so you can't compare covariances — which is why we standardise it into correlation.
The correlation coefficient
The Pearson correlation (r) rescales covariance to a fixed range from −1 to +1, so it's comparable and interpretable:
| r value | Meaning |
|---|---|
| +1 | Perfect positive linear relationship |
| +0.7 | Strong positive |
| 0 | No linear relationship |
| −0.7 | Strong negative |
| −1 | Perfect negative linear relationship |
df["experience"].corr(df["package"]) # a single rdf.corr(numeric_only=True) # a correlation matrix of all pairs
Pearson vs Spearman
- Pearson measures linear association — best for straight-line relationships.
- Spearman measures monotonic association using ranks — robust to outliers and works for curved-but-consistent relationships.
Correlation ≠ causation
The cardinal rule: two variables correlating does not mean one causes the other. A hidden confounder may drive both (ice-cream sales and drownings both rise with temperature), or it may be coincidence. Only a controlled experiment (A/B test, Chapter 5) can establish causation.
Practice
Compute the correlation between experience and package, and build a correlation matrix for a dataset. Find a pair with high correlation and argue whether causation is plausible or a confounder is likely. Compare Pearson and Spearman on a column with an outlier.
Key takeaways
- 01Covariance shows direction of joint movement but depends on units — not comparable.
- 02Correlation (r) rescales it to −1…+1: comparable and interpretable.
- 03Pearson measures linear association; Spearman (rank-based) handles monotonic/curved and resists outliers.
- 04A near-zero Pearson can still hide a strong non-linear relationship — always plot.
- 05Correlation is not causation — a confounder or coincidence may explain it; only experiments prove cause.
