[SciPy-user] fprime for L-BFGS-B

lechtlr lechtlr@yahoo....
Wed Apr 11 15:12:34 CDT 2007


Can anyone give some clues as to how to define gradient array (fprime) for optimize.fmin_l_bfgs_b.

I have attached a simple parameter estimation example below to test fmin_l_bfgs_b with and without fprime. It works with approx_grad (i.e., optimize.fmin_l_bfgs_b(resid, p0, args=(y_meas,x), fprime=None, approx_grad=True). However, when I define, fprime it returns the initial guess. I suspect that I am not defining the fprime correctly. 

Any help would greatly be appreciated.

-Lex

from numpy import *
from scipy import optimize
 
def evalY(p, x):
    return x**3 * p[3] + x**2 * p[2] + x * p[1] + p[0]

# function to be minimized for parameter estimation
def resid(p, y, x):
    err = y - evalY(p, x)
    return dot(err,err)

# fprime for fmin_bfgs
def func_der(p, y, x):

    g = zeros(4,float)

    g0 = 1.0
    g1 = x
    g2 = x**2
    g3 = x**3

    g[0] = g0
    g[1] = dot(g1,g1)
    g[2] = dot(g2,g2)
    g[3] = dot(g3,g3)

    return g

x = array([0., 1., 2., 3., 4., 5.]).astype('d')
coeffs =  [4., 3., 5., 2.]
yErrs = array([0.1, 0.12, -0.1, 0.05, 0,-.02]).astype('d')
    
y_true = evalY(coeffs, x)
y_meas = y_true + yErrs

#Initial guess
p0 = [2., 1.2, 1.1, 1.]

pMin_BFGS, f_BFGS, d_BFGS = optimize.fmin_l_bfgs_b(resid, p0, args=(y_meas,x), fprime=func_der, approx_grad=None)

print "\nFinal parameters:"
print '%s %20s %20s ' % ('Para', 'Actual', 'fmin_BFGS')

for i in range(len(coeffs)):
    print '%s %20.2f %20.2f ' % (i, coeffs[i], pMin_BFGS[i])




       
---------------------------------
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070411/d85b0fa3/attachment.html 


More information about the SciPy-user mailing list