如何解决在Python中拟合泊松直方图的问题
我正在尝试使用泊松分布来拟合一些数据,但这是行不通的。
x = [46,71,106,126,40,27,19,103,46,89,31,70,35,43,82,128,185,47,18,36,96,30,135,72,32,86,76,116,51,23,121,22,107,65,93,25,74,73,111,56,34,28,87,14,54,63,50,62,59,39,15,68,69,37,41,20,44,102,42,57,53,48,10,140,178,206,26,5,17,139,49,122,110,81,83,2,190,124,60,156,58,29,132,100,55,24,66,112,45,13,85,80,33,75,142,67,101,98,11,77,61,127,148,109,52,146,120,136,64,21,3,38,16,196,119,108,104,95,194,173,27] #geiger count
bins = np.histogram_bin_edges(x)
n,bins_edges,patches = plt.hist(x,bins,density=1,facecolor='darkblue',ec='white',log=0)
print(n)
bin_middles = 0.5*(bins_edges[1:] + bins_edges[:-1])
def fit_function(k,lamb):
return poisson.pmf(k,lamb)
parameters,cov_matrix = curve_fit(fit_function,bin_middles,n)
x_plot = np.arange(0,max(x))
plt.plot(x_plot,fit_function(x_plot,*parameters),label='Poisson')
plt.show()
我得到这个结果,但是我们可以看到它不正确
解决方法
您正在使用np.histogram_bin_edges
之类的用于连续分布的函数,而泊松分布是离散的。
根据wikipedia,可以通过仅取样本均值来估算lambda:
from scipy.stats import poisson
import numpy as np
from matplotlib import pyplot as plt
x = [46,71,106,126,40,27,19,103,46,89,31,70,35,43,82,128,185,47,18,36,96,30,135,72,32,86,76,116,51,23,121,22,107,65,93,25,74,73,111,56,34,28,87,14,54,63,50,62,59,39,15,68,69,37,41,20,44,102,42,57,53,48,10,140,178,206,26,5,17,139,49,122,110,81,83,2,190,124,60,156,58,29,132,100,55,24,66,112,45,13,85,80,33,75,142,67,101,98,11,77,61,127,148,109,52,146,120,136,64,21,3,38,16,196,119,108,104,95,194,173,27] bins = np.histogram_bin_edges(x)
n,bins_edges,patches = plt.hist(x,bins,density=1,facecolor='darkblue',ec='white',log=0)
lamd = np.mean(x)
x_plot = np.arange(0,max(x) + 1)
plt.plot(x_plot,poisson.pmf(x_plot,lamd),label='Poisson')
plt.show()
计算出的拉姆达(Lambda)约为60。该图似乎表明,泊松分布与给定样本的拟合度不是很高。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。