I tried to calculate Spearman's rank coefficient by hand.


enter image description here

enter image description here

But when I use python, it returns a different value.

rawdata = pd.DataFrame(
    columns=['Set of A','Set of B'])
correlation, pval = spearmanr(rawdata)
print(f'correlation={correlation:.6f}, p-value={pval:.6f}')

It returns:

correlation=0.585239, p-value=0.222365

What am I doing wrong here?


1 Answer 1


The formula you're using is a simplified version for when there are no ties. Try the full version of the formula:


Where $a_i$ and $b_i$ are the fractional ranks you've already found in your table

  • $\begingroup$ You seem to be writing about the Pearson correlation coefficient. The calculations presented in the question clearly deal with ties (in a standard manner). $\endgroup$
    – whuber
    Feb 14, 2020 at 18:09
  • $\begingroup$ I believe that Spearman correlation is equal to the Pearson correlation of the fractional ranks $\endgroup$
    – CFD
    Feb 14, 2020 at 18:11
  • 1
    $\begingroup$ Thank you for modifying and explaining your notation. Doing the calculation confirms your answer is a correct explanation of the discrepancy (+1). $\endgroup$
    – whuber
    Feb 14, 2020 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.