from numpy import sqrt, pi, exp, linspace, random, empty, array
from scipy.optimize import curve_fit
from lmfit import Model
from timeit import default_timer as timer
def gaussian(x, amp, cen, wid):
return amp * exp(-(x-cen)**2 /wid)
init_vals = [1, 0, 1]
x = linspace(-10,10)
n_points = len(x)
gaussian_true = gaussian(x, 2.33, 0.21, 1.51)
gmod = Model(gaussian)
print '--Single fit'
start = timer()
best_vals, covar = curve_fit(gaussian, x, gaussian_true + random.normal(0, 0.2, n_points), p0=init_vals)
end = timer()
print 'curve fit', best_vals, ' time ', (end - start)
start = timer()
result = gmod.fit(gaussian_true + random.normal(0, 0.2, n_points), x=x, amp=1, cen=0, wid=1)
end = timer()
print 'lmfit', array(result.params.valuesdict().values()), (end - start)
print '--Bootstrap'
results_matrix_curvefit = empty([3,1000])
results_matrix_lmfit = empty([3,1000])
start = timer()
for i in range(1000):
best_vals, covar = curve_fit(gaussian, x, gaussian_true + random.normal(0, 0.2, n_points), p0=init_vals)
results_matrix_curvefit[:,i] = best_vals
end = timer()
print 'curve fit', results_matrix_curvefit.mean(1), ' time ', (end - start)
start = timer()
for i in range(1000):
result = gmod.fit(gaussian_true + random.normal(0, 0.2, n_points), x=x, amp=1, cen=0, wid=1)
results_matrix_lmfit[:,i] = array(result.params.valuesdict().values())
end = timer()
print 'lmfit', results_matrix_lmfit.mean(1), (end - start)
--Single fit
curve fit [ 2.20907852 0.21520868 1.3615026 ] time 0.0145289897919
lmfit [ 2.45153259 0.30430127 1.28085118] time 0.00659894943237
--Bootstrap
curve fit [ 2.34031556 0.20673496 1.51793454] time 0.713760137558
lmfit [ 2.33999564 0.21131336 1.51693473] time 4.11775302887
--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+unsubscribe@googlegroups.com.
To post to this group, send email to lmfi...@googlegroups.com.
Visit this group at https://groups.google.com/group/lmfit-py.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/5a11fd01-ed84-40be-ba46-0302f0ea0fb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To post to this group, send email to lmfi...@googlegroups.com.
Visit this group at https://groups.google.com/group/lmfit-py.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/5a11fd01-ed84-40be-ba46-0302f0ea0fb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
from numpy import exp, linspace, random, sum, log, pi
from lmfit import Parameters, Minimizer
from timeit import default_timer as timer
def gaussian_curve(A, mu, sigma, x):
return A * exp(-(x-mu)*(x-mu)/(2 * sigma * sigma))
def gaussian_residual(params, x, y, err):
return (gaussian_curve(params['A'], params['mu'], params['sigma'], x) - y) / err
def lnprob(p, x, y, err):
resid = gaussian_residual(params, x, y, err)
return -0.5 * sum((resid)**2 + log(2 * pi * err**2))
x = linspace(-10,10)
n_points = len(x)
gaussian_true = gaussian_curve(2.33, 0.21, 1.51, x)
err = random.normal(0, 0.2, n_points)
y_obs = gaussian_true + err
params = Parameters()
params.add('A', value = 1)
params.add('mu', value = 0)
params.add('sigma', value = 1)
mini = Minimizer(lnprob, params, args = (x, y_obs, err))
out = mini.emcee(steps=1000, params=params)
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To post to this group, send email to lmfi...@googlegroups.com.
Visit this group at https://groups.google.com/group/lmfit-py.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/5a11fd01-ed84-40be-ba46-0302f0ea0fb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+unsubscribe@googlegroups.com.
To post to this group, send email to lmfi...@googlegroups.com.
Visit this group at https://groups.google.com/group/lmfit-py.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/61146b2c-a9c7-4d9d-9514-eeea9cf26cc8%40googlegroups.com.
A mistake that wasted an hour of my time I might add.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/61146b2c-a9c7-4d9d-9514-eeea9cf26cc8%40googlegroups.com.
--_____________________________________
Dr. Andrew Nelson
_____________________________________
--_____________________________________
Dr. Andrew Nelson
_____________________________________