Foundations of Data Science slides 📂 Introduction · 9 of 10 41 min read

What Is Correlation? Strength, Direction & the Causation Trap

One number from −1 to +1 that captures how two variables move together — the sign is direction, the size is strength. This tutorial covers Pearson's r and its interpretation bands, a worked study-hours example (r≈0.996), why correlation is never causation, Anscombe's quartet, Pearson vs Spearman, correlation heatmaps, and ML feature selection — with animated diagrams.

🔗

What Is Correlation?

One number, from −1 to +1, that captures how strongly two variables move together — and one famous trap that catches everyone: correlation is not causation.
Pearson r ≠ Causation Always Plot Heatmaps

Press Next → or use ← → arrow keys

Section 01

The Intuition — Ice Cream & Drowning

Two things that rise together — but one doesn't cause the other
Across a year, ice-cream sales and drowning incidents rise and fall almost in lockstep. Statistically, they're strongly correlated. But nobody thinks a scoop of vanilla causes a drowning — a hidden third factor, hot summer weather, drives both.

That's correlation in a nutshell: a single number measuring how tightly two variables move together. It's one of the most useful tools in statistics — and, thanks to examples like this, one of the most misused.
💡
Strength And Direction, In One Number

Correlation measures the strength and direction of the linear relationship between two numeric variables. The sign says which way (together or opposite); the size says how tightly — all packed into one value between −1 and +1.

Section 02 · Diagram

Three Shapes A Scatter Can Take

Positive · r ≈ +0.9 ↑↑ move together None · r ≈ 0 →? no pattern Negative · r ≈ −0.9 ↑↓ move opposite
📈
Read The Cloud, Read The Relationship

Positive: as one rises, so does the other (height & weight). None: a shapeless cloud — the variables are unrelated (shoe size & IQ). Negative: one rises as the other falls (driving speed & travel time).

Section 03 · Pearson r

The Pearson Correlation Coefficient

Pearson's r
r = Σ(x−x̄)(y−ȳ) / √[Σ(x−x̄)²·Σ(y−ȳ)²]
Standardized covariance — always lands between −1 and +1, whatever the units.
Sign vs size
sign = direction · |r| = strength
r = −0.85 is a stronger relationship than r = +0.40 — magnitude wins.
|r|StrengthExample
1.0PerfectCelsius ↔ Fahrenheit
0.7 – 0.9StrongHeight & weight
0.4 – 0.6ModerateStudy hours & exam score
0.1 – 0.3WeakSleep & productivity
0.0NoneShoe size & IQ
Section 03 · Worked Example

Study Hours vs Exam Score — r ≈ 0.996

study hours → exam score → Alice 50 Bob 60 Carol 72 Dan 80 Eve 95
🎓
The Points Almost Sit On A Line

Five students, more hours → higher scores, almost perfectly. The maths: means x̄ = 6, ȳ = 71.4; the sum of products Σ(x−x̄)(y−ȳ) = 220; the sums of squares 40 and 1219.2. Divide and you get r ≈ 0.996 — an almost perfect positive correlation.

Section 04 · The Big Trap

Correlation Does NOT Imply Causation

☀️ Hot weather the hidden cause 🍦 Ice-cream sales 🌊 Drownings no causal link — they just share a cause
⚠️
A Correlation Has Three Possible Causes

When X and Y correlate, it could be that X causes Y, that Y causes X, or — very often — that a hidden third variable (a confounder) drives both. The correlation alone can't tell you which. This is the single most important caveat in all of statistics.

Section 04 · Cautionary Tales

Three Ways A Correlation Fools You

🧴
Sunscreen "Causes" Cancer
Sunscreen sales track skin-cancer rates — but the real driver is UV exposure, which raises both.
👟
Shoe Size ↔ Netflix
Bigger feet correlate with more streaming (r = +0.71). The confounder is simply age — adults have both.
🎫
Reversed Direction
Support tickets correlate with revenue (r = +0.82) — but revenue drives customers drives tickets. Cutting support would backfire.
🕵️
Ask "What Else Could Explain This?"

Before acting on any correlation, hunt for the confounder and check the direction of cause. A multi-million-pound decision has been wrecked by treating a backwards or spurious correlation as if it were cause and effect.

Section 05 · Always Plot

Anscombe's Quartet — Same r, Different Data

① clean linear r = 0.82 ② curved r = 0.82 ③ line + outlier r = 0.82 ④ vertical + point r = 0.82
📊
Four Datasets, One Identical r

Statistician Francis Anscombe built four datasets with the same r = 0.82 (and same means and variances) that look nothing alike — one clean line, one curve, one line ruined by an outlier, one near-vertical with a single stray point. The lesson is permanent: always plot your data; r alone can hide the truth.

Section 05 · Blind Spot

Pearson Only Sees Straight Lines

🙈
A Perfect U-Shape Gives r ≈ 0

Take a flawless parabola, y = x²: as x runs from negative to positive, y falls then rises. The relationship is perfectly deterministic — yet Pearson's r comes out near zero, because the downward and upward halves cancel. Pearson measures only the linear part of a relationship; it's blind to curves.

🔍
"r = 0" Means "No Linear Link" — Not "No Link"

A zero correlation never proves independence; it only rules out a straight-line trend. There could be a strong curved, cyclical, or threshold relationship hiding underneath. Once again: plot it before you conclude anything.

Section 06 · Which Coefficient?

Pearson vs Spearman

PropertyPearson rSpearman ρ
MeasuresLinear relationshipMonotonic (rank-based)
Needs ~normal data?Ideally yesNo
Sensitive to outliers?YesNo
Works on ordinal data?NoYes
Use when…Continuous, roughly normalSkewed, ordinal, or outliers
📐
Report r And A P-Value

Spearman works on ranks, so it captures any consistently increasing or decreasing relationship, even a curved one, and shrugs off outliers. Whichever you use, pair it with a p-value: r = 0.9 from just 5 points is flimsy; r = 0.9 from 100 points is solid. Sample size decides how much to trust the number.

Section 07 · Many Variables

The Correlation Matrix Heatmap

Study Sleep Anxiety Score Study Sleep Anxiety Score 1.00 0.15 −0.45 0.90 0.15 1.00 −0.60 0.30 −0.45 −0.60 1.00 −0.70 0.90 0.30 −0.70 1.00
🌡️
Every Pair, Colour-Coded

A correlation matrix computes r for every pair of variables; a heatmap colours it — deep pink/red for strong positive, deep blue for strong negative, pale for near-zero, and a solid diagonal of 1.0 (each variable with itself). One glance surfaces which variables drive your target (Study → Score = 0.90) and which move against it (Anxiety → Score = −0.70).

Section 08 · In Machine Learning

Correlation For Feature Selection

🎯
Keep: High r With Target
Features strongly correlated with what you're predicting carry signal — prioritize them.
✂️
Drop: |r| > 0.85 Between Features
Two features that mirror each other are redundant. Keeping both causes multicollinearity.
⚖️
Why It Hurts
Redundant features make a linear model's coefficients unstable and impossible to interpret.
💸
The Classic Redundant Pair

If a dataset has both salary and monthly_pay (= salary ÷ 12), they're perfectly correlated — r = 1.0. Feeding both into a regression makes the coefficients wobble wildly without adding information. Spot it in the heatmap, drop one, and the model steadies.

Section 09 · Code

Correlation In Python

import numpy as np
from scipy import stats

x = [2, 4, 6, 8, 10]
y = [50, 60, 72, 80, 95]

np.corrcoef(x, y)[0, 1]        # 0.9964 (Pearson)

stats.pearsonr(x, y)             # (r, p-value) — linear
stats.spearmanr(x, y)            # (rho, p-value) — rank-based

# ── pandas: full matrix + heatmap-ready ──
df.corr().round(3)               # every pair at once
df['study_hours'].corr(df['exam_score'])  # one pair

import seaborn as sns
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')  # visualize
🐼
One Line To The Whole Picture

df.corr() returns the full correlation matrix; wrap it in sns.heatmap(..., cmap='coolwarm') and you instantly see every relationship in the dataset. It's the fastest exploratory step before any modelling — and where you'll catch redundant features.

Section 10 · Golden Rules

Five Rules For Correlation

🏅 Correlation, Distilled
1Correlation ≠ causation. Always ask whether a hidden variable or reversed direction explains it.
2Always plot the scatter. Anscombe's Quartet proves r alone can badly mislead.
3Choose the right coefficient — Pearson for linear/continuous, Spearman for ordinal, skewed, or outlier-prone data.
4Report r and the p-value. A big r from a tiny sample is not trustworthy.
5In ML, drop |r| > 0.85 feature pairs (redundant) and keep features that correlate with the target.
Wrap-Up

You Now Understand Correlation

−1…+1Pearson r range
signDirection
|r|Strength
≠ causeThe big trap
plot!Anscombe's lesson
ρSpearman for ranks
🎯
The Through-Line

Correlation packs the strength and direction of a linear relationship into one number from −1 to +1. It's invaluable for exploration and feature selection — but it never proves causation, it's blind to curves, and it can be forged by outliers. So compute r, plot the scatter, and think before you conclude.

📚
Where To Go Next

Correlation is standardized covariance — study that link next, then move into regression (modelling the line itself) and the tools of causal inference, which finally let you move beyond "they move together" to "this causes that."

🔗 End of tutorial · Press to review, or click Restart