Correlation coefficients compress the relationship between two variables into a single number between −1 and +1, and they are among the most-used tools in an analyst's kit. But there are two coefficients you will meet constantly — Pearson and Spearman — and using the wrong one produces misleading conclusions. The difference comes down to a simple question: are you measuring a straight-line relationship, or just a consistently-increasing one?
This page defines both coefficients, computes them by hand on the same small dataset so you can see exactly how they differ, and gives clear guidance on which to reach for. It builds on the judgment covered in correlation versus causation.
What each coefficient measures
Pearson's r measures linear association. It is high (near +1 or −1) only when the data points fall close to a straight line. Its formula compares how the two variables deviate from their means:
r = sum( (x - mean_x)(y - mean_y) ) / sqrt( sum((x - mean_x)^2) * sum((y - mean_y)^2) )
Spearman's rho measures monotonic association — whether y consistently increases (or decreases) as x increases, regardless of whether the increase is linear. It is computed by converting each variable to ranks and then applying the Pearson formula to those ranks. Because it uses ranks, it ignores the exact spacing of values and cares only about order.
Both range from −1 (perfect negative) through 0 (none) to +1 (perfect positive). The sign is direction; the magnitude is strength.
A worked example computing both
Take a small, illustrative sample where x and y both rise together, but not along a perfectly straight line.
import numpy as np
from scipy.stats import pearsonr, spearmanr
x = np.array([10, 20, 30, 40, 50])
y = np.array([12, 25, 31, 52, 60])
print("Pearson: ", round(pearsonr(x, y)[0], 3)) # 0.987
print("Spearman:", round(spearmanr(x, y)[0], 3)) # 1.0
Let us verify Pearson by hand. The mean of x is 30 and the mean of y is (12+25+31+52+60)/5 = 180/5 = 36. The deviations are:
x-30: -20 -10 0 10 20
y-36: -24 -11 -5 16 24
The numerator, sum of the products, is (−20)(−24) + (−10)(−11) + 0 + (10)(16) + (20)(24) = 480 + 110 + 0 + 160 + 480 = 1230. The sum of squared x-deviations is 400+100+0+100+400 = 1000, and of y-deviations is 576+121+25+256+576 = 1554. So r = 1230 / √(1000 × 1554) = 1230 / √1,554,000 ≈ 1230 / 1246.6 ≈ 0.987.
Now Spearman. Rank x: it is already increasing, so its ranks are 1, 2, 3, 4, 5. Rank y: 12, 25, 31, 52, 60 is also strictly increasing, so its ranks are also 1, 2, 3, 4, 5. The ranks match perfectly, so Spearman = 1.0.
The takeaway: Spearman is a perfect 1.0 because y increases every single time x does — a flawless monotonic relationship. Pearson is 0.987, slightly below 1.0, because the points do not lie on an exactly straight line. Whenever a relationship is monotonic but curved, Spearman reads higher than Pearson.
When to use which
Reach for Pearson when:
- The relationship looks genuinely linear on a scatter plot.
- The variables are continuous and roughly normal.
- There are no extreme outliers (Pearson is sensitive to them).
Reach for Spearman when:
- The data is ordinal (rankings, Likert scales, ratings).
- The relationship is monotonic but curved (exponential, logarithmic growth).
- Outliers are present, since ranks blunt their influence.
A single wild outlier can drag Pearson's r dramatically while barely moving Spearman, because in rank space an outlier is just "the largest," no more extreme than any other top value. When in doubt, plot the scatter first and compute both.
How analysts use correlation
Correlation is the fast screen for relationships. Before modeling, analysts compute a correlation matrix across features to spot which variables move together, flag redundant predictors, and generate hypotheses. In reporting, a correlation quantifies claims like "sessions and revenue are strongly linked (r = 0.8)." The choice between Pearson and Spearman signals sophistication: reaching for Spearman on survey ratings or skewed metrics shows you understand the assumptions.
Always pair the number with a scatter plot. A famous illustration, Anscombe's quartet, shows four datasets with identical Pearson correlations but wildly different shapes — proof that the coefficient alone can hide the real story.
Common mistakes
Using Pearson on ordinal or ranked data. Survey responses like "1 to 5 satisfaction" are ranks, not evenly spaced measurements, so Spearman is more appropriate.
Trusting the number without plotting. A Pearson r near 0 can hide a strong U-shaped relationship, and a high r can be produced by a single outlier. The scatter plot is not optional.
Letting outliers drive Pearson. One extreme point can create or destroy a Pearson correlation. If outliers are real and present, prefer Spearman or investigate the points directly.
Reading correlation as causation. A strong coefficient says the variables move together, not that one causes the other. See correlation versus causation for why the leap is dangerous.
In interviews
Expect "What is the difference between Pearson and Spearman, and when would you use each?" The crisp answer: Pearson for linear relationships on well-behaved numeric data, Spearman for monotonic or ordinal data and when outliers are present, because it works on ranks. A common follow-up is "What does a correlation of 0 mean?" — no linear (or monotonic) relationship, but possibly a strong non-monotonic one, so plot it. Interviewers may show a scatter and ask which coefficient fits. Mentioning Pearson's sensitivity to outliers earns credit.
Where this fits in your learning path
Measuring correlation is the mechanical companion to the judgment in correlation versus causation, and it draws on the deviation-from-the-mean idea behind variance and standard deviation and the summaries in descriptive statistics. Correlation analysis is a core skill in the data analytics learning path and appears throughout the feature-analysis work described in the data analyst roadmap.
Frequently Asked Questions
What is the difference between Pearson and Spearman correlation?
When should I use Spearman instead of Pearson?
What does a correlation coefficient of 0 mean?
Can correlation be negative?
Does a high correlation prove one variable causes the other?
Want to Build Your Career in Data Analytics with AI?
Join CodeBegun and train with working industry engineers — View the Data Analytics curriculum

