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

Pearson's r
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 (%)405560457050
Response (ms)120150165130190140

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:

ExplanationWhat it would mean here
X causes YHigh 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 bothA traffic surge drives both more CPU load and slower responses independently
CoincidenceWith only 6 data points, a strong-looking correlation can arise from chance alone

The Classic Confounding Example

Ice cream sales and drowning deaths — genuinely, strongly correlated
Monthly ice cream sales and monthly drowning deaths correlate strongly and positively across an entire year. Ice cream doesn't cause drowning, and drowning certainly doesn't cause ice cream sales — both are driven by a real confounder: hot summer weather brings out both more swimmers and more ice cream buyers, independently. This is the textbook illustration of exactly why "X and Y move together" is not evidence that either one drives the other.

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

This is exactly why Chapter 6's A/B test matters
Chapter 6's own randomized experiment sidesteps all four explanations above in one move: randomly assigning users to control or treatment means any potential confounder — traffic source, time of day, user type — gets distributed roughly equally between both groups by construction, not by hope. If the treatment group's conversion rate is still significantly different afterward, the only systematic difference between the groups was the treatment itself, ruling out both confounding and reverse causation. Pure observational correlation, however strong, can never make that same guarantee — only randomization can.

Correlation in Code

def pearson_r(x, y): n = len(x) xbar = sum(x) / n ybar = sum(y) / n numerator = sum((x[i] - xbar) * (y[i] - ybar) for i in range(n)) denom = ( sum((xi - xbar) ** 2 for xi in x) * sum((yi - ybar) ** 2 for yi in y) ) ** 0.5 return numerator / denom cpu = [40, 55, 60, 45, 70, 50] response = [120, 150, 165, 130, 190, 140] print(pearson_r(cpu, response)) # 0.997... # Python 3.10+ also has this built in: import statistics print(statistics.correlation(cpu, response)) # matches exactly

Hands-On Exercises

Exercise 1

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.

📄 View solution
Exercise 2

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 solution
Exercise 3

Explain, 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 solution

Chapter 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