[SciPy-User] beginner's question regarding optimize.fmin_l_bfgs_b
Tveraa, Torkild
Torkild.Tveraa@nina...
Tue Oct 12 08:10:04 CDT 2010
Dear All,
I have been able to use the optimize.leastsq - module to minimize a given function (see below), but since my data is sparse I have convergence problems and would ideally be able to put bounds on the parameters. If I have understood this correctly this can be done with the optimize.fmin_l_bfgs_b - module, but I am unable to figure out how to do this. Some helps & hints would be most appreciated :-)
Cheers,
Torkild
-------------------------------------------------------
import numpy
import pylab
from scipy import *
from scipy import optimize
## This is y-data:
y_data = (([0.2867, 0.1171, -0.0087, 0.1326, 0.2415, 0.2878, 0.3133, 0.3701, 0.3996, 0.3728, 0.3551, 0.3587, 0.1408, 0.0416, 0.0708, 0.1142, 0, 0, 0]))
## This is x-data:
t = (([67, 88, 104, 127, 138, 160, 169, 188, 196, 215, 240, 247, 271, 278, 303, 305, 321, 337, 353]))
## This is the equation:
fitfunc = lambda p, x: p[0] + (p[1] -p[0]) * ((1/(1+exp(-p[2]*(t-p[3])))) + (1/(1+exp(p[4]*(t-p[5])))) -1)
##
errfunc = lambda p, x, y: fitfunc(p,x) -y
guess = [0, max(y_data), 0.1, 140, -0.1, 270]
bounds = [(-0.2, 0.1),(0.1,0.97), (0.05,0.8), (120,190), (-0.8, -0.05), (200,300) ]
## This seems to work ok:
p2,success = optimize.leastsq(errfunc, guess, args=(t, y_data),full_output=0)
print 'Estimates from leastsq \n', p2,success
## But this does not:
best, val, d = optimize.fmin_l_bfgs_b(errfunc, guess, bounds=bounds, args=(t, y_data), iprint=2)
More information about the SciPy-User
mailing list