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


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

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyhrp-0.7.2.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.7.2.tar.gz
Algorithm Hash digest
SHA256 c773ecbe5eb75e3ec6554b274e8e30b31ced5bf7a23904459c98cec9a480d472
MD5 393b49f902f7084e66396631644148e7
BLAKE2b-256 966aa92a0bbb99e66b05f0688d5bf3fdaeabcce5ef0ded002ce0a3e5d3c8affa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyhrp-0.7.2-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.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e361eb7e65cb6464240910530d49036a89edf0bc11ec7340ec8d6827811cc1dc
MD5 73ee8265f2ff0a11ad83a21eb7660575
BLAKE2b-256 8aba068783a71b8ac9e8a0d5ecc22691017c034924e397e656cf1078a96c46f6

See more details on using hashes here.

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