numpy.random.power#

random.power(a, size=None)#

从具有正指数 a - 1 的幂分布在 [0, 1] 中抽取样本。

也称为幂函数分布。

注意

新代码应该使用 Generator 实例的 power 方法;请参阅快速入门

参数:
afloat 或 类数组(float)

分布的参数。必须是非负数。

sizeint 或 int 元组,可选

输出形状。如果给定的形状是例如 (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.\]

幂函数分布只是 Pareto 分布的逆。它也可以看作是 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-power-1_00_00.png

将幂函数分布与 Pareto 分布的逆进行比较。

>>> 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-power-1_01_00.png
../../../_images/numpy-random-power-1_01_01.png
../../../_images/numpy-random-power-1_01_02.png