Skip to content

Correlation vs Causation (Pearson, Spearman)

Correlation answers:

  • “Do variables move together?”

It does not answer:

  • “Does X cause Y?”
  • Measures linear relationship
  • Sensitive to outliers
Pearson
import numpy as np
from scipy import stats
 
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([2, 4, 5, 4, 5, 7])
 
r, p = stats.pearsonr(x, y)
print("r:", r)
print("p:", p)
  • Uses ranks
  • Captures monotonic relationships
  • More robust to outliers and non-linearity
Spearman
import numpy as np
from scipy import stats
 
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([10, 9, 7, 6, 3, 1])
 
rho, p = stats.spearmanr(x, y)
print("rho:", rho)
print("p:", p)

Pearson’s r measures how tightly two variables move together: near +1 the points hug a rising line, near 0 there’s no pattern, near −1 they hug a falling line. Watch the cloud tighten and flip as r changes:

sketch Correlation, from strong to none p5.js
r near +1 means points follow a rising line, 0 means no linear pattern, -1 means a falling line.
  • Plot scatter first.
  • Consider transformations (log) if scales vary.
  • Be cautious: confounders can create spurious correlation.
diagram Correlation can hide a confounder mermaid
Two variables can move together purely because a third variable drives both.

Exercise 2 – Spearman for a monotonic (non-linear) trend

Section titled “Exercise 2 – Spearman for a monotonic (non-linear) trend”

Exercise 3 – Spotting a possible confounder

Section titled “Exercise 3 – Spotting a possible confounder”

Continue to Non-Parametric Tests for association and comparison tests that don’t assume normal, linear relationships.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading