约束条件下R的长/短投资组合优化

时间:2014-06-23 20:37:11

标签: r mathematical-optimization portfolio quantitative-finance quadprog

我想解决一个相当常见(和简单)的优化问题,虽然似乎没有关于此的帖子:长/短市场中性最小方差优化。 " R伪代码":

中的优化形式
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

其他问题中缺少的这个问题的关键是&#34;书本大小&#34;约束。在长/短优化中,您需要此约束,否则您将获得无意义的结果。这是二次优化问题,但由于约束中的&#34; abs&#34; ,我们有非线性约束。有一个众所周知的(在某些圈子中我认为)诀窍来改变&#34; abs&#34;从非线性约束到线性约束的约束。我们通过在方程中引入辅助变量来实现这一点(参见lp_solve reference guide: absolute values处的解释)。

在给出多因素风险模型输入的情况下,我已经编写了这个函数来计算最小投资组合方差权重:

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

}

我通过以下单元测试调用(欺骗多因素模型输入):

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)

这会引发以下错误:

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

因此,我的问题是,如何在R中的solve.QP中实现长/短优化中的abs约束?

另外请注意,论文Portfolio Optimization with Transaction Costs显示了如何在Matlab中执行此操作,但这似乎不适用于R中的solve.QP。

0 个答案:

没有答案