【发布时间】:2020-09-24 16:04:03
【问题描述】:
我有一个名为 A 到 X 的 24 种资产的虚拟基准。我有一个虚拟投资组合,它只能交易这些资产的子集 (8)。上下边界表示我在这些资产中的最小和最大权重。我想尽量减少我的投资组合对基准的跟踪误差。我使用solve.QP 来获得解决方案,但我的解决方案不是最佳解决方案。
我的 cov 矩阵和边界是:
cov = data.frame(c(0.016,0.013,0.012,0,0.01,0.012,0.003,-0.001,0.002,0.002,0.002,0.002,0.001,0.002,0.001,0.001,0.003,0.002,0,0.001,-0.005,0,0.001,0.002),c(0.013,0.012,0.011,0.001,0.009,0.01,0.003,-0.001,0.002,0.001,0.002,0.002,0.001,0.002,0.001,0,0.002,0.002,0,0.001,-0.004,0,0.001,0.001),c(0.012,0.011,0.011,0.001,0.007,0.011,0.003,0,0.002,0.001,0.002,0.002,0.002,0.001,0.001,0,0.001,0.002,0,0.001,-0.005,0,0.001,0.001),c(0,0.001,0.001,0.012,0.001,0,0.001,-0.001,0.002,-0.001,0.001,0.001,0.001,0,0.001,0.001,-0.002,0.001,0,-0.001,-0.003,-0.001,0.001,-0.002),c(0.01,0.009,0.007,0.001,0.014,0.007,0.004,0,0.002,0.001,0.002,0.003,0,0.005,0.003,0.003,0.003,0.003,-0.002,0.001,-0.005,-0.001,0.001,0.001),c(0.012,0.01,0.011,0,0.007,0.01,0.003,0,0.002,0.001,0.002,0.002,0.002,0.001,0.001,0,0.001,0.002,0.001,0.001,-0.004,0,0.001,0.001),c(0.003,0.003,0.003,0.001,0.004,0.003,0.008,0.001,0.002,0.003,0.002,0.003,0.002,0.001,0.001,0.001,0,0.001,0,-0.001,0,-0.001,0.001,-0.001),c(-0.001,-0.001,0,-0.001,0,0,0.001,0.001,0,0.002,0,0,0,0,0,0,0,0,0,0,0.002,-0.001,0,0),c(0.002,0.002,0.002,0.002,0.002,0.002,0.002,0,0.004,0.001,0.002,0.002,0.002,0.002,0.001,0.001,0,0.001,0.001,0,-0.002,0,0.002,-0.002),c(0.002,0.001,0.001,-0.001,0.001,0.001,0.003,0.002,0.001,0.004,0.001,0.001,0.001,0.001,0,0,0.001,0,0,-0.001,-0.001,-0.001,0,0),c(0.002,0.002,0.002,0.001,0.002,0.002,0.002,0,0.002,0.001,0.002,0.002,0.001,0.002,0.001,0.001,0.001,0.001,0,0,-0.001,0,0,-0.001),c(0.002,0.002,0.002,0.001,0.003,0.002,0.003,0,0.002,0.001,0.002,0.003,0.001,0.002,0.001,0.001,0,0.001,0,0,0,-0.001,0,-0.001),c(0.001,0.001,0.002,0.001,0,0.002,0.002,0,0.002,0.001,0.001,0.001,0.003,0,0,0,0,0,0.001,0,0,0,0.001,-0.001),c(0.002,0.002,0.001,0,0.005,0.001,0.001,0,0.002,0.001,0.002,0.002,0,0.01,0.002,0.002,0.002,0.002,-0.001,0,-0.001,-0.001,0,0),c(0.001,0.001,0.001,0.001,0.003,0.001,0.001,0,0.001,0,0.001,0.001,0,0.002,0.003,0.002,0,0.001,-0.001,0,-0.001,0,0,0),c(0.001,0,0,0.001,0.003,0,0.001,0,0.001,0,0.001,0.001,0,0.002,0.002,0.004,0,0.001,-0.001,0.001,-0.004,0,0,0),c(0.003,0.002,0.001,-0.002,0.003,0.001,0,0,0,0.001,0.001,0,0,0.002,0,0,0.004,0.001,0,0.001,-0.003,0,0,0.001),c(0.002,0.002,0.002,0.001,0.003,0.002,0.001,0,0.001,0,0.001,0.001,0,0.002,0.001,0.001,0.001,0.003,0,0,-0.002,0,0.001,0),c(0,0,0,0,-0.002,0.001,0,0,0.001,0,0,0,0.001,-0.001,-0.001,-0.001,0,0,0.007,0,0.002,0.001,0.001,-0.001),c(0.001,0.001,0.001,-0.001,0.001,0.001,-0.001,0,0,-0.001,0,0,0,0,0,0.001,0.001,0,0,0.003,0.001,0.002,0.002,0),c(-0.005,-0.004,-0.005,-0.003,-0.005,-0.004,0,0.002,-0.002,-0.001,-0.001,0,0,-0.001,-0.001,-0.004,-0.003,-0.002,0.002,0.001,0.042,0.002,0.001,-0.002),c(0,0,0,-0.001,-0.001,0,-0.001,-0.001,0,-0.001,0,-0.001,0,-0.001,0,0,0,0,0.001,0.002,0.002,0.005,0.002,0),c(0.001,0.001,0.001,0.001,0.001,0.001,0.001,0,0.002,0,0,0,0.001,0,0,0,0,0.001,0.001,0.002,0.001,0.002,0.002,-0.001),c(0.002,0.001,0.001,-0.002,0.001,0.001,-0.001,0,-0.002,0,-0.001,-0.001,-0.001,0,0,0,0.001,0,-0.001,0,-0.002,0,-0.001,0.007))
colnames(cov) = LETTERS[seq( from = 1, to = 24 )]
rownames(cov) = LETTERS[seq( from = 1, to = 24 )]
lowerbound = as.matrix(c(0.164,0.131,0.037,0,0.039,0.029,0,0.085,0,0,0.046,0.059,0,0,0,0,0,0,0,0,0,0,0,0))
upperbound = as.matrix(c(1,1,1,0,1,1,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0))
然后我优化如下:
require(quadprog)
library(Matrix)
Dmat <- nearPD(as.matrix(cov))$mat # make matrix positive definite
N = length(cov)
dvec = rep(0, N)
A.mat = cbind(rep(1, N), diag(1, N), diag(-1, N))
bvec = c(1, lowerbound, -upperbound)
solution = data.frame(round(solve.QP(as.matrix(Dmat), dvec, A.mat, bvec, meq = 0)$solution,2))
rownames(solution) = LETTERS[seq( from = 1, to = 24 )]
solution
这个解决方案不是最优解,资产 H 得到 50%,这不是最优的。你知道我做错了什么吗?
更新 20200612:我尝试了使用 Rsolnp 包中的 solnp 的不同方式:
portfolio = c("A", "B", "C", "E", "F", "H", "J", "K") #the assets owned in the portfolio
weights_bm = c(0.164,0.131,0.037,0.035,0.039,0.029,0.012,0.085,0.014,0.007,0.046,0.059,0.009,0.03,0.019,0.051,0.02,0.015,0.011,0.065, 0.021,0.049,0.047,0.006)
sol_test <- function(portfolio,weights_bm ,lx,ux,cov){
cov = as.matrix(rbind(cbind(cov,cov),cbind(cov,cov)))
wt = rbind(as.matrix(weights_bm*(ux)),as.matrix(-weights_bm))
obj <- function(x){na.omit(t(x) %*% cov %*% x)} #tracking error
lx = rbind(as.matrix(lx),as.matrix(wt[(nrow(wt)/2+1):nrow(wt)]))
ux = rbind(as.matrix(ux),as.matrix(wt[(nrow(wt)/2+1):nrow(wt)]))
equal <- function(x) {
equal <- sum(x)
}
# solver function
sol <- solnp(wt,
fun = obj,
eqfun = equal,
eqB = 1,
LB = as.matrix(lx),
UB = as.matrix(ux))
# output from optimalisation
wt <- sol$pars[1:length(weights_bm )] # optimal portfolio allocation
names(wt) <- colnames(weights_bm)
return(wt)
}
sol_test(portfolio,weights_bm,lowerbound,upperbound,cov)
但是这不起作用,因为投资组合权重的总和为 0.59,并且只是下限,而不是优化的结果。
【问题讨论】:
-
嗨,为什么 50% 不是最优的?结果对我来说看起来很合理。标准只是最小化投资组合方差,资产 H 与资产 A、B、C、E、F 的协方差为负,与资产 K 和 L 的正协方差非常低,后者的方差也较低,权重次之
-
嗨 Chinsoon,很高兴再次收到您的来信。如果是我最小化投资组合方差的情况,那么我的优化器设置不正确。我想尽量减少跟踪误差,而不是投资组合差异。当然,最小化跟踪误差不应该赋予资产 H 如此高的权重。
标签: r optimization linear-programming solver portfolio