直方图python高度_python-3.x

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

我得到的直方图如下所示:

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9lc3drTS5wbmc=

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值