使用 (python) Scipy 拟合伽玛分布
谁能帮我在 python 中拟合伽玛分布?好吧,我有一些数据:X 和 Y 坐标,我想找到适合这个分布的伽马参数......在 Scipy doc,原来确实存在一个 fit 方法,但我不知道如何使用它:s ..首先,参数“数据”必须采用哪种格式,以及如何提供第二个参数(参数),因为这就是我正在寻找的?
Can anyone help me out in fitting a gamma distribution in python? Well, I've got some data : X and Y coordinates, and I want to find the gamma parameters that fit this distribution... In the Scipy doc, it turns out that a fit method actually exists but I don't know how to use it :s.. First, in which format the argument "data" must be, and how can I provide the second argument (the parameters) since that's what I'm looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
生成一些伽玛数据:
这里我们将数据拟合到伽玛分布:
Generate some gamma data:
Here we fit the data to the gamma distribution:
我对 ss.gamma.rvs 函数不满意,因为它可以生成负数,而伽玛分布不应该有负数。因此,我通过预期值 = 均值(数据)和方差 = var(数据)(有关详细信息,请参阅维基百科)来拟合样本,并编写了一个函数,可以在没有 scipy 的情况下生成伽马分布的随机样本(我发现很难正确安装,旁注):
I was unsatisfied with the ss.gamma.rvs-function as it can generate negative numbers, something the gamma-distribution is supposed not to have. So I fitted the sample through expected value = mean(data) and variance = var(data) (see wikipedia for details) and wrote a function that can yield random samples of a gamma distribution without scipy (which I found hard to install properly, on a sidenote):
如果您想要一个长示例,包括有关估计或修复发行版支持的讨论,那么您可以在 https://github.com/scipy/scipy/issues/1359 以及链接的邮件列表消息。
scipy 的主干版本中添加了对拟合期间修复参数(例如位置)的初步支持。
If you want a long example including a discussion about estimating or fixing the support of the distribution, then you can find it in https://github.com/scipy/scipy/issues/1359 and the linked mailing list message.
Preliminary support to fix parameters, such as location, during fit has been added to the trunk version of scipy.
OpenTURNS 有一个简单的方法可以使用
GammaFactory
类来执行此操作。首先,让我们生成一个样本:
然后将 Gamma 拟合到它:
然后我们可以绘制 Gamma 的 PDF:
生成:
有关此主题的更多详细信息,请访问:http://openturns.github.io/openturns/latest/user_manual/_ generated/openturns.GammaFactory.html
OpenTURNS has a simple way to do this with the
GammaFactory
class.First, let's generate a sample:
Then fit a Gamma to it:
Then we can draw the PDF of the Gamma:
which produces:
More details on this topic at: http://openturns.github.io/openturns/latest/user_manual/_generated/openturns.GammaFactory.html
1):“data”变量可以是python列表或元组的格式,也可以是numpy.ndarray的格式,可以通过以下方式获得:
其中上行中的第二个数据应该是列表或元组,包含您的数据。
2:“参数”变量是您可以选择提供给拟合函数作为拟合过程的起点的第一个猜测,因此可以省略。
3:关于@mondano答案的注释。使用矩(均值和方差)来计算伽马参数对于大形状参数(alpha>10)来说相当好,但对于较小的 alpha 值可能会产生较差的结果(参见大气科学中的统计方法< /em> 作者:Wilks 和 THOM,HCS,1958:关于 gamma 分布的注释,Wea,86,117-122,
正如在 scipy 模块中实现的那样。在这种情况下的选择。
1): the "data" variable could be in the format of a python list or tuple, or a numpy.ndarray, which could be obtained by using:
where the 2nd data in the above line should be a list or a tuple, containing your data.
2: the "parameter" variable is a first guess you could optionally provide to the fitting function as a starting point for the fitting process, so it could be omitted.
3: a note on @mondano's answer. The usage of moments (mean and variances) to work out the gamma parameters are reasonably good for large shape parameters (alpha>10), but could yield poor results for small values of alpha (See Statistical methods in the atmospheric scineces by Wilks, and THOM, H. C. S., 1958: A note on the gamma distribution. Mon. Wea. Rev., 86, 117–122.
Using Maximum Likelihood Estimators, as that implemented in the scipy module, is regarded a better choice in such cases.