Correlation

Generate scatter plots with a chosen correlation and see what r does and does not tell you

Intermediate
View

Scatter plot(drag any point; green products (x − x̄)(y − ȳ) push r up, red ones pull it down)

01020304050607080901000102030405060708090100xyx̄ȳr = 0.7

Correlation

Target r
0.7
Sample r now
0.7
r²
0.49
n
40

Step by step

x̄, ȳ
50, 50
sₓ
10√(Σ(x − x̄)² / (n − 1))
sᵧ
10
Σ(x − x̄)(y − ȳ)
2730
positive terms
27 → 2886.3
negative terms
13 → −156.3
cov = Σ / (n − 1)
70
r = cov / (sₓ · sᵧ)
= 70 / (10 × 10)
= 0.7

The generator makes the sample correlation exactly equal to the target, so the first number you see is the target itself. Drag a single point far from the cloud, or press Add an outlier, to see how much one value can move r, especially when n is small. A strong r says the points hug a line; it never says that x causes y.

What r measures

The Pearson correlation coefficient r is a single number between −1 and 1 that describes how closely a scatter plot follows a straight line. Its sign gives the direction (up to the right or down to the right), and its size gives the strength: r = ±1 means every point lies exactly on a line, and r near 0 means no straight-line trend.

The idea behind the formula is simple. Measure each point's deviation from the mean in both directions and multiply. Points in the top-right or bottom-left of the mean crosshair give positive products; points in the other two corners give negative ones. Their sum, scaled by n − 1, is the covariance. Divide by the two standard deviations and you get r, which no longer depends on the units of measurement.

Because r is built from squared-distance quantities, it is sensitive to outliers. With a small sample, one point dragged far from the cloud can turn a strong positive r into a weak or even negative one. Always look at the scatter plot before trusting the number.

Deviations from the means

Draw lines at x̄ and ȳ. Points in the top-right and bottom-left add positive products (x − x̄)(y − ȳ); the other two corners add negative ones.

Covariance has units

Covariance is the average of those products. Its size depends on the units, so 0.5 or 500 can mean the same thing.

r is unit-free

Dividing by both standard deviations rescales covariance to r, which always lies between −1 and 1.

Straight lines only

r measures how tightly the points follow a straight line. A perfect curve can have r = 0.

Key formulas

Sample covariance

cov(x, y) = Σ(x − x̄)(y − ȳ) / (n − 1)

Pearson correlation

r = cov(x, y) / (sₓ sᵧ) = Σ(x − x̄)(y − ȳ) / √(Σ(x − x̄)² · Σ(y − ȳ)²) = Σ zₓzᵧ / (n − 1)

sₓ, sᵧ
sample standard deviations of x and y
zₓ, zᵧ
standardized values (x − x̄)/sₓ and (y − ȳ)/sᵧ

Link to the regression line

slope b = r × sᵧ / sₓ,   r² = fraction of the variance in y explained by the least-squares line

What r does and does not tell you

ClaimTrue?Why
r = 0.9 means a strong straight-line patternUsuallyOnly if the scatter plot looks linear; check Anscombe II to IV
r = 0 means x and y are unrelatedNoy = x² on a symmetric range has r = 0 and a perfect relationship
r is the slope of the lineNoSlope = r × sᵧ / sₓ, so it depends on units; r does not
Changing units changes rNor is unchanged by any positive linear rescaling of x or y
Swapping x and y changes rNoThe formula is symmetric in x and y
A strong r proves x causes yNoA third variable, reverse causation or coincidence can produce the same r

How to use this visualizer

  1. 1
    In Generate & drag, set the target r and the number of points. The generator rescales a random cloud so the sample r matches the target exactly.
  2. 2
    Press New sample to draw a different cloud with the same r, and notice how varied the pictures for one value of r can be.
  3. 3
    Turn on the deviation rectangles. Each point draws a rectangle to the mean crosshair: green ones add to the covariance, red ones subtract.
  4. 4
    Drag one point into an empty corner, or press Add an outlier, and watch r change. Try it with n = 10 and again with n = 200.
  5. 5
    Open Anscombe's quartet: four datasets with the same r ≈ 0.82 and completely different shapes.
  6. 6
    Open Nonlinear: y = x² and slide the x range. A perfect curve gives r = 0 on a symmetric range and r near 1 on a one-sided range.

Correlation is not causation

Ice cream sales and drownings are positively correlated, but ice cream does not cause drowning: hot weather drives both. A strong r can come from a lurking third variable, from y causing x instead of the other way round, or from chance in a small sample. Only a controlled experiment or careful causal analysis can show that changing x changes y.

Practice problems

Problem 1: Compute r by hand

Find the covariance and the correlation r for the points (1, 2), (2, 4), (3, 5), (4, 4), (5, 5).
Show solution

x̄ = 15 / 5 = 3; ȳ = 20 / 5 = 4

x − x̄: −2, −1, 0, 1, 2;  y − ȳ: −2, 0, 1, 0, 1

Σ(x − x̄)(y − ȳ) = 4 + 0 + 0 + 0 + 2 = 6

Σ(x − x̄)² = 4 + 1 + 0 + 1 + 4 = 10;  Σ(y − ȳ)² = 4 + 0 + 1 + 0 + 1 = 6

cov = 6 / 4 = 1.5;  sₓ = √(10/4) ≈ 1.581;  sᵧ = √(6/4) ≈ 1.225

r = 1.5 / (1.581 × 1.225) = 6 / √60 ≈ 0.775

cov = 1.5, r ≈ 0.775 (a fairly strong positive linear association)

Problem 2: Zero correlation, perfect relationship

(a) Compute r for x = −2, −1, 0, 1, 2 with y = x². (b) In another study the correlation between height in cm and weight in kg is 0.6. What is r if height is converted to inches and weight to pounds? What if weight is replaced by 100 − weight?
Show solution

(a) y = 4, 1, 0, 1, 4; x̄ = 0, ȳ = 10 / 5 = 2

x − x̄: −2, −1, 0, 1, 2;  y − ȳ: 2, −1, −2, −1, 2

Σ products = −4 + 1 + 0 − 1 + 4 = 0, so r = 0

(b) Inches and pounds are positive multiples of cm and kg, so r stays 0.6

100 − weight multiplies the deviations by −1, so r flips sign

(a) r = 0 although y is fixed by x; (b) r = 0.6 after unit changes, r = −0.6 for 100 − weight

Related visualizations

Discussion

Questions, ideas or a different approach? Join the conversation.