[SciPy-user] parameter bounds using leastsq

Robert Kern rkern at ucsd.edu
Sat Nov 5 21:47:52 CST 2005

mike cantor wrote:
> Is there any way to enforce upper and/or lower bounds on parameters (x0) 
> optimized by leastsq?  If not can anyone tell me where I might look to hack 
> this?

If you just need the optimizal value and not some (dubious) estimate of
the uncertainty, then you can use one of the constrained minimizers. You
simply have to make an appropriate misfit function:

def f(beta, x):
    # compute values y given parameters beta at points x

def misfit(beta, x, y):
    diff = y - f(beta, x)
    return scipy.sum(diff*diff)

beta_opt = scipy.optimize.fmin_cobyla(f, beta0, constraints, (x, y))

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

More information about the SciPy-user mailing list