Spearman Rank Correlation Calculator

Spearman's rho with ties and exact p-values.

  • Average ranks for ties
  • Exact p for n up to 9
  • Pearson beside it
Two rows of soft clay posts of different heights in a small rounded garden, joined by thin clay strands running between matching places in the two rows.

The guide

A rank correlation only asks who finished ahead of whom, and ties and small samples change the answer

Spearman's rho replaces every value with its place in line, which makes it blind to the shape of a rising curve, but ties, shortcut formulas and p-values need care.

  • 0.920034 rho for the eight tied pairs, ranks averaged
  • 0.922619 what the no-ties shortcut wrongly gives for them
  • 0.007242 exact two-sided p for eight pairs without ties
  • 0.798837 Pearson r for x = 1 to 10 against 2 to the power x
  • -29/165 rho for the ten-person worked example
Read the full guide Fold the guide 7 min · 16 sources

Imagine two judges scoring the same eight performances, one out of ten and the other out of a hundred. Their numbers cannot be compared directly, yet nobody doubts that they might agree. If the first judge’s favourite is also the second judge’s favourite, and the runner-up matches too, the judges agree about the order. Spearman’s rank correlation measures exactly that agreement, and nothing else.

The method is a small trick with a long reach. Throw away the values, keep only each value’s place in line, and then ask how closely the two lines match. The Wikipedia entry on the coefficient puts the definition in one sentence: it is the Pearson correlation computed on the rank variables. Everything else, the formulas, the tables, the tests, follows from that choice.

What ranks keep and what they discard

Ranks keep order and discard distance. A value of 1 and a value of 1,000 sit one place apart if nothing lies between them, and that is all the method knows. The consequence is that any steadily rising relation, straight or bent, looks the same. The Wikipedia page on monotonic functions describes the shape: a function that never decreases, or never increases, so that it preserves order. A perfect Spearman correlation of 1 appears whenever one variable is a monotonic function of the other.

Pearson’s r behaves differently. It measures how well a straight line describes the data, and it reaches 1 only when every point lies on one. Take x from 1 to 10 and y equal to 2 raised to the power x. Every increase in x brings an increase in y, so the ranks match exactly and rho is exactly 1. Pearson’s r is 0.798837, because the doubling curve is far from a line. Seeing both numbers side by side tells you something neither gives alone: the relation is steady but bent.

The same blindness protects against a different problem. The Wikipedia entry notes that rho is less sensitive than Pearson’s r to strong outliers in the tails of both samples, because an outlier is limited to the value of its rank. One wild measurement can drag r a long way. It can drag rho only as far as the end of the line.

Neither number replaces a picture. Anscombe’s quartet, built in 1973, is four small data sets with nearly identical simple statistics that look very different when graphed. A summary number hides the shape, so the page draws the ranks against each other and marks the line where the two ranks are equal.

How ties are ranked

Real data repeat themselves. Two students score 74, three plots yield the same weight. A ranking needs a rule for what place those equal values take. The common rule, called fractional ranking on the Wikipedia page for ranking, gives each equal value the mean of the positions it would have taken. If one item leads two equal items that lead a fourth, the leader is 1, the pair is 2.5 each, and the last is 4.

Try the sample 1, 2, 2, 3, 4, 4, 4, 5. The two 2s span positions 2 and 3, so each gets 2.5. The three 4s span positions 5, 6 and 7, so each gets 6. The ranks are 1, 2.5, 2.5, 4, 6, 6, 6 and 8. The Wikipedia page notes a useful property of the rule: the sum of the ranking numbers is the same as under plain sequential ranking, so the average rank does not move. SciPy’s rankdata function uses this average method by default, and Python’s statistics module, with its ranked option, says that ties are averaged so that equal values receive the same rank.

A tempting alternative is to number tied values 1, 2, 3 in the order they happen to appear. That quietly invents an order that the data do not contain, and the answer then depends on which row you typed first. Pair the sample above with 2, 1, 3, 3, 5, 4, 6, 8. With averaged ranks, rho is 0.920034. Numbering the tied x values in order of appearance, and leaving y averaged, moves it to 0.946125. The move is the work of a ranking rule that has no basis in the numbers.

Why the shortcut formula fails with ties

Many courses teach one formula: rho equals 1 minus 6 times the sum of the squared rank differences, divided by n times n squared minus 1. It is fast, and for data with no ties it is exactly right. The Wikipedia derivation shows where the condition comes from. It treats each rank column as the whole numbers 1 to n, each used once, so the variance of the ranks is n squared minus 1, over 12. Equal values break that assumption.

The page states it flatly: if ties are present, the simplified formula yields incorrect results, and the Pearson correlation of the ranks should be used instead. Apply the shortcut to the tied sample above and it returns 0.922619, close enough to look believable and different from the true 0.920034. A close wrong answer is more dangerous than a far one.

The Wikipedia worked example shows the shortcut in its proper setting. Ten people have an arbitrary score and a count of weekly hours. The squared rank differences add to 194, so rho is 1 minus 6 times 194, over 10 times 99, which is exactly -29/165, or -0.175757. Because there are no ties, the shortcut and the Pearson correlation of the ranks agree to the last digit.

Exact arithmetic helps here. Ranks with halves become whole numbers when doubled, and rho squared then becomes a ratio of two integers that can be reduced and displayed without rounding. When both parts are perfect squares, rho itself is a fraction, as with -29/165.

Where the p-value comes from

A coefficient from a small sample always raises the question whether it could be luck. The usual answer is a t statistic: rho times the square root of n minus 2 over 1 minus rho squared, compared with Student’s t distribution with n minus 2 degrees of freedom. The Wikipedia entry says this statistic is distributed approximately that way under the null hypothesis, and that the justification relies on a permutation argument. The word approximately matters.

The two-sided p-value is the area in both tails, and Wikipedia’s page on the t distribution writes its cumulative distribution in terms of the regularised incomplete beta function. For this test the argument of that function reduces to 1 minus rho squared. The NIST Digital Library of Mathematical Functions gives a continued fraction for the incomplete beta function, which converges quickly on one side of a stated point and is reflected across it on the other. The page uses that fraction, and a Stirling series from the same library for the logarithm of the gamma function.

How good is the approximation? SciPy’s documentation for spearmanr says the p-value does not make strong assumptions about the distributions behind the samples, but it is accurate only for very large samples, over 500 observations, and it advises a permutation test for smaller ones. For the ten-person example the t-based p-value is 0.627188, the figure printed on the Wikipedia page, and a p-value that large is nowhere near any usual threshold, so the approximation cannot change the verdict there. For a small sample whose p-value sits near a threshold, it can.

A permutation test replaces the approximation with counting. Wikipedia describes it as an exact test: if the labels are exchangeable under the null hypothesis, the distribution of the statistic comes from computing it for every possible rearrangement. For rank correlation, shuffling the order of the second column is the rearrangement, and the number of orderings is n factorial. SciPy’s permutation_test makes the same point, computing the exact distribution when the number of resamples requested reaches the number of distinct permutations, and warning that exact tests are feasible only for very small data sets.

Take x from 1 to 8 against 2, 1, 4, 3, 7, 5, 6, 8. The sum of squared rank differences is 10. Of the 40,320 orderings, 146 give 10 or less, and by symmetry another 146 sit equally far on the other side, so 292 are as extreme and the exact two-sided p is 0.007242. The t approximation for the same data gives about 0.00385, roughly half. At eight pairs the approximation is optimistic by a wide margin, and neither number was visible without computing both.

What a strong rank correlation does not say

Suppose rho comes out at 0.9 with a tiny p-value. The honest reading is narrow: in these data, the two orderings agree closely, and that agreement would be rare if the orderings were unrelated. It says nothing about why. Wikipedia’s page on correlation and causation lists the usual alternatives: A causes B, B causes A, a third factor causes both, or the relationship is coincidental. A rank correlation cannot tell them apart, since each produces the same agreement of order.

One further limit is worth keeping in mind. The Wikipedia entry describes rho as assessing how well a relationship can be described by a monotonic function. A relation that rises and then falls, a hill or a valley, is not described well by one, so it can return a rho near zero while the two variables are tightly bound.

The calculator, then, is built to show its work. It reports the pairs it dropped and why, the rank table with ties marked, rho beside Pearson’s r, both p-values when both exist, and a plain statement when the shortcut would mislead. Heavy counting runs in a background thread, in the sense the MDN page on web workers describes, so the page stays responsive, and numbers are grouped with the browser’s own number formatting. None of this decides the question of cause. It makes sure the question being asked of the data is the one the numbers actually answer.

Sources

Every source consulted for this guide. Links checked November 10, 2026.

  1. Spearman's rank correlation coefficient Wikipedia
  2. Pearson correlation coefficient Wikipedia
  3. Ranking Wikipedia
  4. Monotonic function Wikipedia
  5. Permutation test Wikipedia
  6. Student's t-distribution Wikipedia
  7. Correlation does not imply causation Wikipedia
  8. Anscombe's quartet Wikipedia
  9. scipy.stats.spearmanr SciPy documentation
  10. scipy.stats.rankdata SciPy documentation
  11. scipy.stats.permutation_test SciPy documentation
  12. statistics - Mathematical statistics functions Python Software Foundation
  13. Incomplete Beta Functions (DLMF 8.17) NIST Digital Library of Mathematical Functions
  14. Asymptotic Expansions (DLMF 5.11) NIST Digital Library of Mathematical Functions
  15. Web Workers API MDN Web Docs
  16. Intl.NumberFormat MDN Web Docs