[R] optimx

Glenn Schultz glennmschultz at me.com
Sat Nov 29 16:48:22 CET 2014

```Hello All,

I need some help with optimx, mostly due to my apparent lack of imagination than optimx itself.  Below is the pertinent code for which I have a question.  I am fitting to the term structure of swap rates per Cox, Ingersoll, and Ross.  As you can see the objective function is CIRTune.  I have set up the lower and upper bounds of the model for each kappa, lambda, and theta.  Everything works as expected.

However, there is an additional constraint.
2 * kappa * lamba >= sigma^2

My question is as follows:  should the constraint be worked into the function or should I add an additional parameter in the function that is controlled by lower and upper?

#Objective function
CIRTune <- function(param = numeric(), shortrate = numeric(), sigma = .015, cfmatrix = matrix(), matmatrix = matrix()){
kappa =   param[1]
lambda = param[2]
theta =     param[3]

Disc <- CIRBondPrice(kappa = kappa, lambda = lambda, theta = theta, shortrate = shortrate, T= matmatrix,  step = 0, sigma = sigma)

CIRTune <- sqrt((sum(colSums((cfmatrix * Disc))^2))/ncol(matmatrix))
return(CIRTune)
}

# Fit the model to the market
fit <- optimx(par = c(.1, .003, .03),
fn = CIRTune,
method = "L-BFGS-B",
lower = c(rep(.001,3),
upper = rep(1, 3),
shortrate = shortrate,
sigma = .015,
cfmatrix = CIR.CF.Matrix,
matmatrix = CIR.Mat.Matrix)
close(CalCIR1)
return(fit)