Day 7: Spearman's Rank Correlation Coefficient

  • + 0 comments
    n = int(input())
    X = list(map(float, input().split()))
    Y = list(map(float, input().split()))
    
    X_sort = sorted(X)
    Y_sort = sorted(Y)
    
    rank_X = []
    rank_Y = []
    
    for i in range(n):
        for j in range(n):
            if X[i] == X_sort[j]:
                rank_X.append(j+1)
            if Y[i] == Y_sort[j]:
                rank_Y.append(j+1)
    
    d = 0
    for i in range(n):
        d += pow((rank_X[i] - rank_Y[i]), 2)
        
    Spearman_rank_coef = 1 - 6 * d / (n * (n * n - 1))
    print(round(Spearman_rank_coef, 3))