项目作者: tschm

项目描述 :
Run hierarchical risk parity algorithms
高级语言: Python
项目地址: git://github.com/tschm/hrp.git
创建时间: 2020-04-22T20:06:36Z
项目社区:https://github.com/tschm/hrp

开源协议:MIT License

下载


pyhrp

PyPI version
License: MIT
Downloads
Coverage Status
pre-commit.ci status

Open in GitHub Codespaces

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.

Comparing 'ward' with 'single' and bisection

Here’s a simple example

  1. >>> import pandas as pd
  2. >>> from pyhrp.hrp import build_tree
  3. >>> from pyhrp.algos import risk_parity
  4. >>> from pyhrp.cluster import Asset
  5. >>> prices = pd.read_csv("src/tests/resources/stock_prices.csv", index_col=0, parse_dates=True)
  6. >>> prices.columns = [Asset(name=column) for column in prices.columns]
  7. >>> returns = prices.pct_change().dropna(axis=0, how="all")
  8. >>> cov, cor = returns.cov(), returns.corr()
  9. # Compute the dendrogram based on the correlation matrix and Ward's metric
  10. >>> dendrogram = build_tree(cor, method='ward')
  11. >>> dendrogram.plot()
  12. # Compute the weights on the dendrogram
  13. >>> root = risk_parity(root=dendrogram.root, cov=cov)
  14. >>> 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.

  1. >>> from pyhrp.hrp import hrp
  2. >>> 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.

  1. >>> weights = root.portfolio.weights
  2. >>> variance = root.portfolio.variance(cov)
  3. # You can drill deeper into the tree
  4. >>> left = root.left
  5. >>> right = root.right

uv

Starting with

  1. make install

will install uv and create
the virtual environment defined in
pyproject.toml and locked in uv.lock.

marimo

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

  1. make marimo

will install and start marimo.