maxframe.tensor.random.power#
- maxframe.tensor.random.power(a, size=None, chunk_size=None, gpu=None, dtype=None)[源代码]#
从正指数为 a - 1 的幂律分布中抽取 [0, 1] 区间内的样本。
也称为幂函数分布。
- 参数:
a (float or array_like of floats) -- 分布的参数。应大于零。
size (int or tuple of ints, optional) -- 输出形状。如果给定形状例如
(m, n, k),则抽取m * n * k个样本。如果 size 为None``(默认),当 ``a为标量时返回单个值。否则抽取mt.array(a).size个样本。chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小
gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False
dtype (data-type, optional) -- 返回张量的数据类型。
- 返回:
out -- 从参数化的幂律分布中抽取的样本。
- 返回类型:
Tensor or scalar
- 抛出:
ValueError -- 如果 a < 1。
备注
概率密度函数为
\[P(x; a) = ax^{a-1}, 0 \le x \le 1, a>0.\]幂函数分布是帕累托分布的逆分布。它也可以看作是 Beta 分布的特例。
例如,它可用于建模保险索赔的过度报告。
引用
示例
从分布中抽取样本:
>>> import maxframe.tensor as mt
>>> a = 5. # shape >>> samples = 1000 >>> s = mt.random.power(a, samples)
显示样本的直方图以及概率密度函数:
>>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(s.execute(), bins=30) >>> x = mt.linspace(0, 1, 100) >>> y = a*x**(a-1.) >>> normed_y = samples*mt.diff(bins)[0]*y >>> plt.plot(x.execute(), normed_y.execute()) >>> plt.show()
将幂函数分布与帕累托分布的逆进行比较。
>>> from scipy import stats >>> rvs = mt.random.power(5, 1000000) >>> rvsp = mt.random.pareto(5, 1000000) >>> xx = mt.linspace(0,1,100) >>> powpdf = stats.powerlaw.pdf(xx.execute(),5)
>>> plt.figure() >>> plt.hist(rvs.execute(), bins=50, normed=True) >>> plt.plot(xx.execute(),powpdf,'r-') >>> plt.title('np.random.power(5)')
>>> plt.figure() >>> plt.hist((1./(1.+rvsp)).execute(), bins=50, normed=True) >>> plt.plot(xx.execute(),powpdf,'r-') >>> plt.title('inverse of 1 + np.random.pareto(5)')
>>> plt.figure() >>> plt.hist((1./(1.+rvsp)).execute(), bins=50, normed=True) >>> plt.plot(xx.execute(),powpdf,'r-') >>> plt.title('inverse of stats.pareto(5)')