numpy.random.RandomState.standard_gamma#
方法
- random.RandomState.standard_gamma(shape, size=None)#
從標準 Gamma 分佈中抽取樣本。
樣本是從具有指定參數(shape,有時稱為“k”)和 scale=1 的 Gamma 分佈中抽取的。
注意
新程式碼應使用
standard_gamma
方法,此方法屬於Generator
實例;請參閱快速入門。- 參數:
- shapefloat 或 float 的 array_like 物件
參數,必須是非負數。
- sizeint 或 int 的 tuple,可選
輸出形狀。如果給定的形狀是,例如,
(m, n, k)
,則會抽取m * n * k
個樣本。如果 size 為None
(預設值),如果shape
是純量,則會傳回單個值。否則,會抽取np.array(shape).size
個樣本。
- 返回:
- outndarray 或 純量
從參數化的標準 gamma 分佈中抽取的樣本。
另請參閱
scipy.stats.gamma
機率密度函數、分佈或累積密度函數等等。
random.Generator.standard_gamma
新程式碼應使用的方法。
Notes
Gamma 分佈的機率密度為
\[p(x) = x^{k-1}\frac{e^{-x/\theta}}{\theta^k\Gamma(k)},\]其中 \(k\) 是 shape,\(\theta\) 是 scale,而 \(\Gamma\) 是 Gamma 函數。
Gamma 分佈通常用於為電子元件的故障時間建模,並且自然地出現在 Poisson 分佈事件之間的等待時間相關的過程中。
參考文獻
[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., 1. # mean and width >>> s = np.random.standard_gamma(shape, 1000000)
顯示樣本的直方圖,以及機率密度函數
>>> 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()