0

I am finding the optimal parameters for NSS model(model for yield curve estimation) for that I have got this code of Differential evolution in R.[I don't want to use DEoptim library). I am having a trouble in running this code. I have used the following code

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))

}

Error Message in my code

After the calling the function by:

DE(de = de, dataList = dataList, OF = OF2) 

This error is coming

Error in FUN(newX[, i], ...) : unused argument (data = dataList)

I don't what is this error.Please help

Ramu
  • 1
  • 1

1 Answers1

0

This look very much like the code in Calibrating the Nelson-Siegel-Svensson Model. (Disclosure: I have written that code.) In which case, as mentioned by in the comment by jogo, you need to call the function as

DE(de = de, dataList = dataList, OF = OF2)

If that does not solve your problem, you should provide a reproducible example.

By the way, the NMOF package contains a vignette that explains how to fit the Nelson-Siegel-Svensson model (and the code in the vignette is based on the code in the paper).

Enrico Schumann
  • 1,278
  • 7
  • 8
  • Thanks for the response Enrico. I think the reason that am getting this error is because I don't know what is dataList. So can you define dataList? that would be a great help.Thanks – Ramu May 27 '19 at 11:15
  • `dataList` is a list that collects all pieces of information that the objective function requires. The objective function `OF` takes two arguments: the vector of NSS-parameter values, and a list. See the examples in Appendix A of the paper (link is in the answer above). It may actually be easier to go through the vignette that comes with the `NMOF` package (https://cran.r-project.org/web/packages/NMOF/vignettes/DEnss.pdf). – Enrico Schumann May 28 '19 at 13:13