Skip to content Skip to sidebar Skip to footer

Using Scipy To Minimize A Function That Also Takes Non Variational Parameters

I want to use the scipy.optimize module to minimize a function. Let's say my function is f(x,a): def f(x,a): return a*x**2 For a fixed a, I want to minimize f(x,a) with respect t

Solution 1:

Read about the args argument to fmin in its docstring, and use

a = 1
x0 = 1
xopt = fmin(f, x0, xtol=1e-8, args=(a,))

Solution 2:

The args argument is probably the right approach here, but here's another approach that's useful sometimes. First you write a wrapper function for f which will take take a function and an a value as inputs and return a new function where a is fixed.

def fix_a(f, a):
    def f_with_fixed_a(x):
        return f(x, a)
return f_with_fixed_a

Then you can call fmin like this:

xopt = fmin(fix_a(f, a), x0, xtol=1e-8)

If all you need to do is pass in a fixed a, using the args keyword of fmin is this is probably too verbose, but this approach is more flexible and can handle more complex situations (for example if you wanted to make a some function of x).


Solution 3:

For your case the canonical solution by Warren is probably the right choice.

It is, however, worth noting that you can also optimize member functions of classes. In this case you have access to the class variables which can be used as arguments. This is especially useful if your code is already object based and adding a member function to optimize makes sense.

In your example this (overkill) solution would be:

class FunctionHolder(object):
    def __init__(self,a):
        self.a=a
    def f(self, x):
        return x*self.a

f1=FunctionHolder(1)
scipy.optimize.minimize(f1.f, x0)

Post a Comment for "Using Scipy To Minimize A Function That Also Takes Non Variational Parameters"