create hierarchical risk parity portfolio -凯发k8网页登录
this example shows how to compute a hierarchical risk parity (hrp) portfolio. you can use hrp as a technique for portfolio diversification where the assets are divided and weighted according to a hierarchical tree structure. the weights of the assets within a cluster and between clusters can be assigned in many ways. a few ideas of the ways to allocate the weights are:
compute an inverse variance portfolio within each cluster. then, allocate weights to each cluster using a value proportional to the inverse of the variance of the cluster's portfolio.
compute a risk parity portfolio within each cluster. then, use a risk parity allocation strategy to assign each cluster's weights. the risk parity between clusters uses the covariance matrix between the cluster's portfolios. this example focuses on this allocation strategy.
use a bisection approach like the one described in lopez de prado []. for more information, see the example .
begin by loading the data and looking at the correlation between the assets returns.
% load data assetretn = readmatrix("./retns_assets.txt"); [nsample,nassets] = size(assetretn); % compute covariance and correlation matrices sigma = cov(assetretn); c = corrcov(sigma); heatmap(c);
hierachical clustering
hierarchical clustering is a common clustering technique in machine learning. in the context of asset allocation, a hierarchical clustering algorithm is applied to find the distance or similarity between each pair of assets and group them into a multilevel binary hierarchical tree.
begin by deffining a measure of likeness or distance between the assets. the more correlated two assets are, the closer they should be.
% compute the correlation distance matrix
distcorr = ((1-c)/2).^0.5;
use the function to compute the matrix that encodes the hierarchical tree of the assets in the universe. them use the function to visualize the hierarchical structure.
% compute the linkage link = linkage(distcorr); figure; h = dendrogram(link, colorthreshold='default'); set(h, linewidth=2); title('default leaf order');
the covariance or correlation matrix can be rearranged to be very close to a block diagonal matrix using the information obtained from the hierarchical tree. each block in the diagonal shows the assets that are closely related.
% sort assets for quasi-diagonalization
nleafnodes = size(link,1) 1;
rootnodeid = 2*nleafnodes - 1;
sortedidx = getleafnodesingroup(rootnodeid,link);
heatmap(c(sortedidx,sortedidx), xdata=sortedidx, ydata=sortedidx);
the plot shows that there are 6 blocks of assets that are closer together. you can divide the assets into 6 clusters using the function.
% get clusters
t = cluster(link, maxclust=6);
hierarchical risk parity algorithm
given a clustering of the assets, the hrp algorithm presented in this example follows these steps:
build a risk parity portfolio within each cluster. the
hrpportfolio
function in local functions computes the hrp portfolio by receiving a vector with the cluster assignment and a covariance matrix . then, a risk parity portfolio is computed within each cluster by using . the function receives a reduced covariance matrix that only inlcudes the information of the assets within the cluster and it returns the weights of the assets in the cluster
riskbudgetingportfolio(
)
,
where is a matrix whose entries inlcude the covariance information only for the assets in the th cluster.
2. compute each cluster's weight usign the covariance between each cluster's portfolio. now, the function receives a matrix () that represents the covariance between
the cluster's portfolios ().
,
where if asset is in cluster , otherwise . in other words , for clusters and riskbudgetingportfolio(
).
3. the final asset allocation is given by the cluster's risk parity portfolio () multiplied by each cluster's weight ()
.
% compute hrp portfolio
whrp = hrpportfolio(t,sigma)
whrp = 14×1
0.0854
0.0584
0.0423
0.0567
0.1867
0.1648
0.0521
0.0540
0.0539
0.0755
⋮
compare hrp and mean-variance portfolios
define a long-only, fully-invested mean-variance object. then, compute the associated minimum variance portfolio.
% define portfolio object p = portfolio(assetmean=mean(assetretn), assetcovar=sigma); p = setdefaultconstraints(p); % long-only, fully-invested portfolio % min variance portfolio wmv = estimatefrontierlimits(p,'min');
visualize the resulting allocations from these two strategies.
% create pie chart labels to improve plot reading labels = 1:nassets; labels = string(labels); % sort assets following quasi-diagonalization order labels = labels(sortedidx); wmv = wmv(sortedidx); whrp = whrp(sortedidx); % plot pie charts tiledlayout(1,2); % min variance portfolio nexttile pie(wmv(wmv>=1e-8),labels(wmv>=1e-8)) title('min variance portfolio',position=[0,1.5]); % hrp portfolio nexttile pie(whrp,labels) title('hrp portfolio',position=[0,1.5]);
you can see that the hrp portfolio is a much more diversified portfolio as compared to the portfolio obtained using the traditional mean-variance approach. in addition, you can see the following:
the assets that were not correlated with others, assets 5 and 6, represent a much larger area of the pie. in fact, the sum of the areas of assets that are in the same cluster (for example, assets 1 and 10 or assets 11, 13, 3 and 14) are close to the individual areas of assets 5 and 6. this shows that the weights are divided somewhat evenly between clusters and that each cluster's weight is divided somewhat evenly among the assets within the cluster. this is consistent with the hrp theory.
the risk parity portfolio of fully correlated assets with the same variance is an equally weighted portfolio. the same happens for assets that are completely uncorrelated. since all the assets in the universe have a similar variance, and assets between clusters are almost uncorrelated, the weights allocated to each cluster are almost even. and, since the assets within a cluster are meant to be highly correlated, the weights of the assets within a culster are evenly distibuted.
references
lopez de prado, m. "building diversified portfolios that outperform out of sample." the journal of portfolio management. 42(4), 59-69, 2016.
local functions
function pwgt = hrpportfolio(t,sigma) % function that computes a hierarchical risk parity portfolio. the % algorithm first computes a risk parity portfolio for each cluster. then, % each cluster is assigned a weight based on a risk parity allocation of % the covariance between the cluster's portfolios. % get the problem information. nassets = size(sigma,1); nclusters = max(t); % compute the risk parity portfolio within each cluster. w = zeros(nassets,nclusters); for i = 1:nclusters % identify assets in cluster i and the sub-covariance matrix. idx = t == i; tempsigma = sigma(idx,idx); % compute the risk parity portfolio of cluster i. w(idx,i) = riskbudgetingportfolio(tempsigma); end % compute the covariance between the risk parity portfolios of each % cluster. covcluster = w'*sigma*w; % compute the weights of each cluster. wbetween = riskbudgetingportfolio(covcluster); % multiply the weight assigned to each cluster with its portfolio and % assign to the corresponding assets. pwgt = w*wbetween; end function idxingroup = getleafnodesingroup(nodeid, link) % getleafnodesingroup finds all leaf nodes for a given node id % in a linkage matrix. nleaves= size(link, 1) 1; if nodeid > nleaves tempnodeids = link(nodeid-nleaves,1:2); idxingroup = [getleafnodesingroup(tempnodeids(1), link), ... getleafnodesingroup(tempnodeids(2), link)]; else idxingroup = nodeid; end end
see also
|