Calculate the Hierarchical Risk Parity (HRP) weights. The assets are clustered by their correlations, ordered so that similar assets sit next to each other, and the weight is split top-down between the two halves of every cluster in inverse proportion to their variance. It never inverts the covariance matrix, so it stays stable with many or highly correlated assets, where mean-variance weights become erratic.

As in the paper, the clustering uses the Euclidean distance between the columns of the correlation distance matrix √((1 - ρ) / 2): two assets are close when they relate to all the other assets in the same way. Some libraries cluster on the correlation distance itself, which can order the assets differently.

For more information, see the following paper:

  • López de Prado, M. (2016). “Building Diversified Portfolios that Outperform Out of Sample.” The Journal of Portfolio Management, 42(4), 59-69.

Also known as: HRP, hierarchical clustering portfolio.

Hierarchical Risk Parity in Python or via the MCP

Install the Finance Toolkit with:

pip install financetoolkit -U

Then call get_hierarchical_risk_parity_weights as shown below.

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_hierarchical_risk_parity_weights()

Or, after setting up the Finance Toolkit MCP, ask:

How would hierarchical risk parity weight Apple, Microsoft, Johnson & Johnson and ExxonMobil, based on 2021 to 2025?

Which returns:

  Weight Expected Return Volatility Sharpe Ratio Risk Contribution
AAPL 0.1287 0.1933 0.2786 0.5727 0.1599
MSFT 0.1511 0.2017 0.2572 0.6532 0.1498
JNJ 0.5206 0.0984 0.1673 0.3867 0.4742
XOM 0.1996 0.291 0.2702 0.9522 0.2161
Portfolio 1 0.1647 0.1374 0.9534 1

Parameters

get_hierarchical_risk_parity_weights accepts the following parameters.

ParameterTypeDescriptionExample
linkage_methodstrThe linkage of the hierarchical clustering: “single”, “complete”, “average” or “ward”. Defaults to “single”, as in the paper."single", as in the paper
periodstrThe frequency of the returns (daily, weekly, monthly, quarterly or yearly). Defaults to “daily”."weekly"
include_benchmarkboolWhether to include the benchmark as an asset. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the Toolkit’s rounding.2
Share