SciPy Optimization syntax

tkpmep at hotmail.com tkpmep at hotmail.com
Wed Sep 20 15:02:47 EDT 2006


I'm trying to optimize a function using SciPy's optimize.fmin, but am
clearly getting the syntax wrong, and would be grateful for some
guiidance. First, here's the function

def func(Y,x):
    """Y holds samples of a function sampled at t=-3,-2,-1,0,1,2,3.
        Y[3]=0 always.
        func returns the absolute value of the maximum NEGATIVE
        error from a straight line fit with slope x and intercept 0"""

    Y[0] = Y[0] - 3*x
    Y[1] = Y[1] - 2*x
    Y[2] = Y[2] - x
    Y[3] =  0
    Y[4] = Y[4] + x
    Y[5] = Y[5] + 2*x
    Y[6] = Y[6] + 3*x

    error = abs(max(min(Y),0)

    return 0

I'd now like to minimize this using optimize.fmin. I first defined
>>Y = [0, 0, 0, 0, 1, 2, 3]
>>x = 1

and then typed
>>optimize.fmin(func,  args=(Y,x) )

I expected the function to retun x=0 as the optimal value,  but instead
got the following error messsage:
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in -toplevel-
    optimize.fmin(func,args=(optionPnL,x))
TypeError: fmin() takes at least 2 non-keyword arguments (1 given)

I then tried
>>optimize.fmin(func,  x0 =x, args=(Y,x) )

and got a slightly different error message:
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in -toplevel-
    optimize.fmin(func,x0=x, args=(optionPnL,1))
  File "C:\Python24\lib\site-packages\scipy\optimize\optimize.py", line
176, in fmin
    N = len(x0)
TypeError: len() of unsized object

What am I doing wrong, and what's the appropriate fix?

Thanks in advance

Thomas Philips




More information about the Python-list mailing list