Fit a power law to empirical data in Python

Moses Xu picture Moses Xu · Aug 11, 2013 · Viewed 7.1k times · Source

I'm experimenting with fitting a power law to empirical data using the powerlaw module. I have created the following data that follows a power law distribution of exponent 2:

x = range(1,1000)
y = []

for i in x:
    y.append(i**(-2))

I'm expecting the fitted power law to have an exponent of 2. However the resulting exponent deviates from the theoretical value a lot:

    fitted_pl = powerlaw.Fit(y)

    fitted_pl.alpha
    Out[115]: 1.4017584065981563

Could you please advise why this happens, or point out what I've done wrong here?

Thank you for your kind answer!

Answer

Moses Xu picture Moses Xu · Aug 12, 2013

As @DSM pointed out, the powerlaw module deals with fitting an exponent to values drawn/generated from a power law distribution, rather than fitting a regression. To help people who might have similar confusions, below is how one should verify the exponent fitting:

## use a proper power law random number generator (or code your own) 
from networkx.utils import powerlaw_sequence
pl_sequence = powerlaw_sequence(1000,exponent=2.5)

fitted_pl = powerlaw.Fit(pl_sequence)

fitted_pl.alpha
Out[73]: 2.4709012785346314  ##close enough