【问题标题】:Runtimewarning when using scipy.stats.beta.fit使用 scipy.stats.beta.fit 时的运行时警告
【发布时间】:2017-11-19 09:58:15
【问题描述】:

如果我在 python 中运行以下代码

from scipy.stats import norm, beta
sample = beta.rvs(2,5,size=100)
beta_fit = beta.fit(sample)

我收到以下错误

/usr/lib/python3/dist-packages/scipy/stats/_continuous_distns.py:404: RuntimeWarning: invalid 
value encountered in sqrt
sk = 2*(b-a)*sqrt(a + b + 1) / (a + b + 2) / sqrt(a*b)

根据样本的大小,我有时也会遇到其他错误

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py:161: RuntimeWarning: 
The iteration is not making good progress, as measured by the improvement from the last ten iterations.
warnings.warn(msg, RuntimeWarning)

有谁知道为什么会发生这种情况以及如何解决?

谢谢!

【问题讨论】:

  • 仅供参考:Scipy 的 beta 分布有四个参数。有两个形状参数(wikipedia article 中的 α 和 β)。其他两个参数是location and scale;这些调整分布的support。默认情况下,fit() 方法认为所有四个参数都可用于拟合。那是你要的吗?还是只适合形状参数,并保持支持为 [0, 1]?
  • 我想保持支持为[0,1],我该如何强加?我假设 beta.fit 函数采用 loc=0 和 scale=1 的默认值。我错了吗?
  • 默认情况下,fit() 在要拟合的参数中包含locscale。请参阅我的答案以了解如何保持这些固定。

标签: python python-3.x scipy statistics


【解决方案1】:

在评论中您说您希望将支持固定为 [0, 1]。要使用fit() 方法做到这一点,请使用参数floc=0fscale=1。那么只有形状参数会适合数据。

from scipy.stats import beta

sample = beta.rvs(2, 5, size=100)
beta_fit = beta.fit(sample, floc=0, fscale=1)

这也应该消除您看到的警告。出现这些警告是因为当所有四个参数都适合时,代码使用通用数值优化例程来查找最大化可能性的参数,并且该代码中的某些内容正在生成这些警告。 (这可能是一个错误——形状参数应该是正数,因此在生成警告的行中对 sqrt 的调用都不应该得到负数参数。)当您修复位置和比例时,@ 987654326@ 方法解决了一个更简单的数值问题来找到最大似然参数估计,因此它避免了生成警告的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多