Skip to main content

Hierarchial risk parity

Project description

pyhrp

PyPI version License: MIT Downloads CodeFactor Python Version

Coverage

Ruff Renovate enabled

GitHub stars GitHub forks

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

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)

You may expect a weight series here but instead the hrp function returns a Node object. The node simplifies all further post-analysis.

weights = root.portfolio.weights
variance = root.portfolio.variance(cov)

# You can drill deeper into the tree
left = root.left
right = root.right

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.0.0.tar.gz (265.2 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.0.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyhrp-2.0.0.tar.gz
Algorithm Hash digest
SHA256 30ea543181c7ae5da2df193ea450ebc3b539bf8ea8ac43a9229524ff5192db40
MD5 56f11e029b85c64c8da94b24ffa5ac4b
BLAKE2b-256 b4e9ce0799beda1bda8901f907d3a6e292098e56c2b2862165aa68dd2adf0799

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhrp-2.0.0.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.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyhrp-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 573a2e04002855928bbc7dea4d4de6f093529aef52a1d836fc5be7965a72d0df
MD5 b5d8095599e94a4afcbf4a2eaec60e81
BLAKE2b-256 2749da4eeacfea08943ae59cef51a020ab66403160e5e9f5ad30ccf2fe4c7473

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhrp-2.0.0-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