Hierarchial risk parity
Project description
pyhrp
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.
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
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-1.5.1.tar.gz.
File metadata
- Download URL: pyhrp-1.5.1.tar.gz
- Upload date:
- Size: 214.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acf67f884fce66053d5722cdf093b1bc2aeecd49967c3eb3ff71e7397cc839d9
|
|
| MD5 |
a3db6e0ef87af5560b4c8dab67e8bf96
|
|
| BLAKE2b-256 |
6471d9be977a1354199407e0e290fe78af3eefcfe959fe4ed00c45c7b03f0351
|
Provenance
The following attestation bundles were made for pyhrp-1.5.1.tar.gz:
Publisher:
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-1.5.1.tar.gz -
Subject digest:
acf67f884fce66053d5722cdf093b1bc2aeecd49967c3eb3ff71e7397cc839d9 - Sigstore transparency entry: 620590624
- Sigstore integration time:
-
Permalink:
tschm/pyhrp@9aa1658ae2d9e51d4b041268f1d84ed346ee03cf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/tschm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9aa1658ae2d9e51d4b041268f1d84ed346ee03cf -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pyhrp-1.5.1-py3-none-any.whl.
File metadata
- Download URL: pyhrp-1.5.1-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4461053ee4a41b6f3467d869caf00686f00839377c845a70716d64ef028e2213
|
|
| MD5 |
05aeb15e3a6c688e15a81900a323e749
|
|
| BLAKE2b-256 |
736744143cd3a2b262bd3ff00077005c184314364ca4d08cca4ebeb0d97a47c6
|
Provenance
The following attestation bundles were made for pyhrp-1.5.1-py3-none-any.whl:
Publisher:
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-1.5.1-py3-none-any.whl -
Subject digest:
4461053ee4a41b6f3467d869caf00686f00839377c845a70716d64ef028e2213 - Sigstore transparency entry: 620590668
- Sigstore integration time:
-
Permalink:
tschm/pyhrp@9aa1658ae2d9e51d4b041268f1d84ed346ee03cf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/tschm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9aa1658ae2d9e51d4b041268f1d84ed346ee03cf -
Trigger Event:
workflow_dispatch
-
Statement type: