Skip to content
DeepForgeby svx
All problems
HardStatisticsst-400

Spearman Rho from Rank Differences

Problem

With no tied ranks, Spearman's rho is 1 - 6 * sum(d_i^2) / (n (n^2 - 1)), where d_i are the differences between the two rank vectors.

Given the rank differences and n >= 2, return rho.

Hint

Perfect agreement of ranks gives rho equal to one.

Test cases

case 1
in: [[0,0,0],3]
expected: 1
case 2
in: [[1,-1,0],3]
expected: 0.5
case 3
in: [[2,-1,-1],3]
expected: -0.5
Starter codePython
def spearman_rho_from_rank_differences(rank_diffs, n):
    # Your code here
    pass

3 test cases · run your code against them in the editor