Correlation
Generate scatter plots with a chosen correlation and see what r does and does not tell you
Scatter plot(drag any point; green products (x − x̄)(y − ȳ) push r up, red ones pull it down)
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
= 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
Covariance has units
r is unit-free
Straight lines only
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
| Claim | True? | Why |
|---|---|---|
| r = 0.9 means a strong straight-line pattern | Usually | Only if the scatter plot looks linear; check Anscombe II to IV |
| r = 0 means x and y are unrelated | No | y = x² on a symmetric range has r = 0 and a perfect relationship |
| r is the slope of the line | No | Slope = r × sᵧ / sₓ, so it depends on units; r does not |
| Changing units changes r | No | r is unchanged by any positive linear rescaling of x or y |
| Swapping x and y changes r | No | The formula is symmetric in x and y |
| A strong r proves x causes y | No | A third variable, reverse causation or coincidence can produce the same r |
How to use this visualizer
- 1In 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.
- 2Press New sample to draw a different cloud with the same r, and notice how varied the pictures for one value of r can be.
- 3Turn on the deviation rectangles. Each point draws a rectangle to the mean crosshair: green ones add to the covariance, red ones subtract.
- 4Drag 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.
- 5Open Anscombe's quartet: four datasets with the same r ≈ 0.82 and completely different shapes.
- 6Open 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
Show solutionHide 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
Show solutionHide 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.