Skip to content

Data Science

Correlation (Pearson)

This takes O(n) time — a single pass to gather the sums the formula needs.

The idea, in plain English

Correlation answers one question: when one thing goes up, does the other tend to go up too? Picture two friends' moods through the week. If they are cheerful or grumpy on the same days, that is a strong positive correlation. Pearson's correlation turns that relationship into a single number. A value of -1 means perfectly opposite. A value of 0 means no relationship at all. A value of +1 means perfectly together.

How it works

  1. 1Find how far each x-value is from the average x, and each y-value is from the average y.
  2. 2Multiply those two differences together for each pair, then add them all up. This rewards pairs that move together and penalizes pairs that move oppositely.
  3. 3Divide that sum by a measure of how spread out x and y each are on their own. This squeezes the final result into the -1 to +1 range.

When you'd use it

Use this to check whether two measurements are related before assuming one causes the other. For example, ice cream sales and temperature are correlated, but neither directly causes the other; both follow from summer heat.

Common beginner mistakes

  • Do not treat correlation as proof of causation. Two things can move together without one causing the other.
  • Do not assume a correlation near 0 means 'no relationship'. Pearson only catches straight-line relationships, and can miss a strong curved one.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
X: 1 2 3 4 5
Y: 4 3 5 7 6
Correlation: 0.80

Not sure this is the right topic? See the learning paths → or where this leads →