numpy.random.Generator.standard_gamma#

方法

random.Generator.standard_gamma(shape, size=None, dtype=np.float64, out=None)#

从标准 Gamma 分布中抽取样本。

从具有指定参数(形状(有时称为“k”)和尺度=1)的 Gamma 分布中抽取样本。

参数:
shapefloat 或 array_like 的 floats

参数,必须是非负的。

sizeint 或 int 的元组,可选

输出形状。如果给定的形状为,例如,(m, n, k),则抽取 m * n * k 个样本。如果 size 为 None(默认),当 shape 为标量时,返回单个值。否则,抽取 np.array(shape).size 个样本。

dtypedtype, optional

结果所需的 dtype,仅支持 float64float32。字节序必须是本地的。默认值为 np.float64。

outndarray,可选

用于放置结果的备选输出数组。如果 size 不为 None,则它必须与提供的 size 具有相同的形状,并且必须与输出值的类型匹配。

返回:
outndarray 或标量

从参数化的标准 Gamma 分布中抽取的样本。

另请参阅

scipy.stats.gamma

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

备注

Gamma 分布的概率密度为

\[p(x) = x^{k-1}\frac{e^{-x/\theta}}{\theta^k\Gamma(k)},\]

其中 \(k\) 是形状,\(\theta\) 是尺度,\(\Gamma\) 是 Gamma 函数。

Gamma 分布通常用于模拟电子元件的故障时间,并在泊松分布事件之间等待时间相关的过程中自然出现。

参考

[1]

Weisstein, Eric W. “Gamma Distribution.” From MathWorld–A Wolfram Web Resource. https://mathworld.net.cn/GammaDistribution.html

[2]

Wikipedia, “Gamma distribution”, https://en.wikipedia.org/wiki/Gamma_distribution

示例

从分布中绘制样本

>>> shape, scale = 2., 1. # mean and width
>>> rng = np.random.default_rng()
>>> s = rng.standard_gamma(shape, 1000000)

显示样本的直方图以及概率密度函数

>>> import matplotlib.pyplot as plt
>>> import scipy.special as sps  
>>> count, bins, _ = plt.hist(s, 50, density=True)
>>> y = bins**(shape-1) * ((np.exp(-bins/scale))/  
...                       (sps.gamma(shape) * scale**shape))
>>> plt.plot(bins, y, linewidth=2, color='r')  
>>> plt.show()
../../../_images/numpy-random-Generator-standard_gamma-1.png