Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python scipy.stats.powerlaw负指数_Python_Statistics_Scipy_Idl Programming Language - Fatal编程技术网

python scipy.stats.powerlaw负指数

python scipy.stats.powerlaw负指数,python,statistics,scipy,idl-programming-language,Python,Statistics,Scipy,Idl Programming Language,我想为scipy.stats.powerlaw例程提供一个负指数,例如a=-1.5,以便抽取随机样本: """ powerlaw.pdf(x, a) = a * x**(a-1) """ from scipy.stats import powerlaw R = powerlaw.rvs(a, size=100) 为什么需要a>0,如何提供负a以生成随机样本,以及如何提供归一化系数/变换,即 PDF(x,C,a) = C * x**a 文件在这里 谢谢 编辑:我应该补充一点,我正在尝试复制

我想为scipy.stats.powerlaw例程提供一个负指数,例如a=-1.5,以便抽取随机样本:

"""
powerlaw.pdf(x, a) = a * x**(a-1)
"""

from scipy.stats import powerlaw
R = powerlaw.rvs(a, size=100)
为什么需要a>0,如何提供负a以生成随机样本,以及如何提供归一化系数/变换,即

PDF(x,C,a) = C * x**a
文件在这里

谢谢

编辑:我应该补充一点,我正在尝试复制IDL的RANDOMP函数:


在其域上集成的PDF必须等于一个。换句话说,概率密度函数曲线下的面积必须等于1

In [36]: import scipy.integrate as integrate
In [40]: y, err = integrate.quad(lambda x: 0.5*x**(-0.5), 0, 1)

In [41]: y
Out[41]: 0.9999999999999998  # The integral is close to 1
幂律密度函数的域为0

a*x**(a-1)
中的第一个常数
a
是使域[0,1]上的
a*x**(a-1)
积分等于1的规范化常数。所以你不能独立于
a
来选择这个常数


现在,如果您将域更改为远离0的可测量距离,那么是的,您可以为负数
a
定义格式为
C*x**a
的PDF。但是你必须说明你想要哪个域,我认为在
scipy.stats
中没有(目前)一个PDF可供使用。

如果r是均匀随机偏差U(0,1),那么下面表达式中的x是幂律分布随机偏差:

x = xmin * (1-r) ** (-1/(alpha-1))

其中xmin是幂律分布保持的最小(正)值,alpha是分布的指数。

如果要生成幂律分布,可以使用随机偏差。您只需生成一个介于[0,1]之间的随机数,然后应用逆方法()。在这种情况下,概率密度函数为:

p(k)=k^(-gamma)

y是0和1之间的均匀变量

y~U(0,1)

现在要生成分布,只需创建一个数组

nodes = 1000
scale_free_distribution = np.zeros(nodes, float)
k_min = 1.0
k_max = 100*k_min
gamma = 3.0

for n in range(nodes):
    scale_free_distribution[n] = power_law(k_min, k_max,np.random.uniform(0,1), gamma)
这将产生gamma=3.0的幂律分布,如果要修正分布的平均值,必须研究k_min依赖于k_max和平均连通性的原因。

Python包可以做到这一点。考虑<代码> 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')
返回


我的答案与上面维吉尔的答案几乎相同,关键区别在于α实际上是幂律分布的负指数

因此,如果r是均匀随机偏差U(0,1),那么下面表达式中的x是幂律分布随机偏差:

x = xmin * (1-r) ** (-1/(alpha-1))

其中xmin是幂律分布保持的最小(正)值,alpha是分布的负指数,即p(x)=[常数]*x**-alpha

到最后一部分:使用正位置
loc
我们可以移动分布。根据这一解释,可以根据位置
loc
放松对
a
的限制。应该值得进行一些测试,并且应该可以在scipy.stats中进行扩展。@user333700:虽然您可以使用
loc
来移动分发,但是限制
a>0
将保留,因为在生成基础分发之后,最后才执行移动。您是对的,我没有正确地考虑这个问题。它需要一个额外的形状参数来改变和扩大分布的支持。我认为这在scipy中还不可能实现,但我真的想知道为什么这么难。这个pdf有一个分析累积,所以它可以很容易地解决,正如其他人所指出的那样,使用教科书中概述的方法,或者为什么只注意
x=xmin*(r)**(-alpha)
?我不知道。这就是我从亚伦·克劳塞特那里得到的公式。这里的‘a’是什么?它是如何计算的?没有从scipy文档@unutbu获取
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')
x = xmin * (1-r) ** (-1/(alpha-1))