2017-02-15 6 views
0

「nloptr」を使ってrの簡単な関数を最適化していますが、目的関数に引数を渡すのが難しいです。rの引数をnloptrに渡す

require("nloptr") 

Correl <- matrix(c(1,-.3,-.3,1), nrow=2, ncol=2) 
Wghts <- c(.01,.99) 
floor <- .035 
expret <- c(.05,.02) 
pf.return <- function(r, x, threshold=0){ 

return(r * x - threshold) 
} 

pf.vol <- function(x, C){ 

return(sqrt(x %*% C %*% x)) 
} 

res <- nloptr(x0=Wghts,eval_f = pf.vol,eval_g_ineq=pf.return,opts=list(algorithm="NLOPT_GN_ISRES"), x=Wghts,C=Correl)

(私はここにパラメータが不足していますが、私は、私は理解していない行動を強調しようとしている知っている) をこれが与え実行:ここで私が使用しているコードがあります次のエラー:

Error in .checkfunargs(eval_f, arglist, "eval_f") : x' passed to (...) in 'nloptr' but this is not required in the eval_f function.

ちょうど私はX引数なしでそれを実行してみることができます何が起こるかを参照するには:

エラーを返す0

res <- nloptr(x0=Wghts,eval_f = pf.vol,eval_g_ineq=pf.return,opts=list(algorithm="NLOPT_GN_ISRES"), C=Correl)

Error in .checkfunargs(eval_g_ineq, arglist, "eval_g_ineq") : eval_g_ineq requires argument 'x' but this has not been passed to the 'nloptr' function.

だからxは、それが不要であるというエラーをスロー含み、それは省略されていること(少なくとも理解)エラーをスロー省きます。

答えて

1

後世のためのOK:

それらが同じ順序で、引数の同じセットを持っていたように、私は、関数を書き直しました。

x=Wghtsビットも省略しています。これは検索対象のパラメータです。