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.rightStarting with
make installwill install uv and create the virtual environment defined in pyproject.toml and locked in uv.lock.
We install marimo on the fly within the aforementioned virtual environment. Executing
make marimowill install and start marimo.
