Statistical functions (scipy.stats)#

This module contains a large number of probability distributions, summary and frequency statistics, correlation functions and statistical tests, masked statistics, kernel density estimation, quasi-Monte Carlo functionality, and more.

Statistics is a very large area, and there are topics that are out of scope for SciPy and are covered by other packages. Some of the most important ones are:

  • statsmodels: regression, linear models, time series analysis, extensions to topics also covered by scipy.stats.

  • Pandas: tabular data, time series functionality, interfaces to other statistical languages.

  • PyMC: Bayesian statistical modeling, probabilistic machine learning.

  • scikit-learn: classification, regression, model selection.

  • Seaborn: statistical data visualization.

  • rpy2: Python to R bridge.

Probability distributions#

Each univariate distribution is an instance of a subclass of rv_continuous (rv_discrete for discrete distributions):

rv_continuous([momtype, a, b, xtol, ...])

A generic continuous random variable class meant for subclassing.

rv_discrete([a, b, name, badvalue, ...])

A generic discrete random variable class meant for subclassing.

rv_histogram(histogram, *args[, density])

Generates a distribution given by a histogram.

Continuous distributions#

alpha

An alpha continuous random variable.

anglit

An anglit continuous random variable.

arcsine

An arcsine continuous random variable.

argus

Argus distribution

beta

A beta continuous random variable.

betaprime

A beta prime continuous random variable.

bradford

A Bradford continuous random variable.

burr

A Burr (Type III) continuous random variable.

burr12

A Burr (Type XII) continuous random variable.

cauchy

A Cauchy continuous random variable.

chi

A chi continuous random variable.

chi2

A chi-squared continuous random variable.

cosine

A cosine continuous random variable.

crystalball

Crystalball distribution

dgamma

A double gamma continuous random variable.

dpareto_lognorm

A double Pareto lognormal continuous random variable.

dweibull

A double Weibull continuous random variable.

erlang

An Erlang continuous random variable.

expon

An exponential continuous random variable.

exponnorm

An exponentially modified Normal continuous random variable.

exponweib

An exponentiated Weibull continuous random variable.

exponpow

An exponential power continuous random variable.

f

An F continuous random variable.

fatiguelife

A fatigue-life (Birnbaum-Saunders) continuous random variable.

fisk

A Fisk continuous random variable.

foldcauchy

A folded Cauchy continuous random variable.

foldnorm

A folded normal continuous random variable.

genlogistic

A generalized logistic continuous random variable.

gennorm

A generalized normal continuous random variable.

genpareto

A generalized Pareto continuous random variable.

genexpon

A generalized exponential continuous random variable.

genextreme

A generalized extreme value continuous random variable.

gausshyper

A Gauss hypergeometric continuous random variable.

gamma

A gamma continuous random variable.

gengamma

A generalized gamma continuous random variable.

genhalflogistic

A generalized half-logistic continuous random variable.

genhyperbolic

A generalized hyperbolic continuous random variable.

geninvgauss

A Generalized Inverse Gaussian continuous random variable.

gibrat

A Gibrat continuous random variable.

gompertz

A Gompertz (or truncated Gumbel) continuous random variable.

gumbel_r

A right-skewed Gumbel continuous random variable.

gumbel_l

A left-skewed Gumbel continuous random variable.

halfcauchy

A Half-Cauchy continuous random variable.

halflogistic

A half-logistic continuous random variable.

halfnorm

A half-normal continuous random variable.

halfgennorm

The upper half of a generalized normal continuous random variable.

hypsecant

A hyperbolic secant continuous random variable.

invgamma

An inverted gamma continuous random variable.

invgauss

An inverse Gaussian continuous random variable.

invweibull

An inverted Weibull continuous random variable.

irwinhall

An Irwin-Hall (Uniform Sum) continuous random variable.

jf_skew_t

Jones and Faddy skew-t distribution.

johnsonsb

A Johnson SB continuous random variable.

johnsonsu

A Johnson SU continuous random variable.

kappa4

Kappa 4 parameter distribution.

kappa3

Kappa 3 parameter distribution.

ksone

Kolmogorov-Smirnov one-sided test statistic distribution.

kstwo

Kolmogorov-Smirnov two-sided test statistic distribution.

kstwobign

Limiting distribution of scaled Kolmogorov-Smirnov two-sided test statistic.

landau

A Landau continuous random variable.

laplace

A Laplace continuous random variable.

laplace_asymmetric

An asymmetric Laplace continuous random variable.

levy

A Levy continuous random variable.

levy_l

A left-skewed Levy continuous random variable.

levy_stable

A Levy-stable continuous random variable.

logistic

A logistic (or Sech-squared) continuous random variable.

loggamma

A log gamma continuous random variable.

loglaplace

A log-Laplace continuous random variable.

lognorm

A lognormal continuous random variable.

loguniform

A loguniform or reciprocal continuous random variable.

lomax

A Lomax (Pareto of the second kind) continuous random variable.

maxwell

A Maxwell continuous random variable.

mielke

A Mielke Beta-Kappa / Dagum continuous random variable.

moyal

A Moyal continuous random variable.

nakagami

A Nakagami continuous random variable.

ncx2

A non-central chi-squared continuous random variable.

ncf

A non-central F distribution continuous random variable.

nct

A non-central Student's t continuous random variable.

norm

A normal continuous random variable.

norminvgauss

A Normal Inverse Gaussian continuous random variable.

pareto

A Pareto continuous random variable.

pearson3

A pearson type III continuous random variable.

powerlaw

A power-function continuous random variable.

powerlognorm

A power log-normal continuous random variable.

powernorm

A power normal continuous random variable.

rdist

An R-distributed (symmetric beta) continuous random variable.

rayleigh

A Rayleigh continuous random variable.

rel_breitwigner

A relativistic Breit-Wigner random variable.

rice

A Rice continuous random variable.

recipinvgauss

A reciprocal inverse Gaussian continuous random variable.

semicircular

A semicircular continuous random variable.

skewcauchy

A skewed Cauchy random variable.

skewnorm

A skew-normal random variable.

studentized_range

A studentized range continuous random variable.

t

A Student's t continuous random variable.

trapezoid

A trapezoidal continuous random variable.

triang

A triangular continuous random variable.

truncexpon

A truncated exponential continuous random variable.

truncnorm

A truncated normal continuous random variable.

truncpareto

An upper truncated Pareto continuous random variable.

truncweibull_min

A doubly truncated Weibull minimum continuous random variable.

tukeylambda

A Tukey-Lamdba continuous random variable.

uniform

A uniform continuous random variable.

vonmises

A Von Mises continuous random variable.

vonmises_line

A Von Mises continuous random variable.

wald

A Wald continuous random variable.

weibull_min

Weibull minimum continuous random variable.

weibull_max

Weibull maximum continuous random variable.

wrapcauchy

A wrapped Cauchy continuous random variable.

The fit method of the univariate continuous distributions uses maximum likelihood estimation to fit the distribution to a data set. The fit method can accept regular data or censored data. Censored data is represented with instances of the CensoredData class.

CensoredData([uncensored, left, right, interval])

Instances of this class represent censored data.

Multivariate distributions#

multivariate_normal

A multivariate normal random variable.

matrix_normal

A matrix normal random variable.

dirichlet

A Dirichlet random variable.

dirichlet_multinomial

A Dirichlet multinomial random variable.

wishart

A Wishart random variable.

invwishart

An inverse Wishart random variable.

multinomial

A multinomial random variable.

special_ortho_group

A Special Orthogonal matrix (SO(N)) random variable.

ortho_group

An Orthogonal matrix (O(N)) random variable.

unitary_group

A matrix-valued U(N) random variable.

random_correlation

A random correlation matrix.

multivariate_t

A multivariate t-distributed random variable.

multivariate_hypergeom

A multivariate hypergeometric random variable.

normal_inverse_gamma

Normal-inverse-gamma distribution.

random_table

Contingency tables from independent samples with fixed marginal sums.

uniform_direction

A vector-valued uniform direction.

vonmises_fisher

A von Mises-Fisher variable.

scipy.stats.multivariate_normal methods accept instances of the following class to represent the covariance.

Covariance()

Representation of a covariance matrix

Discrete distributions#

bernoulli

A Bernoulli discrete random variable.

betabinom

A beta-binomial discrete random variable.

betanbinom

A beta-negative-binomial discrete random variable.

binom

A binomial discrete random variable.

boltzmann

A Boltzmann (Truncated Discrete Exponential) random variable.

dlaplace

A Laplacian discrete random variable.

geom

A geometric discrete random variable.

hypergeom

A hypergeometric discrete random variable.

logser

A Logarithmic (Log-Series, Series) discrete random variable.

nbinom

A negative binomial discrete random variable.

nchypergeom_fisher

A Fisher's noncentral hypergeometric discrete random variable.

nchypergeom_wallenius

A Wallenius' noncentral hypergeometric discrete random variable.

nhypergeom

A negative hypergeometric discrete random variable.

planck

A Planck discrete exponential random variable.

poisson

A Poisson discrete random variable.

poisson_binom

A Poisson Binomial discrete random variable.

randint

A uniform discrete random variable.

skellam

A Skellam discrete random variable.

yulesimon

A Yule-Simon discrete random variable.

zipf

A Zipf (Zeta) discrete random variable.

zipfian

A Zipfian discrete random variable.

An overview of statistical functions is given below. Many of these functions have a similar version in scipy.stats.mstats which work for masked arrays.

Summary statistics#

describe(a[, axis, ddof, bias, nan_policy])

Compute several descriptive statistics of the passed array.

gmean(a[, axis, dtype, weights, nan_policy, ...])

Compute the weighted geometric mean along the specified axis.

hmean(a[, axis, dtype, weights, nan_policy, ...])

Calculate the weighted harmonic mean along the specified axis.

pmean(a, p, *[, axis, dtype, weights, ...])

Calculate the weighted power mean along the specified axis.

kurtosis(a[, axis, fisher, bias, ...])

Compute the kurtosis (Fisher or Pearson) of a dataset.

mode(a[, axis, nan_policy, keepdims])

Return an array of the modal (most common) value in the passed array.

moment(a[, order, axis, nan_policy, center, ...])

Calculate the nth moment about the mean for a sample.

lmoment(sample[, order, axis, sorted, ...])

Compute L-moments of a sample from a continuous distribution

expectile(a[, alpha, weights])

Compute the expectile at the specified level.

skew(a[, axis, bias, nan_policy, keepdims])

Compute the sample skewness of a data set.

kstat(data[, n, axis, nan_policy, keepdims])

Return the n th k-statistic ( 1<=n<=4 so far).

kstatvar(data[, n, axis, nan_policy, keepdims])

Return an unbiased estimator of the variance of the k-statistic.

tmean(a[, limits, inclusive, axis, ...])

Compute the trimmed mean.

tvar(a[, limits, inclusive, axis, ddof, ...])

Compute the trimmed variance.

tmin(a[, lowerlimit, axis, inclusive, ...])

Compute the trimmed minimum.

tmax(a[, upperlimit, axis, inclusive, ...])

Compute the trimmed maximum.

tstd(a[, limits, inclusive, axis, ddof, ...])

Compute the trimmed sample standard deviation.

tsem(a[, limits, inclusive, axis, ddof, ...])

Compute the trimmed standard error of the mean.

variation(a[, axis, nan_policy, ddof, keepdims])

Compute the coefficient of variation.

find_repeats(arr)

Find repeats and repeat counts.

rankdata(a[, method, axis, nan_policy])

Assign ranks to data, dealing with ties appropriately.

tiecorrect(rankvals)

Tie correction factor for Mann-Whitney U and Kruskal-Wallis H tests.

trim_mean(a, proportiontocut[, axis])

Return mean of array after trimming a specified fraction of extreme values

gstd(a[, axis, ddof])

Calculate the geometric standard deviation of an array.

iqr(x[, axis, rng, scale, nan_policy, ...])

Compute the interquartile range of the data along the specified axis.

sem(a[, axis, ddof, nan_policy, keepdims])

Compute standard error of the mean.

bayes_mvs(data[, alpha])

Bayesian confidence intervals for the mean, var, and std.

mvsdist(data)

'Frozen' distributions for mean, variance, and standard deviation of data.

entropy(pk[, qk, base, axis, nan_policy, ...])

Calculate the Shannon entropy/relative entropy of given distribution(s).

differential_entropy(values, *[, ...])

Given a sample of a distribution, estimate the differential entropy.

median_abs_deviation(x[, axis, center, ...])

Compute the median absolute deviation of the data along the given axis.

Frequency statistics#

cumfreq(a[, numbins, defaultreallimits, weights])

Return a cumulative frequency histogram, using the histogram function.

percentileofscore(a, score[, kind, nan_policy])

Compute the percentile rank of a score relative to a list of scores.

scoreatpercentile(a, per[, limit, ...])

Calculate the score at a given percentile of the input sequence.

relfreq(a[, numbins, defaultreallimits, weights])

Return a relative frequency histogram, using the histogram function.

binned_statistic(x, values[, statistic, ...])

Compute a binned statistic for one or more sets of data.

binned_statistic_2d(x, y, values[, ...])

Compute a bidimensional binned statistic for one or more sets of data.

binned_statistic_dd(sample, values[, ...])

Compute a multidimensional binned statistic for a set of data.

Random Variables#

Normal([mu, sigma])

Normal distribution with prescribed mean and standard deviation.

Mixture(components, *[, weights])

Representation of a mixture distribution.

make_distribution(dist)

Generate a ContinuousDistribution from an instance of rv_continuous

Quasi-Monte Carlo#

Contingency Tables#

Masked statistics functions#

Other statistical functionality#

Transformations#

boxcox(x[, lmbda, alpha, optimizer])

Return a dataset transformed by a Box-Cox power transformation.

boxcox_normmax(x[, brack, method, ...])

Compute optimal Box-Cox transform parameter for input data.

boxcox_llf(lmb, data)

The boxcox log-likelihood function.

yeojohnson(x[, lmbda])

Return a dataset transformed by a Yeo-Johnson power transformation.

yeojohnson_normmax(x[, brack])

Compute optimal Yeo-Johnson transform parameter.

yeojohnson_llf(lmb, data)

The yeojohnson log-likelihood function.

obrientransform(*samples)

Compute the O'Brien transform on input data (any number of arrays).

sigmaclip(a[, low, high])

Perform iterative sigma-clipping of array elements.

trimboth(a, proportiontocut[, axis])

Slice off a proportion of items from both ends of an array.

trim1(a, proportiontocut[, tail, axis])

Slice off a proportion from ONE end of the passed array distribution.

zmap(scores, compare[, axis, ddof, nan_policy])

Calculate the relative z-scores.

zscore(a[, axis, ddof, nan_policy])

Compute the z score.

gzscore(a, *[, axis, ddof, nan_policy])

Compute the geometric standard score.

Statistical distances#

wasserstein_distance(u_values, v_values[, ...])

Compute the Wasserstein-1 distance between two 1D discrete distributions.

wasserstein_distance_nd(u_values, v_values)

Compute the Wasserstein-1 distance between two N-D discrete distributions.

energy_distance(u_values, v_values[, ...])

Compute the energy distance between two 1D distributions.

Sampling#

Fitting / Survival Analysis#

fit(dist, data[, bounds, guess, method, ...])

Fit a discrete or continuous distribution to data

ecdf(sample)

Empirical cumulative distribution function of a sample.

logrank(x, y[, alternative])

Compare the survival distributions of two samples via the logrank test.

Directional statistical functions#

directional_stats(samples, *[, axis, normalize])

Computes sample statistics for directional data.

circmean(samples[, high, low, axis, ...])

Compute the circular mean of a sample of angle observations.

circvar(samples[, high, low, axis, ...])

Compute the circular variance of a sample of angle observations.

circstd(samples[, high, low, axis, ...])

Compute the circular standard deviation of a sample of angle observations.

Sensitivity Analysis#

sobol_indices(*, func, n[, dists, method, rng])

Global sensitivity indices of Sobol'.

Plot-tests#

ppcc_max(x[, brack, dist])

Calculate the shape parameter that maximizes the PPCC.

ppcc_plot(x, a, b[, dist, plot, N])

Calculate and optionally plot probability plot correlation coefficient.

probplot(x[, sparams, dist, fit, plot, rvalue])

Calculate quantiles for a probability plot, and optionally show the plot.

boxcox_normplot(x, la, lb[, plot, N])

Compute parameters for a Box-Cox normality plot, optionally show it.

yeojohnson_normplot(x, la, lb[, plot, N])

Compute parameters for a Yeo-Johnson normality plot, optionally show it.

Univariate and multivariate kernel density estimation#

gaussian_kde(dataset[, bw_method, weights])

Representation of a kernel-density estimate using Gaussian kernels.

Warnings / Errors used in scipy.stats#

DegenerateDataWarning([msg])

Warns when data is degenerate and results may not be reliable.

ConstantInputWarning([msg])

Warns when all values in data are exactly equal.

NearConstantInputWarning([msg])

Warns when all values in data are nearly equal.

FitError([msg])

Represents an error condition when fitting a distribution to data.

Result classes used in scipy.stats#

Warning

These classes are private, but they are included here because instances of them are returned by other statistical functions. User import and instantiation is not supported.