权重约束下的均值方差优化

时间:2016-03-29 14:06:16

标签: r optimization portfolio

我想找到投资组合的权重,以最大化3风险资产案例的锐化率。所有资产的权重总和应该等于2,资产1的权重被强制为1,所有资产权重> = 0(即问题是通过仅调整资产资产2的权重来最大化投资组合风险调整后的回报3,它们不超过1且> = 0)。这是使用quadprog编程问题的正确方法吗?

    library(quadprog)
    covmat <- matrix(c(3.235343e-02, -3.378191e-03, -1.544574e-05,
                       -3.378191e-03,  8.769166e-03,  1.951734e-06,
                       -1.544574e-05,  1.951734e-06,  2.186799e-06),3,3)


    A <- rbind(c(1,1,1),diag(3))
    b <- c(2,1,0,0)  # those are the constraints, sum of weights are 2 and weights of asset1 = 1 
    c <- c(0,0.1,0.05)  # those are the assets returns, asset1 hasd a zero return but I want him to have a 100% weight out of the available 200% in my problem

# solve QP model
    solve.QP(covmat,dvec=c,Amat=t(A),bvec=b,meq=2)$solution

1 个答案:

答案 0 :(得分:0)

Solve.QP未优化夏普比率(SR)。如帮助?solve.QP中所述,它正在最小化此功能:

min(-d^T b + 1/2 b^T D b) with the constraints A^T b >= b_0.

如果您想要最大化SR尝试此http://comisef.wikidot.com/tutorial%3atangencyportfolio 但显然这是为了优化而没有限制。

嗯,有可能优化最小化几个给定回报的风险。换句话说,通过限制识别有效边界(EF)的相关段,并计算夏普比率(SR)。切线显然将是最大化SR的投资组合。

鉴于您的数据和限制:

#Find EF
#Min variance portfolio
aMat <- cbind(rep(1,nrow(covmat)),diag(1,nrow(covmat)))
bVec  <- c(2,1,0,0)
zeros <- array(0, dim = c(nrow(covmat),1))
solQP <- solve.QP(covmat, zeros, aMat, bVec, meq = 1)

# weights and return for minimum variance portfolio
w.mv <- solQP$solution
r.mv<-t(w.mv) %*% excret

#Identify tangent approximately
#for that find min var portfolio for a relevant sequence of returns
#adding a return restriction to the optimization

sret<-seq(r.mv,max(excret)*1.1,length.out =50) #the maximum by try and error
sret<-sort(unique(c(max(excret),sret)))

rp=array(r.mv,1)
sp=sqrt(t(w.mv) %*% covmat %*% w.mv)
wp=t(matrix(w.mv))
aMatt <- cbind(excret,aMat)
# solve min var for every given return
for (ri in sret[-1]){
  bVect  <- c(ri,bVec)
  solQP <- solve.QP(covmat, zeros, aMatt, bVect, meq = 2)
  wp=rbind(wp, solQP$solution)
  rp<-c(rp,t(solQP$solution) %*% excret)
  sp<-c(sp,sqrt(t(solQP$solution) %*% covmat %*% solQP$solution))
}
IS=rp/sp #sharpe index
cbind(wp,sp,rp,IS)
wp[which.max(IS),] #tangent
cbind(wp,sp,rp,IS)[which.max(IS),]
plot(c(sp,diag(covmat)^.5),c(rp,excret))

#As you can see in the plot you have a corner solution
(c.sol<-c(1,1,0))
c(st=sqrt(t(c.sol) %*% covmat %*% c.sol),
rt=t(c.sol) %*% excret,
ISt=t(c.sol) %*% excret/sqrt(t(c.sol) %*% covmat %*% c.sol))

输出将是:

> wp[which.max(IS),] #tangent
[1] 1.00000000 0.98339763 0.01660237
> cbind(wp,sp,rp,IS)[which.max(IS),]
                                         sp         rp 
1.00000000 0.98339763 0.01660237 0.18490315 0.09916988 
        IS 
0.53633418 
> plot(c(sp,diag(covmat)^.5),c(rp,excret))
> (c.sol<-c(1,1,0))
[1] 1 1 0
> c(st=sqrt(t(c.sol) %*% covmat %*% c.sol),
+ rt=t(c.sol) %*% excret,
+ ISt=t(c.sol) %*% excret/sqrt(t(c.sol) %*% covmat %*% c.sol))
       st        rt       ISt 
0.1853813 0.1000000 0.5394288 

enter image description here