- Joined
- 1/4/12
- Messages
- 8
- Points
- 13
Hi guys,
I ran into a technical problem while working on the Portfolio optimization in Matlab. Currently, I have 100 stocks, and I am meant to select 30 stocks so that the portfolio has the minimum variance for any given expected return. No shorting is allowed. Below is the Matlab code I wrote to find the efficient frontier. however, it does not satisfy the constraint there are totally 30 stocks in the portfolio. I used the quadratic programming. Anyone has ideas to restrict my portfolio to 30 stocks in Matlab?
% return_sh is the historical returns for the 100 stocks, muP is the expected return vector, and cov_sh is the 100 by 100 covariance matrix.
Aeq = [ones(size(return_sh')); return_sh'];
% no short
lb = zeros(size(return_sh));
ub = ones (size(return_sh));
%portfolio weight
weight_sh = NaN(length(muP),length(return_sh)) ;
%portfolio volatilities
sdP_sh = NaN(length(muP),1);
for i=1:length(muP)
% minimize volatility for certain expected returns
beq = [1 ;muP(i)];
[weight_sh(i,, sdP_sh(i)] = quadprog(2*cov_sh, zeros(size(return_sh)),[] ,[] , Aeq, beq,lb, ub );
I ran into a technical problem while working on the Portfolio optimization in Matlab. Currently, I have 100 stocks, and I am meant to select 30 stocks so that the portfolio has the minimum variance for any given expected return. No shorting is allowed. Below is the Matlab code I wrote to find the efficient frontier. however, it does not satisfy the constraint there are totally 30 stocks in the portfolio. I used the quadratic programming. Anyone has ideas to restrict my portfolio to 30 stocks in Matlab?
% return_sh is the historical returns for the 100 stocks, muP is the expected return vector, and cov_sh is the 100 by 100 covariance matrix.
Aeq = [ones(size(return_sh')); return_sh'];
% no short
lb = zeros(size(return_sh));
ub = ones (size(return_sh));
%portfolio weight
weight_sh = NaN(length(muP),length(return_sh)) ;
%portfolio volatilities
sdP_sh = NaN(length(muP),1);
for i=1:length(muP)
% minimize volatility for certain expected returns
beq = [1 ;muP(i)];
[weight_sh(i,, sdP_sh(i)] = quadprog(2*cov_sh, zeros(size(return_sh)),[] ,[] , Aeq, beq,lb, ub );