如何从 lmfit 模型中抽取样本?

How to draw samples from lmfit model?

我在 lmfit 中有一个适合我的数据的倾斜高斯模型。现在我想从中抽取一个样本,但我无法在文档中找到如何做?在我的例子中,自己简单地实现模型函数的正确方法是 the skewed normal distribution 还是 lmfit 中有一个函数?

我的代码:

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())

直到有人能找到这个函数,或者确认它不存在,我就是这样做的:

def pdf(x):
    return 1/sqrt(2*pi) * exp(-x**2/2)

def cdf(x):
    return (1 + erf(x/sqrt(2))) / 2

def skew(x,e=0,w=1,a=0):
    t = (x-e) / w
    return 2 / w * pdf(t) * cdf(a*t)
    # You can of course use the scipy.stats.norm versions
    # return 2 * norm.pdf(t) * norm.cdf(a*t)

复制自this answer

lmfit 中使用的偏斜高斯的定义在 http://lmfit.github.io/lmfit-py/builtin_models.html#skewedgaussianmodel 代码在 https://github.com/lmfit/lmfit-py/blob/master/lmfit/lineshapes.py#L213

相信您要找的就是"inverse transform sampling"。有关如何执行此操作的提示,请参阅 http://www.nehalemlabs.net/prototype/blog/2013/12/16/how-to-do-inverse-transformation-sampling-in-scipy-and-numpy/。 lmfit 中没有内置方法,因为 lmfit 不一定断言所拟合的模型是概率分布函数。可能值得考虑添加这样的功能。