Spearman Rank Correlation
Descriptive Statistics
Unlock Monotonic Relationships That Pearson Misses
When your data is ranked, ordinal, or simply not linear, Pearson's r gives misleading answers. Spearman's ρ works on ranks instead of raw values, making it the go-to measure for non-parametric association.
Key things Spearman's rank correlation helps you understand:
- Ordinal data — Measure association in Likert scales, rankings, and satisfaction surveys where raw values lack meaning.
- Non-linear trends — Detect monotonic relationships that increase or decrease together but not at a constant rate.
- Outlier resilience — Because it uses ranks, extreme values have far less influence on the result.
When Pearson says "no relationship," Spearman often reveals the hidden monotonic connection.
What is Spearman Rank Correlation?
Definition
Spearman's ρ (rho) measures the monotonic relationship between two variables using ranks instead of raw values. It is the non-parametric alternative to Pearson's r.
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
np.random.seed(42)
# Monotonic but non-linear relationship
x = np.random.uniform(0, 10, 50)
y = np.exp(x / 5) + np.random.normal(0, 2, 50)
r_pearson, _ = stats.pearsonr(x, y)
r_spearman, _ = stats.spearmanr(x, y)
print(f"Pearson r = {r_pearson:.4f}")
print(f"Spearman ρ = {r_spearman:.4f}")
When to Use Spearman vs Pearson
| Feature | Pearson r | Spearman ρ |
|---|---|---|
| Relationship type | Linear | Monotonic |
| Data type | Continuous | Ordinal or continuous |
| Outlier sensitivity | High | Low |
| Normality required | Yes | No |
| Rank transformation | No | Yes |
# Example with ordinal data (ranked preferences)
preference_a = [1, 2, 3, 4, 5]
preference_b = [2, 1, 4, 3, 5]
rho, p_value = stats.spearmanr(preference_a, preference_b)
print(f"Spearman ρ = {rho:.4f}, p = {p_value:.4f}")
Tie Handling
# When data has tied ranks, Spearman uses a corrected formula
x = [1, 2, 2, 3, 4, 5]
y = [2, 3, 3, 4, 5, 6]
rho, p_value = stats.spearmanr(x, y)
print(f"With ties: ρ = {rho:.4f}, p = {p_value:.4f}")
Common Applications
- Ordinal data analysis — Likert scales, rankings, satisfaction surveys
- Non-linear monotonic relationships — exponential growth patterns
- Robust correlation — when outliers are present
- Medical research — pain scales, severity rankings
Spearman Rank Correlation in Machine Learning
| ML Application | Rank Correlation Usage | Why |
|---|---|---|
| Feature selection | Non-linear relationships | Pearson misses monotonic trends |
| Ordinal targets | Rank-based models | Ordinal regression |
| Robust correlation | Outlier-resistant | Better than Pearson with outliers |
| Ranking models | Learning to rank | Evaluate ranking quality |
import numpy as np
from scipy.stats import spearmanr, pearsonr
from sklearn.feature_selection import mutual_info_regression
np.random.seed(42)
n = 200
# Non-linear monotonic relationship
x = np.random.uniform(0, 10, n)
y = np.sin(x) + np.random.randn(n) * 0.3
pearson_r, _ = pearsonr(x, y)
spearman_r, _ = spearmanr(x, y)
print(f"Non-linear monotonic relationship:")
print(f"Pearson: r = {pearson_r:.3f} (underestimates)")
print(f"Spearman: ρ = {spearman_r:.3f} (captures monotonic trend)")
Key Takeaways
Spearman's ρ measures monotonic association — whether variables tend to move together in the same direction, regardless of linearity.
Computed on ranks — robust to outliers and non-normal distributions.
ρ = +1 means perfect monotonic increase; ρ = -1 means perfect monotonic decrease.
Use Spearman when data is ordinal, the relationship is non-linear but monotonic, or outliers are present. Use Pearson when the relationship is linear and assumptions are met.
"Ranks don't lie — they tell you what direction the data is moving, even when the relationship refuses to be linear."
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions