【问题标题】:Fitting a pareto distribution with (python) Scipy使用(python)Scipy 拟合帕累托分布
【发布时间】:2011-03-15 14:44:30
【问题描述】:

我有一个我知道具有帕累托分布的数据集。有人可以指出如何在 Scipy 中拟合这个数据集吗?我得到了下面的代码来运行,但我不知道返回给我的是什么(a,b,c)。另外,得到a,b,c后,如何使用它们计算方差?

import scipy.stats as ss 
import scipy as sp

a,b,c=ss.pareto.fit(data)

【问题讨论】:

    标签: python scipy distribution


    【解决方案1】:

    要非常小心地拟合幂律!! 许多报道的幂律实际上都被幂律严重拟合了。有关所有详细信息,请参阅 Clauset et al.(如果您无权访问该期刊,请参阅 arxiv)。他们在文章中有一个companion website,该文章现在链接到 Python 实现。不知道它是否使用 Scipy,因为我上次使用时使用了他们的 R 实现。

    【讨论】:

    【解决方案2】:

    这是一个快速编写的版本,从 Rupert 提供的参考页面中获取了一些提示。 这目前正在 scipy 和 statsmodels 中进行,并且需要具有一些固定或冻结参数的 MLE,这仅在主干版本中可用。 尚无参数估计或其他结果统计的标准误差。

    '''estimating pareto with 3 parameters (shape, loc, scale) with nested
    minimization, MLE inside minimizing Kolmogorov-Smirnov statistic
    
    running some examples looks good
    Author: josef-pktd
    '''
    
    import numpy as np
    from scipy import stats, optimize
    #the following adds my frozen fit method to the distributions
    #scipy trunk also has a fit method with some parameters fixed.
    import scikits.statsmodels.sandbox.stats.distributions_patch
    
    true = (0.5, 10, 1.)   # try different values
    shape, loc, scale = true
    rvs = stats.pareto.rvs(shape, loc=loc, scale=scale, size=1000)
    
    rvsmin = rvs.min() #for starting value to fmin
    
    
    def pareto_ks(loc, rvs):
        est = stats.pareto.fit_fr(rvs, 1., frozen=[np.nan, loc, np.nan])
        args = (est[0], loc, est[1])
        return stats.kstest(rvs,'pareto',args)[0]
    
    locest = optimize.fmin(pareto_ks, rvsmin*0.7, (rvs,))
    est = stats.pareto.fit_fr(rvs, 1., frozen=[np.nan, locest, np.nan])
    args = (est[0], locest[0], est[1])
    print 'estimate'
    print args
    print 'kstest'
    print stats.kstest(rvs,'pareto',args)
    print 'estimation error', args - np.array(true)
    

    【讨论】:

      【解决方案3】:

      假设您的数据格式如下

      import openturns as ot
      data = [
          [2.7018013],
          [8.53280352],
          [1.15643882],
          [1.03359467],
          [1.53152735],
          [32.70434285],
          [12.60709624],
          [2.012235],
          [1.06747063],
          [1.41394096],
      ]
      sample = ot.Sample([[v] for v in data])
      

      您可以使用 OpenTURNS 库的 ParetoFactory 轻松拟合 Pareto 分布:

      distribution = ot.ParetoFactory().build(sample)
      

      你当然可以打印出来:

      print(distribution)
      >>> Pareto(beta = 0.00317985, alpha=0.147365, gamma=1.0283)
      

      或绘制其 PDF:

      from openturns.viewer import View
      
      pdf_graph = distribution.drawPDF()
      pdf_graph.setTitle(str(distribution))
      View(pdf_graph, add_legend=False)
      

      文档中提供了有关ParetoFactory 的更多详细信息。

      【讨论】:

      • 我试过运行你的代码,它对我不起作用。首先,我认为应该是distribution = ot.ParetoFactory.build(data)。其次,我得到了一个TypeError: Wrong number or type of arguments for overloaded function 'ParetoFactory_build'. 看起来 Openturns 库想要一个特定的“样本”格式,但我不明白它应该如何格式化。
      • 感谢@Tropilio 的反馈。应该是:distribution = ot.ParetoFactory().build(data)。我更正了上面的代码。要被 OpenTURNS 库理解,样本应该是形状(大小、尺寸)的。在我的示例中,它是 (10, 1)。
      【解决方案4】:

      在将数据传递给 OPENTURNS 中的 build() 函数之前,请确保以这种方式进行转换:

      data = [[i] for i in data]
      

      因为 Sample() 函数可能会返回错误。

      仅供参考@Tropilio

      【讨论】:

      • 这如何回答这个问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2021-04-19
      • 1970-01-01
      • 2018-12-24
      相关资源
      最近更新 更多