0
$\begingroup$

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

2 Answers 2

1

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)
  • 0
    Thanks so much also what else I need to add to prevent short selling ? (all weights >= 0)2012-02-22
  • 0
    The easiest way is to include the argument `lb` initialized to all zeros (look at the documentation I linked to).2012-02-22
1

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.

  • 0
    I am just solving very similar Markowitz optimalization problem so I have a question about your matlab code. What do you calculate in variable **rets**? And why you multiplies mean and cov 250 * ? Thanks.2016-02-18
  • 0
    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.2016-05-02