Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy.optimize: how to restrict argument values

Tags:

python

scipy

I'm trying to use scipy.optimize functions to find a global minimum of a complicated function with several arguments. scipy.optimize.minimize seems to do the job best of all, namely, the 'Nelder-Mead' method. However, it tends to go to the areas out of arguments' domain (to assign negative values to arguments that can only be positive) and thus returns an error in such cases. Is there a way to restrict the arguments' bounds within the scipy.optimize.minimize function itself? Or maybe within other scipy.optimize functions?

I've found the following advice:

When the parameters fall out of the admissible range, return a wildly huge number (far from the data to be fitted). This will (hopefully) penalize this choice of parameters so much that curve_fit will settle on some other admissible set of parameters as optimal.

given in this previous answer, but the procedure will take a lot of computational time in my case.

like image 801
Alexandra Shchukina Avatar asked Oct 08 '13 09:10

Alexandra Shchukina


People also ask

What is JAC in SciPy minimize?

jac : bool or callable, optional Jacobian (gradient) of objective function. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg. If jac is a Boolean and is True, fun is assumed to return the gradient along with the objective function.

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.

How do you minimize a function in Python?

To minimize the function we can use "scipy. optimize. minimize" function and further there are some methods we can use to minimize the function. Build a Chatbot in Python from Scratch!


1 Answers

The minimize function has a bounds parameter which can be used to restrict the bounds for each variable when using the L-BFGS-B, TNC, COBYLA or SLSQP methods.

For example,

import scipy.optimize as optimize  fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 res = optimize.minimize(fun, (2, 0), method='TNC', tol=1e-10) print(res.x) # [ 1.          2.49999999]  bnds = ((0.25, 0.75), (0, 2.0)) res = optimize.minimize(fun, (2, 0), method='TNC', bounds=bnds, tol=1e-10) print(res.x) # [ 0.75  2.  ] 
like image 134
unutbu Avatar answered Sep 20 '22 15:09

unutbu