Hello,
I'm wondering if it's possible to compute the uncertainties for the different components of a composite model with a built-in function similar to what can be done with ModelResult.eval_uncertainty. Alternatively, any directions on how I can compute them myself would be much appreciated.
Below is a minimal working example from the "Fitting Multiple Peaks" example in the documentation with the Gauss2 dataset from
NIST StRD where I just included extra noise. In this example I'd be interested in the uncertainties of the individual gaussians and exponential models instead of the uncertainties on the composite model (in magenta).
Kind regards,
Jaime
######################################################
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from lmfit.models import ExponentialModel, GaussianModel
dat = pd.read_csv('Gauss2.dat', sep='\s+', header=None)
x = dat[1]
yy = dat[0]
# Adding extra noise
rand = np.random.RandomState(10)
noise = np.random.normal(0, 5, 250)
y = yy + noise
exp_mod = ExponentialModel(prefix='exp_')
pars = exp_mod.guess(y, x=x)
gauss1 = GaussianModel(prefix='g1_')
pars.update(gauss1.make_params())
pars['g1_center'].set(value=105, min=75, max=125)
pars['g1_sigma'].set(value=15, min=3)
pars['g1_amplitude'].set(value=2000, min=10)
gauss2 = GaussianModel(prefix='g2_')
pars.update(gauss2.make_params())
pars['g2_center'].set(value=155, min=125, max=175)
pars['g2_sigma'].set(value=15, min=3)
pars['g2_amplitude'].set(value=2000, min=10)
mod = gauss1 + gauss2 + exp_mod
init = mod.eval(pars, x=x)
out = mod.fit(y, pars, x=x)
dely = out.eval_uncertainty(sigma=3)
print(out.fit_report(min_correl=0.5))
comps = out.eval_components(x=x)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(x, y)
ax.plot(x, comps['g1_'], '--', label='Gaussian component 1')
ax.plot(x, comps['g2_'], '--', label='Gaussian component 2')
ax.plot(x, comps['exp_'], '--', label='Exponential component')
ax.fill_between(x, out.best_fit+dely, out.best_fit-dely, color='magenta', alpha=0.5, zorder=2, label='3-sigma level unc.')
ax.legend()
plt.show()
