I have inherited some code that is trying to minimize a function using scipy.optimize.minimize
. I am having trouble understanding some of the inputs to the fun
and jac
arguments
The call to minimize looks something like this:
result = minimize(func, jac=jac_func, args=(D_neg, D, C), method = 'TNC' ...other arguments)
func
looks like the following:
def func(G, D_neg, D, C):
#do stuff
jac_func
has the following structure:
def jac_func(G, D_neg, D, C):
#do stuff
What I don't understand is where the G
input to func
and jac_func
is coming from. Is that somehow specified in the minimize
function, or by the fact that the method
is specified as TNC
? I've tried to do some research into the structure of this optimization function but I'm having trouble finding the answer I need. Any help is greatly appreciated
The short answer is that G
is maintained by the optimizer as part of the minimization process, while the (D_neg, D, and C)
arguments are passed in as-is from the args
tuple.
By default, scipy.optimize.minimize
takes a function fun(x)
that accepts one argument x
(which might be an array or the like) and returns a scalar. scipy.optimize.minimize
then finds an argument value xp
such that fun(xp)
is less than fun(x)
for other values of x
. The optimizer is responsible for creating values of x
and passing them to fun
for evaluation.
But what if you happen to have a function fun(x, y)
that has some additional parameter y
that needs to be passed in separately (but is considered a constant for the purposes of the optimization)? This is what the args
tuple is for. The documentation tries to explain how the args tuple is used, but it can be a little hard to parse:
args: tuple, optional
Extra arguments passed to the objective function and its derivatives (Jacobian, Hessian).
Effectively, scipy.optimize.minimize
will pass whatever is in args
as the remainder of the arguments to fun
, using the asterisk arguments notation: the function is then called as fun(x, *args)
during optimization. The x
portion is passed in by the optimizer, and the args
tuple is given as the remaining arguments.
So, in your code, the value of the G
element is maintained by the optimizer while evaluating possible values of G
, and the (D_neg, D, C)
tuple is passed in as-is.