Spearman Rank Correlation

MediumStatisticsMathArray

Description

Given two arrays x and y of equal length, return the Spearman rank correlation coefficient: the Pearson correlation computed on the ranks of the values (tied values receive their average rank). Round the result to 4 decimal places.

Examples

Input:[1,2,3,4], [1,2,3,4]
Output:1
Explanation:

Spearman correlation is Pearson correlation applied to the ranks of the data, so it captures monotonic association even when the relationship is not linear.

Input:[1,2,3,4], [4,3,2,1]
Output:-1
Explanation:

Spearman correlation is Pearson correlation applied to the ranks of the data, so it captures monotonic association even when the relationship is not linear.

Input:[1,2,3,4,5], [2,1,4,3,5]
Output:0.8
Explanation:

Spearman correlation is Pearson correlation applied to the ranks of the data, so it captures monotonic association even when the relationship is not linear.

Constraints

  • 2 ≤ length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.