doc_uvars_params.pyΒΆ

[[Model]]
    ((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='exp_'))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 46
    # data points      = 250
    # variables        = 8
    chi-square         = 1247.52821
    reduced chi-square = 5.15507524
    Akaike info crit   = 417.864631
    Bayesian info crit = 446.036318
    R-squared          = 0.99648654
[[Variables]]
    g1_amplitude:   4257.77390 +/- 42.3837959 (1.00%) (init = 4000)
    g1_center:      107.030957 +/- 0.15006877 (0.14%) (init = 105)
    g1_sigma:       16.6725785 +/- 0.16048211 (0.96%) (init = 15)
    g2_amplitude:   2493.41716 +/- 36.1697062 (1.45%) (init = 2000)
    g2_center:      153.270104 +/- 0.19466774 (0.13%) (init = 155)
    g2_sigma:       13.8069453 +/- 0.18680153 (1.35%) (init = 15)
    exp_amplitude:  99.0183277 +/- 0.53748650 (0.54%) (init = 100)
    exp_decay:      90.9508838 +/- 1.10310767 (1.21%) (init = 100)
    g1_fwhm:        39.2609214 +/- 0.37790648 (0.96%) == '2.3548200*g1_sigma'
    g1_height:      101.880229 +/- 0.59217051 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'
    g2_fwhm:        32.5128709 +/- 0.43988398 (1.35%) == '2.3548200*g2_sigma'
    g2_height:      72.0455939 +/- 0.61721985 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma)'
[[Correlations]] (unreported correlations are < 0.500)
    C(g1_amplitude, g1_sigma)   = +0.8243
    C(g2_amplitude, g2_sigma)   = +0.8154
    C(exp_amplitude, exp_decay) = -0.6946
    C(g1_sigma, g2_center)      = +0.6842
    C(g1_center, g2_amplitude)  = -0.6689
    C(g1_center, g2_sigma)      = -0.6520
    C(g1_amplitude, g2_center)  = +0.6477
    C(g1_center, g2_center)     = +0.6205
    C(g1_center, g1_sigma)      = +0.5075
    C(g1_amplitude, exp_decay)  = -0.5074
### Peak Area:
Peak1: 4257.77390+/-42.38380
Peak2: 2493.41716+/-36.16971
Sum  : 6751.19106+/-46.50802
Correlation of peak1 and peak2 areas:            -0.30712
Stderr Quadrature sum for peak1 and peak2 area2: 55.71924
Stderr of peak1 + peak2 area, with correlation:  46.50802
### Peak Centroid:
Peak Centroid: 124.10846+/-0.14074
[[Model]]
    ((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='exp_'))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 46
    # data points      = 250
    # variables        = 8
    chi-square         = 1247.52821
    reduced chi-square = 5.15507524
    Akaike info crit   = 417.864631
    Bayesian info crit = 446.036318
    R-squared          = 0.99648654
[[Variables]]
    g1_amplitude:   4257.77390 +/- 42.3837959 (1.00%) (init = 4000)
    g1_center:      107.030957 +/- 0.15006877 (0.14%) (init = 105)
    g1_sigma:       16.6725785 +/- 0.16048211 (0.96%) (init = 15)
    g2_amplitude:   2493.41716 +/- 36.1697062 (1.45%) (init = 2000)
    g2_center:      153.270104 +/- 0.19466774 (0.13%) (init = 155)
    g2_sigma:       13.8069453 +/- 0.18680153 (1.35%) (init = 15)
    exp_amplitude:  99.0183277 +/- 0.53748650 (0.54%) (init = 100)
    exp_decay:      90.9508838 +/- 1.10310767 (1.21%) (init = 100)
    g1_fwhm:        39.2609214 +/- 0.37790648 (0.96%) == '2.3548200*g1_sigma'
    g1_height:      101.880229 +/- 0.59217051 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'
    g2_fwhm:        32.5128709 +/- 0.43988398 (1.35%) == '2.3548200*g2_sigma'
    g2_height:      72.0455939 +/- 0.61721985 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma)'
    post_area:      6751.19106 +/- 46.5080249 (0.69%) == 'g1_amplitude + g2_amplitude'
    post_centroid:  124.108459 +/- 0.14074482 (0.11%) == '(g1_amplitude*g1_center+ g2_amplitude*g2_center)/(g1_amplitude + g2_amplitude)'
[[Correlations]] (unreported correlations are < 0.500)
    C(g1_amplitude, g1_sigma)   = +0.8243
    C(g2_amplitude, g2_sigma)   = +0.8154
    C(exp_amplitude, exp_decay) = -0.6946
    C(g1_sigma, g2_center)      = +0.6842
    C(g1_center, g2_amplitude)  = -0.6689
    C(g1_center, g2_sigma)      = -0.6520
    C(g1_amplitude, g2_center)  = +0.6477
    C(g1_center, g2_center)     = +0.6205
    C(g1_center, g1_sigma)      = +0.5075
    C(g1_amplitude, exp_decay)  = -0.5074
[[Model]]
    ((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='exp_'))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 46
    # data points      = 250
    # variables        = 8
    chi-square         = 1247.52821
    reduced chi-square = 5.15507524
    Akaike info crit   = 417.864631
    Bayesian info crit = 446.036318
    R-squared          = 0.99648654
[[Variables]]
    g1_amplitude:   4257.77390 +/- 42.3837959 (1.00%) (init = 4000)
    g1_center:      107.030957 +/- 0.15006877 (0.14%) (init = 105)
    g1_sigma:       16.6725785 +/- 0.16048211 (0.96%) (init = 15)
    g2_amplitude:   2493.41716 +/- 36.1697062 (1.45%) (init = 2000)
    g2_center:      153.270104 +/- 0.19466774 (0.13%) (init = 155)
    g2_sigma:       13.8069453 +/- 0.18680153 (1.35%) (init = 15)
    exp_amplitude:  99.0183277 +/- 0.53748650 (0.54%) (init = 100)
    exp_decay:      90.9508838 +/- 1.10310767 (1.21%) (init = 100)
    g1_fwhm:        39.2609214 +/- 0.37790648 (0.96%) == '2.3548200*g1_sigma'
    g1_height:      101.880229 +/- 0.59217051 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'
    g2_fwhm:        32.5128709 +/- 0.43988398 (1.35%) == '2.3548200*g2_sigma'
    g2_height:      72.0455939 +/- 0.61721985 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma)'
    area:           6751.19106 +/- 46.5080249 (0.69%) == 'g1_amplitude + g2_amplitude'
    centroid:       124.108459 +/- 0.14074482 (0.11%) == '(g1_amplitude*g1_center+ g2_amplitude*g2_center)/(g1_amplitude + g2_amplitude)'
    post_area:      6751.19106 +/- 46.5080249 (0.69%) == 'g1_amplitude + g2_amplitude'
    post_centroid:  124.108459 +/- 0.14074482 (0.11%) == '(g1_amplitude*g1_center+ g2_amplitude*g2_center)/(g1_amplitude + g2_amplitude)'
[[Correlations]] (unreported correlations are < 0.500)
    C(g1_amplitude, g1_sigma)   = +0.8243
    C(g2_amplitude, g2_sigma)   = +0.8154
    C(exp_amplitude, exp_decay) = -0.6946
    C(g1_sigma, g2_center)      = +0.6842
    C(g1_center, g2_amplitude)  = -0.6689
    C(g1_center, g2_sigma)      = -0.6520
    C(g1_amplitude, g2_center)  = +0.6477
    C(g1_center, g2_center)     = +0.6205
    C(g1_center, g1_sigma)      = +0.5075
    C(g1_amplitude, exp_decay)  = -0.5074

# <examples/doc_uvars_params.py>
import numpy as np

from lmfit.models import ExponentialModel, GaussianModel

dat = np.loadtxt('NIST_Gauss2.dat')
x = dat[:, 1]
y = dat[:, 0]

bkg = ExponentialModel(prefix='exp_')
gauss1 = GaussianModel(prefix='g1_')
gauss2 = GaussianModel(prefix='g2_')

mod = gauss1 + gauss2 + bkg

pars = mod.make_params(g1_center=105, g1_amplitude=4000, g1_sigma={'value': 15, 'min': 0},
                       g2_center=155, g2_amplitude=2000, g2_sigma={'value': 15, 'min': 0},
                       exp_amplitude=100, exp_decay=100)

out = mod.fit(y, pars, x=x)

print(out.fit_report(min_correl=0.5))

# Area and Centroid of the combined peaks
# option 1: from output uncertainties values

uvar_g1amp = out.uvars['g1_amplitude']
uvar_g2amp = out.uvars['g2_amplitude']
uvar_g1cen = out.uvars['g1_center']
uvar_g2cen = out.uvars['g2_center']

print("### Peak Area: ")
print(f"Peak1: {out.params['g1_amplitude'].value:.5f}+/-{out.params['g1_amplitude'].stderr:.5f}")
print(f"Peak2: {out.params['g2_amplitude'].value:.5f}+/-{out.params['g2_amplitude'].stderr:.5f}")
print(f"Sum  : {(uvar_g1amp + uvar_g2amp):.5f}")


area_quadrature = np.sqrt(out.params['g1_amplitude'].stderr**2 + out.params['g2_amplitude'].stderr**2)

print(f"Correlation of peak1 and peak2 areas:            {out.params['g1_amplitude'].correl['g2_amplitude']:.5f}")
print(f"Stderr Quadrature sum for peak1 and peak2 area2: {area_quadrature:.5f}")
print(f"Stderr of peak1 + peak2 area, with correlation:  {(uvar_g1amp+uvar_g2amp).std_dev:.5f}")

print("### Peak Centroid: ")

centroid = (uvar_g1amp * uvar_g1cen + uvar_g2amp * uvar_g2cen) / (uvar_g1amp + uvar_g2amp)

print(f"Peak Centroid: {centroid:.5f}")

# option 2: define corresponding variables after fit


def post_fit(result):
    "example post fit function"
    result.params.add('post_area', expr='g1_amplitude + g2_amplitude')
    result.params.add('post_centroid', expr='(g1_amplitude*g1_center+ g2_amplitude*g2_center)/(g1_amplitude + g2_amplitude)')


mod.post_fit = post_fit

out = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.5))

# option 3: define corresponding variables before fit
pars.add('area', expr='g1_amplitude + g2_amplitude')
pars.add('centroid', expr='(g1_amplitude*g1_center+ g2_amplitude*g2_center)/(g1_amplitude + g2_amplitude)')

out = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.5))
# <end examples/doc_uvars_params.py>

Total running time of the script: (0 minutes 0.077 seconds)

Gallery generated by Sphinx-Gallery