Pearson's measures how closely points follow a straight line. Sometimes that is the wrong question. If every extra hour of practice raises a score, but by less each time, the relationship is perfectly "more of one means more of the other", yet the points bend away from any straight line and Pearson's comes out below 1. And a single extreme value can drag Pearson's a long way.
Spearman's rank correlation keeps only the order of the values and throws away their sizes:
x by its rank within x: the smallest value gets rank 1, the next smallest rank 2, and so on. Rank y the same way, separately.where and are now the ranks of and .
Ties. Equal values share the average of the ranks they would occupy. In [40, 10, 40, 25] the two 40s would take ranks 3 and 4, so each gets 3.5, and the ranks are [3.5, 1, 3.5, 2].
Task: write spearman(x, y) that returns Spearman's rank correlation rounded to 4 decimal places.
x and y have the same length , and position in both lists describes the same item.