【问题标题】:QQplot for discrete distribution用于离散分布的 QQplot
【发布时间】:2020-05-05 01:45:23
【问题描述】:

我有一个集合,其样本是离散值(特别是队列随时间变化的大小)。现在我想找到它们属于哪个发行版。为了实现这个目标,我会采取与其他数量相同的方式,即绘制 qqplot,启动

import statsmodels.api as sm 
sm.qqplot(df, dist = 'geom', sparams = (.5,), line ='s', alpha = 0.3, marker ='.')

如果dist 不是离散随机变量(例如“exp”或“norm”)并且确实我曾经得到一些结果,但当分布是离散的(例如,“geom”)时,我会得到

AttributeError: 'geom_gen' object has no attribute 'fit'

我在互联网上搜索了如何制作 qqplot(或类似的东西)以发现我的样本属于哪个分布,但我什么也没找到

【问题讨论】:

    标签: python python-3.x distribution quantile


    【解决方案1】:
    def discreteQQ(x_sample):
    
        p_test = np.array([])
        for i in range(0, 1001):
            p_test = np.append(p_test, i/1000)
            i = i + 1
    
        x_sample = np.sort(x_sample)
        x_theor = stats.geom.rvs(.5, size=len(x_sample))
        ecdf_sample = np.arange(1, len(x_sample) + 1)/(len(x_sample)+1)
    
        x_theor = stats.geom.ppf(ecdf_sample, p=0.5)
    
        for p in p_test:
            plt.scatter(np.quantile(x_theor, p), np.quantile(x_sample, p), c = 'blue')
    
        plt.xlabel('Theoretical quantiles')
        plt.ylabel('Sample quantiles')
        plt.show()
    

    【讨论】:

      【解决方案2】:

      使用 scipy.stats.geom 生成理论几何分布,使用 statsmodels 的 ProbPlot 转换样本和理论数据,并将它们传递给 statsmodels 的 qqplot_2samples。

      import numpy as np
      from scipy import stats
      import matplotlib.pyplot as plt
      
      from statsmodels.graphics.gofplots import ProbPlot
      from statsmodels.graphics.gofplots import qqplot_2samples
      
      p_theor = 1/4 # The probability we check for
      p_sample = 1/5 # The true probability of the sample distribution
      
      # The experimental data
      x_sample = stats.geom.rvs(p_sample, size=50)
      
      # The model data
      x_theor = stats.geom.rvs(p_theor, size=100)
      
      qqplot_2samples(ProbPlot(x_sample), ProbPlot(x_theor), line='45')
      plt.show()
      

      【讨论】:

      • 我想我明白你的意思,但我是 Python 新手,因此我想问你,如果你不介意,告诉我我发布的函数是否基于您的答案,(希望)在给定样本集的情况下绘制 QQplot 是正确的
      • 我在哪里犯错?
      • 如果您修改您的代码,我将不胜感激。我知道QQ图是什么,我的问题是我不懂Python
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      相关资源
      最近更新 更多