[SciPy-user] Fitting with global parameters optimize.leastsq
Steve Schmerler
elcorto at gmx.net
Mon Nov 6 16:11:53 EST 2006
Iain Day wrote:
> Steve Schmerler wrote:
>> Iain Day wrote:
>>> Dear All,
>>>
>>> I apologise in advance if this has been covered elsewhere, I can't
>>> seem to get my head around it.
>>>
>>> I'm trying to globally fit a series of traces to the same function
>>> with some common and some local parameters. I have a 2D array
>>> (raw_signals) which contains each data trace as its columns. I have a
>>> 1D array of the time points.
>>>
>>> I'd like to fit an exponential of the form:
>>> y(t) = A * exp(-t/B) + C
>>>
>>> and I'd like C to be global across all data sets, but A and B to be
>>> local to each trace.
>>>
>>> So far I've put A and B into arrays ntraces long. I'm trying to keep
>>> the code as general as possible as I've got lots to fit each with
>>> differing numbers of traces.
>>>
>>> I've followed the example on the SciPy cookbook and I see the
>>> principle but I'm not sure ow to extend it in my case.
>>>
>>> I've got some code which generates the error (cost) function as an
>>> array of the same dimensions as raw_signals, but I can't see how to
>>> use optimize.leastsq with that.
>>>
>>
>> I'm not sure if I understood what you're doing, but your error
>> function must return an 1D array of residuals, no 2D (like raw_signals).
>
> Sure, I'm working on that now!
>
>> import numpy as nu
>> from scipy.optimize import leastsq
>>
>> # t is your time array
>> # x the parameter array for the current trace
>> # y = raw_signals[:,i] = the (i+1)th column
>>
>> # fit a trace
>> def model(t, x):
>> A = x[0]
>> B = x[1]
>> C = x[2]
>> return A * nu.exp(-t/B) + C
>>
>> def residuals(x, t, y):
>> return y - model(t, x)
>>
>> x_sol = leastsq(residuals, x0, args = (t,y))
>
> Right, this is the sort of thing I want to do, except I'd like to
> simulateously fit all y[:] using the *same* C, i.e. globally fit them.
> Hope that's clearer.
>
> I'm messing about with scipy.reshape at the moment and sort of getting
> somewhere. The residuals is now a 1D array, as is t and y. However, when
> I use leastsq I get the following error:
>
> File "/sw/lib/python2.4/site-packages/scipy/optimize/minpack.py", line
> 229, in leastsq
> retval =
> _minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
>
> minpack.error: Result from function call is not a proper array of floats.
>
> I'm obviously not quite getting this!!
>
> Thanks for your help,
>
Could you post an example code producing this error?
--
cheers,
steve
Random number generation is the art of producing pure gibberish as
quickly as possible.
More information about the SciPy-User
mailing list