Miguel
Miguel

Reputation: 7627

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 to x.

With scipy I can import for example the fmin function (I have an old scipy: v.0.9.0), give an initial value x0 and then optimize (documentation):

from scipy.optimize import fmin
x0 = [1]
xopt = fmin(f, x0, xtol=1e-8)

which fails because f takes two arguments and fmin is passing only one (actually, I haven't even defined a yet). If I do:

from scipy.optimize import fmin
x0 = [1]
a = 1
xopt = fmin(f(x,a), x0, xtol=1e-8)

the calculation will also fail because "x is not defined". However, if I define x then there is no variational parameter to optimize.

How do I allow non-variational parameters to be used as function arguments here?

Upvotes: 4

Views: 12232

Answers (3)

Bernhard
Bernhard

Reputation: 2251

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)

Upvotes: 0

Bi Rico
Bi Rico

Reputation: 25813

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).

Upvotes: 3

Warren Weckesser
Warren Weckesser

Reputation: 114801

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,))

Upvotes: 8

Related Questions