2015-06-29 392 views
1

我想从幂指数分布(a = -2)的幂律分布中绘制一个介于2到15之间的随机变量。我发现以下内容:Python:从幂律分布生成随机数

r = scipy.stats.powerlaw.rvs(a, loc = 2, scale = 13, size = 1000) 

但它并不采用负数。

任何人都知道一条出路?在numpy.randomscipy.stats定义

+0

请尝试(1/2)。实际上应该是同样的事情。 –

+4

有人在这里回答了这个问题:http://stackoverflow.com/questions/17882907/python-scipy-stats-powerlaw-negative-exponent - 如果你从中理解了一些东西。 – Faflok

+0

@RobFoley,我需要检查它的不同负指数.. -1.5,-2,-3等。因此,我希望如果我能得到更一般的建议:( @Faflok我读了..但它并没有帮助我摆脱'a> 0'条件。 – Panchi

回答

9

幂律分布并不在答案解释this question数学意义上的负a定义:他们是因为在零奇不normalizable。所以,不幸的是,数学说'不'。

您可以定义一个pdf的比例为x^{g-1}g < 0不包含零的区间,如果这就是你以后的分布。

pdf(x) = const * x**(g-1)a <= x <= b,从一个统一的变量(np.random.random)转变为:

In [3]: def rndm(a, b, g, size=1): 
    """Power-law gen for pdf(x)\propto x^{g-1} for a<=x<=b""" 
    ...:  r = np.random.random(size=size) 
    ...:  ag, bg = a**g, b**g 
    ...:  return (ag + (bg - ag)*r)**(1./g) 

然后你就可以做,例如,

In [4]: xx = rndm(1, 2, g=-2, size=10000) 

等。

为了完整起见,这里是PDF格式:

In [5]: def pdf(x, a, b, g): 
    ag, bg = a**g, b**g 
    ....:  return g * x**(g-1)/(bg - ag) 

这一切都假定a < bg != 0。对于a=0,b=1g > 0,这些公式应与numpy.powerscipy.stats.powerlaw一致。