Spearman Rank Correlation

The Spearman Rank Correlation is the Pearson correlation of the ranks of the returns (Spearman, 1904): it captures any co-movement in which one asset rises when the other does, linear or not, and the size of an extreme return does not matter beyond its rank.

The formula for the Spearman Rank Correlation is the correlation of the ranks \(R_{i , t}\) of the returns of the two assets over the T periods, which without ties equals the form with the differences of the ranks; the distance \(d_{i , j}\) is the correlation distance:

\[\rho_{i,j} = \operatorname{corr}\left(R_{i}, R_{j}\right)\] \[\rho_{i,j} = 1 - \frac{6 \cdot \sum_{t=1}^{T} (R_{i,t} - R_{j,t})^{2}}{T \cdot \left(T^{2} - 1\right)}\] \[d_{i,j} = \sqrt{\frac{1 - \rho_{i,j}}{2}}\]

It lies between -1 and 1 like the Pearson Correlation, and the two differ most when a few large returns dominate the latter.

It is the codependence that the hierarchical optimizers (HRP, HERC, NCO) and get_asset_clusters take, or the distance they cluster on. Hierarchical portfolio construction groups the assets by how much they move together and spreads the capital across the groups, so the codependence decides which assets end up together. The Pearson correlation captures only linear co-movement on average; the other codependences capture co-movement in ranks, in large moves only, in any form of dependence or in crashes. Each comes with a distance, zero for assets that move as one, that the clustering works on (Mantegna, 1999).

Also known as: Spearman’s rho, rank correlation.

In the Finance Toolkit this is get_codependence_matrix with method="spearman", the same function as Codependence Matrix.

Example · AAPL, MSFT, JNJ, XOM · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_codependence_matrix:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT", "JNJ", "XOM"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.portfolio.get_codependence_matrix(method="spearman")

Which returns:

References

  • Paper Spearman, C. (1904). “The Proof and Measurement of Association between Two Things.” The American Journal of Psychology, 15(1), 72-101.
Share