拟合直方图上的泊松分布

问题描述 投票:1回答:1

尽管有很多关于将泊松分布拟合到直方图上的帖子,但是跟踪了所有这些帖子,它们似乎都不适合我。

我想在这个直方图上拟合泊松分布,我已经绘制了这样的:

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的直方图,这是技术上从泊松分布的逆的随机采样数据。所以我试着计算分布的jacobian,这样我就可以对变量进行更改,但它仍然不起作用。我觉得我在这里遗漏了一些不是编码而是数学相关的东西。

python-3.x statistics distribution curve-fitting poisson
1个回答
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)接近零。

这意味着无论是什么,如果你的速率是2000,泊松函数将返回0。

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

© www.soinside.com 2019 - 2024. All rights reserved.