Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Power Law Distribution Fitting in python (and fortran and cython)
Branch: master

add new distribution sanity check and fix the test distribution

container.  The method will never recover the "correct" xmin because the
exponential distribution really is indistinguishable from a powerlaw.
latest commit b3318821cb
@adamginsburg adamginsburg authored


Power-law Distribution Fitting

This is a python implementation of a power-law distribution fitter. The code here was originally hosted on agpy but was moved and re-packaged to make cleaner.

API Documentation

See also, an alternate implementation of the same algorithm with additional bells & whistles.


I've attempted to make the file work nicely, but it includes some hacks, so if you run into trouble, please report it on github:

git clone
cd plfit
python install

If python install doesn't work, you can try the following:

To install the cython function, run: python build_ext --inplace

To install the fortran function:

cd plfit/plfit/
f2py -c fplfit.f -m fplfit --fcompiler=gfortran


Aaron Clauset et al. address the issue of fitting power-laws to distributions on this website and in their paper Power-law distributions in empirical data. I have created a python implementation of their code because I didn't have matlab or R and wanted to do some power-law fitting.

Power-laws are very commonly used in astronomy and are typically used to describe the initial mass function (IMF), the core mass function (CMF), and often luminosity distributions. Most distributions in astronomy tend to be apparent power-laws because the source counts are too few or too narrow to distinguish powerlaws from log-normal and other distributions. But, to this end, I've included the testing mechanism to test for consistency with a power law as described in the above paper.

The python internal documentation is complete. A brief description of relevant functions is included here for convenience:

plfit is implemented as a class. This means that you import plfit, and declare an instance of the plfit class:

import plfit
X = rand(1000)
myplfit = plfit.plfit(X)

The results of the fit are printed to the screen (if desired) and are stored as part of the object.

alpha_ and kstest_ are functions used internally to determine the ks-statistic and alpha values as a function of xmin.

There are 3 predefined plotting functions:
  • alphavsks plots alpha on the y-axis vs. the ks statistic value on the x-axis with the 'best-fit' alpha value plotted with error bars. These plots are a useful way to determine if other values of xmin are similarly good fits.
  • plotcdf plots the cumulative distribution function along with the best-fit power law
  • plotpdf plots a histogram of the PDF with the best fit power law. It defaults to log binning (i.e. a linear power-law fit) but can do dN/dS and linear binning as well.
Other useful functions:
  • test_pl uses the fitted power-law as the starting point for a monte-carlo test of whether the powerlaw is an acceptable fit. It returns a "p-value" that should be >0.1 if a power-law fit is to be considered (though a high p-value does not ensure that the distribution function is a power law!).
  • plexp_inv creates a cutoff power-law distribution with an exponential tail-off. It is useful for tests.
  • pl_inv creates a pure cutoff power-law distribution
  • test_fitter uses the previous two functions to test the fitter's ability to return the correct xmin and alpha values for large numbers of iterations

The powerlaw fitter is very effective at returning the correct value of alpha but not as good at returning the correct value of xmin.

There are 3 implementations of the code internals. fplfit.f is a fortran function, cplfit.pyx is a cython function, and is the wrapper and includes a python-only implementation that requires numpy. FORTRAN is fastest, follow closely by cython. Python is ~3x slower.

As of November 21, 2011, there is a pure python (i.e., no numpy) implementation at <> - you can just put this file in your local working directory and import it, since it contains no requirements beyond pure python. It's slower and hobbled, but it works, and perhaps will run fast with pypy.

For usage examples, see

A very simple example:

import plfit
from numpy.random import rand,seed

# generate a power law using the "inverse" power-law generator code

# use the numpy version to fit (usefortran=False is only needed if you installed the fortran version)
# output should look something like this:
# PYTHON plfit executed in 0.201362 seconds
# xmin: 0.621393 n(>xmin): 263 alpha: 2.39465 +/- 0.0859979   Log-Likelihood: -238.959   ks: 0.0278864 p(ks): 0.986695

# generate some plots
from pylab import *


If you use this code, please cite Clauset et al 2009 and consider posting a comment below.

Direction citations to the source are welcome! The python translation has been cited in the following works (and perhaps others?):

Bitdeli badge
Something went wrong with that request. Please try again.