Day 7: Spearman's Rank Correlation Coefficient

  • + 0 comments
    n = int(input())
    a = list(map(float, input().split()))
    b = list(map(float, input().split()))
    ranks = lambda x : [sorted(x).index(i)+1 for i in x]
    spear = lambda n,a,b : 1-6*sum([(i[0]-i[1])**2 for i in list(zip(ranks(a),ranks(b)))]) / (n*(n**2-1))
    print(round(spear(n,a,b),3))