I am using MATLAB to do an optimisation. The QP minimisation problem is set up in the standard form shown below. The optimisation is used to calculate the weights (x vector in the equation below) of a portfolio.

Min    0.5 (x'Fx) + c'x


x_low <= x <= x_up
b_low <= Ax <= b_up


c is a n x 1 vector in the objective function
x is a n x 1 vector (weights of the stocks in the portfolio)
F is a n x n matrix in the objective function
A is the linear constraint matrix
b_low & up  are the lower and upper bounds for the linear constraints

Trying to follow an example but have two issues. Firstly say the portfolio has 500 stocks the x vector passed into the optimiser (x here is our initial guess) will have the dimension of 1000 x 1. The second 500 will have the opposite sign of the first 500, I do not understand why this is?

Also the F matrix does something similar. Say I have a matrix R which contains some risk factors, which is 500 x 500.

Then F is set to the following (sorry not sure how to show matrices on this site properly)

 F =  R   -R
     -R    R

Again why would you do this?


The solver is actually Tomlab (user guide of the solve is here link).

Further Info

Just stepping through the code.

x0 is passed as an intial guess vector 1000 x 1. The first 500 weights are the previous weights. The next 500 weights are all set to zero.

x_up is obviously also a 1000 x 1 vector to. Looking further into the code. The first 500 weights are the upper bounds on the buys the next 500 are the upper bounds on the sells.

x_low is the same but for the lower bounds. First 500 weights are the lower bounds on the buys the next 500 are the lower bounds for the sales.

  • 1
    $\begingroup$ Please provide some more information about the constraints the example employs. There are techniques to reformulate optimization problems that simplify those but I doubt thats the problem here. Does your example use a different solver than matlab? Have you tried solving the example your way and compared the solutions? Further more: If you look at F and multiply with vectors $(x,-x)^T$ and $(x,-x)$ from both sides, you will miss the factor $1/2$. Probably the author moved into the $c$ but without any more information its really hard to say... $\endgroup$
    – vanguard2k
    Oct 1, 2014 at 13:52
  • $\begingroup$ The constraints are pretty simple just limit the exposure to some factor (say size). There isn't a huge amount of information I can give on the constraints. However I believe you are correct to assume that this is not a case of using some technique to simplify the problem. I have not tried solving the example my way as I have to use this solver, which works I'm just not understanding the setup $\endgroup$
    – mHelpMe
    Oct 1, 2014 at 13:58

1 Answer 1


OK one thing that comes to my mind is the standard trick to reformulate constraints like $|x_i|<=c$ (limiting exposure of $x_i$ while still allowing negative weights). Notice, that $|x_i| \leq c$ is not a linear constraint, so the solver wont work in this case. A little trick can help:

You split up the variables into positive and negative parts: $x_i=x_i^+ + x_i^-$ Now, by definition, $x_i^+ \geq 0$ and $x_i^-\leq 0$

now, notice that $|x_i|= x_i^+ - x_i^-$ and the constraint becomes linear:

$$ x_i^+ - x_i^- \leq c, \quad x_i^+ \geq 0, \quad x_i^-\leq 0$$

If you have other linear constraints for $x_i$, you simply plug in $x_i = x_i^+ + x_i^-$.

This method will double the dimensions but magically "linearises" the constraints. Of yourse, you need to reformulate the whole functional (for example $F = 1/2* (R,-R; -R,R)$ which almost corresponds to your observation)

  • $\begingroup$ that probably does make sense. As some of the portfolio's are allowed to go short. So if the portfolio was long only there would be no need for this 'trick'? Sorry I am being stupid but why is |xi| <= c not a linear constraint? $\endgroup$
    – mHelpMe
    Oct 1, 2014 at 14:25
  • $\begingroup$ Its not linear because $|-x|\neq-|x|$ which would hold for a linear function. $\endgroup$
    – vanguard2k
    Oct 1, 2014 at 14:31
  • $\begingroup$ yes of course! Last question, on splitting up the variables. if I have a 3 stock portfolio with the weights x = [0.8, -0.3, 0.5]. I would split this up making x = [0.8, -0.3, 0.5, -0.8, 0.3, -0.5]? $\endgroup$
    – mHelpMe
    Oct 1, 2014 at 14:39
  • 1
    $\begingroup$ Hm, thats why I wasn't sure this is the right explanation because $x$ should be $x = [0.8,0,0.5,0,-0.3,0]$ in this case (splitting $x$ up in a positive and a negative part). Now, $x(1:3)+x(4:6)$ is the original portfolio and $x(1:3) - x(4:6)$ its component-wise absolute value. $\endgroup$
    – vanguard2k
    Oct 1, 2014 at 14:46
  • $\begingroup$ I've just been stepping through the code to try find out more. I've updated the post - hopefully this will shed some more light on it $\endgroup$
    – mHelpMe
    Oct 1, 2014 at 15:10

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.