【问题标题】:How to draw samples from lmfit model?如何从 lmfit 模型中抽取样本?
【发布时间】:2017-12-06 02:56:44
【问题描述】:

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

【问题讨论】:

    标签: lmfit


    【解决方案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 不一定断言要拟合的模型是概率分布函数。可能值得考虑添加这样的功能。

    【讨论】:

    • 感谢您提供这些资源。这是正确的答案。
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 2013-01-30
      • 2021-01-20
      相关资源
      最近更新 更多