What this calculator does
A correlation coefficient is a single number between −1 and +1 that describes how closely two variables move together. Positive values mean they rise together, negative values mean one falls as the other rises, and values near zero mean there is no consistent pattern.
The calculator returns two coefficients. Pearson’s r measures how well the points follow a straight line. Spearman’s ρ (rho) measures how consistently the relationship goes in one direction, whether or not it is straight. For each one you get a t-test of whether the true correlation could be zero. For r you also get a confidence interval.
Pearson or Spearman: which one to report
- Use Pearson for two continuous measurements where you expect a straight-line relationship, such as height and weight or ad spend and clicks.
- Use Spearman when the relationship curves but keeps the same direction, when a few extreme values could distort the result, or when the data is ordinal, such as survey ratings or class ranks.
- When the two disagree, look at the scatter plot. A Spearman value well above Pearson usually means a curved but steady relationship. A Pearson value well above Spearman usually means one or two extreme points are creating most of the correlation.
The formulas
Pearson’s r divides how much X and Y vary together by how much each varies on its own:
Sxy = Σ(x − x̄)(y − ȳ) · Sxx = Σ(x − x̄)² · Syy = Σ(y − ȳ)²
Spearman’s ρ replaces every value with its rank (tied values share the average of their ranks) and then applies the same formula to the ranks. When there are no ties this reduces to the shortcut you may have seen in textbooks, where d is the difference between the two ranks of each pair:
Both coefficients are tested with t = r√(n − 2) / √(1 − r²) on n − 2 degrees of freedom. The confidence interval for Pearson’s r uses Fisher’s z transformation, z = artanh(r), whose standard error is 1 / √(n − 3).
Worked example: experience and salary
Press Load example to use this data: years of experience and annual salary in ₹ LPA for 12 data analysts in Bengaluru, from freshers at ₹3.5 LPA to a 15-year analytics lead at ₹42 LPA.
- Pearson r = 0.9396 (p < 0.0001, 95% CI 0.7937 to 0.9833). Strong, and squaring it gives R² = 0.8829.
- Spearman ρ = 0.9983 (p < 0.0001). Almost perfect: every extra year of experience comes with a higher salary.
Why the gap? Salary does not rise in a straight line. It climbs slowly in the early years and much faster later, so the points bend upward and a straight line misses the top end. Spearman only asks whether the order matches, which it almost exactly does. If you take the log of salary first, the curve straightens and Pearson’s r rises to 0.9990. That is the practical signal: a large gap between the two coefficients tells you a transformation or a non-linear model may suit the data better than a straight line.
Reading the result
- Sign gives the direction. Size gives the strength. A correlation of −0.8 is just as strong as +0.8.
- The p-value tests whether the true correlation could be zero. With a large sample, even a tiny correlation such as 0.05 can be significant without being useful.
- The confidence interval is often more informative than the p-value. An r of 0.6 with an interval of 0.1 to 0.85 says the data is consistent with anything from weak to very strong.
- Correlation is not causation. Ice cream sales and AC sales in Chennai rise together because of summer, not because one drives the other. See causal inference vs correlation for how analysts separate the two.
Getting the same result in Excel and Python
In Excel, =CORREL(A2:A13, B2:B13) returns Pearson’s r. For Spearman, rank each column
with =RANK.AVG(A2, $A$2:$A$13, 1) in helper columns and run CORREL on the ranks.
In Python:
from scipy import stats
experience = [0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15]
salary_lpa = [3.5, 4.2, 4.0, 5.1, 6.0, 7.2, 8.5, 9.8, 13, 18, 24, 42]
pr = stats.pearsonr(experience, salary_lpa)
print(pr.statistic, pr.pvalue, pr.confidence_interval()) # 0.9396, 5.7e-06, (0.794, 0.983)
sr = stats.spearmanr(experience, salary_lpa)
print(sr.statistic, sr.pvalue) # 0.9983, 1.3e-13
Common mistakes
- Reading r = 0 as “no relationship”. A U-shaped pattern, such as stress against performance, can have r close to zero while the relationship is strong. Always look at the scatter plot.
- Mixing groups. Combining data from two cities or two product lines can create a correlation that exists in neither group on its own, or hide one that does.
- Correlating totals that share a component. Revenue and profit, or total marks and one subject’s marks, correlate partly because one contains the other.