Mixture#
- class scipy.stats.Mixture(components, *, weights=None)[source]#
Representation of a mixture distribution.
A mixture distribution is the distribution of a random variable defined in the following way: first, a random variable is selected from
componentsaccording to the probabilities given byweights, then the selected random variable is realized.- Parameters:
- componentssequence of UnivariateDistribution
The underlying instances of UnivariateDistribution. All must have scalar shape parameters (if any); e.g., the
meanmust return a scalar.- weightssequence of floats, optional
The corresponding probabilities of selecting each random variable. Must be non-negative and sum to one. The default behavior is to weight all components equally.
- Attributes:
- componentssequence of UnivariateDistribution
The underlying instances of UnivariateDistribution.
- weightsndarray
The corresponding probabilities of selecting each random variable.
Methods
support()Support of the random variable
sample([shape, rng, method])Random sample from the distribution.
moment([order, kind, method])Raw, central, or standard moment of positive integer order.
mean(*[, method])Mean, a measure of location
median(*[, method])Median, a measure of location
mode(*[, method])Mode, the most likely value
variance(*[, method])Variance, a measure of dispersion
standard_deviation(*[, method])Standard deviation, a measure of dispersion
skewness(*[, method])Skewness, a measure of asymmetry
kurtosis(*[, method])Kurtosis, a measure of tailedness
pdf(x, /, *[, method])Probability density function
logpdf(x, /, *[, method])Log of the probability density function
cdf(x[, y, method])Cumulative distribution function
icdf(p, /, *[, method])Inverse of the cumulative distribution function.
ccdf(x[, y, method])Complementary cumulative distribution function
iccdf(p, /, *[, method])Inverse complementary cumulative distribution function.
logcdf(x[, y, method])Log of the cumulative distribution function
ilogcdf(p, /, *[, method])Inverse of the logarithm of the cumulative distribution function.
logccdf(x[, y, method])Log of the complementary cumulative distribution function
ilogccdf(p, /, *[, method])Inverse of the log of the complementary cumulative distribution function.
entropy(*[, method])Differential entropy
Notes
Methods
median,mode,entropy,logentropy,icdf,iccdf,ilogcdf, andilogccdfare currently implemented only when all components are continuous distributions.The following abbreviations are used throughout the documentation.
PDF: probability density function
CDF: cumulative distribution function
CCDF: complementary CDF
entropy: differential entropy
log-F: logarithm of F (e.g. log-CDF)
inverse F: inverse function of F (e.g. inverse CDF)
Array API Standard Support
Mixturehas support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.Library
CPU
GPU
NumPy
✅
n/a
CuPy
n/a
✅
PyTorch
⛔
⛔
JAX
⛔
⛔
Dask
⛔
n/a
See Support for the array API standard for more information.
References
[1]Mixture distribution, Wikipedia, https://en.wikipedia.org/wiki/Mixture_distribution
[2]Zero-inflated model, Wikipedia, https://en.wikipedia.org/wiki/Zero-inflated_model
Examples
A mixture of normal distributions:
>>> import numpy as np >>> from scipy import stats >>> import matplotlib.pyplot as plt >>> X1 = stats.Normal(mu=-2, sigma=1) >>> X2 = stats.Normal(mu=2, sigma=1) >>> mixture = stats.Mixture([X1, X2], weights=[0.4, 0.6]) >>> print(f'mean: {mixture.mean():.2f}, ' ... f'median: {mixture.median():.2f}, ' ... f'mode: {mixture.mode():.2f}') mean: 0.40, median: 1.04, mode: 2.00 >>> x = np.linspace(-10, 10, 300) >>> plt.plot(x, mixture.pdf(x)) >>> plt.title('PDF of normal distribution mixture') >>> plt.show()
A zero-inflated Poisson model.
>>> Poisson = stats.make_distribution(stats.poisson) >>> lmb = 10 # expected Poisson count >>> pi = 0.4 # probability of extra zeros >>> X = stats.Binomial(n=0, p=0) # degenerate distribution; all zero >>> Y = Poisson(mu=lmb) >>> ZIP = stats.Mixture((X, Y), weights=(pi, 1-pi)) >>> true_mean = (1 - pi) * lmb # see e.g. [2] >>> print(np.allclose(ZIP.mean(), true_mean)) True >>> true_variance = lmb*(1 - pi)*(1 + pi*lmb) # see e.g. [2] >>> print(np.allclose(ZIP.variance(), true_variance)) True