我目前正在尝试寻找投资组合的最佳权重。借助optim()函数(无额外包),我将以下代码用于同等加权的投资组合(https://i.stack.imgur.com/ZGW5l.png和https://i.stack.imgur.com/35PhJ.png):
# Compute variance-covariance matrix
Sigma <- cov_matrix
N <- 6
# Set up the minimization problem
ObjFuncERC <- function (x, Sigma) {
sum <- 0
R <- Sigma %*% x
for (i in 1:N) {
for (j in 1:N) {
sum <- sum + (x[i]*R[i] - x[j]*R[j])^2
}
}
sum
}
x0 <- matrix(1/N, nrow = N, ncol = 1)
optim(par = x0, fn = ObjFuncERC, Sigma = cov_matrix)
结果:
structure(list(par = structure(c(0.136739825862469, 0.270039438143228,
0.217957203964446, 0.140979346451311, 0.12434667631434, 0.164147197927427
), .Dim = c(6L, 1L)), value = 1.05030199202692e-16, counts = structure(c(401L,
NA), .Names = c("function", "gradient")), convergence = 0L, message = NULL), .Names = c("par",
"value", "counts", "convergence", "message"))
方差-协方差矩阵(cov_matrix)为:
structure(c(0.0003770166788353, 3.90516782053526e-05, 3.5555558058545e-05,
0.000104505686460224, 4.29463203471184e-05, 4.24141140985486e-05,
3.90516782053526e-05, 7.56192621107786e-05, 3.02766243363913e-05,
4.06047891327263e-05, 4.52423036784686e-05, 3.25724476378454e-05,
3.5555558058545e-05, 3.02766243363913e-05, 0.000145265786909066,
4.26705403743996e-05, 3.88921978856041e-05, 3.17829593802571e-05,
0.000104505686460224, 4.06047891327263e-05, 4.26705403743996e-05,
0.000293995816289813, 6.71083319622826e-05, 5.85548395279348e-05,
4.29463203471184e-05, 4.52423036784686e-05, 3.88921978856041e-05,
6.71083319622826e-05, 0.000496795497737421, 5.31253499915505e-05,
4.24141140985486e-05, 3.25724476378454e-05, 3.17829593802571e-05,
5.85548395279348e-05, 5.31253499915505e-05, 0.000270041991742317
), .Dim = c(6L, 6L), .Dimnames = list(c("As1", "As2", "As3",
"As4", "As5", "As6"), c("As1", "As2", "As3", "As4", "As5",
"As6")))
我尝试将其用于均方差分析(https://en.wikipedia.org/wiki/Modern_portfolio_theory),但是我不知道该过程。我试图改变
sum <- sum + (x[i]*R[i] - x[j]*R[j])^2
在
sum <- sum + (x[i]*R[i]*x[j]*R[j])
甚至是
sum <- sum + (x[i]*x[j]*Sigma[i,j])
,但结果无关紧要。有人有解决方案吗?
谢谢! :3