這個問題很混亂,有很多不相關的信息,但在基本點上保持模糊。我會盡我所能解釋它。
我認爲你在做什麼之後是:給定一個來自未知分佈的有限樣本,獲得一個固定值的新樣本的概率是多少?
我不確定是否有一個普遍的答案,但無論如何這將是一個問題,要求統計或數學人。我的猜測是,你需要對分配本身做一些假設。
但是,對於實際情況,找出新值將位於採樣分佈的哪個分箱中可能就足夠了。
因此,假設我們有一個分配x
,我們分爲bins
。我們可以使用numpy.histogram
來計算直方圖h
。然後由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
所以概率爲5.8%的樣本在0.77左右的bin中取值。
一個不同的選擇是插入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()
給誰就給誰downvoted我的帖子,你會爲什麼這樣我就可以不管我做錯了完善詳細點嗎? – DarthLazar