尽管将Poisson分布拟合到直方图上的职位过多,但都遵循了所有原则,但似乎没有一个对我有用。
我希望在此直方图中拟合泊松分布,如下所示:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.misc import factorial
def poisson(t, rate, scale): #scale is added here so the y-axis
# of the fit fits the height of histogram
return (scale*(rate**t/factorial(t))*np.exp(-rate))
lifetimes = 1/np.random.poisson((1/550e-6), size=100000)
hist, bins = np.histogram(lifetimes, bins=50)
width = 0.8*(bins[1]-bins[0])
center = (bins[:-1]+bins[1:])/2
plt.bar(center, hist, align='center', width=width, label = 'Normalised data')
popt, pcov = curve_fit(poisson, center, hist, bounds=(0.001, [2000, 7000]))
plt.plot(center, poisson(center, *popt), 'r--', label='Poisson fit')
# import pdb; pdb.set_trace()
plt.legend(loc = 'best')
plt.tight_layout()
我得到的直方图如下所示:
我给出了7000的比例猜测,以将分布缩放到与我绘制的直方图的y轴相同的高度,并给出2000的猜测作为速率参数,因为它是2000 > 1/550e-6 。 如您所见,拟合的红色虚线在每个点均为0。 奇怪的是pdb.set_trace()告诉我poisson(center, *popt)给了我一个0值的列表。
126 plt.plot(center, poisson(center, *popt), 'r--', label='Poisson fit')
127 import pdb; pdb.set_trace()
--> 128 plt.legend(loc = 'best')
129 plt.tight_layout()
130
ipdb>
ipdb> poisson(center, *popt)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
这没有意义。 我想要的是在直方图上拟合泊松分布,以便找到泊松分布方程的最佳系数。 我怀疑这可能与它有关,因为我是在绘制lifetimes直方图,这是从泊松分布的逆函数中技术上随机采样的数据。 因此,我尝试计算分布的雅可比关系,以便可以更改变量,但仍然无法使用。 我觉得我在这里想念的不是编码而是数学相关的东西。