离散分布的QQplot

2024-10-01 15:46:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个集合,其样本是离散值(特别是队列随时间变化的大小)。现在我想找出它们属于什么分布。为了实现这一目标,我将采取与其他数量相同的方式,即绘制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(或类似的东西),以确定我的样本属于哪个分布,但我什么也没找到


Tags: importapi目标数量队列distas方式
2条回答
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()

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

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

enter image description here

相关问题 更多 >

    热门问题