Skip to content
AI-grafen
DAI developerStatistics and probability· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Correlation and causation

Be able to compute a correlation and explain why it does not show causation.

Practise in Mattegrafen ↗ · Regressionsanalys och korrelationskoeffi

Prerequisites

Intuition

Correlation (r between −1 and 1) measures how much two variables co-vary linearly. Ice cream sales and drownings correlate strongly — but ice cream does not cause drowning. The summer causes both: a confounder.

Other traps: reverse causation (firefighters do not cause fires, even though more firefighters = a larger fire), selection (among admitted students the entrance test correlates negatively with school grades — because both were used in the admission), chance (with 1 000 variables you always find some that correlate).

An ML model finds correlations. It knows nothing about causation. It is you who has to know that before the model is used to change anything.

Code

import numpy as np
rng = np.random.default_rng(0)

summer = rng.uniform(0, 1, 200)             # the hidden confounder
ice_cream = 100 * summer + rng.normal(0, 10, 200)
drowning = 5 * summer + rng.normal(0, 1, 200)
print(np.corrcoef(ice_cream, drowning)[0, 1])   # ≈ 0.8 — a strong correlation with no causation

# control for the summer: the residuals
res_i = ice_cream - np.polyval(np.polyfit(summer, ice_cream, 1), summer)
res_d = drowning - np.polyval(np.polyfit(summer, drowning, 1), summer)
print(np.corrcoef(res_i, res_d)[0, 1])      # ≈ 0 — the relationship disappears

To claim causation: a randomised experiment (an A/B test) or very careful control for every confounder — and even then with reservations.

Mastery means

  • Computes and interprets the Pearson correlation
  • Gives examples of a confounder and of reverse causation
  • Explains what is required to claim causation

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences