[SciPy-user] Using fmin

David Warde-Farley dwf@cs.toronto....
Wed Mar 25 23:33:55 CDT 2009


On 25-Mar-09, at 5:48 PM, Moofle wrote:

> I am not sure how to pass alpha, omega, beta into the fmin method.  
> To be honest,
> I am not even completely sure about how that method even works even  
> though I
> have looked at the documentation for hours. In particular, I dont  
> know what the
> args=() is used for!! I can solve this in excel using solver and  
> AMPL, but I
> sure would appreciate a few hints!!


If you're simultaneously optimizing over all of these parameters,  
you'll need to write a function that takes as its first parameter a  
vector argument, then unpack it. Are the u_i's held constant?

"args" is for parameters to your function that don't change from one  
step to the next. They are passed in every time your function is  
called as additional arguments.

For example, if I wanted to minimize (x - p + q)**2, but didn't want  
to hardcode values of p and q into my function, I'd do something like

def foo(x, p, q):
	return (x - p + q)**2

and then call fmin_powell(foo, 20, args=(5,3)). Then every time foo  
gets called it will receive 5 as its argument for p and 3 as it's  
argument for q, with 20 as the starting value.

In [8]: fmin_powell(foo, 20, args=(30,2))
Optimization terminated successfully.
          Current function value: 0.000000
          Iterations: 2
          Function evaluations: 20
Out[8]: array(28.000000000000039)



If, as I suspect, the u's are constant, I'd do something like this:

def myfunction(parameters, u):
	alpha, beta, omega = parameters
	total = 0
	for u_i in u:
		v_i = ... # fill in code for v_i
		total += ...  # fill in code for the i'th term
	return total


Then call

fmin_powell(myfunction, initialguesses, args=(U,))

Where initialguesses is an array containing the initial value for  
alpha, beta, and omega, and U is an array containing the u_i's.

NOTE that args=(U,) creates a tuple of length 1. args=U would treat  
each u_i as a separate argument to the function.

David


More information about the SciPy-user mailing list