Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带约束的R中的长短期投资组合优化_R_Mathematical Optimization_Portfolio_Quantitative Finance_Quadprog - Fatal编程技术网

带约束的R中的长短期投资组合优化

带约束的R中的长短期投资组合优化,r,mathematical-optimization,portfolio,quantitative-finance,quadprog,R,Mathematical Optimization,Portfolio,Quantitative Finance,Quadprog,我想解决一个相当常见(且简单)的优化问题,尽管似乎没有关于这方面的帖子:多头/空头市场中性最小方差优化。“R伪码”中的优化形式: 因此,我的问题是,如何在R中的solve.QP中的长/短优化中实现abs约束? 作为进一步说明,本文展示了如何在Matlab中实现这一点,但是这在R中的solve.QP中似乎不起作用。来自优秀的系统投资者工具箱,请参见130/30优化()谢谢@Osssan;那里有很棒的代码,尽管我希望知道为什么我的方法不起作用;他的代码也解决了风险/回报的权衡问题,而不是我试图解决的

我想解决一个相当常见(且简单)的优化问题,尽管似乎没有关于这方面的帖子:多头/空头市场中性最小方差优化。“R伪码”中的优化形式:

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


作为进一步说明,本文展示了如何在Matlab中实现这一点,但是这在R中的solve.QP中似乎不起作用。

来自优秀的系统投资者工具箱,请参见130/30优化()谢谢@Osssan;那里有很棒的代码,尽管我希望知道为什么我的方法不起作用;他的代码也解决了风险/回报的权衡问题,而不是我试图解决的优化问题(以最小的预期回报);我不知道这对我所遇到的错误是否有意义。是的,我知道这可能不会直接回答你的问题,但可能函数
min.risk.portfolio
min.te.portfolio
等可能会让你对你的具体问题有一些见解。我知道这很旧,但我现在刚刚找到这篇文章。你的股票回报在哪里?我看到了numStocks,但我不知道你从哪里得到的。我敢打赌,你有一个或多个安全性的坏数据。请看下面的链接。按原样运行它。当你觉得舒服的时候,把那些股票代码换成你真正想分析的。
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
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!