Python 包powerlaw可以做到这一点。考虑a>1
具有概率密度函数的幂律分布
f(x) = c * x^(-a)
对于x > x_min
和f(x) = 0
其他。这c
是一个归一化因子,确定为
c = (a-1) * x_min^(a-1).
在下面的示例中,a = 1.5
将x_min = 1.0
随机样本估计的概率密度函数与上述表达式中的 PDF 进行比较给出了预期的结果。
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as pl
import numpy as np
import powerlaw
a, xmin = 1.5, 1.0
N = 10000
# generates random variates of power law distribution
vrs = powerlaw.Power_Law(xmin=xmin, parameters=[a]).generate_random(N)
# plotting the PDF estimated from variates
bin_min, bin_max = np.min(vrs), np.max(vrs)
bins = 10**(np.linspace(np.log10(bin_min), np.log10(bin_max), 100))
counts, edges = np.histogram(vrs, bins, density=True)
centers = (edges[1:] + edges[:-1])/2.
# plotting the expected PDF
xs = np.linspace(bin_min, bin_max, 100000)
pl.plot(xs, [(a-1)*xmin**(a-1)*x**(-a) for x in xs], color='red')
pl.plot(centers, counts, '.')
pl.xscale('log')
pl.yscale('log')
pl.savefig('powerlaw_variates.png')
返回