Cluster-based portfolio allocation: HRP, Schur risk parity, and 1/N
Project description
pyhrp
A recursive implementation of the Hierarchical Risk Parity (HRP) approach by Marcos Lopez de Prado. We take advantage of the scipy.cluster.hierarchy package.
Motivation
Mean-variance optimisation is often unstable in practice because small estimation errors in expected returns can lead to large and concentrated weight shifts. Hierarchical Risk Parity avoids explicit return forecasting and instead allocates risk recursively along a clustering tree built from asset co-movement. By grouping correlated assets before sizing positions, HRP tends to distribute risk across more independent sources, which can improve diversification. In short, HRP keeps the intuition of risk budgeting while adding structure from correlation-based clustering.
Method comparison
The method argument controls how the first clustering tree is built:
| Linkage method | When to use it |
|---|---|
ward |
Default choice when you want compact, variance-minimizing clusters and generally stable, balanced trees. |
single |
Useful when preserving nearest-neighbour chains matters (can create long, unbalanced trees on noisy data). |
average |
Middle ground between single and complete when you want moderate sensitivity to pairwise distances. |
complete |
Prefer when you want tighter, diameter-controlled clusters and to avoid chaining effects from single. |
Setting bisection=True keeps the leaf order induced by the chosen linkage
method, then rebuilds the tree by repeatedly splitting that ordered list in half.
This often produces a more balanced hierarchy than the raw linkage tree and
matches the bisection-style construction discussed in HRP literature.
Here's a simple example
import polars as pl
from pyhrp.hrp import build_tree, compute_cov, compute_corr
from pyhrp.algos import risk_parity
prices = pl.read_csv("tests/resources/stock_prices.csv", try_parse_dates=True).drop("date")
returns = prices.select(pl.all().pct_change()).drop_nulls().fill_null(0.0)
cov = compute_cov(returns)
cor = compute_corr(returns)
# Compute the dendrogram based on the correlation matrix and Ward's metric
dendrogram = build_tree(cor, method='ward')
dendrogram.plot()
# Compute the weights on the dendrogram
root = risk_parity(root=dendrogram.root, cov=cov)
root.portfolio.plot(names=dendrogram.names)
For your convenience you can bypass the construction of the covariance and correlation matrix, and the construction of the dendrogram.
from pyhrp.hrp import hrp
root = hrp(prices=prices, method="ward", bisection=False)
Interpreting results
The hrp() function returns a Cluster node (the tree root), not a plain weight
series. You can navigate the hierarchy directly via root.left and root.right
to inspect how the recursive allocation split risk at each branch. To get a flat
asset-to-weight mapping for downstream use, access root.portfolio.weights.
weights = root.portfolio.weights
variance = root.portfolio.variance(cov)
# You can drill deeper into the tree
left = root.left
right = root.right
The comparison image above is generated from code in
book/marimo/demo.py. Regenerate it with:
uv run --with kaleido book/marimo/demo.py
uv
Starting with
make install
will install uv and create the virtual environment defined in pyproject.toml and locked in uv.lock.
marimo
We install marimo on the fly within the aforementioned virtual environment. Executing
make marimo
will install and start marimo.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyhrp-2.2.5.tar.gz.
File metadata
- Download URL: pyhrp-2.2.5.tar.gz
- Upload date:
- Size: 341.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86a7c2cedfecea65810b805ca7a24d5a3e595600d095f5031a46af02b736917c
|
|
| MD5 |
acf0698be22ad8943b4fa7131df1950d
|
|
| BLAKE2b-256 |
3fc898810fc782882f9c1a5d28799e49b5e90b0b142d96ce8d35abfa804c951d
|
Provenance
The following attestation bundles were made for pyhrp-2.2.5.tar.gz:
Publisher:
rhiza_release.yml on tschm/pyhrp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyhrp-2.2.5.tar.gz -
Subject digest:
86a7c2cedfecea65810b805ca7a24d5a3e595600d095f5031a46af02b736917c - Sigstore transparency entry: 1642141406
- Sigstore integration time:
-
Permalink:
tschm/pyhrp@92c1f10eee10784649b65b16dbf9d7291d05e7b5 -
Branch / Tag:
refs/tags/v2.2.5 - Owner: https://github.com/tschm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
rhiza_release.yml@92c1f10eee10784649b65b16dbf9d7291d05e7b5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyhrp-2.2.5-py3-none-any.whl.
File metadata
- Download URL: pyhrp-2.2.5-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb4a0aad1dd1c7ae654f33abd6f813288af571ac92fe4bd087ee83ac5c4623c4
|
|
| MD5 |
1559e38bdace73dc8b7b060aa1d1e9ab
|
|
| BLAKE2b-256 |
1b6b1ad4e11c0e4d9fe6121d6d56641ec99e131b2a16842b8739b1448813f375
|
Provenance
The following attestation bundles were made for pyhrp-2.2.5-py3-none-any.whl:
Publisher:
rhiza_release.yml on tschm/pyhrp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyhrp-2.2.5-py3-none-any.whl -
Subject digest:
bb4a0aad1dd1c7ae654f33abd6f813288af571ac92fe4bd087ee83ac5c4623c4 - Sigstore transparency entry: 1642141513
- Sigstore integration time:
-
Permalink:
tschm/pyhrp@92c1f10eee10784649b65b16dbf9d7291d05e7b5 -
Branch / Tag:
refs/tags/v2.2.5 - Owner: https://github.com/tschm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
rhiza_release.yml@92c1f10eee10784649b65b16dbf9d7291d05e7b5 -
Trigger Event:
push
-
Statement type: