Distance Correlation

The Distance Correlation measures how much two assets depend on each other, linearly or not: it is zero only when their returns are independent (Székely, Rizzo and Bakirov, 2007), so it also captures the co-movement the Pearson Correlation misses.

The formula for the Distance Correlation starts from the doubly centred distances \(A_{s , t}\) between the returns of one asset in the periods s and t, the distances \(a_{s , t}\) = abs(x_s - \(x_{t}\)) minus their row and column averages plus their overall average, and \(B_{s , t}\) for the other asset:

\[V_{x,y} = \frac{\sum_{s=1}^{T} \sum_{t=1}^{T} A_{s,t} \cdot B_{s,t}}{T^{2}}\]

The distance correlation \(R_{x , y}\) scales the squared distance covariance \(V_{x , y}\) by those of each asset with itself, and the distance \(d_{x , y}\) follows from it:

\[R_{x,y} = \sqrt{\frac{V_{x,y}}{\sqrt{V_{x,x} \cdot V_{y,y}}}}\] \[d_{x,y} = \sqrt{1 - R_{x,y}}\]

It lies between 0 and 1 and does not show the direction of the dependence: assets that move against each other also count as dependent.

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: dCor, Brownian correlation.

In the Finance Toolkit this is get_codependence_matrix with method="distance", 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="distance")

Which returns:

References

  • Paper Székely, G. J., Rizzo, M. L. and Bakirov, N. K. (2007). “Measuring and Testing Dependence by Correlation of Distances.” The Annals of Statistics, 35(6), 2769-2794.
  • Library Referenced against skfolio’s DistanceCorrelation (Distance Correlation)
  • Library Shares similar logic as skfolio’s DistanceCorrelation (Distance Correlation)
  • Library Shares similar logic as Riskfolio-Lib’s dcorr_matrix (Distance Correlation)
Share