卡方检验给出错误的结果。我应该拒绝提议的分配吗?

时间:2019-07-09 02:45:19

标签: matplotlib scipy chi-squared scipy-optimize

我想在我的数据点上拟合泊松分布,并希望基于卡方检验来决定是否接受或拒绝该提议的分布。我只使用了10个观察结果。这是我的代码

#Fitting function:  
def Poisson_fit(x,a):
    return (a*np.exp(-x))
#Code
hist, bins= np.histogram(x, bins=10, density=True)
print("hist: ",hist)
#hist:  [5.62657158e-01, 5.14254073e-01, 2.03161280e-01, 5.84898068e-02,
1.35995217e-02,2.67094169e-03,4.39345778e-04,6.59603327e-05,1.01518320e-05, 
1.06301906e-06]
XX = np.arange(len(hist))
print("XX: ",XX)
#XX:  [0 1 2 3 4 5 6 7 8 9]
plt.scatter(XX, hist, marker='.',color='red')
popt, pcov = optimize.curve_fit(Poisson_fit, XX, hist)
plt.plot(x_data, Poisson_fit(x_data,*popt), linestyle='--',color='red', 
label='Fit')
print("hist: ",hist)
plt.xlabel('s')
plt.ylabel('P(s)')


#Chisquare test:
f_obs =hist
#f_obs:  [5.62657158e-01, 5.14254073e-01, 2.03161280e-01, 5.84898068e-02,
1.35995217e-02, 2.67094169e-03, 4.39345778e-04, 6.59603327e-05,
1.01518320e-05, 1.06301906e-06]

f_exp= Poisson_fit(XX,*popt)
f_exp:  [6.76613820e-01, 2.48912314e-01, 9.15697229e-02, 3.36866185e-02,
1.23926144e-02, 4.55898806e-03, 1.67715798e-03, 6.16991940e-04,
2.26978650e-04, 8.35007789e-05]
chi,p_value=chisquare(f_obs,f_exp)

print("chi: ",chi)
print("p_value: ",p_value)
chi:  0.4588956658201067
p_value:  0.9999789643475111`

我使用10个观察值,所以自由度将是9。对于这个自由度,我在卡方分布表上找不到我的p值和chi值。我的代码有什么问题吗?或者我的输入值太小而导致测试失败?如果接受P值> 0.05分布。虽然p值较大,为0.999,但为此无法在表格上找到卡方值0.4588。我认为我的代码有问题。如何解决此错误?

这个返回的chi值是尾巴的临界值吗?如何检查提出的假设?

0 个答案:

没有答案