All problems
Spearman Rank Correlation with Ties
Problem
Return the Spearman rank correlation between x and y, using average ranks for tied values and then the Pearson formula on the ranks. Return 0.0 when fewer than two pairs are given or either rank vector is constant.
Hint
Rank both lists with average ranks for ties, then correlate the ranks.
Test cases
case 1
in: [[1,2,3,4],[1,4,9,16]]
expected: 1
case 2
in: [[1,2,3],[3,2,1]]
expected: -1
case 3
in: [[1,2,2,3],[1,2,3,4]]
expected: 0.9486832980505138
case 4
in: [[5,5,5],[1,2,3]]
expected: 0
case 5
in: [[1,2,3,4],[2,1,4,3]]
expected: 0.6
Starter codePython
def spearman_correlation(x, y):
# Your code here
pass5 test cases · run your code against them in the editor