python scipy.stats.powerlaw negative exponent

后端 未结 5 1560
终归单人心
终归单人心 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 16:53

    The Python package powerlaw can do this. Consider for a>1 a power law distribution with probability density function

    f(x) = c * x^(-a) 
    

    for x > x_min and f(x) = 0 otherwise. Here c is a normalization factor and is determined as

    c = (a-1) * x_min^(a-1).
    

    In the example below it is a = 1.5 and x_min = 1.0 and comparing the probability density function estimated from the random sample with the PDF from the expression above gives the expected result.

    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')
    

    returns

提交回复
热议问题