Does anyone know a scipy/numpy module which will allow to fit exponential decay to data?
Google search returned a few blog posts, for example – http://exnumerus.blogspot.com/2010/04/how-to-fit-exponential-decay-example-in.html , but that solution requires y-offset to be pre-specified, which is not always possible
EDIT:
curve_fit works, but it can fail quite miserably with no initial guess for parameters, and that is sometimes needed. The code I’m working with is
#!/usr/bin/env python
import numpy as np
import scipy as sp
import pylab as pl
from scipy.optimize.minpack import curve_fit
x = np.array([ 50., 110., 170., 230., 290., 350., 410., 470.,
530., 590.])
y = np.array([ 3173., 2391., 1726., 1388., 1057., 786., 598.,
443., 339., 263.])
smoothx = np.linspace(x[0], x[-1], 20)
guess_a, guess_b, guess_c = 4000, -0.005, 100
guess = [guess_a, guess_b, guess_c]
exp_decay = lambda x, A, t, y0: A * np.exp(x * t) + y0
params, cov = curve_fit(exp_decay, x, y, p0=guess)
A, t, y0 = params
print "A = %s\nt = %s\ny0 = %s\n" % (A, t, y0)
pl.clf()
best_fit = lambda x: A * np.exp(t * x) + y0
pl.plot(x, y, 'b.')
pl.plot(smoothx, best_fit(smoothx), 'r-')
pl.show()
which works, but if we remove “p0=guess”, it fails miserably.
You have two options:
scipy.optimize.curve_fitThe first option is by far the fastest and most robust. However, it requires that you know the y-offset a-priori, otherwise it’s impossible to linearize the equation. (i.e.
y = A * exp(K * t)can be linearized by fittingy = log(A * exp(K * t)) = K * t + log(A), buty = A*exp(K*t) + Ccan only be linearized by fittingy - C = K*t + log(A), and asyis your independent variable,Cmust be known beforehand for this to be a linear system.If you use a non-linear method, it’s a) not guaranteed to converge and yield a solution, b) will be much slower, c) gives a much poorer estimate of the uncertainty in your parameters, and d) is often much less precise. However, a non-linear method has one huge advantage over a linear inversion: It can solve a non-linear system of equations. In your case, this means that you don’t have to know
Cbeforehand.Just to give an example, let’s solve for y = A * exp(K * t) with some noisy data using both linear and nonlinear methods:
Note that the linear solution provides a result much closer to the actual values. However, we have to provide the y-offset value in order to use a linear solution. The non-linear solution doesn’t require this a-priori knowledge.