Skip to main content

Cluster-based portfolio allocation: HRP, Schur risk parity, and 1/N

Project description

pyhrp

PyPI version Python Downloads License CodeFactor Rhiza

Coverage

A recursive implementation of the Hierarchical Risk Parity (HRP) approach by Marcos Lopez de Prado. We take advantage of the scipy.cluster.hierarchy package.

Comparing 'ward' with 'single' and bisection

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 import build_tree, compute_cov, compute_corr, risk_parity

prices = pl.read_csv("tests/resources/stock_prices.csv", try_parse_dates=True).drop("date")

returns = (
    prices.select(pl.all().pct_change())
    .filter(pl.any_horizontal(pl.all().is_not_null()))
    .fill_null(0.0)
    .fill_nan(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 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

pyhrp-2.3.1.tar.gz (357.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyhrp-2.3.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file pyhrp-2.3.1.tar.gz.

File metadata

  • Download URL: pyhrp-2.3.1.tar.gz
  • Upload date:
  • Size: 357.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhrp-2.3.1.tar.gz
Algorithm Hash digest
SHA256 dfd2f6c7d91ea586ed799502e757046c4103f3af3b49bc4bb19346793d3c4f30
MD5 5b3ee191f37b3c4d28ddfa8485eb4177
BLAKE2b-256 d2d262efc16e6efef2070d09c6e69f5782f540614b87e6a514128f322cee5aa8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhrp-2.3.1.tar.gz:

Publisher: rhiza_release.yml on tschm/pyhrp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhrp-2.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyhrp-2.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhrp-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ebee81117feef9a6e8087b110c1ce1bb6192f9591a2c82f2f8a0e73f2be4beb
MD5 1cc3e1da53c696805e6a90f8c2035519
BLAKE2b-256 5bc137dda54b2ddb58a6958b8af2d837d0ca5cea6acd935d066a4c39240f2b6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhrp-2.3.1-py3-none-any.whl:

Publisher: rhiza_release.yml on tschm/pyhrp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page