python 中的加权直方图错误

Error on weighted histogram in python

我想通过对该容器中的权重平方和 (sumw2) 求平方根(泊松误差)来计算该容器高度的误差。当使用 matplotlib 或 numpy(或与此相关的任何其他库)对数据进行直方图时,有什么方法可以获得权重总和(sumw)and/or sumw2?

假设我在一个 numpy 数组 x 中有一些数据,在另一个 numpy 数组中有一些权重 w,为了得到直方图我会这样

n, bins, patches = pyplot.hist(x,weights=w)

n, bins = numpy.histogram(x,weights=w)

在这两种情况下,我都不知道 w 的哪些条目属于哪个 bin,对吗?

编辑:目前我正在使用 YODA 来执行此操作。从我的角度来看,缺点是 YODA 直方图一次只能填充一个数据点。

根据 numpy documentation,权重

An array of weights, of the same shape as a. Each value in a only contributes its associated weight towards the bin count (instead of 1). If density is True, the weights are normalized, so that the integral of the density over the range remains 1.

这意味着 w 中的每个值都应与 x 中的值相关联。如果您想对 bin 加权并绘制它们,您可以首先找到 bin 的值,将它们乘以权重,最后使用 bar.

绘制它们
val, pos = np.histogram(np.arange(1000))
w_val = val * w
plt.bar(pos[1:], w_val)


评论更新:

啊,不好意思,我一开始的问题好像没看懂。实际上,您可以使用 pos 来查找与每个 bin 相关的单元格,并使用这些信息计算您的权重函数。

for left, right in zip(pos, pos[1:): 
    ix = np.where((x >= left) & (x <= right))[0] 
    sumw2 = np.sum(w[ix] ** 2) 

考虑一个数组 x,权重 wx 中由 wbins 加权的数据直方图由下式给出:

n, bins = np.histogram(x, bins=bins, weights=w)

n 的相关误差可以计算为:

n_err = np.sqrt(np.histogram(x, bins=bins, weights=w**2)[0])

请注意,如果数据未加权(即 (w == 1).all()),则误差会降低到“标准”np.sqrt(n)