scipy.stats.

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 components according to the probabilities given by weights, 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 mean must 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 (raw first moment about the origin)

median(*[, method])

Median (50th percentile)

mode(*[, method])

Mode (most likely value)

variance(*[, method])

Variance (central second moment)

standard_deviation(*[, method])

Standard deviation (square root of the second central moment)

skewness(*[, method])

Skewness (standardized third moment)

kurtosis(*[, method])

Kurtosis (standardized fourth moment)

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, and ilogccdf are 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)

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()
../../_images/scipy-stats-Mixture-1_00_00.png

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