Skip to main content

...

Project description

pyhrp

DeepSource

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)

Installation:

pip install pyhpr

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

Uploaded Source

Built Distribution

pyhrp-0.6.7-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyhrp-0.6.7.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.4.2 CPython/3.10.11 Linux/5.15.0-1036-azure

File hashes

Hashes for pyhrp-0.6.7.tar.gz
Algorithm Hash digest
SHA256 6dbfc86a192e341ba749b19ca11c1a47b6e04049ffc6127b602fc62f7eb087dc
MD5 b67c9a93003f184044daaf2512fad47f
BLAKE2b-256 951313feabe1bf5a63a64296f6b34133c09fe36d0bac0d4c17d5137e381d46e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyhrp-0.6.7-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.4.2 CPython/3.10.11 Linux/5.15.0-1036-azure

File hashes

Hashes for pyhrp-0.6.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4766bcf55b3e76102acab96ac885259b9671d35e933404acb4db5d75ec73f6d4
MD5 a78d09776e9f653c668aee4bcc01c927
BLAKE2b-256 a49fae02480755a39a02987cd6c80cc01711197a743c95708f9f7159f3182b8a

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