我正在寻找 NSS 模型(收益率曲线估计模型)的最佳参数,因为我在 R 中得到了这个差分进化代码。[我不想使用 DEoptim 库)。我在运行此代码时遇到问题。我使用了以下代码
DE <- function(de,dataList,OF)
{
# random numbers: like rand(m,n)/randn(m,n) in Matlab
mRU <- function(m,n){
return(array(runif(m*n), dim = c(m,n)))
}
mRN <- function(m,n){
return(array(rnorm(m*n), dim = c(m,n)))
}
shift <- function(x)
{
rr <- length(x)
return(c(x[rr],x[1:(rr-1)]))
}
# penalty
pen <- function(mP,pso,vF)
{
minV <- pso$min
maxV <- pso$max
ww <- pso$ww
# max constraint: if larger than maxV, element in A is positiv
A <- mP - as.vector(maxV)
A <- A + abs(A)
# max constraint: if smaller than minV, element in B is positiv
B <- as.vector(minV) - mP
B <- B + abs(B)
# beta 1 + beta2 > 0
C <- ww*((mP[1,]+mP[2,])-abs(mP[1,]+mP[2,]))
A <- ww * colSums(A + B)*vF - C
return(A)
}
# main algorithm
# ------------------------------------------------------------------
# set up initial population
mP <- de$min + diag(de$max - de$min) %*% mRU(de$d,de$nP)
# include extremes
mP[,1:de$d] <- diag(de$max)
mP[,(de$d+1):(2*de$d)] <- diag(de$min)
# evaluate initial population
vF <- apply(mP,2,OF,data = dataList)
# constraints
vP <- pen(mP,de,vF)
vF <- vF + vP
# keep track of OF
Fmat <- array(NaN,c(de$nG,de$nP))
for (g in 1:de$nG){
# update population
vI <- sample(1:de$nP,de$nP)
R1 <- shift(vI)
R2 <- shift(R1)
R3 <- shift(R2)
# prelim. update
mPv = mP[,R1] + de$F * (mP[,R2] - mP[,R3])
if(de$R > 0){mPv <- mPv + de$R * mRN(de$d,de$nP)}
mI <- mRU(de$d,de$nP) > de$CR
mPv[mI] <- mP[mI]
# evaluate updated population
vFv <- apply(mPv,2,OF,data = dataList)
# constraints
vPv <- pen(mPv,de,vF)
vFv <- vFv + vPv
vFv[!(is.finite(vFv))] <- 1000000
# find improvements
logik <- vFv < vF
mP[,logik] <- mPv[,logik]
vF[logik] <- vFv[logik]
Fmat[g,] <- vF
} # g in 1:nG
sGbest <- min(vF)
sgbest <- which.min(vF)[1]
# return best solution
return(list(beta = mP[,sgbest], OFvalue = sGbest, popF = vF, Fmat= Fmat))
}
我的代码中的错误消息
通过以下方式调用函数后:
DE(de = de, dataList = dataList, OF = OF2)
这个错误来了
Error in FUN(newX[, i], ...) : unused argument (data = dataList)
我不知道这是什么错误。请帮忙