# 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, ${\mu}_{0}$, is modified to obtain different points on the efficient frontier. These problems are defined as

$$\begin{array}{l}\mathrm{min}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\Sigma \mathit{x}\\ \mathrm{st}.\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}\sum _{\mathit{i}=1}^{\mathit{n}}{\mathit{x}}_{\mathit{i}}=1\\ \text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\mu \ge {\mu}_{0}\\ \text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}\mathit{x}\ge 0\end{array}$$

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);

The `Portfolio`

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

$$\begin{array}{l}\mathrm{min}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\Sigma \mathit{x}\\ \mathrm{st}.\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}\sum _{\mathit{i}=1}^{\mathit{n}}{\mathit{x}}_{\mathit{i}}=1\\ \text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}\mathit{x}\ge 0\end{array}$$

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 as$\mathit{X}$:

$$\mathit{X}=\left\{\mathit{x}\text{\hspace{0.17em}}|\text{\hspace{0.17em}}\mathit{x}\ge 0,\text{\hspace{0.17em}}\sum _{\mathit{i}=1}^{\mathit{n}}{\mathit{x}}_{\mathit{i}}=1\right\}$$

### Equally Weighted Portfolio

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

$$\mathrm{HH}\left(\mathit{x}\right)=\sum _{\mathit{i}=1}^{\mathit{n}}{\mathit{x}}_{\mathit{i}}^{2}$$

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 $\underset{\mathit{x}\in \mathit{X}}{\mathrm{min}}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\mathit{x}$. Since the constraints in $\mathit{X}$ are the default constraints, the solution of this problem is the equally weighted (EW) portfolio. If $\mathit{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 $\underset{\mathit{x}\in \mathit{X}}{\mathrm{min}}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\Sigma \mathit{x}+{\lambda}_{\mathrm{HH}}{\mathit{x}}^{\mathit{T}}\mathit{x}$. 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)
```

This plot shows how the penalized strategy returns weights that are between the minimum variance portfolio and the EW portfolio. In fact, choosing ${\lambda}_{\mathrm{HH}}=0$ returns the minimum variance solution, and as ${\lambda}_{\mathrm{HH}}\to \infty ,$ the solution approaches the EW portfolio.

### Most Diversified Portfolio

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

$$\mathrm{MDP}\left(\mathit{x}\right)=-\sum _{\mathit{i}=1}^{\mathit{n}}{\sigma}_{\mathit{i}}{\mathit{x}}_{\mathit{i}}$$

where ${\sigma}_{\mathit{i}}$ represents the standard deviation of asset $\mathit{i}$.

The MDP is the portfolio that maximizes the diversification ratio:

$$\phi \left(\mathit{x}\right)=\text{\hspace{0.17em}}\frac{{\mathit{x}}^{\mathit{T}}\sigma}{\sqrt{{\mathit{x}}^{\mathit{T}}\Sigma \mathit{x}}}$$

The diversification ratio $\phi \left(\mathit{x}\right)$ is equal to `1`

if the portfolio is fully invested in one asset or if all assets are perfectly correlated. For all other cases, $\phi \left(\mathit{x}\right)>1$. If $\phi \left(\mathit{x}\right)\approx 1$, there is no diversification, so the goal is to find the portolio that maximizes $\phi \left(\mathit{x}\right)$:

$$\underset{\mathit{x}\in \mathit{X}}{\mathrm{max}}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}\frac{{\sigma}^{\mathit{T}}\mathit{x}}{\sqrt{{\mathit{x}}^{\mathit{T}}\Sigma \mathit{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 ${\stackrel{\u02c6}{\lambda}}_{\mathrm{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 is$\underset{\mathit{x}\in \mathit{X}}{\text{\hspace{0.17em}}\mathrm{min}}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\Sigma \mathit{x}-{\lambda}_{\mathrm{MDP}}\text{\hspace{0.17em}}{\sigma}^{\mathit{T}}\mathit{x}$.

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)
```

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 ${\lambda}_{\mathrm{MDP}}=0$ returns the minimum variance solution and values of ${\lambda}_{\mathrm{MDP}}\in \left[0,{\stackrel{\u02c6}{\lambda}}_{\mathrm{MDP}}\right]$ 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

$$\mathrm{ERC}\left(\mathit{x}\right)=-\sum _{\mathit{i}=1}^{\mathit{n}}\mathrm{ln}\left({\mathit{x}}_{\mathit{i}}\right)$$

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

$$\begin{array}{l}\underset{\mathit{y}\ge 0}{\mathrm{min}}\text{\hspace{0.17em}}{\mathit{y}}^{\mathit{T}}\Sigma \text{\hspace{0.17em}}\mathit{y}\\ \mathrm{st}.\text{\hspace{0.17em}\hspace{0.17em}}\sum _{\mathit{i}=1}^{\mathit{n}}\mathrm{ln}\left({\mathit{y}}_{\mathit{i}}\right)\ge \mathit{c}\end{array}$$

and by defining $\mathit{x}$, the ERC portfolio with default constraints, as $\mathit{x}=\frac{\mathit{y}}{{\sum}_{\mathit{i}}{\mathit{y}}_{\mathit{i}}}$, where $\mathit{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 $$\underset{\mathit{x}\in \mathit{X}}{\mathrm{min}}\text{\hspace{0.17em}\hspace{0.17em}\hspace{0.17em}}{\mathit{x}}^{\mathit{T}}\Sigma \mathit{x}-{\lambda}_{\mathrm{ERC}}\text{\hspace{0.17em}}\sum _{\mathit{i}=1}^{\mathit{n}}\mathrm{ln}\left({\mathit{x}}_{\mathit{i}}\right)$$.

Similar to the case for the MDP penalized formulation, there exists a ${\stackrel{\u02c6}{\lambda}}_{\mathrm{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)
```

Comparable to the two diversification measures above, here the penalized strategy weights are between the minimum variance portfolio and the ERC portfolio. Choosing ${\lambda}_{\mathrm{ERC}}=0$ returns the minimum variance solution and the values of ${\lambda}_{\mathrm{ERC}}\in \left[0,{\stackrel{\u02c6}{\lambda}}_{\mathrm{ERC}}\right]$ 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')

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')

This `boxchart`

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

$${\left(\mathrm{PRC}\right)}_{\mathit{i}}=\frac{{\mathit{x}}_{\mathit{i}}{\left(\Sigma \mathit{x}\right)}_{\mathit{i}}}{{\mathit{x}}^{\mathit{T}}\Sigma \mathit{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')

This `boxchart`

shows the distribution of the correlations of each asset with its respective portfolio. The correlation of asset $\mathit{i}$ to its portfolio is computed with the following formula:

$${\rho}_{\mathrm{iP}}=\frac{{\left(\Sigma \mathit{x}\right)}_{\mathit{i}}}{{\sigma}_{\mathit{i}}\sqrt{{\mathit{x}}^{\mathit{T}}\Sigma \mathit{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

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.Richard, J. C., & Roncalli, T. "Smart Beta: Managing Diversification of Minimum Variance Portfolios."

*Risk-Based and Factor Investing*. Elsevier, 2015, pp. 31–63.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

`estimatePortSharpeRatio`

| `estimateFrontier`

| `estimateFrontierByReturn`

| `estimateFrontierByRisk`