首页 > 解决方案 > 为什么我不能使用卡方检验拟合泊松分布?装修有什么问题?

问题描述

我想检查一组数据点是否遵循泊松分布。为此,我首先使用 10 个 bin 绘制直方图并计算每个 bin 中的值。然后我尝试使用以下代码拟合泊松分布。最后我的卡方值太大了。我已经经历了几个例子,其中卡方值与卡方分布表进行比较是合理的。我的输入数据 x 已经标准化了用于绘制直方图的 256 个值。由于输入数据包含正值和负值。归一化后,我得到了合理的直方图形状,如附图所示。我真的很困惑为什么卡方值太大?如果我将值从 256 增加到 1000,直方图形状看起来仍然类似于泊松分布,但 chisqure 值也显示出大幅增加。我该如何解决这个问题?

`x =np.array(flatten_list)
x = Ls_Store/Ls_Store.mean()
#Formula of Poisson distribution:
def Poisson_fit(mu,x):
    return (((mu**x) * np.exp(-mu))/math.factorial(x)) 

fig = plt.figure()
ax = fig.add_subplot(111)

hist,bins,patches=plt.hist(x,bins=10,label='h=5.0')
print("bin_counts: ",hist)
#bin_count: [94. 64. 32. 30. 16. 11.  3.  1.  3.  1.]

x_ = np.arange(len(hist))

fx = np.multiply(x_,hist)

mu = np.sum(fx)/np.sum(hist)
print("mu: ",mu)
#mu:  1.5490196078431373
result=[]
for k in range(len(x_)):
    result.append(Poisson_fit(mu,x_[k]))
result = np.array(result)
h = np.sum(hist)

#Counts in Poisson
m= h*result

#Implement Chisquare Test
f_obs =hist
f_exp= m
print("f_obs: ",f_obs)
print("f_exp: ",f_exp)
chisquare =[]
for h in range(len(f_obs)):
    chisquare.append(((f_obs[h]-f_exp[h])**2)/f_exp[h])
chisquare = np.array(chisquare)
print(np.sum(chisquare))
#394.74825860641397`

标签: python-3.xmatplotlibscipystatisticschi-squared

解决方案


推荐阅读