在直方图上拟合泊松分布

时间:2019-03-25 00:43:52

标签: python-3.x statistics distribution curve-fitting poisson

尽管将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()

我得到的直方图如下:

enter image description here

我给出了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的直方图,该直方图在技术上是从泊松分布逆中随机采样的数据。因此,我尝试计算分布的雅可比关系,以便可以更改变量,但仍然无法使用。我觉得我在这里想念的不是编码而是数学相关的东西。

1 个答案:

答案 0 :(得分:0)

您正在计算的舍入为零。比率为2000,刻度为7000,泊松公式将减少为:

7000 * 2000 ^ t /(e ^(2000)* t!)

使用斯特林的近似值t! 〜(2 * pi * t)^(1/2)*(t / e)^ t您会得到:

[7000 * 2000 ^ t] / [Sqrt(2 * pi * t)* e ^(2000-t)*(t ^ t)]〜泊松(t)

我使用python获取了poisson(t)的前几个值:

poisson(1) -> 0
poisson(2) -> 0
poisson(3) -> 0

使用Wolfram alpha,对于所有大于零的实数,您发现分母的导数大于分子的导数。因此,随着t变大,泊松(t)接近零。

这意味着无论t是多少,如果您的费率为2000,则泊松函数将返回0。

很抱歉格式化。他们还不让我发布TeX。