0
试图在R中运行简单的ROI优化,但经过数小时的烦躁后,我不知所措。我不断收到错误:R中使用多参数F_objective函数进行ROI优化
Error in .check_function_for_sanity(F, n) :
cannot evaluate function 'F' using 'n' = 5 parameters.
下面是示例代码:
library(ROI)
library(nloptr)
library(ROI.plugin.nloptr)
#Generate some random data for this example
set.seed(3142)
myRet = matrix(runif(100 * 5, -0.1, 0.1), ncol = 5)
myCovMatrix = cov(myRet)
myRet <- myRet
myCovMatrix <- myCovMatrix
# Sample weights
w <- rep(1/ncol(myRet), ncol(myRet))
#Define functions for the optimisation
diversificationRatio = function(w, covMatrix)
{
weightedAvgVol = sum(w * sqrt(diag(covMatrix)))
portfolioVariance = (w %*% covMatrix %*% w)[1,1]
- 1 * weightedAvgVol/sqrt(portfolioVariance)
}
# Check that the F_objective function works:
diversificationRatio(w, myCovMatrix)
# Now construct the F_objective
foo <- F_objective(F = diversificationRatio, n = (ncol(myRet)))
有多少参数传递给n
任何想法?
我使用F_constraints()碰上了相关的问题,但提出了另外一个问题。我真的希望你能帮助,但不确定如何标记你,因此这个评论。真的很感激任何意见:https://stackoverflow.com/questions/44932853/roi-optimisation-in-r-using-f-constraint – csrvermaak