numpy.random.RandomState.power#

方法

random.RandomState.power(a, size=None)#

從具有正指數 a - 1 的冪分佈中抽取 [0, 1] 中的樣本。

也稱為冪函數分佈。

注意

新程式碼應使用 power 方法,此方法屬於 Generator 實例;請參閱快速入門

參數:
afloat 或 float 的類陣列 (array_like)

分佈的參數。必須為非負數。

size整數或整數元組,選用

輸出形狀。如果給定的形狀為,例如 (m, n, k),則會抽取 m * n * k 個樣本。如果 size 為 None (預設),則當 a 為純量時,會傳回單一值。否則,會抽取 np.array(a).size 個樣本。

傳回值:
outndarray 或純量

從參數化冪分佈中抽取的樣本。

引發:
ValueError

如果 a <= 0。

參見

random.Generator.power

新程式碼應使用此方法。

註解

機率密度函數為

\[P(x; a) = ax^{a-1}, 0 \le x \le 1, a>0.\]

冪函數分佈只是帕雷托分佈的反函數。它也可以被視為 Beta 分佈的特例。

它用於例如對保險索賠的過度申報進行建模。

參考文獻

[1]

Christian Kleiber, Samuel Kotz, “Statistical size distributions in economics and actuarial sciences”, Wiley, 2003.

[2]

Heckert, N. A. and Filliben, James J. “NIST Handbook 148: Dataplot Reference Manual, Volume 2: Let Subcommands and Library Functions”, National Institute of Standards and Technology Handbook Series, June 2003. https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/powpdf.pdf

範例

從分佈中抽取樣本

>>> a = 5. # shape
>>> samples = 1000
>>> s = np.random.power(a, samples)

顯示樣本的直方圖,以及機率密度函數

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, bins=30)
>>> x = np.linspace(0, 1, 100)
>>> y = a*x**(a-1.)
>>> normed_y = samples*np.diff(bins)[0]*y
>>> plt.plot(x, normed_y)
>>> plt.show()
../../../_images/numpy-random-RandomState-power-1_00_00.png

比較冪函數分佈與帕雷托分佈的反函數。

>>> from scipy import stats 
>>> rvs = np.random.power(5, 1000000)
>>> rvsp = np.random.pareto(5, 1000000)
>>> xx = np.linspace(0,1,100)
>>> powpdf = stats.powerlaw.pdf(xx,5)  
>>> plt.figure()
>>> plt.hist(rvs, bins=50, density=True)
>>> plt.plot(xx,powpdf,'r-')  
>>> plt.title('np.random.power(5)')
>>> plt.figure()
>>> plt.hist(1./(1.+rvsp), bins=50, density=True)
>>> plt.plot(xx,powpdf,'r-')  
>>> plt.title('inverse of 1 + np.random.pareto(5)')
>>> plt.figure()
>>> plt.hist(1./(1.+rvsp), bins=50, density=True)
>>> plt.plot(xx,powpdf,'r-')  
>>> plt.title('inverse of stats.pareto(5)')
../../../_images/numpy-random-RandomState-power-1_01_00.png
../../../_images/numpy-random-RandomState-power-1_01_01.png
../../../_images/numpy-random-RandomState-power-1_01_02.png