scipy.stats.powerlaw — SciPy v1.16.0 Manual (original) (raw)

scipy.stats.powerlaw = <scipy.stats._continuous_distns.powerlaw_gen object>[source]#

A power-function continuous random variable.

As an instance of the rv_continuous class, powerlaw object inherits from it a collection of generic methods (see below for the full list), and completes them with details specific for this particular distribution.

Methods

Notes

The probability density function for powerlaw is:

\[f(x, a) = a x^{a-1}\]

for \(0 \le x \le 1\), \(a > 0\).

powerlaw takes a as a shape parameter for \(a\).

The probability density above is defined in the “standardized” form. To shift and/or scale the distribution use the loc and scale parameters. Specifically, powerlaw.pdf(x, a, loc, scale) is identically equivalent to powerlaw.pdf(y, a) / scale withy = (x - loc) / scale. Note that shifting the location of a distribution does not make it a “noncentral” distribution; noncentral generalizations of some distributions are available in separate classes.

For example, the support of powerlaw can be adjusted from the default interval [0, 1] to the interval [c, c+d] by setting loc=c andscale=d. For a power-law distribution with infinite support, seepareto.

powerlaw is a special case of beta with b=1.

Examples

import numpy as np from scipy.stats import powerlaw import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1)

Get the support:

a = 0.659 lb, ub = powerlaw.support(a)

Calculate the first four moments:

mean, var, skew, kurt = powerlaw.stats(a, moments='mvsk')

Display the probability density function (pdf):

x = np.linspace(powerlaw.ppf(0.01, a), ... powerlaw.ppf(0.99, a), 100) ax.plot(x, powerlaw.pdf(x, a), ... 'r-', lw=5, alpha=0.6, label='powerlaw pdf')

Alternatively, the distribution object can be called (as a function) to fix the shape, location and scale parameters. This returns a “frozen” RV object holding the given parameters fixed.

Freeze the distribution and display the frozen pdf:

rv = powerlaw(a) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

Check accuracy of cdf and ppf:

vals = powerlaw.ppf([0.001, 0.5, 0.999], a) np.allclose([0.001, 0.5, 0.999], powerlaw.cdf(vals, a)) True

Generate random numbers:

r = powerlaw.rvs(a, size=1000)

And compare the histogram:

ax.hist(r, density=True, bins='auto', histtype='stepfilled', alpha=0.2) ax.set_xlim([x[0], x[-1]]) ax.legend(loc='best', frameon=False) plt.show()

../../_images/scipy-stats-powerlaw-1.png