Skip to main content

Python for Hierarchical 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 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.0.8.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

pyhrp-0.0.8-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyhrp-0.0.8.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.0

File hashes

Hashes for pyhrp-0.0.8.tar.gz
Algorithm Hash digest
SHA256 36a9367f42ec6ff1e99c0c4d576ca9b02c346e29d285a529abb55871be0092c5
MD5 ed511f50deffcba66131fc06661ddec2
BLAKE2b-256 0bd41cd20decd2e234312ed7cf03215a8ace7edb9f3fb652c0ffb8860b1b6777

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyhrp-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.0

File hashes

Hashes for pyhrp-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 35753957ca9c873b3bb0ff8c91351c20ec6f9137c0e7c542affb08615ac8b2e8
MD5 f1200eb642e6e51587ca14c9a0e136df
BLAKE2b-256 2c2dd333bea8cdcb7b8d950f3fb81127ca5dff2488db7d0c969422d2f17c8750

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