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

时间:2017-07-02 21:32:34

标签: lmfit

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

2 个答案:

答案 0 :(得分:1)

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

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

复制

答案 1 :(得分:1)

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

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