python scipy.stats.powerlaw negative exponent

后端 未结 5 1535
终归单人心
终归单人心 2020-12-30 16:38

I want to supply a negative exponent for the scipy.stats.powerlaw routine, e.g. a=-1.5, in order to draw random samples:

\"\"\"
powerlaw.pdf(x, a) = a * x**(         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 17:06

    If you want to generate power-law distribution, you can use a random deviation. You just have to generate a random number between [0,1] and apply the inverse method (Wolfram). In this case, the probability density function is:

    p(k) = k^(-gamma)

    and y is the variable uniform between 0 and 1.

    y ~ U(0,1)

    import numpy as np
    
    def power_law(k_min, k_max, y, gamma):
        return ((k_max**(-gamma+1) - k_min**(-gamma+1))*y  + k_min**(-gamma+1.0))**(1.0/(-gamma + 1.0))
    

    Now to generate a distribution, you just have to create an array

    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)
    

    This will work to generate a power-law distribution with gamma=3.0, if you want to fix the average of distribution, you have to study Complex Networks cause the k_min depends of k_max and the average connectivity.

提交回复
热议问题