Main Content

Diversify Portfolios Using Custom Objective

This example shows three techniques of asset diversification in a portfolio using the estimateCustomObjectivePortfolio function with a Portfolio object. The purpose of asset diversification is to balance the exposure of the portfolio to any given asset to reduce volatility over a period of time. Given the sensitivity of the minimum variance portfolio to the estimation of the covariance matrix, some practitioners have added diversification techniques to the portfolio selection with the hope of minimizing risk measures other than the variance measures such as turnover, maximum drawdown, and so on.

This example applies these common diversification techniques:

Additionally, this example demonstrates penalty methods that you can use to achieve different degrees of diversification. In those methods, you add a penalty term to the estimateCustomObjectivePortfolio function to balance the level of variance reduction and the diversification of the portfolio.

Retrieve Market Data and Define Mean-Variance Portfolio

Begin by loading and computing the expected returns and their covariance matrix.

% Load data
load('port5.mat');
% Store returns and covariance
mu = mean_return;
Sigma = Correlation .* (stdDev_return * stdDev_return');

Define a mean-variance portfolio using a Portfolio object with default constraints to create a fully invested, long-only portfolio.

% Create a mean-variance Portfolio object with default constraints
p = Portfolio('AssetMean',mu,'AssetCovar',Sigma);
p = setDefaultConstraints(p);

One of the many features of the Portfolio object is that it can obtain the efficient frontier of the portfolio problem. The efficient frontier is computed by solving a series of optimization problems for which the return level of the portfolio, μ0, is modified to obtain different points on the efficient frontier. These problems are defined as

min   xTΣxst.    i=1nxi=1          xTμμ0          x0

The advantage of using the Portfolio object to compute the efficient frontier is that you can obtain without having to manually formulate and solve the multiple optimization problems shown above.

plotFrontier(p);

Figure contains an axes object. The axes object with title E f f i c i e n t blank F r o n t i e r contains an object of type line.

The Portfolio object can also compute the weights associated with the minimum variance portfolio, which is defined by the following problem.

min   xTΣxst.    i=1nxi=1          x0

The minimum variance weights is the benchmark against which all the weights of the diversification strategies are compared.

wMinVar = estimateFrontierLimits(p,'min');

To learn more about the problems that you can solve with the Portfolio object, see When to Use Portfolio Objects Over Optimization Toolbox.

Specify Diversification Techniques

This section presents the three diversification methods. Each of the three diversification methods is associated with a diversification measure and that diversification measure defines a penalty term to achieve different diversification levels. The diversification, obtained by adding the penalty term to the objective function, ranges from the behavior achieved by the minimum variance portfolio to the behavior of the EW, ECR, and MDP, respectively.

The default portfolio has only one equality constraint and a lower bound for the assets weights. The weights must be nonnegative and they must sum to 1. The feasible set is represented asX:

X={x|x0,i=1nxi=1}

Equally Weighted Portfolio

One of the diversification measures is the Herfindahl-Hirschman (HH) index defined as:

HH(x)=i=1nxi2

This index is minimized when the portfolio is equally weighted. The portfolios obtained from using this index as a penalty have weights that satisfy the portfolio constraints and that are more evenly weighted.

The portfolio that minimizes the HH index is minxX   xTx. Since the constraints in X are the default constraints, the solution of this problem is the equally weighted (EW) portfolio. If X had extra constraints, the solution would be the portfolio that satisfies all the constraints and, at the same time, keeps the weights as equal as possible. Use the function handle HHObjFun for the Herfindahl-Hirschman (HH) index with the estimateCustomObjectivePortfolio function.

% Maximize the HH diversification (by minimizing the HH index)
HHObjFun = @(x) x'*x;
% Solution that minimizes the HH index
wHH = estimateCustomObjectivePortfolio(p,HHObjFun);

The portfolio that minimizes the variance with the HH penalty is minxX   xTΣx+λHHxTx. Use the function handle HHMixObjFun for the HH penalty with the estimateCustomObjectivePortfolio function.

% HH penalty parameter
lambdaHH = 1e-2;
% Variance + Herfindahl-Hirschman (HH) index
HHMixObjFun = @(x) x'*p.AssetCovar*x + lambdaHH*(x'*x);
% Solution that accounts for risk and HH diversification
wHHMix = estimateCustomObjectivePortfolio(p,HHMixObjFun);

Plot the weights distribution for the minimum variance portfolio, the equal weight portfolio, and the penalized strategy.

% Plot different strategies
plotAssetAllocationChanges(wMinVar,wHHMix,wHH)

Figure contains 3 axes objects. Axes object 1 with title Minimum Variance contains an object of type bar. Axes object 2 with title Mixed Strategy contains an object of type bar. Axes object 3 with title Maximum Diversity contains an object of type bar.

This plot shows how the penalized strategy returns weights that are between the minimum variance portfolio and the EW portfolio. In fact, choosing λHH=0 returns the minimum variance solution, and as λHH, the solution approaches the EW portfolio.

Most Diversified Portfolio

The diversification index associated to the most diversified portfolio (MDP) is defined as

MDP(x)=-i=1nσixi

where σi represents the standard deviation of asset i.

The MDP is the portfolio that maximizes the diversification ratio:

φ(x)=xTσxTΣx

The diversification ratio φ(x) is equal to 1 if the portfolio is fully invested in one asset or if all assets are perfectly correlated. For all other cases, φ(x)>1. If φ(x)1, there is no diversification, so the goal is to find the portolio that maximizes φ(x):

maxxX   σTxxTΣx

Unlike the HH index, the MDP goal is not to obtain a portfolio whose weights are evenly distributed among all assets, but to obtain a portfolio whose selected (nonzero) assets have the same correlation to the portfolio as a whole. Use the function handle MDPObjFun for the most diversified portfolio (MDP) with the estimateCustomObjectivePortfolio function.

% Maximize the diversification ratio
sigma = sqrt(diag(p.AssetCovar));
MDPObjFun = @(x) (sigma'*x)/sqrt(x'*p.AssetCovar*x);
% Solution of MDP
wMDP = estimateCustomObjectivePortfolio(p,MDPObjFun, ...
    ObjectiveSense="maximize");

The following code shows that there exists a value λˆMDP>0 such that the MDP problem and the problem with its penalized version are equivalent.The portfolio that minimizes the variance with the MDP penalty isminxX   xTΣx-λMDPσTx.

Define an MDP penalty parameter and solve for MDP using the function handle MDPMixObjFun for the MDP penalty with the estimateCustomObjectivePortfolio function.

% MDP penalty parameter
lambdaMDP = 1e-2;
% Variance + Most Diversified Portfolio (MDP)
MDPMixObjFun = @(x) x'*p.AssetCovar*x - lambdaMDP*(sigma'*x);
% Solution that accounts for risk and MDP diversification
wMDPMix = estimateCustomObjectivePortfolio(p,MDPMixObjFun);

Plot the weights distribution for the minimum variance portfolio, the MDP, and the penalized strategy.

% Plot different strategies
plotAssetAllocationChanges(wMinVar,wMDPMix,wMDP)

Figure contains 3 axes objects. Axes object 1 with title Minimum Variance contains an object of type bar. Axes object 2 with title Mixed Strategy contains an object of type bar. Axes object 3 with title Maximum Diversity contains an object of type bar.

In this plot, the penalized strategy weights are between the minimum variance portfolio and the MDP. This result is the same as with the HH penalty, where choosing λMDP=0 returns the minimum variance solution and values of λMDP[0,λˆMDP] return asset weights that range from the minimum variance behavior to the MDP behavior.

Equal Risk Contribution Portfolio

The diversification index associated with the equal risk contribution (ERC) portfolio is defined as

ERC(x)=-i=1nln(xi)

This index is related to a convex reformulation shown by Maillard [1] that computes the ERC portfolio. The authors show that you can obtain the ERC portfolio by solving the following optimization problem

miny0yTΣyst.  i=1nln(yi)c

and by defining x, the ERC portfolio with default constraints, as x=yiyi, where c>0 can be any constant. You implement this procedure in the riskBudgetingPortfolio function.

The purpose of the ERC portfolio is to select the assets weights in such a way that the risk contribution of each asset to the portfolio volatility is the same for all assets.

% ERC portfolio
wERC = riskBudgetingPortfolio(p.AssetCovar);

The portfolio that minimizes the variance with the ERC penalty is minxX   xTΣx-λERCi=1nln(xi).

Similar to the case for the MDP penalized formulation, there exists a λˆERC such that the ERC problem and its penalized version are equivalent. Use the function handle ERCMixObjFun for the ERC penalty with the estimateCustomObjectivePortfolio function.

% ERC penalty parameter
lambdaERC = 3e-6; % lambdaERC is so small because the log of a number
                  % close to zero (the portfolio weights) is large
% Variance + Equal Risk Contribution (ERC)
ERCMixObjFun = @(x) x'*p.AssetCovar*x - lambdaERC*sum(log(x));
% Solution that accounts for risk and ERC diversification
wERCMix = estimateCustomObjectivePortfolio(p,ERCMixObjFun);

Plot the weights distribution for the minimum variance portfolio, the ERC, and the penalized strategy.

% Plot different strategies
plotAssetAllocationChanges(wMinVar,wERCMix,wERC)

Figure contains 3 axes objects. Axes object 1 with title Minimum Variance contains an object of type bar. Axes object 2 with title Mixed Strategy contains an object of type bar. Axes object 3 with title Maximum Diversity contains an object of type bar.

Comparable to the two diversification measures above, here the penalized strategy weights are between the minimum variance portfolio and the ERC portfolio. Choosing λERC=0 returns the minimum variance solution and the values of λERC[0,λˆERC] return asset weights that range from the minimum variance behavior to the ERC portfolio behavior.

Compare Diversification Strategies

Compute the number of assets that are selected in each portfolio. Assume that an asset is selected if the weight associated to that asset is above a certain threshold.

% Build a weights table
varNames = {'MinVariance','MixedHH','HH','MixedMDP','MDP', ...
    'MixedERC','ERC'};
weightsTable = table(wMinVar,wHHMix,wHH,wMDPMix,wMDP, ...
    wERCMix,wERC,'VariableNames',varNames);
% Number of assets with nonzero weights
cutOff = 1e-3; % Weights below cut-off point are considered zero.
[reweightedTable,TnonZero] = tableWithNonZeroWeights(weightsTable, ...
    cutOff,varNames);
display(TnonZero)
TnonZero=1×7 table
                       MinVariance    MixedHH    HH     MixedMDP    MDP    MixedERC    ERC
                       ___________    _______    ___    ________    ___    ________    ___

    Nonzero weights        11           104      225       23       28       225       225

As discussed above, the HH penalty goal is to obtain more evenly weighted portfolios. The portfolio that maximizes the HH diversity (and corresponds to the EW portfolio when only the default constraints are selected) has the largest number of assets selected and the weights of these assets are closer together. You can see this latter quality in the following boxchart. Also, the strategy that adds the HH index as a penalty function to the objective has a larger number of assets than the minimum variance portfolio but less than the portfolio that maximizes HH diversity. The ERC portfolio also selects all the assets because all weigths need to be nonzero to have some risk contribution.

% Boxchart of portfolio weights
figure;
matBoxPlot = reweightedTable.Variables;
matBoxPlot(matBoxPlot == 0) = NaN;
boxchart(matBoxPlot)
xticklabels(varNames)
title('Weights Distribution')
xlabel('Strategies')
ylabel('Weight')

Figure contains an axes object. The axes object with title Weights Distribution contains an object of type boxchart.

This boxchart shows the spread of the assets' positive weights for the different portfolios. As previously discussed, the weights of the portfolio that maximize the HH diversity are all the same. If the portfolio had other types of constraints, the weights would not all be the same, but they would have the lowest variance. The ERC portfolio weights also have a small variance. In fact, you can observe as the number of assets increases as the variance of the ERC portfolio weights becomes smaller.

The weights variability of the MDP is smaller than the variability of the minimum variance weights. However, it is not necessarily true that the MDP's weights will have less variability than the minimum variance weights because the goal of the MDP is not to obtain equally weighted assets, but to distribute the correlation of each asset with its portfolio evenly.

% Compute and plot the risk contribution of each individual
% asset to the portfolio
riskContribution = portfolioRiskContribution(p.AssetCovar, ...
    weightsTable.Variables);
% Remove small contributions
riskContribution(riskContribution < 1e-3) = NaN;

% Compare percent contribution to portofolio risk
boxchart(riskContribution)
xticklabels(varNames)
title('Percent Contributions to Portfolio Risk')
xlabel('Strategies')
ylabel('PCRs')

Figure contains an axes object. The axes object with title Percent Contributions to Portfolio Risk contains an object of type boxchart.

This boxchart shows the percent risk contribution of each asset to the total portfolio risk. The percent risk contribution is computed as

(PRC)i=xi(Σx)ixTΣx

As expected, all the ERC portfolio assets have the same risk contribution to the portfolio. As discussed after the weights distribution plot, if the problem had other types of constraints, the risk contribution of the ERC portfolio would not be the same for all assets, but they would have the lowest variance. Also, the behavior shown in this picture is similar to the behavior shown by the weights distribution.

% Compute and plot the correlation of each individual asset to its
% portfolio
corrAsset2Port = correlationInfo(p.AssetCovar, ...
    weightsTable.Variables);
% Boxchart of assets to portfolio correlations
figure
boxchart(corrAsset2Port)
xticklabels(varNames)
title('Correlation of Individual Assets to Their Portfolio')
xlabel('Strategies')
ylabel('Correlation')

Figure contains an axes object. The axes object with title Correlation of Individual Assets to Their Portfolio contains an object of type boxchart.

This boxchart shows the distribution of the correlations of each asset with its respective portfolio. The correlation of asset i to its portfolio is computed with the following formula:

ρiP=(Σx)iσixTΣx

The MDP is the portfolio whose correlations are closer together and this is followed by the strategy that uses the MDP penalty term. In fact, if the portfolio problem allowed negative weights, then all the assets of the MDP would have the same correlation to its portfolio. Also, both the HH (EW) and ERC portfolios have almost the same correlation variability.

References

  1. Maillard, S., Roncalli, T., & Teïletche, J. "The Properties of Equally Weighted Risk Contribution Portfolios." The Journal of Portfolio Management, 36(4), 2010, pp. 60–70.

  2. Richard, J. C., & Roncalli, T. "Smart Beta: Managing Diversification of Minimum Variance Portfolios." Risk-Based and Factor Investing. Elsevier, 2015, pp. 31–63.

  3. Tütüncü, R., Peña, J., Cornuéjols, G. Optimization Methods in Finance. United Kingdom: Cambridge University Press, 2018.

Local Functions

function [] = plotAssetAllocationChanges(wMinVar,wMix,wMaxDiv)
% Plots the weights allocation from the strategies shown before 

figure
t = tiledlayout(1,3);
nexttile
bar(wMinVar')
axis([0 225 0 0.203])
title('Minimum Variance')
nexttile
bar(wMix')
axis([0 225 0 0.203])
title('Mixed Strategy')
nexttile
bar(wMaxDiv')
axis([0 225 0 0.203])
title('Maximum Diversity')
ylabel(t,'Asset Weight')
xlabel(t,'Asset Number')

end

function [weightsTable,TnonZero] = ...
    tableWithNonZeroWeights(weightsTable,cutOff,varNames)
% Creates a table with the number of nonzero weights for each strategy

% Select only meaningful weights
funSelect = @(x) (x >= cutOff).*x./sum(x(x >= cutOff));
weightsTable = varfun(funSelect,weightsTable);

% Number of assets with positive weights
funSum = @(x) sum(x > 0);
TnonZero = varfun(funSum,weightsTable);
TnonZero.Properties.VariableNames = varNames;
TnonZero.Properties.RowNames = {'Nonzero weights'};

end

function [corrAsset2Port] = correlationInfo(Sigma,portWeights)
% Returns a matrix with the correlation of each individual asset to its
% portfolio

nX = size(portWeights,1); % Number of assets
nP = size(portWeights,2); % Number of portfolios

auxM = eye(nX);
corrAsset2Port = zeros(nX,nP);
for j = 1:nP
    % Portfolio's standard deviation
    sigmaPortfolio = sqrt(portWeights(:,j)'*Sigma*portWeights(:,j));
    for i = 1:nX
        % Assets's standard deviation
        sigmaAsset = sqrt(Sigma(i,i));
        % Asset to portfolio correlation
        corrAsset2Port(i,j) = (auxM(:,i)'*Sigma*portWeights(:,j))/...
            (sigmaAsset*sigmaPortfolio);
    end
end

end

function [riskContribution] = portfolioRiskContribution(Sigma,...
    portWeights)
% Returns a matrix with the risk contribution of each asset to
% the underlying portfolio.

nX = size(portWeights,1); % Number of assets
nP = size(portWeights,2); % Number of portfolios

riskContribution = zeros(nX,nP);
for i = 1:nP
    weights = portWeights(:,i);
    % Portfolio variance
    portVar = weights'*Sigma*weights;
    % Marginal constribution to portfoli risk (MCR)
    margRiskCont = weights.*(Sigma*weights)/sqrt(portVar);
    % Percent contribution to portfolio risk
    riskContribution(:,i) = margRiskCont/sqrt(portVar);
end

end

See Also

| | |

Related Examples

More About