What Is Correlation?
Press Next → or use ← → arrow keys
The Intuition — Ice Cream & Drowning
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.
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.
Three Shapes A Scatter Can Take
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).
The Pearson Correlation Coefficient
| |r| | Strength | Example |
|---|---|---|
| 1.0 | Perfect | Celsius ↔ Fahrenheit |
| 0.7 – 0.9 | Strong | Height & weight |
| 0.4 – 0.6 | Moderate | Study hours & exam score |
| 0.1 – 0.3 | Weak | Sleep & productivity |
| 0.0 | None | Shoe size & IQ |
Study Hours vs Exam Score — r ≈ 0.996
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.
Correlation Does NOT Imply Causation
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.
Three Ways A Correlation Fools You
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.
Anscombe's Quartet — Same r, Different Data
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.
Pearson Only Sees Straight Lines
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.
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.
Pearson vs Spearman
| Property | Pearson r | Spearman ρ |
|---|---|---|
| Measures | Linear relationship | Monotonic (rank-based) |
| Needs ~normal data? | Ideally yes | No |
| Sensitive to outliers? | Yes | No |
| Works on ordinal data? | No | Yes |
| Use when… | Continuous, roughly normal | Skewed, ordinal, or outliers |
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.
The Correlation Matrix Heatmap
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).
Correlation For Feature Selection
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.
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
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.
Five Rules For Correlation
You Now Understand Correlation
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.
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