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#
An alpha continuous random variable.
An anglit continuous random variable.
An arcsine continuous random variable.
Argus distribution
A beta continuous random variable.
A beta prime continuous random variable.
A Bradford continuous random variable.
A Burr (Type III) continuous random variable.
A Burr (Type XII) continuous random variable.
A Cauchy continuous random variable.
A chi continuous random variable.
A chi-squared continuous random variable.
A cosine continuous random variable.
Crystalball distribution
A double gamma continuous random variable.
A double Pareto lognormal continuous random variable.
A double Weibull continuous random variable.
An Erlang continuous random variable.
An exponential continuous random variable.
An exponentially modified Normal continuous random variable.
An exponentiated Weibull continuous random variable.
An exponential power continuous random variable.
An F continuous random variable.
A fatigue-life (Birnbaum-Saunders) continuous random variable.
A Fisk continuous random variable.
A folded Cauchy continuous random variable.
A folded normal continuous random variable.
A generalized logistic continuous random variable.
A generalized normal continuous random variable.
A generalized Pareto continuous random variable.
A generalized exponential continuous random variable.
A generalized extreme value continuous random variable.
A Gauss hypergeometric continuous random variable.
A gamma continuous random variable.
A generalized gamma continuous random variable.
A generalized half-logistic continuous random variable.
A generalized hyperbolic continuous random variable.
A Generalized Inverse Gaussian continuous random variable.
A Gibrat continuous random variable.
A Gompertz (or truncated Gumbel) continuous random variable.
A right-skewed Gumbel continuous random variable.
A left-skewed Gumbel continuous random variable.
A Half-Cauchy continuous random variable.
A half-logistic continuous random variable.
A half-normal continuous random variable.
The upper half of a generalized normal continuous random variable.
A hyperbolic secant continuous random variable.
An inverted gamma continuous random variable.
An inverse Gaussian continuous random variable.
An inverted Weibull continuous random variable.
An Irwin-Hall (Uniform Sum) continuous random variable.
Jones and Faddy skew-t distribution.
A Johnson SB continuous random variable.
A Johnson SU continuous random variable.
Kappa 4 parameter distribution.
Kappa 3 parameter distribution.
Kolmogorov-Smirnov one-sided test statistic distribution.
Kolmogorov-Smirnov two-sided test statistic distribution.
Limiting distribution of scaled Kolmogorov-Smirnov two-sided test statistic.
A Landau continuous random variable.
A Laplace continuous random variable.
An asymmetric Laplace continuous random variable.
A Levy continuous random variable.
A left-skewed Levy continuous random variable.
A Levy-stable continuous random variable.
A logistic (or Sech-squared) continuous random variable.
A log gamma continuous random variable.
A log-Laplace continuous random variable.
A lognormal continuous random variable.
A loguniform or reciprocal continuous random variable.
A Lomax (Pareto of the second kind) continuous random variable.
A Maxwell continuous random variable.
A Mielke Beta-Kappa / Dagum continuous random variable.
A Moyal continuous random variable.
A Nakagami continuous random variable.
A non-central chi-squared continuous random variable.
A non-central F distribution continuous random variable.
A non-central Student's t continuous random variable.
A normal continuous random variable.
A Normal Inverse Gaussian continuous random variable.
A Pareto continuous random variable.
A pearson type III continuous random variable.
A power-function continuous random variable.
A power log-normal continuous random variable.
A power normal continuous random variable.
An R-distributed (symmetric beta) continuous random variable.
A Rayleigh continuous random variable.
A relativistic Breit-Wigner random variable.
A Rice continuous random variable.
A reciprocal inverse Gaussian continuous random variable.
A semicircular continuous random variable.
A skewed Cauchy random variable.
A skew-normal random variable.
A studentized range continuous random variable.
A Student's t continuous random variable.
A trapezoidal continuous random variable.
A triangular continuous random variable.
A truncated exponential continuous random variable.
A truncated normal continuous random variable.
An upper truncated Pareto continuous random variable.
A doubly truncated Weibull minimum continuous random variable.
A Tukey-Lamdba continuous random variable.
A uniform continuous random variable.
A Von Mises continuous random variable.
A Von Mises continuous random variable.
A Wald continuous random variable.
Weibull minimum continuous random variable.
Weibull maximum continuous random variable.
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#
A multivariate normal random variable.
A matrix normal random variable.
A Dirichlet random variable.
A Dirichlet multinomial random variable.
A Wishart random variable.
An inverse Wishart random variable.
A multinomial random variable.
A Special Orthogonal matrix (SO(N)) random variable.
An Orthogonal matrix (O(N)) random variable.
A matrix-valued U(N) random variable.
A random correlation matrix.
A multivariate t-distributed random variable.
A multivariate hypergeometric random variable.
Normal-inverse-gamma distribution.
Contingency tables from independent samples with fixed marginal sums.
A vector-valued uniform direction.
A von Mises-Fisher variable.
scipy.stats.multivariate_normal
methods accept instances
of the following class to represent the covariance.
Representation of a covariance matrix
Discrete distributions#
A Bernoulli discrete random variable.
A beta-binomial discrete random variable.
A beta-negative-binomial discrete random variable.
A binomial discrete random variable.
A Boltzmann (Truncated Discrete Exponential) random variable.
A Laplacian discrete random variable.
A geometric discrete random variable.
A hypergeometric discrete random variable.
A Logarithmic (Log-Series, Series) discrete random variable.
A negative binomial discrete random variable.
A Fisher's noncentral hypergeometric discrete random variable.
A Wallenius' noncentral hypergeometric discrete random variable.
A negative hypergeometric discrete random variable.
A Planck discrete exponential random variable.
A Poisson discrete random variable.
A Poisson Binomial discrete random variable.
A uniform discrete random variable.
A Skellam discrete random variable.
A Yule-Simon discrete random variable.
A Zipf (Zeta) discrete random variable.
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, keepdims, nan_policy])
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.
quantile
(x, p, *[, method, axis, ...])
Compute the p-th quantile of the data along the specified axis.
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#
make_distribution
(dist)
Generate a UnivariateDistribution class from a compatible object
Normal
([mu, sigma])
Normal distribution with prescribed mean and standard deviation.
Uniform
(*[, a, b])
Uniform distribution.
Binomial
(*, n, p, **kwargs)
Binomial distribution with prescribed success probability and number of trials
Mixture
(components, *[, weights])
Representation of a mixture distribution.
order_statistic
(X, /, *, r, n)
Probability distribution of an order statistic
truncate
(X[, lb, ub])
Truncate the support of a random variable.
abs
(X, /)
Absolute value of a random variable
exp
(X, /)
Natural exponential of a random variable
log
(X, /)
Natural logarithm of a non-negative random variable
Quasi-Monte Carlo#
Contingency Tables#
Masked statistics functions#
- Statistical functions for masked arrays (
scipy.stats.mstats
)- Summary statistics
- Frequency statistics
- Correlation functions
- Statistical tests
- Transformations
- Other
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, *[, axis, keepdims, ...])
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#
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, ...])
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.