Compute the optimal portfolio minimizing the variance (this is a quadratic programming problem). The optimal variance is also computed.
[portf,varoptim]=markowitz(price,ER,r,dt);
Give the historical prices of a set of assets, compute the optimal portfolios solving four different problems. All of them minimize the Variance of the portfolio subject to the constraint of having given expected return. The four problems are different for the following characteristics
1) optimal portfolio allowing short selling and investing in the riskless asset;
2) optimal portfolio allowing short selling but without investing in the riskless asset;
3) optimal portfolio not allowing short selling and investing in the riskless asset;
4) optimal portfolio not allowing short selling and without investing in the riskless asset.
Finally the Variance of the optimal portfolio is computed. The procedure uses "qld" function.
First we generate 100x3 log-normally distributed daily asset prices with 0.1/250 mean and 0.2/sqrt(250) variance. The first prices of the three assets are 25 euros, 50 euros, and 110 euros respectively.
-->M=grand(100,3,'nor',0.1/250,0.2/sqrt(250));
-->price=exp(cumsum(M,1))*diag([25 50 110]);
Now we can use the function on these prices.
-->[portf,varoptim]=markowitz(price,0.05,0.01,1/250)
Draw frontiers? (y/n) y
! with short selling without short selling !
! with r without r with r without r !
varoptim =
0.0001508 0.0232294 0.0001508 0.0249650
portf =
- 0.0009446 0.6612090 - 1.444D-34 0.7614173
0.0599034 - 0.0729724 0.0599186 0.
0.0157164 0.4117634 0.0157684 0.2385827
We see that the third or the second asset are sold (negative portfolio component) when it is possible. If short selling is not allowed, then its weight is either positive or zero.
The function also asks if one wants to see the optimal frontiers (this is optional since the procedure takes some time). The figure which is created shows four frontiers corresponding to the four different problems (the legend explains the meanings).
Francesco Menoncin - Brescia University - 2010