Build state-of-the-art portfolios with machine learning

February 24, 2024
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

Build state-of-the-art portfolios with machine learning

Portfolio optimization usually requires an estimate of the future returns of the assets in the portfolio. This is hard because we can’t see into the future.

Traditional risk parity uses a quadratic optimizer

A cutting edge technique called Hierarchical Risk Parity (HRP) uses graph theory and machine learning to build a hierarchical structure of the investments.

By the end of today’s newsletter, you’ll be able to create your own HRP-based portfolio among 25 sector-based ETFs.

Are you ready?

Build state-of-the-art portfolios with machine learning

HRP was introduced by Marcos Lopez de Prado in a 2016 paper. HRP applies graph theory and machine learning to build a diversified portfolio based on the covariance matrix.

HRP is unlike traditional portfolio optimization methods. It can create an optimized portfolio when the covariance matrix is ill-degenerated or singular. This is impossible for quadratic optimizers.

Research has shown HRP to deliver lower out-of-sample variance than traditional optimization methods.

Let’s get started!

Imports and set up

We’ll use the excellent RiskFolio-Lib to build our HRP portfolio and OpenBB for market data.

1import pandas as pd
2from openbb import obb
3import riskfolio as rp

We’ll use market data from 25 sector-based ETFs to construct our portfolio.

1assets = [
2    "XLE", "XLF", "XLU", "XLI", "GDX", 
3    "XLK", "XLV", "XLY", "XLP", "XLB", 
4    "XOP", "IYR", "XHB", "ITB", "VNQ", 
5    "GDXJ", "IYE", "OIH", "XME", "XRT", 
6    "SMH", "IBB", "KBE", "KRE", "XTL", 
7]
8
9data = (
10    obb
11    .equity
12    .price
13    .historical(assets, provider="yfinance")
14    .to_df()
15    .pivot(columns="symbol", values="close")
16)
17
18returns = data.pct_change().dropna()

This code uses OpenBB to download the market data as a DataFrame and generate the daily returns for each ETF.

Build the optimal portfolio

We can plot the dendrogram to visualize which ETFs are clustered together.

1ax = rp.plot_dendrogram(
2    returns=returns,
3    codependence="pearson",
4    linkage="single",
5    k=None,
6    max_k=10,
7    leaf_order=True,
8    ax=None,
9)

The result is the following image.

Build state-of-the-art portfolios with machine learning. A cutting edge technique called Hierarchical Risk Parity (HRP) uses machine learning instead.

The plot visualizes the hierarchical clustering of assets based on their historical return correlations. It illustrates how clusters of assets are merged at each hierarchical level and can give us insight into the correlation structure within a portfolio. The method takes asset returns and a clustering method to compute and plot the dendrogram.

Building the optimal portfolio based on the hierarchy is one line of code.

1w port = rp.HCPortfolio(returns=returns)
2w = port.optimization(
3    model="HRP",
4    codependence="pearson",
5    rm="MV",
6    rf=0.05,
7    linkage="single",
8    max_k=10,
9    leaf_order=True,
10)

The codependence parameter is set to "pearson" to use the Pearson correlation to measure the relationships between asset returns. The risk measure is set to "MV" for minimum variance which minimizes the portfolio's overall volatility.

Additional parameters like linkage, max_k, and leaf_order are specified to fine-tune the clustering and dendrogram construction process.

The result is a pandas Series with the optimal weight for each of the assets.

Visualize the results

RiskFolio-Lib makes it easy to visualize the results of the optimization.

1ax = rp.plot_pie(
2    w=w,
3    title="HRP Naive Risk Parity",
4    others=0.05,
5    nrow=25,
6    cmap="tab20",
7    height=8,
8    width=10,
9    ax=None,
10)

This code generates a pie chart which with the weights of each asset.

Build state-of-the-art portfolios with machine learning. A cutting edge technique called Hierarchical Risk Parity (HRP) uses machine learning instead.

We can also visualize the risk contribution of each asset.

1ax = rp.plot_risk_con(
2    w=w,
3    cov=returns.cov(),
4    returns=returns,
5    rm="MV",
6    rf=0,
7    alpha=0.05,
8    color="tab:blue",
9    height=6,
10    width=10,
11    t_factor=252,
12    ax=None,
13)

The risk contribution of each asset in a portfolio quantifies how much individual assets contribute to the total risk, considering both their own volatility and their correlation with other assets. We can see the highest risk contribution is from OIH which is an oil ETF.

Build state-of-the-art portfolios with machine learning. A cutting edge technique called Hierarchical Risk Parity (HRP) uses machine learning instead.

Risk contribution is important for identifying assets that disproportionately increase portfolio risk.

Next steps

We only scratched the surface of HRP. As a next step, add the assets in your portfolio and optimize it for a risk measure other than variance. You can try “MAD”, “CVaR”, or “VaR.” Check the documentation for details.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.