1

I recently got very interested in portfolio optimization and started playing around in R, to create a minimum variance portfolio,

library(quadprog)
Dmat <- matrix(c(356.25808, 12.31581, 261.88302, 12.31581, 27.24840,
18.50515,261.88302, 18.50515,535.45960), nrow=3, ncol=3)
dvec <- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1)
A.Equality <- matrix(c(1,1,1), ncol=1)
Amat <- cbind(A.Equality, dvec, diag(3), -diag(3))
bvec <- c(1, 5.2, rep(0, 3), rep(-0.5, 3))
qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=1)

Example above has the following constraints ( example from here)

There are 4 constraints:

  • sum of weights equal to 1
  • portfolio expected return equals to 5.2%
  • each asset weight greater than 0
  • each asset weight smaller than .5

I am currently trying to refresh my matrix/vector math, I would really appreciate if someone could tell me how you add the individual constraints together in aMat and bvec and the basic algebra background for it. And as another question how a constraint for weights <0 (shorting) would look like.

thanks in advance

Community
  • 1
  • 1
ThatQuantDude
  • 687
  • 8
  • 23

1 Answers1

1

First step is to write down the mathematical model. That could look like:

enter image description here

The next part is to implement this in R's quadprog. That could look like:

enter image description here

  • Adding comments to the code may help to understand it later
  • Quadprog does not allow simple lower- and upper-bounds on the variables, so we need to convert these to >= inequalities.
  • Notice that Quadprog minimizes 0.5*x'Qx. That has the same result as minimizing x'Qx.
  • Shorting can be allowed by using other lower-bounds on x.
  • Your data makes the model infeasible. I loosened the upper-bound on allocations from 0.5 to 0.8.
Erwin Kalvelagen
  • 11,005
  • 2
  • 8
  • 31
  • Thanks Erwin very helpful, just as a follow up, if i set lo < 0 I will allow weights to become negative i.e shorting ? And why is up negative (-up)? – ThatQuantDude Dec 08 '15 at 21:16
  • Yes, lo<0 will allow shorting. I pass on -up because the constraint x <= up translates to -x >= -up which is the form quadprog expects. – Erwin Kalvelagen Dec 08 '15 at 21:21
  • Ah ok, perfect. Also is there a way of already passing in an initial guess for the weights, e.g for a rebalance to use the old weights to make the solver to converge faster ? – ThatQuantDude Dec 09 '15 at 09:14
  • 1
    That depends on the solver. R's quadprog does not have this functionality. Note that these QPs often solve quite fast unless we want to solve huge problems. For those problems there exist powerful commercial solvers. – Erwin Kalvelagen Dec 09 '15 at 09:31
  • I was looking at using constrOptim yesterday as I had the suspicious that solve.QP got me stuck in a local minimum which isn't the overall best solution. Also I guess moving from linear constraints to non-linear ones (e.g risk model) I will have the benefit of using SOCP. – ThatQuantDude Dec 09 '15 at 09:50
  • 1
    There are no local optima in standard portfolio models (they are convex problems). Trying out different solvers is often a good idea however. – Erwin Kalvelagen Dec 09 '15 at 10:08
  • Hi Erwin, sorry to bother you once more, but I have a follow up question to see if i got the concept right. Let's say I want to model a constraint to not have more change than +- 3% in one sector could I do sth like Tech Telco Pharma Stock1 1 0 0 Stock2 0 1 0 Stock3 0 0 1 Stock1 -1 0 0 Stock2 0 -1 0 Stock3 0 0 -1 and b_vec = (-0.03, -0.03, -0.03, 0.03, 0.03, 0.03) Or how would you do that ? thanks – ThatQuantDude Jan 07 '16 at 15:20
  • Yes. Translate % change in lower and upper bounds and then plug in bvec. – Erwin Kalvelagen Jan 07 '16 at 16:03
  • can appreciate it has been a while since this exchange took place, but I'm looking through worked out examples of how you can do portfolio optimization via quadprog in stackoverflow and this is one of the better ones. I've followed the example and it's laid out great! I'm wondering how you would go about introducing leverage into the mix? I.e. allowing portfolio weights to go up to 2 or 200%? Any help appreciated! – codesaurus Nov 25 '19 at 12:14
  • If no borrowing cost, just interpret the results as percentage allocations of any sized budget. – Erwin Kalvelagen Nov 25 '19 at 14:19
  • sure, makes sense, thanks! I actually managed to implement it in the formulation by amending the "b" term to (c(2, totalReturn, lo, -up), was just about to update. – codesaurus Nov 25 '19 at 16:17