Using Matlab quadprog to solve markowitz model

I have the markowitz model shown below and I need to use the quadprog function to solve it (i.e get the values for w_i values). However I am a bit new to mat lab and not sure which definition of quadprog to use. Could someone help me with this ? thanks

-

You need an n * n covariance matrix sigma and a vector of expected returns r.

Your objective is to minimize 1/2 * w' * sigma * w subject to r' * w > r_target and ones(1,n) * w = 1. Therefore, following the documentation on the Mathworks website you should call quadprog with

H = sigma
f = zeros(n,1)
A = r'
b = r_target
Aeq = ones(1,n)
beq = 1


That is,

w = quadprog(H,f,A,b,Aeq,beq)

-
Thanks so much also what else I need to add to prevent short selling ? (all weights >= 0) – Cemre Feb 22 '12 at 18:33
The easiest way is to include the argument lb initialized to all zeros (look at the documentation I linked to). – Chris Taylor Feb 22 '12 at 18:42

I do not fully agree to the answer of my predecessor or want to make some enhancements:

• $A$ needs to contain negative values because the linear constraints are defined as $Ax \leq b$.
• You can forbid short selling, when you extend (add) a linear constraint:

A = [A;-eye(nAssets)];
b = [b;zeros(1,nAssets)];

• I was too stupid to annualize the returns and covariance.

The following code helped me to solve the Markowitz model:

data = []; %your data as column based price matrix

nAssets = size(data, 2);
rets = data(2:end, :)./data(1:end-1,:)-1;

%annualize the returns and covariance
mu = 250 * mean(rets);
sigma = 250 * cov(rets);

%formulate the problem/optimization
r_target = 0.10; %r_target is the required return
f = zeros(nAssets, 1);    %there is no constant
A = [-mu; -eye(nAssets)]; %besides the returns we forbid short selling
b = [-r_target; zeros(nAssets, 1)]; % required return and weights greater/eqauls 0
Aeq = ones(1, nAssets);   %All weights should sum up...
beq = 1;                  %... to one (1)

%solve the optimization

Hi, I "annualize" the returns (mu) and the coveriance (sigma). Because it is a linear transformation you could do it at the end (fprintf). Pls let me know what you think. – Markus May 2 at 19:22