optimizing_with_nloptr on R

Hello

I have a problem when calibrating a function because i receive error messages:
nloptr_: Error in is.nloptr(ret) : objective in x0 returns NA
optim_: L-BFGS-B requires finite values of 'fn'
nlm_: it seems to ignore NA but can't impose constraints on parameters (please, let me know if it is possible and how)
etc.
If works for some range of observations but fails for others (strange). I tried several starting values but still the same.
My question is simple: how to ask to ignore error messages and yield back the parameters values?
Thanks a lot

If by "parameters values" you mean an optimal (or near optimal) solution, I don't think you'll get one by ignoring the messages. NA values in the objective function mean the objective is somehow undefined. You need to fix that.

The nlm package is designed for unconstrained optimization. If you need to impose linear constraints, you might look at constrOptim. If you need to impose nonlinear constraints, you will need to try other packages, or else implement a barrier or penalty approach on your own.

Hello

Thank you.

=> sure but i don't understand how except that i fixed bound that restrict the region of each parameters and maybe this induce undefined objective function?

Your idea is very good indedd with "constrOptim" but i dont know how to fix the feasible region with ui and ci. I would like to know how to fix ui <- matrix(c( 0, 1, 0, 0, 1), ncol = ..) and ci <- c(0, 1, -1, 0) if you help me please. Thanks again.

Putting bounds on the feasible region can make the problem infeasible, but it cannot make the objective function undefined. Letting the objective function do something like dividing by 0 can result in a result of Inf, but I would not expect the optimizer to come up with NA. I would look closely at the code used to calculate the objective to see if there is any way a valid input could result in it returning NA.

If your ci contains four values, presumably you have four linear inequality constraints defining your feasible region. In that case, your ui parameter should be a 4 x n matrix where n is the number of variables.

Hello
Thanks a lot.
Interesting point.

  • to see if there is any way a valid input could result in it returning NA.: exact but still i try and don't see error in the code (nlm works fine because no boundaries) or in the data so it seems errors are difficult to understand + Some of messages are "in sqrt(v * T) : Production of NaN" which means that i receive negative values of my variable v

*I want to learn how to fix ci and ui. My constraint on my 5 parameters are:
a1 > 0, a2 > 0, a3 > 0, a4 > 0, and b belong to [-1,+1]

Again thank you

First off, you may not be able to enforce strict inequalities on the ai parameters. You may have to settle for >= 0.

Second, you have what are called box constraints (simply bounds on the variables). So you don't actually need constrOptim. You can try the optim function. Assuming your parameters are ordered (a1, ..., a4, b), you would supply the arguments lower = c(0, 0, 0, 0, -1) and upper = c(Inf, Inf, Inf, Inf, 1) to the function.

Thank you for your response.
In fact, i did already optim with lower/upper (put 5-10 upper limit not inf) and did have the same error message. I believe constrptim is more powerful but i may be wrong. I never understood the ui ci.

If you write a linear constraint system in the format Ax >= b, where x is your vector of variables (what you call parameters), then ui is the A matrix and ci is the b vector. In your case we can write x = c(a1, ..., a4, b). The A matrix would have 6 rows (one constraint on each of the four a variables, two constraints on b) and five columns (one for each variable. The b vector would be (0, 0, 0, 0, -1, -1), where the first four constraints are of the form ai >= 0 and the last two are b >= -1 and -b >= -1. So for constrOptim you would use ci <- c(0, 0, 0, 0, -1, -1). The ui input would be given by

ui <- matrix(
  c(1, 0, 0, 0, 0,
    0, 1, 0, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 0, 1, 0,
    0, 0, 0, 0, 1,
    0, 0, 0, 0, -1),
  nrow = 6, byrow = TRUE
)

Many thanks.
Can you please check what you wrote: " the last two are b >= -1 and -b >= -1. So for constrOptim you would use ci <- c(0, 0, 0, 0, -1, -1) ." You meant perhaps b<=1 and `ci <- c(0, 0, 0, 0, -1, 1)?
Again thank you.

This will be my last response to this thread. The constrOptim function expects all constraints to be inequalities in the direction >=. So b <= 1 becomes -b >= -1.

Thank you very much for your clear explanation !