REW

What Is The Power Law In Python?

Published Aug 29, 2025 5 min read
On this page

A power law in Python refers to a functional relationship where a relative change in one quantity results in a proportional relative change in another, raised to a constant exponent.

This relationship, defined as p(x)∝x−αp open paren x close paren ∝ x raised to the negative alpha power

𝑝(𝑥)∝𝑥−𝛼

, is common in statistical distributions for real-world phenomena exhibiting "heavy-tailed" or "fat-tailed" behavior, meaning very large, rare events occur more frequently than in, for instance, a normal (Gaussian) distribution. Analyzing power laws in Python is primarily accomplished using the powerlaw and SciPy libraries.

The concept of power laws

Mathematical definition

The general form of a power-law relationship between two variables, xx

𝑥

and p(x)p open paren x close paren

𝑝(𝑥)

, is:p(x)∝x−αp open paren x close paren ∝ x raised to the negative alpha power

𝑝(𝑥)∝𝑥−𝛼

This means p(x)=Cx−αp open paren x close paren equals cap C x raised to the negative alpha power

𝑝(𝑥)=𝐶𝑥−𝛼

for some normalization constant Ccap C

𝐶

.

  • p(x)p open paren x close paren

    𝑝(𝑥)

    is the probability of observing an event of size xx

    𝑥

    .

  • αalpha

    𝛼

    (alpha) is the scaling exponent, which governs the relationship's steepness. A higher αalpha

    𝛼

    means the distribution falls off more steeply, making large events rarer.

A key property of power laws is scale invariance. When plotted on a log-log scale, a power-law distribution appears as a straight line. This is because taking the logarithm of both sides of the equation, p(x)=Cx−αp open paren x close paren equals cap C x raised to the negative alpha power

𝑝(𝑥)=𝐶𝑥−𝛼

, yields:log(p(x))=log(C)−αlog(x)log open paren p open paren x close paren close paren equals log open paren cap C close paren minus alpha log x

log(𝑝(𝑥))=log(𝐶)−𝛼log(𝑥)

This is the equation of a straight line, y=b+mxy equals b plus m x

𝑦=𝑏+𝑚𝑥

, where y=log(p(x))y equals log open paren p open paren x close paren close paren

𝑦=log(𝑝(𝑥))

, x=log(x)x equals log x

𝑥=log(𝑥)

, the slope m=−αm equals negative alpha

𝑚=−𝛼

, and the y-intercept b=log(C)b equals log open paren cap C close paren

𝑏=log(𝐶)

.

Characteristics of power-law distributions

  • Heavy tails: Power-law distributions are characterized by their "heavy tails," where the probability of extremely large values is much higher than in other distributions like the normal distribution.
  • No characteristic scale: Unlike normal distributions, which are centered around a mean, a power-law distribution has no single, characteristic scale. The distribution's shape looks similar regardless of the scale it is viewed on.
  • The 80/20 rule: Power laws often reflect the "80/20 rule" (or Pareto principle), where a small number of events or items account for a disproportionately large share of the results.

Fitting and analysis with the powerlaw library

The specialized powerlaw Python package provides robust statistical methods for fitting power-law distributions to empirical data. It is the most common and statistically sound approach in Python.

Installation

You can install the package using pip:

pip install powerlaw

Use code with caution.

It requires NumPy, SciPy, and Matplotlib as dependencies.

Basic usage

The primary workflow with the powerlaw library is to create a Fit object, which automatically performs maximum likelihood fitting and finds the optimal cutoff (xmin) for the power-law behavior.

import powerlaw
import numpy as np
# Generate some sample data with a power-law tail
# This is a synthetic dataset for demonstration
data = np.random.power(a=2.5, size=1000)
data = data * 1000 + 1 # Rescale and shift for better visualization
# Fit the data to a power-law distribution
fit = powerlaw.Fit(data)
# Access the fitting results
print(f"Power law exponent (alpha): {fit.power_law.alpha}")
print(f"Lower cutoff for fitting (xmin): {fit.power_law.xmin}")

Use code with caution.

Comparing distributions

A critical feature of the powerlaw package is the ability to statistically compare a power-law fit with other heavy-tailed distributions, such as the log-normal or exponential distributions.

# Compare the power-law fit to a log-normal distribution
R, p = fit.distribution_compare('power_law', 'lognormal')
print(f"Log-likelihood ratio (R): {R}")
print(f"p-value: {p}")
# A positive R means the first distribution (power law) is more likely.
# A small p-value (e.g., < 0.05) suggests the difference in fit is significant.
if p < 0.05 and R > 0:
    print("\nPower-law is a significantly better fit than log-normal.")
elif p < 0.05 and R < 0:
    print("\nLog-normal is a significantly better fit than power-law.")
else:
    print("\nIt is difficult to distinguish between the two distributions.")

Use code with caution.

Visualization

Plotting the data on a log-log scale is the standard method for visually assessing a power-law distribution. The powerlaw package integrates with Matplotlib to make this easy.

Visualization can be done by plotting the complementary cumulative distribution function (CCDF) on a log-log scale to compare the original data with fitted distributions like the power-law and log-normal.

While less statistically rigorous for distributions than the powerlaw package, SciPy can be used for fitting simple power-law functions to log-transformed data. This involves defining a power-law function and using scipy.optimize.curve_fit to find the parameters, then plotting the results on a log-log scale.

Power-law distributions are observed in various fields, including social sciences (wealth, city populations, word frequency), natural sciences (earthquake magnitudes, animal metabolic rates), and computer science (Internet topology, website traffic).

Analyzing power-law behavior requires careful consideration. Visual inspection alone is insufficient, and using statistically sound methods like those in the powerlaw package is recommended. Choosing appropriate alternative distributions for comparison is also crucial.

Enjoyed this article? Share it with a friend.