#436Spearman Rank CorrelationEasyStatisticsML System DesignEvaluation MetricsAsked atAnthropic · OpenAI · Google
Spearman Rank Correlation
Background
Spearman's rho is Pearson's correlation applied to ranks instead of raw values. It captures monotonic relationships and is the canonical calibration target for LLM-as-judge against human gold sets (production target ).
Problem statement
Implement spearman_corr(x, y). Replace each value by its 1-indexed rank (averaging ranks for ties), then apply Pearson:
Input
x,y- array-like of floats, same length .
Output
floatin .
Examples
Input: x=[1,2,3,4,5], y=[2,4,6,8,10]
Output: 1.0 # perfectly monotonic
Input: x=[1,2,3,4,5], y=[5,4,3,2,1]
Output: -1.0
Constraints
- Use average ranks for ties (the standard convention).
- Lengths must match and be . Raise ValueError otherwise.
Notes
- vs Pearson. Pearson is sensitive to outliers; Spearman is robust because it only uses ranks. Use Spearman whenever the relationship is monotonic but not linear.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example perfectly increasing gives one
- •Example perfectly decreasing gives minus one
- •Sample with ties averaged gives one