2017-04-18 12 views
0

Rで単純なROI最適化を実行しようとしていますが、私はエラーを取得しておいてください。複数引数のF_objective関数を使用したRでのROI最適化

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に合格する方法多くのパラメータ上の任意のアイデア:ここ

Error in .check_function_for_sanity(F, n) : 
    cannot evaluate function 'F' using 'n' = 5 parameters. 

は、サンプルコードのですか?

答えて

1

F_objectiveは、引数が1つのみの関数を想定しているため、ラッパー関数を記述する必要があります。

#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: 
wrapper <- function(x) diversificationRatio(x, myCovMatrix) 

# Now construct the F_objective 
o <- OP(F_objective(F = wrapper, n = (ncol(myRet)))) 

ROI_applicable_solvers(o) 

start <- runif(ncol(myRet)) 
s <- ROI_solve(o, solver = "nloptr", start = start, method = "NLOPT_LD_SLSQP") 
s 
solution(s) 
+0

私はF_constraints()を使用して関連する問題に遭遇しましたが、別の質問がありました。私は本当にあなたが助けることができますが、あなたにタグを付ける方法がわからないことを願っています。アドバイスをいただければ幸いです。https://stackoverflow.com/questions/44932853/roi-optimisation-in-r-using-f-constraint – csrvermaak

関連する問題