【问题标题】:how to create a qq plot between two samples of different size in python?如何在python中两个不同大小的样本之间创建一个qq图?
【发布时间】:2017-03-07 21:01:20
【问题描述】:
我得到了一个原始样本数据及其模拟数据(不要问我是如何模拟的),我想检查直方图是否匹配。所以最好的方法是通过qqplot 但statsmodels 库不允许不同大小的样本。
【问题讨论】:
-
欢迎来到 Stack Overflow。像这样要求起点的问题不适合 Stack Overflow 的格式。添加有关您尝试过的内容的一些详细信息,包括代码示例。并详细说明您遇到的具体问题。 Check here 了解更多帮助他人帮助您的技巧。
标签:
python
matplotlib
histogram
data-analysis
【解决方案1】:
构建 qq 图需要在两组中找到对应的分位数并将它们相互绘制。在一个集合大于另一个集合的情况下,通常的做法是取较小集合的分位数水平,并使用线性插值来估计较大集合中的相应分位数。此处对此进行了描述:http://www.itl.nist.gov/div898/handbook/eda/section3/qqplot.htm
手动操作相对简单:
import numpy as np
import pylab
test1 = np.random.normal(0, 1, 1000)
test2 = np.random.normal(0, 1, 800)
#Calculate quantiles
test1.sort()
quantile_levels1 = np.arange(len(test1),dtype=float)/len(test1)
test2.sort()
quantile_levels2 = np.arange(len(test2),dtype=float)/len(test2)
#Use the smaller set of quantile levels to create the plot
quantile_levels = quantile_levels2
#We already have the set of quantiles for the smaller data set
quantiles2 = test2
#We find the set of quantiles for the larger data set using linear interpolation
quantiles1 = np.interp(quantile_levels,quantile_levels1,test1)
#Plot the quantiles to create the qq plot
pylab.plot(quantiles1,quantiles2)
#Add a reference line
maxval = max(test1[-1],test2[-1])
minval = min(test1[0],test2[0])
pylab.plot([minval,maxval],[minval,maxval],'k-')
pylab.show()