1

I would like to solve a fairly common (and simple) optimization problem, though it seems there are no posts on this: long/short market neutral minimum variance optimization. The form of the optimization in "R pseudo-code":

min (t(h) %*% D %*% h ) s.t.    # minimize portfolio variance, h weights, D covar matrix
  sum(h) == 0                   # market neutral; weights sum to 0
  sum(abs(h)) == 1              # book-size/fully-invested; abs(weights) sum to 1
  h %*% e >= threshold          # the portfolio expected return is > some threshold
  h <= maxPos                   # each long position is less than some maxPos
  h >= -maxPos                  # each short position is greater than -maxPos

The key in this question which is missing in other questions is the "book-size" constraint. In long/short optimization, you need this constraint otherwise you get nonsense results. This is a quadratic optimization problem however because of the "abs" in the constraints, we have non-linear constraints. There is a well-known (in certain circles I suppose) trick to transform an "abs" constraint from a non-linear constraint to a linear constraint. We do this by introducing auxiliary variables into the equation (see this explanation at lp_solve reference guide: absolute values).

I have written this function to calculate the minimum portfolio variance weights, given a multi-factor risk model input:

portSolveMinVol <- function(er,targetR,factorVols,factorCorrel,idioVol) {
  require(quadprog)

  # min ( -d'b + 1/2 b'Db)
  # A'b >= b_0

  # b = weights --> what we are solving for
  # D = covariance matrix
  # d = we can set this to zero as we have no linear term in the objective function

  # set up the A matrix with all the constraints
  #   weights sum to 0
  #   abs weights sum to 1
  #   max pos < x, greater than -x
  #   return > some thresh

  numStocks <- length(er) # er is the expected return vector
  numAbs <- numStocks # this is redundant but I do this to make the code easier to read
  VCV <- factorVols %*% t(factorVols) * factorCorrel  # factor covariance matrix
  S <- matrix(0,ncol=numStocks,nrow=numStocks)
  diag(S) <- idioVol * idioVol # stock specific covariance (i.e., 0's except for diagonal)
  common <- factorBetas %*% VCV %*% t(factorBetas) # stock common risk covar matrix

  # need to fill in the Dmat b/c of the abs constraint
  Dmat <- matrix(0,ncol=numStocks+numAbs,nrow=numStocks+numAbs)
  Dmat[1:numStocks,1:numStocks] <- (common + S)  # full covariance matrix

  dvec <- rep(0,numStocks + numAbs)  # ignored but solve.QP wants it

  # A'b >= b_0
  Amat <- matrix(0,nrow= 3,ncol=numStocks + numAbs)
  Amat[1,] <- c(rep(1,numStocks),rep(0,numAbs)) # sum weights equal zero
  Amat[2,] <- c(rep(0,numStocks),rep(1,numAbs)) # sum abs weights equal 1

  Amat[3,] <- c(er,rep(0,numAbs)) # expected return >= threshold

  # add contraints on min and max pos size
  maxpos <- matrix(0,nrow=numStocks,ncol=numStocks + numAbs)
  minpos <- matrix(0,nrow=numStocks,ncol=numStocks + numAbs)
  for(i in 1:numStocks) {
    maxpos[i,i] = -1  # neg and neg b/c of >= format of contraints
    minpos[i,i] = 1  # pos and neg b/c of >= format of contraints
  }

  absmaxpos <- matrix(0,nrow=numStocks,ncol=numStocks + numAbs)
  absminpos <- matrix(0,nrow=numStocks,ncol=numStocks + numAbs)

  # add contraints on the sum(abs(wi)) = 1 and each
  for(i in 1:numStocks) {
    absmaxpos[i,i] = 1
    absmaxpos[i,i+numAbs] = -1

    absminpos[i,i] = 1
    absminpos[i,i+numAbs] = 1
  }

  # Set up the Amat

  Amat <- rbind(Amat,maxpos,minpos,absmaxpos,absminpos)

  # set up the rhs
  bvec <- c(0,                         # sum of weights
            1,                         # sum of abs weights
            0.005,                     # min expected return
            rep(-0.025,numStocks),     # max pos
            rep(-0.025,numStocks),     # min pos
            rep(0,numAbs),             # abs long dummy var
            rep(0,numAbs))             # abs short dummy var

  # meq is the number of first constraints that are equality
  res <- solve.QP(Dmat, dvec, t(Amat), bvec, meq=2)

  res

}

Which I call with the following unit testing (spoofing the multi-factor model inputs):

set.seed(1)
nStocks <- 100
nBetas <- 5
er <-rnorm(nStocks,mean=0.0012,0.0075)
factorVols <- 0.08 + runif(nBetas,0,0.15)
factorCorrel <- matrix(rep(0,nBetas*nBetas),nrow=nBetas,ncol=nBetas)
for(i in 1:(nBetas)) {
  for(j in 1:(nBetas)) {
    factorCorrel[i,j] = rnorm(1,mean=0.2,sd=0.05)
    factorCorrel[j,i] = factorCorel[i,j]
  }
}
diag(factorCorrel) <- 1
idioVol <- abs(rnorm(nStocks,mean=0.01,sd=0.05))
res <- portSolveMinVol(er,0.005,factorVols,factorCorrel,idioVol)

This throws the following error:

Error in solve.QP(Dmat, dvec, t(Amat), bvec, meq = 2) :    matrix D in
quadratic function is not positive definite!

As such, my question is, how does one implement the abs constraint in long/short optimization in solve.QP in R?

As a further note, the paper Portfolio Optimization with Transaction Costs shows how to do this in Matlab, however this does not seem to work in solve.QP in R.

4
  • From the excellent Systematic Investor Toolbox see 130/30 optimization (systematicinvestor.wordpress.com/2011/10/18/…) Jun 23, 2014 at 20:44
  • Thank you @Osssan; there is great code there, though I was hoping to learn why my approach does not work; also his code solves the risk/return trade-off, not the optimization I am try to solve (with a minimum expected return); I don't know if that is meaningful for the error I am getting.
    – Jonathan
    Jun 24, 2014 at 15:38
  • Yes, I was aware that this will not probably answer your question directly but maybe the functions min.risk.portfolio,min.te.portfolio etc. might give you some insight on your specific problem. Jun 24, 2014 at 15:47
  • 1
    I know this is old, but I just found this post right now. Where are your stock returns being fed in? I see 'numStocks' but I don't see where you're getting that from. I'll bet anything you have bad data for one security, or more. Take a look at the link below. Run it, as is. When you're comfortable with it, swap out those tickers for the ones you REALLY want to analyze. economistatlarge.com/portfolio-theory/r-optimized-portfolio
    – ASH
    Oct 1, 2015 at 6:44

0

Your Answer

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