Search code examples
roptimizationdifferential-evolution

R: How to add additional constraints to DEoptim


I am trying to minimize an objective function using DEoptim, subject to a simple constraint. I am not clear as to how to add the simple constraint to the call to DEoptim. Here is the objective function:

obj_min <- function(n,in_data) {
    gamma <- in_data$Gamma
    delta <- in_data$Delta
    theta <- in_data$Theta
    gammaSum <- sum(n * gamma)
    deltaSum <- sum(n * delta)
    thetaSum <- sum(n * theta)
    abs((EPC * gammaSum - 2 * abs(deltaSum)) / thetaSum )
}

My mapping function (to impose integer constraints) is as follows:

 mappingFun <- function(x) {
    x[1:length(x)] <- round(x[1:length(x)], 0)
 }

My call to DEoptim is:

 out <- DEoptim(DTRRR_min, lower = c(rep(-5, length(in_data[, 1]))),
        upper = c(rep(5, length(in_data[, 1]))),
        fnMap = mappingFun, DEoptim.control(trace = F),in_data)

My in_data object (data frame) is:

   Underlying.Price  Delta  Gamma   Theta   Vega    Rho Implied.Volatility
 1            40.69 0.9237 3.2188 -0.7111 2.0493 0.0033             0.3119
 2            40.69 0.7713 6.2267 -1.6352 4.3240 0.0032             0.3402
 3            40.69 0.5822 8.4631 -2.0019 5.5782 0.0338             0.3229
 4            40.69 0.3642 8.5186 -1.8403 5.3661 0.0210             0.3086
 5            40.69 0.1802 6.1968 -1.2366 3.7517 0.0093             0.2966

I would like to add a simple constraint that:

 sum(n * delta) = target

In other words, the summation of the optimized parameters, n, multiplied by the deltas in my in_data data frame sum to a target of some sort. For simplicity, lets just say 0.5. How do I impose

 sum(n * delta) = 0.5

as a constraint? Thank you for your help!


Solution

  • DEOptim package description says

    Implements the differential evolution algorithm for global optimization of a realvalued function of a real-valued parameter vector.

    The concept of global optimization doesn't have place for constraints and it is also known as unconstrained optimization. So sorry but its not possible directly. Having said that you can always use "Lagrange's multiplier" hack if you must do it. To do it you need to do something like:

    abs((EPC * gammaSum - 2 * abs(deltaSum))/thetaSum) - lambda* (sum(n * delta) - 0.5)
    

    where you penalizing slack of your constraint.