Search code examples
lmfit

How to draw samples from lmfit model?


I have a skewed Gaussian model in lmfit that fits my data. Now I would like to draw a sample from it, but I could nowhere in the documentation find how? Is the correct approach to simply implement the model function yourself in my case the skewed normal distribution or is there a function in lmfit for this?

My code:

model = SkewedGaussianModel()

params = model.make_params(amplitude=60, center=30, sigma=10, gamma=0)

result = model.fit(y, params, x=x)
print(result.fit_report())
plt.plot(x, result.best_fit)
plt.show()
# something like this
print(result.model.eval(random.random())

Solution

  • The definition for skewed Gaussian used in lmfit is given at http://lmfit.github.io/lmfit-py/builtin_models.html#skewedgaussianmodel with code at https://github.com/lmfit/lmfit-py/blob/master/lmfit/lineshapes.py#L213.

    I believe that what you are looking for is the "inverse transform sampling". See http://www.nehalemlabs.net/prototype/blog/2013/12/16/how-to-do-inverse-transformation-sampling-in-scipy-and-numpy/ for hints on how to do that. There is not a built-in method for this in lmfit, as lmfit does not necessarily assert that the model being fitted is a probability distribution function. It might be worth considering adding such a capability.