R:投资组合优化

时间:2018-08-10 08:06:51

标签: r optimization simulation portfolio quadratic-programming

我遇到了优化问题。我有5种不同的资产及其10.000天的(模拟)退货历史记录。我也有一个带有10.000个模拟非资产收入的向量。我想找到最佳的资产权重,以便在将资产组合绩效添加到非资产绩效中时,其资产组合绩效经历最大可能的分散效应(即,资产组合绩效矢量和非资产绩效矢量之和的方差应该最小)。非常感谢您的帮助!

这里有一些示例代码:

library(quadprog)
library(Matrix)
library(matrixcalc)

set.seed(1223)

port_value <- 8e9

#generating reproducible returns for 10000 simulated days

d_mean <- c(5,9,12,-1,7)
d_sd <- c(8,13,16,1,10)

smpl_l <- as.list(c(1:5))
rnd_l <- lapply(smpl_l, function(x) rnorm(10000, d_mean[x], d_sd[x]))
dat <- do.call(cbind, rnd_l)

#setup for the optimization

cov_m <- cov(dat) #covariance matrix

  if(is.positive.definite(cov_m)){ #if not positive definite, transform into nearest positive definite matrix
    Dmat <- cov_m
  } else {
    Dmat <- nearPD(cov_m)$mat #Dmat is to be minimized
  }

dvec <- matrix(colMeans(dat), nrow=5, ncol=1) #vector of retuns with which we want to minimize Dmat
A.Equality <- matrix(rep(1,5), ncol=1) #contraint setup for all weights sum up to 1
Amat <- cbind(A.Equality, dvec)
bvec <- c(1, 5.2) #for the beginning, there are just two constraints

qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=1)
weights <- qp$solution

d <- rowSums(sweep(dat, MARGIN=2, weights,`*`)) 
dscaled <- d*port_value/100 #dscaled is the portfolio performance vector


nonasset <- rnorm(10000, mean = 0, sd = 335628000) #the non-asset performance vector

var(dscaled+nonasset) #this should be minimized by choosing the "best" weights for the assets.

0 个答案:

没有答案