Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 · Correlation and Regression

The Pearson Correlation Coefficient

Compute Pearson's r step by step from deviations, interpret its size and sign, see how outliers affect it, and compare your calculation with NumPy.

The previous lesson introduced correlation as a number between -1 and +1. This lesson shows exactly where it comes from, so that you can compute it by hand, in code, and understand what it is really measuring.

The idea

For each point, measure how far x is from its mean and how far y is from its mean: dx = x - x̄, dy = y - ȳ. Multiply them. When both are above average, or both below, the product is positive. When one is above and the other below, it is negative. Add all the products. A large positive total means the two variables tend to move together.

To make the result independent of units, divide by the spread of each variable:

Text
r = Σ(dx · dy) / √( Σ(dx²) · Σ(dy²) )

A worked example

Five students record hours studied (x) and test score (y):

Student x (hours) y (score)
1 1 52
2 2 55
3 3 61
4 4 66
5 5 71

The means are x̄ = 3 and ȳ = 61.

x y dx dy dx·dy dx² dy²
1 52 -2 -9 18 4 81
2 55 -1 -6 6 1 36
3 61 0 0 0 0 0
4 66 1 5 5 1 25
5 71 2 10 20 4 100
Sum 49 10 242
Text
r = 49 / √(10 × 242) = 49 / √2420 ≈ 49 / 49.19 ≈ 0.996

A value this close to +1 indicates a very strong positive linear relationship.

Python Compute r from scratch and compare with NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Properties worth knowing

  • r has no units, and always satisfies -1 ≤ r ≤ 1.
  • r = +1 or -1 only if all points lie exactly on a straight line.
  • The sign shows direction; the size shows strength. A rough guide: below 0.3 weak, 0.3 to 0.7 moderate, above 0.7 strong. These cut-offs are conventions, not rules.
  • Swapping x and y gives the same r.
  • Changing units (cm to m, or Celsius to Fahrenheit) does not change r. A decreasing linear change of units flips only the sign.
  • r² is the share of the variation in y explained by a linear relationship with x. Here r² = 2401/2420 ≈ 0.992.

Correlation can be zero for a perfect pattern

Take x = -3, -2, ..., 3 and y = x². The points trace a perfect parabola, yet the positive and negative products cancel exactly, and r = 0.

Python A perfect curve with r = 0, and the effect of an outlier

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Spearman's rank correlation (a glimpse)

Pearson's r is sensitive to outliers and measures only linear association. A related measure, Spearman's rank correlation, first replaces each value by its rank and then applies the same formula. It detects any steadily increasing or decreasing relationship. You will meet it in more advanced courses.

Be careful with tiny samples. With 5 points, a high r can occur by chance. Report r together with the sample size and the plot.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which value of r indicates the strongest linear relationship?

    Strength depends on the size |r|. -0.9 is closest to 1 in size, and it is a strong negative relationship.
  2. 2. Hours studied and score have r = 0.8. What does r² = 0.64 mean?

    r² is the proportion of variation in y explained by the linear relationship with x. It does not prove causation.
  3. 3. If heights are converted from centimeters to meters, the correlation with weight...

    r is unit-free, so a positive linear rescaling leaves it unchanged.
  4. 4. Points follow a perfect parabola symmetric about the y-axis. What is r?

    The pattern is not linear and the products cancel, so r = 0.

Summary

  • r = Σ(dx·dy) / √(Σdx² · Σdy²), always between -1 and +1.
  • It is unit-free, symmetric in x and y, and measures only linear association.
  • r² is the proportion of variation explained linearly.
  • Outliers can change r a lot; always look at the scatter plot.

Next: The Least-Squares Regression Line