[SciPy-user] parameter bounds using leastsq

Eric Zollars zollars at caltech.edu
Mon Nov 7 12:44:40 CST 2005

Robert-
Could you flesh out this answer some more?  I've had to do this in the
past and was sure I was missing something.  In the simplest case a set of:
y1 = (b0 + b1*x1 + b2*x2)1
.
.
yn = (b0 + b1*x1 + b2*x2)n

In fortran I would pass x as a 2d matrix to the function misfit.  What
do you do in scipy if x is a vector at each point y?

Eric

Robert Kern wrote:
> 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))
>