[英]how to create a qq plot between two samples of different size in python?
我得到了原始樣本數據及其模擬數據(不要問我如何模擬),我想檢查直方圖是否匹配。 因此,最好的方法是使用qqplot
但是statsmodels
庫不允許使用大小不同的樣本。
構造一個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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.