I am trying to formulate this simple MVO utility function with a linear transaction cost penalty added using Quadprog in MATLAB

tcost = 0.001; lambda = 4; mu = vector of expected returns (say 4x1) S = covariance matrix (4x4)

max w'*mu - lambda *w'Sw - lambda_TC * tcost *sum(abs(w(i) - w0(i))

I understand in order to be linear we can replace w(i) - w0(i) with a variable y(i) replacing the above equation to be

max w'*mu - lambda *w'Sw - lambda_TC * tcost *sum(y(i))

and we add the constraints y(i)>= w(i) - w0(i) and y(i)>= - (w(i) - w0(i))

I additionally have a long only constraint and the sum of the weights should sum to 1

I am unsure how to formulate this in quadprog in MATLAB. Any help will be appreciate.

Thanks very much!


1 Answer 1


I'm not that familiar with MATLAB. However, in quadratic programming the main issue I've found is setting up the problem correctly and then the coding becomes much easier.

As you noted this problem can be expressed as a quadratic cone problem and solved by quadprog but a good amount of more work needs to be done to get this in the correct form.

  1. You should convince yourself that under the change of variables y = w - w0 you suggested that the problem actually becomes the below with sum(y) = 0 (assuming sum(x0) = 1) and we no longer have to worry about w.

    max y'*mu - lambda * y'Sy - lambda_tc * tcost * sum(abs(y)) + constant

You can calculate that constant if you want but because we are maximizing it doesn't matter. The problem is this problem is still not in a quadradic programming form because it still has a absolute value function.

  1. The next step is to get rid of the abs(). The trick here is to double the number of optimization variables as you allude to in the question. We call y = (y_plus - y_minus)/2 where now we have two new constraints y_plus >= w - w0 and y_minus >= -(w - w0).

  2. This appears to make the problem more complicated, but note if we define z to be the vector of (y_plus, y_minus) for 8 variables we can rewrite the problem again as the below with sum(z) = 0.

    max z'*mu - lambda * z'Sz - lambda_tc * tcost * sum(z)

where mu and S have extended to double their original size. mu is just repeated twice with a sign flip (now has 2*4 members) and H now has two block diagonal S matrices and block zeros on the off diagonals (8 by 8 matrix in total). However, now we have removed the absolute value from the problem!

  1. To put this in standard form we just need to combine the linear terms in z. So the equations for 'f' in quadprod would look like (Please double check my signs here)

    (mu_i - lambda_tc * tcost) * yplus_i and (mu_i + lambda_tc * tcost) * yminus_i

  2. Now this problem can be put in the quadprog with the above 'f' and extended (doubled-size) 'S' as 'H'. Aeq,beq can constrain sum(z) = 0 and A,b constrain each yplus_i>0 and yminus_i<0 as y_plus are changes to w that are positive and y_minus are changes that are negative.

  • $\begingroup$ Thanks v much. Super super helpful. However I am still confused on how the last formulation $\endgroup$
    – qfd
    Nov 11, 2014 at 4:07
  • $\begingroup$ Thanks v much. Super super helpful. However I am still confused on how the last formulation. Max z'mu etc can be fit into the form x'mu - lambda * xSx', the tcost penalty term is not included in the quadprog formulation whereas in 3. The penalty term is still included $\endgroup$
    – qfd
    Nov 11, 2014 at 4:15
  • $\begingroup$ also does it mean values for lambda_tc and tcost do not matter then? $\endgroup$
    – qfd
    Nov 11, 2014 at 12:23
  • $\begingroup$ could you give me an example of how the Aeq, beq, and A and b matrix will look like as well. Thanks very much! $\endgroup$
    – qfd
    Nov 11, 2014 at 22:21
  • $\begingroup$ tcosts matter very much. I edited the problem to make it easier to understand how the linear terms combine. The changes in 'f' are probably the most complicated part here. $\endgroup$
    – rhaskett
    Nov 12, 2014 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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