Spearman Rank Correlation
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
[1,2,3,4], [1,2,3,4]1Spearman correlation is Pearson correlation applied to the ranks of the data, so it captures monotonic association even when the relationship is not linear.
[1,2,3,4], [4,3,2,1]-1Spearman correlation is Pearson correlation applied to the ranks of the data, so it captures monotonic association even when the relationship is not linear.
[1,2,3,4,5], [2,1,4,3,5]0.8Spearman 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.