[SciPy-user] Fitting with global parameters optimize.leastsq
elcorto at gmx.net
Mon Nov 6 14:53:28 CST 2006
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).
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
B = x
C = x
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))
Random number generation is the art of producing pure gibberish as
quickly as possible.
More information about the SciPy-user