本文簡要介紹 python 語言中 numpy.random.Generator.hypergeometric
的用法。
用法:
random.Generator.hypergeometric(ngood, nbad, nsample, size=None)
從超幾何分布中抽取樣本。
樣本是從具有指定參數的超幾何分布中抽取的,好(做出好的選擇的方法),不好(做出錯誤選擇的方法),以及樣本(抽樣的項目數,小於等於總和
ngood + nbad
)。- ngood: int 或 數組 整數
做出良好選擇的方法的數量。必須為非負數且小於 10**9。
- nbad: int 或 數組 整數
做出錯誤選擇的方法的數量。必須為非負數且小於 10**9。
- nsample: int 或 數組 整數
抽樣的項目數。必須是非負數且小於
ngood + nbad
。- size: int 或整數元組,可選
輸出形狀。如果給定的形狀是,例如,
(m, n, k)
, 然後m * n * k
樣本被抽取。如果尺寸是None
(默認),如果返回單個值好,不好, 和樣本都是標量。否則,np.broadcast(ngood, nbad, nsample).size
樣本被抽取。
- out: ndarray 或標量
從參數化的超幾何分布中抽取樣本。每個樣本是從一組 ngood 好項目和 nbad 壞項目中隨機選擇的大小為 nsample 的子集中的好項目的數量。
參數:
返回:
注意:
超幾何分布的概率密度為
其中 和
對於 P(x) 的概率
x
抽取樣品中的良好結果,g =好, b =不好, 和 n =樣本.考慮一個裝有黑白大理石的骨灰盒,其中 ngood 是黑色的,nbad 是白色的。如果您在沒有放回的情況下繪製 nsample 個球,則超幾何分布說明了繪製的樣本中黑球的分布。
請注意,此分布與二項分布非常相似,隻是在這種情況下,樣本是在沒有放回的情況下抽取的,而在二項分布的情況下,樣本是有放回的(或者樣本空間是無限的)。隨著樣本空間變大,這種分布接近二項式。
參數好和不好每個必須小於10**9.對於非常大的參數,用於計算樣本的算法[4]由於浮點計算中的精度損失而崩潰。對於如此大的值,如果樣本也不大,分布可以近似為二項分布,二項式(n=nsample, p=ngood/(ngood + nbad)).
參考:
倫特納,馬文,“Elementary Applied Statistics”,博格登和奎格利,1972 年。
Weisstein, Eric W. “超幾何分布”。來自MathWorld-A Wolfram Web 資源。http://mathworld.wolfram.com/HypergeometricDistribution.html
維基百科,“Hypergeometric distribution”,https://en.wikipedia.org/wiki/Hypergeometric_distribution
Stadlober, Ernst,“用於生成離散隨機變量的製服比例方法”,計算與應用數學雜誌,31,第 181-189 頁(1990 年)。
1:
2:
3:
4:
例子:
從分布中抽取樣本:
>>> rng = np.random.default_rng() >>> ngood, nbad, nsamp = 100, 2, 10 # number of good, number of bad, and number of samples >>> s = rng.hypergeometric(ngood, nbad, nsamp, 1000) >>> from matplotlib.pyplot import hist >>> hist(s) # note that it is very unlikely to grab both bad items
假設你有一個裝有 15 顆白色彈珠和 15 顆黑色彈珠的骨灰盒。如果你隨機拉出 15 顆彈珠,其中 12 顆或更多顆是一種顏色的可能性有多大?
>>> s = rng.hypergeometric(15, 15, 15, 100000) >>> sum(s>=12)/100000. + sum(s<=3)/100000. # answer = 0.003 ... pretty unlikely!
相關用法
- Python numpy Generator.multivariate_normal用法及代碼示例
- Python numpy Generator.standard_normal用法及代碼示例
- Python numpy Generator.bytes用法及代碼示例
- Python numpy Generator.shuffle用法及代碼示例
- Python numpy Generator.choice用法及代碼示例
- Python numpy Generator.random用法及代碼示例
- Python numpy Generator.logseries用法及代碼示例
- Python numpy Generator.uniform用法及代碼示例
- Python numpy Generator.standard_t用法及代碼示例
- Python numpy Generator.standard_cauchy用法及代碼示例
- Python numpy Generator.normal用法及代碼示例
- Python numpy Generator.poisson用法及代碼示例
- Python numpy Generator.power用法及代碼示例
- Python numpy Generator.geometric用法及代碼示例
- Python numpy Generator.laplace用法及代碼示例
- Python numpy Generator.vonmises用法及代碼示例
- Python numpy Generator.noncentral_f用法及代碼示例
- Python numpy Generator.gamma用法及代碼示例
- Python numpy Generator.multivariate_hypergeometric用法及代碼示例
- Python numpy Generator.weibull用法及代碼示例
- Python numpy Generator.gumbel用法及代碼示例
- Python numpy Generator.pareto用法及代碼示例
- Python numpy Generator.noncentral_chisquare用法及代碼示例
- Python numpy Generator.negative_binomial用法及代碼示例
- Python numpy Generator.binomial用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.random.Generator.hypergeometric。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。