[SciPy-Dev] optimize: what should happen if objective functions return non-finite numbers?

Pauli Virtanen pav at iki.fi
Wed Jun 15 14:53:22 EDT 2016


Wed, 15 Jun 2016 09:19:45 +1000, Andrew Nelson kirjoitti:
> Consider the following example which raises an AssertionError:
> 
> import numpy as np from scipy.optimize import minimize 
> def func1(x):
>     return np.nan
> x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
> res = minimize(func1, x0, method='l-bfgs-b')
> assert(res.success = False)

The design decision so far has been to not communicate this type of 
failure with exceptions.

Rather, the optimizer should assume the point is out of the domain of the 
objective function.

Whether the optimizer can continue, depends on the method. If it cannot, 
it should return with success=False, and an appropriate error message set.

> minimize simply returns the starting values of: res.x == x0. The reason
> I came up with this example is that unsanitised datasets sometimes
> contain nan or inf. Thus, if func1 was calculating chi2 and you were
> using minimize then the entire fit would appear to succeed (res.success
> is True), but the output would be garbage. 

Why would res.success be true? If it is true when it is clear local 
minimum is not reached, that is a bug.

> import numpy as np from scipy.optimize import curve_fit 
> def func2(x, a, b, c):
>     return a * np.exp(-b * x) + c
> 
> def func3(x, a, b, c):
>     return np.nan
> 
> xdata = np.linspace(0, 4, 50)
> y = func2(xdata, 2.5, 1.3, 0.5)
> ydata = y + 0.2 * np.random.normal(size=len(xdata))
> 
> popt, pcov = curve_fit(func3, xdata, ydata)
> print(popt)
> 
> Whilst there is a warning (OptimizeWarning: Covariance of the parameters
> could not be estimated) it's not a clear indicator that something has
> gone wrong.
> The behaviour one might expect in both examples could be to see a
> ValueError raised if there arenp.nan values returned from the objective
> function. I'm not totally sure of what to do if +/- np.inf is returned
> (-inf would be a very good global minimum).

I think this is a different issue --- the only way curve_fit communicates 
fit failures in general is via setting the estimated covariances to 
infinity. This is not so convenient, but that's how it works currently.

-- 
Pauli Virtanen




More information about the SciPy-Dev mailing list