質問は無関係な情報がたくさんあるが、本質的な点ではあいまいである。私はそれを私ができる最高のものに補間しようとします。
未知の分布の有限サンプルを与えられた場合、固定値で新しいサンプルを得る確率はどのくらいですか?
一般的な回答があるかどうかはわかりませんが、統計や数学の人々に質問するような質問にもなります。私の推測では、配布自体についていくつかの仮定をする必要があるということです。
しかし実際のケースでは、サンプリングされた分布のどのビンに新しい値があるかを知ることで十分かもしれません。
したがって、x
という分布があり、これをbins
に分けます。ヒストグラムh
は、numpy.histogram
を使用して計算できます。各ビン内の値を見つける確率は、h/h.sum()
によって与えられる。
値がv=0.77
であり、その分布の確率を知りたい場合は、この値を挿入する必要があるビン配列のインデックスind
を検索することで、属するビンを見つけることができます配列はソートされたままになります。これは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付近でサンプリングします。
異なるオプションは、確率を見つけるために、ビンの中心間でヒストグラムを補間することです。
以下のコードでは、質問のピクチャと同様の分布をプロットし、両方の方法を使用しています。最初は頻度ヒストグラム、2番目は累積分布です。
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()
![enter image description here](https://i.stack.imgur.com/NVK7q.png)
私のポストをdownvoted誰に、あなたはので、私は私が間違ってやっている何を改善できる理由について詳しく説明しますか? – DarthLazar