Probability Distributions in SciPy

The named probability distributions as scipy.stats provides them, and how to use them on data. Frozen distribution objects and their methods (pmf, pdf, cdf, sf, ppf, isf, rvs, interval, stats); the loc and scale convention behind an exponential parameterized by its mean and a uniform(2, 5) that runs from 2 to 7; sf against 1 - cdf in the far tail; ppf for discrete distributions and the sf(k - 1) rule; t against normal tails; a normal model for 2,630 men's heights that matches the data to a few thousandths; prescription counts that a Poisson model misses by a factor of four at zero and a negative binomial fits; and fit() with a free location.

scipy.stats gives each named distribution as an object with the same methods, and this page documents the conventions that decide their answers. sf(k) is $P(X > k)$, so "at least $k$" is sf(k - 1). 1 - norm.cdf(z) returns exactly 0 from $z \approx 8.29$, while norm.sf(8.5) is $9.48 \times 10^{-18}$. loc and scale shift and stretch a standard form: expon(scale=2) has mean 2, and uniform(2, 5) runs from 2 to 7. fit() also estimates the location unless floc fixes it. On NHANES 2017 to 2018 data, a normal fitted to 2,630 men's heights matches the observed shares to a few thousandths. A Poisson model for 5,524 adults' prescription counts (mean 2.30, variance 9.46) puts 10% at zero against the observed 41%; a negative binomial puts 35%.

A probability distribution describes the values a random variable can take and how likely each is. Statistics uses a small family of named distributions over and over: the binomial for counts of successes, the Poisson for counts of events, the normal for measurements that cluster symmetrically, Student's $t$ for standardized means. Each is a formula with a few parameters, and in Python each is an object in scipy.stats with the same set of methods. Using them well is mostly a matter of knowing what those methods compute, how scipy names the parameters, and how to check whether a distribution describes the data at all.

The distributions themselves, with their derivations, means, variances and the conditions under which they arise, are covered in the Applied Statistics pages under Theoretical Probability Distributions: The Binomial Distribution, The Poisson Distribution and The Normal Distribution, with Student's $t$ in The Chi-Squared and t Distributions. Here they are summarized in a sentence each and put to work on data from the 2017 to 2018 National Health and Nutrition Examination Survey (NHANES): hypertension among the 5,182 adults of Data Collection and Sampling, men's heights and the number of prescription medicines taken in the past month.

Random variables and their distributions

A random variable assigns a number to each outcome of a random process: the number of adults with hypertension among ten chosen at random, or the height of one man. A discrete random variable takes separate values, usually counts, and its distribution is given by a probability mass function (pmf), $P(X = k)$ for each value $k$. A continuous random variable takes any value in an interval, every single value has probability zero, and its distribution is given by a probability density function (pdf) $f(x)$ whose area over an interval is the probability of landing in it. Both have a cumulative distribution function (cdf), $F(x) = P(X \le x)$, which is the most useful of the three in practice because every probability of an interval can be written with it.

Distribution objects in scipy

Each distribution in scipy.stats can be called with its parameters to give a frozen distribution, an object with the parameters fixed:

from scipy import stats
X = stats.binom(10, 0.5475)        # number with hypertension among 10 adults
X.pmf(5), X.cdf(3), X.sf(6), X.ppf(0.5), X.mean(), X.var()

Here $p = 0.5475$ is the share of the 5,182 adults with hypertension, and $X$ counts how many of 10 adults chosen independently with that probability have it, a binomial distribution because each of a fixed number of independent trials has the same probability of success. The methods are the same for every distribution:

Method Discrete Continuous For $X \sim \text{Binomial}(10, 0.5475)$
pmf(k) / pdf(x) $P(X = k)$ density $f(x)$ (not a probability) pmf(5) = 0.2352
cdf(x) $P(X \le x)$ $P(X \le x)$ cdf(3) = 0.1050
sf(x) $P(X > x)$ $P(X > x)$ sf(6) = $P(X \ge 7)$ = 0.2607
ppf(q) smallest $k$ with $F(k) \ge q$ $F^{-1}(q)$ ppf(0.5) = 5
isf(q) inverse of sf inverse of sf
rvs(size, random_state) random draws random draws
interval(c) central range with probability at least $c$ central range with probability $c$ interval(0.95) = (2, 8)
stats("mvsk") mean, variance, skewness, excess kurtosis 5.4747, 2.4775, $-0.0603$, $-0.1964$

The mean $np = 5.4747$ and variance $np(1-p) = 2.4775$ are the binomial formulas derived in The Binomial Distribution. The same methods exist without freezing, as stats.binom.pmf(5, 10, 0.5475), with the parameters passed to every call; the frozen form is clearer when one distribution is used many times. A Bernoulli variable, a single trial, is stats.bernoulli(p), the same as stats.binom(1, p): both give $P(X = 1) = 0.5475$.

Discrete distributions: three traps

"At least" needs sf(k - 1). sf(k) is $P(X > k)$, strictly greater. For a count, $P(X \ge 7) = P(X > 6)$ is sf(6) = 0.2607; sf(7) = 0.0968 is $P(X \ge 8)$. The same off-by-one affects 1 - cdf(k), which is also $P(X > k)$. For continuous variables the distinction disappears, because single values have probability zero.

ppf returns a value with cumulative probability at least $q$. For the binomial above, cdf(4) = 0.2670 and cdf(5) = 0.5022, so ppf(q) is 5 for every $q$ between 0.2670 and 0.5022. The inverse of a step function is not unique, and scipy chooses the smallest value whose cdf reaches $q$. That choice is the quantile function $Q$ of Random Variables and Distribution Functions, which proves the property that makes it an inverse: $Q(q) \le x$ exactly when $q \le F(x)$.

interval covers at least, not exactly, the requested probability. interval(0.95) returns (2, 8), and $P(2 \le X \le 8) = 0.9729$: the discrete distribution cannot hit 0.95 exactly, so the interval is wider. Tests and intervals for proportions inherit this, which is why exact binomial tests are conservative (Inference for Proportions and Means).

Tails: sf rather than 1 - cdf

In floating-point arithmetic, 1 - cdf(x) loses all precision once cdf(x) rounds to 1. For the standard normal:

$z$ norm.sf(z) 1 - norm.cdf(z)
3 $1.3499 \times 10^{-3}$ $1.3499 \times 10^{-3}$
6 $9.8659 \times 10^{-10}$ $9.8659 \times 10^{-10}$
8.5 $9.4795 \times 10^{-18}$ 0
10 $7.6199 \times 10^{-24}$ 0

By $z = 8.5$ the subtraction returns exactly zero (from $z \approx 8.29$ on, as Problem 3 shows), while sf computes the tail directly. Very small tail probabilities matter in practice for p-values from large samples and in multiple-testing corrections that multiply them. For the most extreme cases, logsf returns the logarithm: norm.logsf(40) is $-804.608$, while norm.sf(40) underflows to 0. Quantiles far in the tail likewise come from isf: norm.isf(1e-12) = 7.0345.

Location and scale

Most continuous distributions in scipy take two standard arguments, loc and scale, which shift and stretch a standard form: if $Z$ has the standard form, $X = \text{loc} + \text{scale} \times Z$. The convention is uniform across scipy, which is its advantage, and it does not always match the parameters a textbook uses, which is its trap.

  • Normal: norm(loc=mu, scale=sigma); scale is the standard deviation, not the variance. norm(120, 16).var() is 256.
  • Exponential: expon(scale=beta) has mean $\beta$, so a rate $\lambda$ is passed as scale=1/lam. $P(T \le 1)$ is 0.3935 for expon(scale=2), a mean of 2, and 0.8647 for a rate of 2 (scale=0.5). The two readings of "exponential with parameter 2" differ by a factor of more than two.
  • Uniform: uniform(loc, scale) is uniform on $[\text{loc}, \text{loc} + \text{scale}]$. stats.uniform(2, 5) runs from 2 to 7, not from 2 to 5.
  • Discrete distributions take loc only, as a shift: poisson(2.3, loc=1) has mean 3.3.

Reading the parameterization in the documentation of each distribution before using it is the only reliable rule. Newer versions of scipy add a second interface, classes such as stats.Normal(mu=..., sigma=...) with explicitly named parameters and methods named ccdf and icdf for the survival and inverse functions; scipy 1.15.2 has Normal and Uniform, and scipy 1.18.1 also Binomial and Logistic (checked by running both versions on 23 November 2026).

Student's t against the normal

When a mean is standardized by an estimated standard deviation, the result follows Student's $t$ distribution with $n - 1$ degrees of freedom rather than the normal, because the estimate adds variability (The Chi-Squared and t Distributions derives it). Its tails are heavier, and the difference shrinks as the degrees of freedom grow:

$P(T > 2)$ 0.975 quantile Standard deviation
$t$, 3 df 0.0697 3.1824 1.7321
$t$, 10 df 0.0367 2.2281 1.1180
$t$, 30 df 0.0273 2.0423 1.0351
$t$, 100 df 0.0241 1.9840 1.0102
Normal 0.0228 1.9600 1

The 0.975 quantile is the multiplier of a 95% interval. With 10 degrees of freedom the $t$ multiplier, 2.2281, is about 14% larger than 1.96, and using the normal quantile instead, as stats.norm.interval does, makes the interval too narrow; Inferential Statistics in Python shows the difference on a sample.

Does a distribution fit the data?

Sometimes it does and sometimes it does not, and the data decide. A named distribution is a model, and data are not obliged to follow it. Two checks on NHANES show one model that fits, the normal for men's heights, and one that does not, the Poisson for prescription counts.

Men's heights and the normal distribution

The 2,630 men with a measured height have mean 173.481 cm. stats.norm.fit(h) estimates the two parameters by maximum likelihood, which for the normal gives the sample mean and the standard deviation with divisor $n$, 7.675 cm (7.676 with divisor $n - 1$). The fitted model then predicts probabilities that can be compared with the shares in the data:

Quantity Normal model Data
$P(H > 190)$ 0.0157 0.0152
$P(H < 160)$ 0.0395 0.0414
$P(165 < H < 180)$ 0.6676 0.6654
90th percentile 183.32 cm 183.40 cm

The agreement is within a few thousandths. The sample skewness is 0.0065 and the excess kurtosis $-0.0634$, both close to the normal's zero, and the correlation of the normal probability plot from stats.probplot is 0.99983. The Normal Distribution discusses these checks and formal tests of normality on the same population, and Visualizing Data with One Feature why heights of men and women together are not bimodal even though each sex's are normal.

Left: heights of 2,630 men with the normal distribution fitted by maximum likelihood; the curve follows the histogram closely. Right: the number of prescription medicines taken in the past month by 5,524 adults, against a Poisson distribution and a negative binomial distribution with the same mean. The Poisson puts 10% of adults at zero where the data have 41%; the negative binomial, with the data's variance as well, comes much closer.
Left: heights of 2,630 men with the normal distribution fitted by maximum likelihood; the curve follows the histogram closely. Right: the number of prescription medicines taken in the past month by 5,524 adults, against a Poisson distribution and a negative binomial distribution with the same mean. The Poisson puts 10% of adults at zero where the data have 41%; the negative binomial, with the data's variance as well, comes much closer.

Prescription counts and the Poisson distribution

The number of prescription medicines an adult took in the past month is a count, and the Poisson distribution is the standard first model for counts of events. It has one parameter, the mean $\lambda$, and its variance equals its mean (The Poisson Distribution). For the 5,524 adults with a recorded count, the mean is 2.2989 but the variance is 9.4578, four times larger, and 41.4% take no medicine at all. A Poisson distribution with the same mean, stats.poisson(2.2989), gives $P(X = 0) = 0.1004$, a quarter of the observed share; and it gives 10 or more medicines a probability of 0.000143, where the data have 3.6%.

The mismatch is overdispersion (The Poisson Distribution tests for it with Fisher's index of dispersion, and Overdispersion in Count Data treats its causes and remedies): the counts vary more than a Poisson model allows, because people differ in their underlying rates. Most take nothing; some take many, for several chronic conditions. A negative binomial distribution, which arises when Poisson rates themselves vary across people according to a gamma distribution (Problem 1 derives it), has a second parameter that lets the variance exceed the mean. In scipy's parameterization, nbinom(n, p) has mean $n(1-p)/p$ and variance $n(1-p)/p^2$; matching these to the sample mean and variance gives

$$p = \frac{\bar{x}}{s^2} = \frac{2.2989}{9.4578} = 0.2431, \qquad n = \frac{\bar{x}\,p}{1-p} = 0.7382.$$

Medicines Data Poisson Negative binomial
0 0.4144 0.1004 0.3520
1 0.1531 0.2307 0.1967
2 0.0965 0.2652 0.1294
3 0.0787 0.2032 0.0894
4 0.0634 0.1168 0.0632
5 0.0478 0.0537 0.0454
6 0.0391 0.0206 0.0328
10 or more 0.0360 0.000143 0.0368

The negative binomial matches the tail and most of the excess at zero; it still underestimates the zeros, 0.352 against 0.414, which suggests that taking no medicine at all is partly a separate state (no chronic condition) rather than the low end of a continuum. Models for such data add a separate probability of a structural zero. The broader point is that the choice of distribution is a claim about the data that should be checked, and that for counts the first check, the variance against the mean, takes one line.

Fitting and simulating

dist.fit(data) estimates parameters by maximum likelihood, and by default it estimates loc and scale too, even for distributions whose standard form starts at zero. For exponential data, which start at zero, the free location is estimated at the smallest observation. In 200 samples of 50 from an exponential distribution with mean 2, for each of 30 seeds, the average fitted loc was 0.0396 (median over seeds; IQR 0.0370 to 0.0410) and the average fitted scale 1.9615, slightly too small; fixing the location with expon.fit(x, floc=0) gave the maximum-likelihood estimate of the mean, the sample mean, averaging 2.0026. Problem 2 shows that 0.04 and 1.96 are the exact expectations of the two free-location estimates. The effect is small here, but for distributions with shape parameters, such as the gamma or Weibull, a free location can change the fit substantially. Fixing parameters that the model fixes (floc=0, fscale=...) is the safe habit.

dist.rvs(size=..., random_state=rng) draws random values, reproducibly when rng is a seeded NumPy Generator: stats.binom(10, 0.5475).rvs(size=8, random_state=np.random.default_rng(0)) returns 5, 6, 8, 9, 4, 3, 5, 5 on every run. Simulation from fitted distributions is the basis of parametric bootstrap methods and of checking a model by comparing data simulated from it with the data observed; Probability Fundamentals covers Generators and the Monte Carlo error of such simulations.

Common misstatements

Example (1 - cdf(k) for "greater than"). It is correct, but it loses precision in the far tail, where sf(k) should be used. For a discrete variable it is also $P(X > k)$, strictly greater, so "at least $k$" is sf(k - 1), as the section on discrete traps shows.

Example (the density is for plotting). The value of a pdf is not a probability, but it is not only a plotting aid either: it is what maximum likelihood fitting maximizes and what Bayes' theorem for continuous variables multiplies.

Worked problems

The problems extend the article on its own data. Every number is recomputed, with assertions, in the review-pass block of tools/experiments/data-science/probability-distributions.py.

Problem 1 (where the negative binomial comes from). Show that if each adult's count is Poisson with a rate drawn from a gamma distribution, the counts have the negative binomial distribution, and read the fitted parameters for the prescription counts in those terms.

Solution. The approach is to condition on the rate and then average over it. Let an adult's count be $X$ with $X \mid \Lambda \sim \text{Poisson}(\Lambda)$, and let $\Lambda$ have the gamma density $f(\lambda) = \lambda^{r-1} e^{-\lambda/\theta}/(\Gamma(r)\theta^r)$ with shape $r$ and scale $\theta$. Then

$$P(X = k) = \int_0^\infty \frac{e^{-\lambda}\lambda^k}{k!}\, f(\lambda)\, d\lambda = \frac{1}{k!\,\Gamma(r)\,\theta^r}\int_0^\infty \lambda^{k + r - 1} e^{-\lambda(1 + 1/\theta)}\, d\lambda = \frac{\Gamma(k + r)}{k!\,\Gamma(r)}\, \frac{\theta^k}{(1 + \theta)^{k + r}},$$

using $\int_0^\infty \lambda^{a-1}e^{-b\lambda}\,d\lambda = \Gamma(a)/b^a$. With $p = 1/(1 + \theta)$ this is $\frac{\Gamma(k + r)}{k!\,\Gamma(r)}\, p^r (1 - p)^k$, scipy's nbinom(r, p). The moments follow from the law of total variance without the pmf: $E[X] = E[\Lambda] = r\theta = \mu$, and $\operatorname{Var}(X) = E[\operatorname{Var}(X \mid \Lambda)] + \operatorname{Var}(E[X \mid \Lambda]) = E[\Lambda] + \operatorname{Var}(\Lambda) = \mu + \mu^2/r$. The first term is Poisson variation; the second is variation in the rates between people. For the prescription counts, $r = 0.7382$, so the second term is $2.2989^2/0.7382 = 7.1589$ of the variance 9.4578, three quarters of it. The rates themselves have a coefficient of variation of $1/\sqrt{r} = 1.164$: adults differ in their underlying rate by more than the rate's own mean. The share of zeros is $E[e^{-\Lambda}] = (1 + \theta)^{-r} = p^r = 0.2431^{0.7382} = 0.3520$, the table's value. Integrating the Poisson probabilities against the fitted gamma numerically gives 0.1967, 0.1294 and 0.0454 for 1, 2 and 5 medicines, the negative binomial's values. Overdispersion in Count Data proves the variance decomposition for rates that vary, and Overdispersion and Quasi-Likelihood treats what can be inferred when only the mean and variance are trusted.

Problem 2 (the free-location fit, exactly). Derive the maximum-likelihood estimates of loc and scale for exponential data with both free, and their expectations for samples of 50 with mean 2.

Solution. With location $a$ and scale $\beta$, the density is $\beta^{-1}e^{-(x - a)/\beta}$ for $x \ge a$ and 0 below. The log-likelihood of a sample $x_1, \ldots, x_n$ is $-n\log\beta - \sum_i (x_i - a)/\beta$ if $a \le \min_i x_i$ and $-\infty$ otherwise. For fixed $\beta$ it increases with $a$, so the maximum is at the largest allowed value, $\hat{a} = \min_i x_i$. Then setting the derivative in $\beta$ to zero gives $\hat{\beta} = \bar{x} - \min_i x_i$. scipy's fit returns exactly these: for the seed-0 sample, the minimum 0.0026 and $\bar{x} - 0.0026 = 2.2251$. The minimum of $n$ independent exponentials with mean $\beta$ is exponential with mean $\beta/n$, because $P(\min > t) = (e^{-t/\beta})^n = e^{-nt/\beta}$. So $E[\hat{a}] = 2/50 = 0.04$ and $E[\hat{\beta}] = 2 - 0.04 = 1.96$, the values the simulation found (0.0396 and 1.9615). Multiplying $\hat{\beta}$ by $n/(n-1)$ removes its bias: $1.9615 \times 50/49 = 2.0015$. The general lesson is that a free location for a distribution with a hard lower limit is estimated at the smallest observation, which is always above the true limit, and the scale absorbs the error.

Problem 3 (where 1 - cdf collapses). Find the $z$ beyond which 1 - norm.cdf(z) returns exactly zero, and show how inaccurate it is before that.

Solution. Doubles just below 1 are spaced $2^{-53} = 1.11 \times 10^{-16}$ apart, so the cdf, rounded to the nearest double, becomes exactly 1 once the true tail falls below half a spacing, $2^{-54}$. The $z$ with that tail is norm.isf(2**-54) = 8.29. Before that, 1 - cdf can only return a whole multiple of $2^{-53}$. At $z = 8$ the true tail is $6.22 \times 10^{-16}$, while 1 - cdf returns $6 \times 2^{-53} = 6.66 \times 10^{-16}$, 7% too large; at $z = 8.2$ it returns a single unit, $1.11 \times 10^{-16}$, against $1.20 \times 10^{-16}$; at 8.3 it returns 0. sf evaluates the tail directly and keeps full relative precision.

Open questions

  • Does a zero-inflated negative binomial explain the excess of adults taking no medicine? The negative binomial puts 35.2% at zero against 41.4% observed. Fitting a zero-inflated negative binomial by maximum likelihood to the 5,524 counts, and comparing it with the plain fit by a likelihood ratio and by the table of frequencies, would show whether a separate "no chronic condition" state is needed or the misfit lies elsewhere.
  • How much of the overdispersion does age explain? A negative binomial regression of the counts on age and sex (Poisson Regression and Overdispersion and Quasi-Likelihood give the framework) would show how much of the variation in rates, a coefficient of variation of 1.16 here, is between age groups and how much remains within them.
  • Is the normal model as good for women's heights and within groups? The same checks (tail shares, the 90th percentile, the probability-plot correlation) for the women and for each race and Hispanic-origin group would show whether the agreement within a few thousandths holds generally or is helped by mixing groups with different means.

Where it leads

A distribution describes how a single random quantity varies. The next article, Inferential Statistics in Python, uses the sampling distributions of statistics, the normal and Student's $t$ among them, to turn one sample into intervals and tests, and checks each tool against a population whose answers are known. The distributions themselves are derived in Theoretical Probability Distributions, the $t$, chi-squared and F distributions from normal samples in Sampling Distributions from the Normal: Chi-Squared, t and F, and maximum likelihood, which fit uses, in the Statistics collection's Likelihood and Estimation unit.

In Python

import numpy as np
from scipy import stats

X = stats.binom(10, 0.5475)
X.pmf(5), X.cdf(3), X.sf(6)                       # P(X = 5), P(X <= 3), P(X >= 7)
stats.norm.sf(8.5), stats.norm.logsf(40)          # tails without cancellation
stats.expon(scale=1 / rate)                       # scale is the mean, not the rate
stats.t(df=9).ppf(0.975)                          # multiplier for a 95% interval, n = 10

mu, sd = stats.norm.fit(heights)                  # maximum likelihood: sd uses divisor n
(osm, osr), (slope, intercept, r) = stats.probplot(heights, dist="norm")
m, v = counts.mean(), counts.var(ddof=1)
nb = stats.nbinom(m * (m / v) / (1 - m / v), m / v)   # negative binomial by moments
stats.expon.fit(x, floc=0)                        # fix parameters the model fixes
X.rvs(size=1000, random_state=np.random.default_rng(0))

The numbers in this article are computed by tools/experiments/data-science/probability-distributions.py in the site's repository, from the NHANES tables built by exploredata.py and psdata.py; the results are the same with scipy 1.15.2 and 1.18.1 except for the list of new-style classes (both rerun on 23 November 2026). The figure is drawn by tools/figures/probability-distributions.py.

References

Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 7 revisions