Question about the parameters of the fit

47 views
Skip to first unread message

Boris Deshev

unread,
Dec 30, 2023, 10:20:06 AM12/30/23
to lmfit-py
Hi, 
I am fitting the built-in SkewedGaussianModel to some data. The fit seems to be working well but when I print the parameters of the fitted model the values seems to be off. Looking at the attached plot I expect the amplitude to be around 30, the center around 6, sigma around 2, I guess. When I print the parameters I get
{'amplitude': 152.51586530547021, 'center': 4.95450125813345, 'sigma': 3.5260258280394083, 'gamma': 3.074519008172669}
What am I missing?
test_lmfit_skewed_gaussian.png

The code used to make this plot:
#-----
import numpy as np
import matplotlib.pyplot as plt
from lmfit.models import SkewedGaussianModel
np.random.seed(42)

bins = np.linspace(0, 15, 101)

# Generate something like a skewed Gaussian distribution
mult_x = np.linspace(1, 2, 1000)
data_y = (np.random.randn(1000) + 5) * mult_x

# Make the histogram
vals, bins = np.histogram(data_y, bins=bins)
x = (bins[1:]+bins[:-1])/2

# Fit a model
model = SkewedGaussianModel()
params = model.make_params(amplitude=np.max(vals),\
                           center=x[np.argmax(vals)],\
                           sigma=1,\
                           gamma=2)
Dgau = model.fit(vals, params=params, x=x)

# Get the fitted values
fitted_values = Dgau.eval(x=x)

# Plot
plt.plot(x, vals)
plt.plot(x,fitted_values)
plt.axvline(x=Dgau.values['center'], color='black', lw=0.5)
plt.show()
print(Dgau.values)
#-----

Cheers,
Boris

Matt Newville

unread,
Dec 30, 2023, 10:52:36 AM12/30/23
to lmfi...@googlegroups.com
On Sat, Dec 30, 2023 at 9:20 AM Boris Deshev <des...@gmail.com> wrote:
Hi, 
I am fitting the built-in SkewedGaussianModel to some data. The fit seems to be working well but when I print the parameters of the fitted model the values seems to be off. Looking at the attached plot I expect the amplitude to be around 30, the center around 6, sigma around 2, I guess. When I print the parameters I get
{'amplitude': 152.51586530547021, 'center': 4.95450125813345, 'sigma': 3.5260258280394083, 'gamma': 3.074519008172669}
What am I missing?


Amplitude, center, and sigma are those of the unskewed (unit-normalized) Gaussian.  That amplitude is not the maximum value of that Gaussian, it would be the area.    Similarly, the center would be the center of the Gaussian.  But that gets skewed by multiplying by an error function, centered at center, and with gamma and sigma controlling how sharp that rise is.

--Matt 

Boris Deshev

unread,
Dec 30, 2023, 12:03:12 PM12/30/23
to lmfi...@googlegroups.com
Got it. Thanks!

--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESbrfU74Qsr2_3eKgym%2B5qSubW6%3DLrNnryKn%3DRVu6CJ7uGw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages