Skip to main content

...

Project description

pyhrp

PyPI version Apache 2.0 License Downloads Coverage Status pre-commit.ci status

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 dist, linkage, tree, _hrp

prices = pd.read_csv("test/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()
links = linkage(dist(cor.values), method='ward')
node = tree(links)

rootcluster = _hrp(node, cov)

ax = dendrogram(links, orientation="left")
ax.get_figure().savefig("dendrogram.png")

For your convenience you can bypass the construction of the covariance and correlation matrix, the links and the node, e.g. the root of the tree (dendrogram).

import pandas as pd
from pyhrp.hrp import hrp

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

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

print(cluster.weights)
print(cluster.variance)
# You can drill into the graph by going downstream
print(cluster.left)
print(cluster.right)

## Poetry

We assume you share already the love for [Poetry](https://python-poetry.org).
Once you have installed poetry you can perform

```bash
make install

to replicate the virtual environment we have defined in pyproject.toml and locked in poetry.lock.

Jupyter

We install JupyterLab on fly within the aforementioned virtual environment. Executing

make jupyter

will install and start the jupyter lab.

Project details


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.0.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

pyhrp-1.0.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyhrp-1.0.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for pyhrp-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c06924b22525412998f98633736dc7449e9327340294a649c32d9624b813c411
MD5 0b78956e9f09c32f13244fd2aeac1aa2
BLAKE2b-256 2dbc2f19c51960a24e59edf89401fe2558d78ec3bd2e7c86e9fca40ef7ac2943

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyhrp-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for pyhrp-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c640771ce8f155b706f7fcaea351e202981c7a83221c3e5b9ee1f378921a2de
MD5 4a2b1347d4b1b561136bdddce0299c9c
BLAKE2b-256 203cf81f9888dfdbeb777b8fd29401e2ec751829369ff91932569cdc7c8e6c92

See more details on using hashes here.

Supported by

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