Skip to main content

Hierarchial risk parity

Project description

pyhrp

PyPI version License: MIT Downloads CodeFactor Python Version

CI pre-commit Documentation

Ruff Renovate enabled

GitHub stars GitHub forks

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

Comparing 'ward' with 'single' and bisection

Here's a simple example

import pandas as pd
from pyhrp.hrp import build_tree
from pyhrp.algos import risk_parity

prices = pd.read_csv("tests/resources/stock_prices.csv", index_col=0, parse_dates=True)

returns = prices.pct_change().dropna(axis=0, how="all")
cov, cor = returns.cov(), returns.corr()

# 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)
ax = 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-1.6.2.tar.gz (340.9 kB view details)

Uploaded Source

Built Distribution

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

pyhrp-1.6.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyhrp-1.6.2.tar.gz
Algorithm Hash digest
SHA256 9395299d020491cdbd9ba2ac4cab69419dc6fd58ff66445c2e74368eecd7276e
MD5 5d8ec3fd5f7c5456da3a0429a85b06ec
BLAKE2b-256 56e0e85b77cd1bdf95dd6ed4c7c44b79a7c08e9466659406198d7b2984b9f762

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for pyhrp-1.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 112f53345b39e156c28b4232eadc27c2edcc8d01d57cce959014c45ff5832b7f
MD5 70cb9ef5fae544ed7582d0348cbcfcff
BLAKE2b-256 25252ad52e32c790b5b9908820071fb29a36084d3df3ddcf578f740d0171cde4

See more details on using hashes here.

Provenance

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