numpy.random.RandomState.gamma#
method
- random.RandomState.gamma(shape, scale=1.0, size=None)#
从 Gamma 分布 中抽取样本.
样本是从具有指定参数 shape (有时表示为 “k”) 和 scale (有时表示为 “theta”) 的 Gamma 分布中抽取的,其中两个参数都 > 0.
- 参数:
- shapefloat 或 floats 的类数组对象
gamma 分布的形状.必须是非负的.
- scalefloat 或 float 的类数组,可选
gamma 分布的尺度.必须是非负的.默认为 1.
- sizeint 或 int 的元组,可选.
输出形状.如果给定形状是,例如,“(m, n, k)”,则会抽取“m * n * k”个样本.如果 size 为“None”(默认值),则当
shape和scale都是标量时,将返回单个值.否则,将抽取“np.broadcast(shape, scale).size”个样本.
- 返回:
- outndarray 或标量
从参数化的 gamma 分布中抽取的样本.
参见
scipy.stats.gamma概率密度函数,分布或累积密度函数等.
random.Generator.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.wolfram.com/GammaDistribution.html
[2]Wikipedia, “Gamma distribution”, https://en.wikipedia.org/wiki/Gamma_distribution
示例
从分布中抽取样本:
>>> shape, scale = 2., 2. # mean=4, std=2*sqrt(2) >>> s = np.random.gamma(shape, scale, 1000)
显示样本的直方图,以及概率密度函数:
>>> import matplotlib.pyplot as plt >>> import scipy.special as sps >>> count, bins, ignored = 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()