Spearman’s Rank Correlation Coefficient

Maths: Statistics for machine learning

2 min read

Published Oct 22 2025, updated Oct 23 2025


40
0
0
0

Machine LearningMathsNumPyPandasPythonStatistics

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.

Spearmans Formula

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 np
from scipy.stats import spearmanr
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# Example data: non-linear monotonic relationship
x = np.arange(1, 11)
y = np.log(x) * 10 + np.random.normal(0, 0.5, 10) # log relationship (non-linear but monotonic)

# Spearman's correlation
rho, 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.")

# Visualize
df = 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.982
P-value: 0.0000
Reject 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)

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact