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(maximum_weight={"JNJ": 0.3})

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, with at most 30% in Johnson & Johnson?

Which returns:

  Weight Expected Return Volatility Sharpe Ratio Risk Contribution
AAPL 0.1287 0.1932 0.2786 0.5725 0.1307
MSFT 0.1511 0.2017 0.2571 0.6534 0.1184
JNJ 0.3 0.0987 0.1673 0.3884 0.1503
XOM 0.4201 0.2911 0.2702 0.9524 0.6006
Portfolio 1 0.2073 0.1569 1.1059 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
codependencestrThe correlation the assets are clustered on, through the distance √((1 - ρ) / 2): “pearson” (the linear correlation of the covariance matrix), “spearman” (the rank correlation of the returns, Spearman, 1904) or “kendall” (the rank correlation τ_b of the returns, Kendall, 1938). The rank correlations depend only on the order of the returns, so a few extreme returns weigh less. Defaults to “pearson”."pearson"
periodstrThe frequency of the returns (daily, weekly, monthly, quarterly or yearly). Defaults to “daily”."weekly"
covariancestrThe estimator of the covariance matrix: “sample” (ddof=1), “ledoit_wolf” (towards the scaled identity, Ledoit and Wolf, 2004), “ledoit_wolf_constant_correlation” (towards the average correlation, Ledoit and Wolf, 2004), “ledoit_wolf_single_factor” (towards the covariance that the benchmark as a single factor implies, Ledoit and Wolf, 2003), “oas” (Chen et al., 2010), “semicovariance” (the co-movement below a zero return, Estrada, 2007), “exponential” (recent periods weigh most, RiskMetrics) or “denoised” (the eigenvalues of the correlation matrix that random returns could produce averaged, Laloux et al., 2000). The shrinkage and denoised estimators stabilize the weights when there are many assets compared to the number of periods. Defaults to “sample”."sample"
covariance_spanintThe span in periods of the “exponential” covariance; the weight of a period halves after about span / 2.9 periods. Defaults to 180.180
minimum_weightfloat | dict[str, float]The lowest weight of every asset, or per asset, e.g. {“AAPL”: 0.05} (the others keep 0); it cannot be negative. Defaults to 0.0
maximum_weightfloat | dict[str, float]The highest weight of every asset, or per asset, e.g. {“AAPL”: 0.3} (the others keep 1). Defaults to 1.1
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