Spearman’s Rank Correlation Coefficient
Maths: Statistics for machine learning
2 min read
Published Oct 22 2025, updated Aug 17 2026
Guide Sections
Guide Comments
Spearman’s correlation measures the strength and direction of a monotonic relationship between two variables.
It’s a non-parametric test — meaning it doesn’t assume normality or linearity.
In simple terms:
“Spearman’s correlation checks whether two variables tend to increase or decrease together — even if the relationship isn’t perfectly straight.”
The Formula
Spearman’s ρ is computed using the ranks of the data rather than their raw values.

Where:
- di = difference between the ranks of each pair (xᵢ and yᵢ)
- n = number of observations
When to Use Spearman’s vs Pearson’s
- Data are continuous and linear - use Pearson’s r
- Data are ordinal (ranked) - use Spearman’s ρ
- Relationship is non-linear but monotonic - use Spearman’s ρ
- Data contain outliers or are not normal - use Spearman’s ρ
Interpretation of ρ
ρ value | Relationship | Description |
+1.0 | Perfect positive monotonic | As X increases, Y always increases |
+0.7 to +0.9 | Strong positive | High ranks of X → high ranks of Y |
0 | No relationship | No monotonic pattern |
–0.7 to –0.9 | Strong negative | High X → low Y |
–1.0 | Perfect negative monotonic | As X increases, Y always decreases |
Hypothesis Testing
- H₀ (Null Hypothesis) - No correlation between the two variables (ρ = 0)
- H₁ (Alternative Hypothesis) - There is a significant correlation (ρ ≠ 0)
If p ≤ 0.05, reject H₀ → significant monotonic correlation.
If p > 0.05, fail to reject H₀ → no significant correlation.
Example in Python
Let’s see how Spearman’s correlation performs on a non-linear but monotonic dataset.
import numpy as npfrom scipy.stats import spearmanrimport seaborn as snsimport matplotlib.pyplot as pltimport pandas as pd# Example data: non-linear monotonic relationshipx = np.arange(1, 11)y = np.log(x) * 10 + np.random.normal(0, 0.5, 10) # log relationship (non-linear but monotonic)# Spearman's correlationrho, p = spearmanr(x, y)print(f"Spearman's rho: {rho:.3f}")print(f"P-value: {p:.4f}")if p < 0.05: print("Reject H₀ — significant monotonic correlation.")else: print("Fail to reject H₀ — no significant correlation.")# Visualizedf = pd.DataFrame({'X': x, 'Y': y})sns.scatterplot(data=df, x='X', y='Y')sns.lineplot(x='X', y=np.log(x)*10, color='red', label='Underlying Trend')plt.title(f"Spearman's Correlation: ρ = {rho:.3f}, p = {p:.4f}")plt.show()Example Output:
Spearman's rho: 0.982P-value: 0.0000Reject H₀ — strong monotonic relationship.Even though the relationship isn’t linear (log-shaped), Spearman detects a strong correlation while Pearson’s r might underestimate it.
Pearson vs Spearman — Key Differences
Feature | Pearson’s r | Spearman’s ρ |
Relationship type | Linear | Monotonic (increasing/decreasing) |
Data type | Continuous | Ordinal or continuous |
Assumes normality? | Yes | No |
Sensitive to outliers? | Yes | No |
Uses | Raw values | Ranked values |
Test type | Parametric | Non-parametric |
Assumptions
- Variables are ordinal, interval, or ratio - Not categorical
- Relationship is monotonic - Always increasing or decreasing
- Data have no strong ties - (Spearman can still handle them with corrections)