Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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 enter image description here

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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)
share|improve this answer
    
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
%data = xlsread('your_excel_sheet.xlsx');

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
w = quadprog(sigma,f,A,b,Aeq,beq);

%print the solution
fprintf(2, 'Risk: %.3f%%\n', sqrt(w'*sigma*w)*100);
fprintf(2, 'Ret: %.3f%%\n', w'*mu'*100);

Please let me know if you wish a more detailed description or some example data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.