Correlation vs. Causation
Statistical Inference & Applied Statistics
Chapter 7 · Correlation vs. Causation
Every chapter so far tested a single variable, or compared two groups on one metric. This chapter asks a different question: do two different variables move together? And — the far more important half of the chapter — what an honest "yes" actually does and doesn't let you conclude.
The Pearson Correlation Coefficient
r = Σ((x−x̄)(y−ȳ)) / √(Σ(x−x̄)² × Σ(y−ȳ)²) — ranges from −1 (perfect negative linear relationship) through 0 (no linear relationship) to +1 (perfect positive linear relationship)
Worked example: CPU usage (%) and response time (ms) across six servers:
| CPU (%) | 40 | 55 | 60 | 45 | 70 | 50 |
|---|---|---|---|---|---|---|
| Response (ms) | 120 | 150 | 165 | 130 | 190 | 140 |
r ≈ 0.997 — an extremely strong positive linear relationship: as CPU usage rises, response time rises right along with it, almost perfectly linearly across this data.
The Question This Number Can't Answer
A correlation this strong is tempting to read as "high CPU usage causes slow responses." That might well be true here — but the number r = 0.997 itself doesn't establish it. Any observed correlation between two variables has four possible explanations:
| Explanation | What it would mean here |
|---|---|
| X causes Y | High CPU usage genuinely slows down response handling |
| Y causes X (reverse causation) | Slow-running requests themselves consume more CPU while they're stuck processing |
| A confounder Z causes both | A traffic surge drives both more CPU load and slower responses independently |
| Coincidence | With only 6 data points, a strong-looking correlation can arise from chance alone |
The Classic Confounding Example
A more directly engineering-relevant version: "time spent on a page" correlates positively with "conversion rate" — but this is a strong candidate for reverse causation, not X causing Y. Users who are already leaning toward converting tend to read more carefully and spend longer on the page because they're interested — the extra time is a symptom of interest, not necessarily a cause of the sale.
What Actually Establishes Causation
Correlation in Code
Hands-On Exercises
A developer logs hours of sleep and number of bugs introduced the next day across six days: sleep = [7, 5, 8, 4, 6, 7], bugs = [1, 4, 0, 6, 3, 2]. Compute the Pearson correlation coefficient, and state whether it's a strong positive, strong negative, or weak relationship.
A team observes that teams with more Slack messages per day also close more tickets per day, with a strong positive correlation. Using this chapter's own four explanations, propose one plausible story for each of "X causes Y," "Y causes X," and "a confounder Z causes both" for this specific observation.
📄 View solutionExplain, using this chapter's own finding, exactly why randomly assigning users to control and treatment groups (Chapter 6's A/B testing) rules out confounding as an explanation for an observed difference, in a way that simply observing two already-correlated variables in the wild never can.
📄 View solutionChapter 7 Quick Reference
- Pearson's r: ranges −1 to +1, measuring the strength and direction of a linear relationship between two variables
- Any correlation has four possible explanations: X causes Y, Y causes X (reverse causation), a confounder Z causes both, or coincidence
- The classic example: ice cream sales and drowning deaths correlate strongly — both driven by hot weather, neither causing the other
- "Time on page" correlating with conversion is a strong candidate for reverse causation, not X causing Y
- Only randomization (Chapter 6's A/B test) rules out confounding by construction — pure observational correlation, however strong, cannot
- Next chapter: Linear regression as statistical inference