powerlaw
raw JSON → 2.0.0 verified Fri May 01 auth: no python
A toolbox for testing if a probability distribution fits a power law, including fitting power laws, comparing to other distributions via likelihood ratio tests, and plotting. Current version is 2.0.0 (released 2023-12-31), with major refactor from v1.x. Release cadence is irregular.
pip install powerlaw Common errors
error AttributeError: module 'powerlaw' has no attribute 'Fit' ↓
cause Old code that imported a submodule incorrectly or used an outdated import pattern.
fix
Use
import powerlaw and then fit = powerlaw.Fit(data). error TypeError: Fit() got an unexpected keyword argument 'xmin' ↓
cause The API changed in v2.0; `xmin` is no longer a direct argument to `Fit`; use `Fit(data, xmin=value)` may still work but check docs.
fix
Use
fit = powerlaw.Fit(data, xmin=...) and refer to v2.0 docs; if using v2.0, the argument is still supported but check for other changes. error ValueError: Data must be positive ↓
cause Power law fitting requires all data points > 0.
fix
Filter your data:
data = data[data > 0] before fitting. Warnings
breaking Version 2.0.0 is a major refactor with API changes. The previous API (e.g., `powerlaw.Fit` returning a `Result` object with `.power_law.alpha`) has been simplified. Ensure you update code and refer to new documentation. ↓
fix Review the new API: `Fit` now directly returns the fitted parameters (alpha, xmin) as attributes. Use `fit.distribution_compare(...)` for comparisons.
deprecated Old plotting functions from v1.x (e.g., `fit.plot_pdf()`) may be removed. Use `fit.plot_ccdf()` or `fit.plot_pdf()` as per new API. ↓
fix See the documentation for new plotting methods; they have similar names but may have different signatures.
gotcha Data with values <= 0 will cause errors. Power law distributions are defined for positive values only. ↓
fix Ensure your data is strictly positive (e.g., remove zeros and negative values before fitting).
Imports
- powerlaw wrong
from powerlaw import powerlawcorrectimport powerlaw
Quickstart
import powerlaw
import numpy as np
# Generate sample data
x = np.random.pareto(2.5, 1000)
# Fit power law
fit = powerlaw.Fit(x)
# Print alpha and xmin
print(fit.alpha, fit.xmin)
# Compare to exponential
R, p = fit.distribution_compare('power_law', 'exponential')