quadprog optimization

Ram Ahluwalia picture Ram Ahluwalia · May 10, 2012 · Viewed 7.8k times · Source

Here's an interesting puzzle.

Below is an R snippet that identifies the tangency point of a quadratic function with respect to a line drawn from the point (0,rf) on the y-axis.

For those familiar with portfolio theory, this point is in return and risk space and the solution is set of weights that define the tangency portfolio (max sharpe ratio). The snippet allows for negative weights (i.e. shorts) and there is one equality weight constraint which requires the sum of the weights = 1.

require(quadprog)

# create artifical data
nO     <- 100     # number of observations
nA     <- 10      # number of assets
mData  <- array(rnorm(nO * nA, mean = 0.001, sd = 0.01), dim = c(nO, nA))
rf     <- 0.0001     # riskfree rate (2.5% pa)
mu     <- apply(mData, 2, mean)    # means
mu2    <- mu - rf                  # excess means

# qp
aMat  <- as.matrix(mu2)
bVec  <- 1 # set expectation of portfolio excess return to 1
zeros <- array(0, dim = c(nA,1))
solQP <- solve.QP(cov(mData), zeros, aMat, bVec, meq = 1)

# rescale variables to obtain weights
w <- as.matrix(solQP$solution/sum(solQP$solution))

# compute sharpe ratio
SR <- t(w) %*% mu2 / sqrt(t(w) %*% cov(mData) %*% w)

My question -- how to adapt the code to solve for the optimal set of weights such that the sum of weights sum to an arbitrary number (including the corner case of a self-financing portfolio where the sum of weights = 0) as opposed to unity?

Alternatively, you might consider adding an element 'cash' to the covariance matrix with variance-covariance of 0, and add an equality constraint requiring the weight on cash = 1. However this matrix would be not be positive semi-definite. Also I suspect the non-cash weights might be trivially zero.

Answer

Vincent Zoonekynd picture Vincent Zoonekynd · May 10, 2012

Let us first explain why this actually produces the maximum Sharpe ratio portfolio.

We want w to maximize w' mu / sqrt( w' V w ). But that quantity is unchanged if we multiply w by a number (it is "homogeneous of degree 0"): we can therefore impose w' mu = 1, and the problem of maximizing 1 / sqrt( w' V w ) is equivalent to minimizing w' V w. The maximum Sharpe ratio portfolio is not unique: they form a line. If we want the weights to sum up to 1 (or any other non-zero number), we just have to rescale them.

If we want the weights to sum up to 0, we can add that constraint to the problem -- it only works because the constraint is also homogeneous of degree 0. You will still need to rescale the weights, e.g., to be 100% long and 100% short.

solQP <- solve.QP(cov(mData), zeros, 
  cbind(aMat,1), 
  c(bVec,0), 
  meq = 2
)

# Let us compare with another solver
V <- cov(mData)
library(Rsolnp)
r <- solnp(
  rep(1/length(mu), length(mu)),
  function(w) - t(w) %*% mu2 / sqrt( t(w) %*% V %*% w ),
  eqfun = function(w) sum(w),
  eqB   = 0,
  LB = rep(-1, length(mu))
)
solQP$solution / r$pars  # constant