numpy.random.Generator.hypergeometric

方法

random.Generator.hypergeometric(ngood, nbad, nsample, size=None)

从超几何分布中提取样本。

样本来自具有指定参数的超几何分布, ngood (好的选择方法), nbad (做出错误选择的方法),以及 nsample (抽样项目数,小于或等于 ngood + nbad

参数
ngoodint或类似于int的数组

好的选择方法有很多种。必须为非负且小于10**9。

nbadint或类似于int的数组

做出错误选择的方法有很多。必须为非负且小于10**9。

nsampleint或类似于int的数组

抽样项目数。必须为非负且小于 ngood + nbad .

sizeint或int的元组,可选

输出形状。如果给定的形状是,例如, (m, n, k) 然后 m * n * k 取样。如果尺寸是 None (默认),如果 ngoodnbadnsample 都是鳞片。否则, np.broadcast(ngood, nbad, nsample).size 取样。

返回
outndarray或scalar

从参数化超几何分布中提取样本。每个样本是在随机选择的大小子集内的好项目数 nsample 从一套 ngood 好的物品和 nbad 坏东西。

参见

multivariate_hypergeometric

从多元超几何分布中抽取样本。

scipy.stats.hypergeom

概率密度函数、分布或累积密度函数等。

笔记

超几何分布的概率密度是

System Message: WARNING/2 (p(x)=\frac \binom g x \binom b n-x \binom g+b n,)

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Missing } inserted. <inserted text> } l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Missing } inserted. <inserted text> } l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Ambiguous; you need another { and }. <argument> \@@over l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Extra }, or forgotten $. \frac #1#2->{\begingroup #1\endgroup \@@over #2} l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Missing { inserted. <inserted text> { l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Missing } inserted. <inserted text> } l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Missing } inserted. <inserted text> } l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Ambiguous; you need another { and }. <argument> \@@over l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Extra }, or forgotten $. \frac #1#2->{\begingroup #1\endgroup \@@over #2} l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... x \binom b n-x \binom g+b n,\end{split} ! Missing { inserted. <inserted text> { l.14 ... x \binom b n-x \binom g+b n,\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 524 bytes). Transcript written on math.log.

在哪里? 0 \le x \le nn-b \le x \le g

对于P(x),概率 x 抽检结果良好,g= ngood ,乙= nbad ,和n= nsample .

想想一个有黑白大理石的瓮, ngood 他们中的大多数是黑人和黑人 nbad 他们是白人。如果你画 nsample 无替换的球,则超几何分布描述了黑球在抽取样本中的分布。

注意,这种分布与二项式分布非常相似,只是在这种情况下,样本是不替换地抽取的,而在二项式情况下,样本是用替换的方式抽取的(或者样本空间是无限的)。当样本空间变大时,这种分布接近二项式分布。

论点 ngoodnbad 每个必须小于 10**9 . 对于非常大的参数,用于计算样本的算法 [4] 由于浮点计算中的精度损失而崩溃。对于如此大的值,如果 nsample 也不是很大,分布可以用二项分布来近似, binomial(n=nsample, p=ngood/(ngood + nbad)) .

工具书类

1

Lenner,Marvin,“基础应用统计学”,Bogden和Quigley,1972年。

2

超几何分布〉,摘自《数学世界——一个Wolfram网络资源》。http://mathworld.wolfram.com/hypergeometricdistribution.html

3

维基百科,“超几何分布”,https://en.wikipedia.org/wiki/hypergeometric_distribution

4

Ernst Stadlober,“产生离散随机变量的均匀比方法”,《计算与应用数学杂志》,31,第181-189页(1990年)。

实例

从分发中抽取样本:

>>> 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个黑色大理石的 urn 。如果你随机抽取15颗弹珠,那么其中12颗或更多是一种颜色的可能性有多大?

>>> s = rng.hypergeometric(15, 15, 15, 100000)
>>> sum(s>=12)/100000. + sum(s<=3)/100000.
#   answer = 0.003 ... pretty unlikely!