【问题标题】:Plotting data points on where they fall in a distribution绘制数据点在分布中的位置
【发布时间】:2024-01-13 16:52:01
【问题描述】:

假设我有一个大型数据集,可以在其中进行某种分析。可以查看概率分布中的值。

现在我有了这个庞大的数据集,然后我想将已知的实际数据与它进行比较。首先,我的数据集中有多少值与已知数据具有相同的值或属性。例如:

这是一个累积分布。实线来自模拟生成的数据,降低的强度只是预测的百分比。然后,星星是观测(已知)数据,根据生成的数据绘制。

我做的另一个例子是点在直方图上的投影效果如何:

我很难标记已知数据点在生成数据集中的位置,并将其与生成数据的分布一起累积绘制。

如果我要尝试检索落在生成数据附近的点数,我会这样开始(不正确):

def SameValue(SimData, DefData, uncert):
     numb = [(DefData-uncert) < i < (DefData+uncert) for i in SimData]
     return sum(numb)

但是我无法计算落在值范围内的点,然后将它们全部设置到我可以绘制的位置。关于如何收集这些数据并将其投影到累积分布上的任何想法?

【问题讨论】:

  • 无论谁对我的帖子投了反对票,你能详细说明为什么我可以改进我做错的地方吗?

标签: python arrays numpy matplotlib distribution


【解决方案1】:

这个问题非常混乱,有很多不相关的信息,但在关键点上保持模糊。我会尽力解释它。

我认为您所追求的是以下内容:给定来自未知分布的有限样本,以固定值获得新样本的概率是多少?

我不确定是否有一个通用的答案,但无论如何这都是要问统计或数学人员的问题。我的猜测是你需要对分布本身做一些假设。

然而,对于实际情况,找出新值位于采样分布的哪个 bin 可能就足够了。

所以假设我们有一个分布x,我们将其划分为bins。我们可以使用numpy.histogram 计算直方图h。然后在每个 bin 中找到一个值的概率由 h/h.sum() 给出。
有一个值v=0.77,我们想知道根据分布的概率,我们可以通过在bin数组中查找索引ind来找出它所属的bin,这个值需要在哪里插入数组以保持排序。这可以使用numpy.searchsorted 来完成。

import numpy as np; np.random.seed(0)

x = np.random.rayleigh(size=1000)
bins = np.linspace(0,4,41)
h, bins_ = np.histogram(x, bins=bins)
prob = h/float(h.sum())

ind = np.searchsorted(bins, 0.77, side="right")
print prob[ind] # which prints 0.058

因此,在 0.77 左右对 bin 中的值进行采样的概率为 5.8%。

另一种选择是在 bin 中心之间插入直方图,以找到概率。

在下面的代码中,我们绘制了一个类似于问题中图片的分布,并使用了两种方法,第一种用于频率直方图,第二种用于累积分布。

import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt

x = np.random.rayleigh(size=1000)
y = np.random.normal(size=1000)
bins = np.linspace(0,4,41)
h, bins_ = np.histogram(x, bins=bins)
hcum = np.cumsum(h)/float(np.cumsum(h).max())

points = [[.77,-.55],[1.13,1.08],[2.15,-.3]]
markers = [ur'$\u2660$',ur'$\u2665$',ur'$\u263B$']
colors = ["k", "crimson" , "gold"]
labels = list("ABC")

kws = dict(height_ratios=[1,1,2], hspace=0.0)
fig, (axh, axc, ax) = plt.subplots(nrows=3, figsize=(6,6), gridspec_kw=kws, sharex=True)

cbins = np.zeros(len(bins)+1)
cbins[1:-1] = bins[1:]-np.diff(bins[:2])[0]/2.
cbins[-1] = bins[-1]
hcumc = np.linspace(0,1, len(cbins))
hcumc[1:-1] = hcum
axc.plot(cbins, hcumc, marker=".", markersize="2", mfc="k", mec="k" )
axh.bar(bins[:-1], h, width=np.diff(bins[:2])[0], alpha=0.7, ec="C0", align="edge")
ax.scatter(x,y, s=10, alpha=0.7)

for p, m, l, c in zip(points, markers, labels, colors):
    kw = dict(ls="", marker=m, color=c, label=l, markeredgewidth=0, ms=10)
    # plot points in scatter distribution
    ax.plot(p[0],p[1], **kw)
    #plot points in bar histogram, find bin in which to plot point
    # shift by half the bin width to plot it in the middle of bar
    pix = np.searchsorted(bins, p[0], side="right")
    axh.plot(bins[pix-1]+np.diff(bins[:2])[0]/2., h[pix-1]/2., **kw)
    # plot in cumulative histogram, interpolate, such that point is on curve.
    yi = np.interp(p[0], cbins, hcumc)
    axc.plot(p[0],yi, **kw)
ax.legend()
plt.tight_layout()  
plt.show()

【讨论】:

  • 非常感谢您抽出宝贵时间给出简洁的答案。我将看看我是否可以使用我的数据来完成这项工作,并看看我可以从那里去哪里。