Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does scipy.minimize handle NaN's?

I'm using the SLSQP solver in scipy.minimize to solve a constrained optimization problem. Very often the solver will try parameter values that violate the constraints. When these constraints are violated, the objective function returns a nan. This would seem to pose problems, as my approximated Jacobian is full of nan's nearly every time it is recalculated. More often than not, the optimization terminates in exit mode 8: Positive directional derivative for linesearch. I suspect the nan's in the approximated Jacobian to be the scource of this. My question then is how does scipy.minimize handle nan's? Are they benign, or should they be converted to a large (or even infinite) number? To the best of my knowledge, this information is not covered anywhere in the Scipy documentation.

like image 689
Peter Avatar asked Mar 24 '18 04:03

Peter


People also ask

What does Scipy minimize do?

SciPy optimize provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear problems (with support for both local and global optimization algorithms), linear programing, constrained and nonlinear least-squares, root finding, and curve fitting.

Is Scipy optimize multithreaded?

NumPy/SciPy's functions are usually optimized for multithreading.


1 Answers

There are checks in scipy for nans depending on which search algorithm you use. You'll have to check the source of each search algorithm. It generally doesn't affect minimisers (unless you use non-discriminatory methods) but it really messes up maximisation. In general, scipy lands up using numpy arrays. The best way to understand what happens is with the following simple example:

>>> x = [-np.nan, np.nan, 1, 2, 3, np.nan] # some random sequence of numbers and nans 
>>> np.sort(x)
array([ 1.,  2.,  3., nan, nan, nan])

The np.nan is always seen as the largest number thus, you have to account for this explicitly in your search algorithm such that these solutions are rejected from future iterations. As to interpreting +/- nans see this if the backend implementations are in fortran - which is sometimes the case.

like image 64
newkid Avatar answered Oct 23 '22 14:10

newkid