Skip to content
Prev 61201 / 63424 Next

Bug in optim for specific orders of magnitude

Thanks for all of your replies.

I would like to clarify that I am not expecting optim to return a good
result in these cases, as I am aware of the difficulties that come with the
numerical precision for such small numbers. I would be happy for optim to
take zero steps and return the initial value after it sees the gradient as
close enough to 0. What I think is a bug is for optim to return an error
for a specific range of numbers between 1e-309 and 1e-320 while not giving
an error for values on either side. I think the behavior should be the same
as for smaller numbers where it simply returns the starting point after
estimating the gradient to be 0.

To give an idea of where I was seeing the error, consider a function that
is mostly near 0, but has some areas of interest, for example:
f <- function(x) {-exp(-sum(x^2))}

As long as the starting point is near the peak, optim works fine, but if
you give it a bad starting point it will see the function as flat and
return the starting point.

Again, it is fine that it is not able to find the optimum in the scenario
of starting far from the peak, but I believe that it should NOT return an
error when the initial value falls within a specific range. The bug is that
it returns an error, not that it fails to optimize.

Thanks,
Collin

On Fri, Dec 23, 2022 at 1:52 PM Duncan Murdoch <murdoch.duncan at gmail.com>
wrote: