Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

We now have everything we need for an overview of the most important discrete distributions: the pmf from the chapter Random variables and distributions, expected value, variance and entropy from the chapter Expected value and variance, and from the chapter Numpy the machinery for actually computing with them.

We present every distribution in the same format – parameters, pmf, expected value, variance, entropy, example applications and the name in scipy.stats – just as we will later do for the continuous distributions.

Convention: discrete probability spaces

In this chapter Ω\Omega is always at most countable, and usually even finite. Then the power set P(Ω)\mathcal{P}(\Omega) is a σ-algebra, so every subset of Ω\Omega is an event, and the probability measure PP is completely determined by the values

p(ω):=P({ω})p(\omega) := P(\{\omega\})

on the individual outcomes – because every event A⊆ΩA \subseteq \Omega is the (at most countable) disjoint union of its elements, so that P(A)=∑ω∈Ap(ω)P(A) = \sum_{\omega \in A} p(\omega). This function pp is the probability mass function (pmf); discrete density is another name in common use. Everything we want to know about a discrete distribution can be read off it:

E(X)=∑xx pX(x),V(X)=E(X2)−(EX)2,H(X)=−∑xpX(x)log⁡2pX(x).\mathbb{E}(X) = \sum_{x} x\,p_X(x), \qquad \mathbb{V}(X) = \mathbb{E}(X^2) - {\left(\mathbb{E}X\right)}^2, \qquad H(X) = -\sum_{x} p_X(x)\log_2 p_X(x).

Discrete distributions in SciPy

scipy.stats provides objects for discrete distributions just as it does for continuous ones. The main difference: where a continuous distribution has a density pdf, a discrete distribution has a pmf, and the method is accordingly called pmf.

The methods we need over and over again are the same for every distribution. If X is a discrete random variable from scipy.stats, then:

wantedcall
P(X=k)P(X = k)X.pmf(k)
P(X≤k)P(X \leq k)X.cdf(k)
P(X>k)P(X > k)X.sf(k), equivalent to 1 - X.cdf(k)
P(X≥k)P(X \geq k)X.sf(k-1)
P(X<k)P(X < k)X.cdf(k-1)
a sample of size nnX.rvs(size=n)
E(X)\mathbb{E}(X), V(X)\mathbb{V}(X), σ(X)\sigma(X)X.mean(), X.var(), X.std()

The sf stands for survival function.

P(X = 3)   = 0.2668279319999998
P(X <= 3)  = 0.6496107184000002
P(X > 3)   = 0.3503892815999998 = 1 - P(X <= 3) = 0.3503892815999998
P(X >= 3)  = 0.6172172136000003

Discrete uniform distribution

  • Parameter nn, notation Uni⁡{1,…,n}\operatorname{Uni}\{1,\dots,n\}

  • pmf p(k)=1np(k) = \dfrac{1}{n} for k∈{1,…,n}k \in \{1,\dots,n\}

  • Expected value n+12\dfrac{n+1}{2}

  • Variance n2−112\dfrac{n^2-1}{12}

  • Entropy log⁡2n\log_2 n – the maximum among all distributions on a set with nn elements (see Expected value and variance, section Entropy)

  • Example applications:

    • The fair die: n=6n=6, so E(X)=3.5\mathbb{E}(X) = 3.5 and V(X)=3512≈2.92\mathbb{V}(X) = \frac{35}{12} \approx 2.92.

    • An idealised random number generator that returns integers in a fixed range.

  • In scipy.stats: st.randint(1, n+1) – careful: as with range, the upper bound is exclusive.

We already know the expected value. For the variance we use V(X)=E(X2)−(EX)2\mathbb{V}(X) = \mathbb{E}(X^2) - (\mathbb{E}X)^2. With the summation formula ∑i=1ni2=n(n+1)(2n+1)6\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6} we get

E(X2)=1n∑i=1ni2=(n+1)(2n+1)6,\mathbb{E}(X^2) = \frac{1}{n}\sum_{i=1}^n i^2 = \frac{(n+1)(2n+1)}{6},

and therefore

V(X)=(n+1)(2n+1)6−(n+1)24=(n+1)(2(2n+1)−3(n+1))12=(n+1)(n−1)12=n2−112.\mathbb{V}(X) = \frac{(n+1)(2n+1)}{6} - \frac{(n+1)^2}{4} = \frac{(n+1)\left( 2(2n+1) - 3(n+1) \right)}{12} = \frac{(n+1)(n-1)}{12} = \frac{n^2-1}{12}.
E(X) = 3.5  expected: 3.5
V(X) = 2.9166666666666665  expected: 2.9166666666666665
<Figure size 1200x400 with 2 Axes>

Categorical distribution

  • Parameter a probability vector p=(p1,…,pn)∈Δnp = (p_1,\dots,p_n) \in \Delta_n, that is pi≥0p_i \geq 0 and ∑ipi=1\sum_i p_i = 1

  • pmf p(ωi)=pip(\omega_i) = p_i on a set Ω={ω1,…,ωn}\Omega = \{\omega_1,\dots,\omega_n\} with nn elements

  • Expected value (if we identify ωi\omega_i with the number ii) ∑i=1ni pi\sum_{i=1}^n i\,p_i – which in general does not simplify any further

  • Variance ∑i=1ni2pi−(∑i=1ni pi)2\sum_{i=1}^n i^2 p_i - \left(\sum_{i=1}^n i\,p_i\right)^2

  • Entropy −∑ipilog⁡2pi-\sum_i p_i \log_2 p_i

  • Example applications:

    • An urn with mm balls in nn colours, nin_i of them of colour ii; drawing with replacement gives pi=nimp_i = \frac{n_i}{m}.

    • Any survey with a fixed list of mutually exclusive answers.

  • In scipy.stats: no object of its own, but st.rv_discrete(values=(values, p)) builds one; if all you want is to draw samples, rng.choice(values, p=p) from the chapter Numpy is enough.

The parameter space is exactly the simplex Δn\Delta_n we met in the chapter Random variables and distributions: the uniform distribution sits at its centre, the Dirac distributions at its vertices. The categorical distribution is therefore the most general distribution on a finite set – every distribution that follows in this chapter is either a special case of it or built from it. The four jumper colours from that chapter are a categorical distribution too, with n=4n=4.

E(X) = 2.4  recomputed: 2.4
V(X) = 0.6400000000000006
H(X) = 1.6854752972273344 bits
for comparison, uniform distribution on 4 values: 2.0 bits
<Figure size 1200x400 with 2 Axes>

Bernoulli distribution

  • Parameter p∈[0,1]p \in [0,1], notation Ber⁡(p)\operatorname{Ber}(p)

  • pmf p(1)=pp(1) = p and p(0)=1−pp(0) = 1-p on Ω={0,1}\Omega = \{0,1\}

  • Expected value pp

  • Variance p(1−p)p(1-p)

  • Entropy h(p):=−plog⁡2p−(1−p)log⁡2(1−p)h(p) := -p\log_2 p - (1-p)\log_2(1-p), the binary entropy function

  • Example applications:

    • A single coin toss, unfair coins included.

    • Any yes/no feature: a component is defective or it is not, an email is spam or it is not.

  • In scipy.stats: st.bernoulli(p)

The Bernoulli distribution is the special case n=2n=2 of the categorical distribution, and the simplest nontrivial case there is – it has one parameter. Expected value and variance are quick to compute: E(X)=0⋅(1−p)+1⋅p=p\mathbb{E}(X) = 0\cdot(1-p) + 1\cdot p = p, and since X2=XX^2 = X (because 02=00^2=0 and 12=11^2=1), we also have E(X2)=p\mathbb{E}(X^2) = p and therefore

V(X)=E(X2)−(EX)2=p−p2=p(1−p).\mathbb{V}(X) = \mathbb{E}(X^2) - {\left(\mathbb{E}X\right)}^2 = p - p^2 = p(1-p).

The variance is maximal at p=12p = \frac12 and vanishes for p∈{0,1}p \in \{0,1\} – there the outcome is no longer random at all.

In the chapter Random variables and distributions we already estimated a value π^\hat\pi from data without giving it a name: it was the parameter of a Bernoulli distribution.

The binary entropy function

The entropy of a Bernoulli distribution, as a function of pp, deserves a look of its own. Let us differentiate it. With log⁡2x=ln⁡xln⁡2\log_2 x = \frac{\ln x}{\ln 2} we have

ddp(−plog⁡2p)=−log⁡2p−1ln⁡2,ddp(−(1−p)log⁡2(1−p))=log⁡2(1−p)+1ln⁡2,\frac{d}{dp}\left( -p\log_2 p \right) = -\log_2 p - \frac{1}{\ln 2}, \qquad \frac{d}{dp}\left( -(1-p)\log_2(1-p) \right) = \log_2(1-p) + \frac{1}{\ln 2},

so the two constants cancel and what remains is

h′(p)=log⁡2(1−p)−log⁡2p=−log⁡2p1−p.h'(p) = \log_2(1-p) - \log_2 p = -\log_2\frac{p}{1-p}.

The only zero is at p=1−pp = 1-p, that is, at p=12p = \frac12: the fair coin toss has the maximal entropy, namely h(12)=1h(\frac12) = 1 bit. That fits: the uniform distribution is the maximum entropy distribution.

<Figure size 1200x400 with 2 Axes>
E(X) = 0.3  V(X) = 0.21  H(X) = 0.8812908992306927 bits

Binomial distribution

  • Parameters n∈Nn \in \mathbb{N} and p∈[0,1]p \in [0,1], notation Bin⁡(n,p)\operatorname{Bin}(n,p)

  • pmf p(k)=(nk)pk(1−p)n−kp(k) = \dbinom{n}{k}p^k(1-p)^{n-k} for k∈{0,1,…,n}k \in \{0,1,\dots,n\}

  • Expected value npnp

  • Variance np(1−p)np(1-p)

  • Entropy: no convenient closed expression; it is smaller than log⁡2(n+1)\log_2(n+1), since the maximum would be the uniform distribution on {0,…,n}\{0,\dots,n\}

  • Example applications:

    • Tossing the same coin nn times and counting the heads.

    • Drawing a sample of nn components and counting the defective ones.

    • An urn with two colours, NN balls of which n1n_1 are red, p=n1Np = \frac{n_1}{N}, drawing nn times with replacement.

  • In scipy.stats: st.binom(n, p)

The binomial distribution arises from the Bernoulli distribution by repetition. If X1,…,XnX_1,\dots,X_n are independent random variables with Xi∼Ber⁡(p)X_i \sim \operatorname{Ber}(p), then

X:=∑i=1nXiX := \sum_{i=1}^n X_i

counts the number of successes, and X∼Bin⁡(n,p)X \sim \operatorname{Bin}(n,p). So it has two parameters.

Why the pmf looks the way it does: by independence, any one fixed pattern of kk successes and n−kn-k failures – say 1,1,0,1,0,…1,1,0,1,0,\dots – has probability pk(1−p)n−kp^k(1-p)^{n-k}, whichever positions the successes occupy. And there are exactly (nk)\binom{n}{k} such patterns, one for each subset of {1,…,n}\{1,\dots,n\} with kk elements. Summing over them gives the formula.

Expected value and variance now come for free. The expected value follows from linearity:

E(X)=∑i=1nE(Xi)=n p.\mathbb{E}(X) = \sum_{i=1}^n \mathbb{E}(X_i) = n\,p.

For the variance we need the independence – this is exactly the rule of calculation from the section Rules of calculation for the variance:

V(X)=∑i=1nV(Xi)=n p(1−p).\mathbb{V}(X) = \sum_{i=1}^n \mathbb{V}(X_i) = n\,p(1-p).

Without independence this would be false. Here you can see clearly what that rule is good for.

E(X) = 3.0  expected: 3.0
V(X) = 2.1  expected: 2.0999999999999996
<Figure size 1200x400 with 2 Axes>

Which pp fits the data best?

So far we have treated pp as given and asked for the probability of kk successes. Now we turn the question around: we have found k=3k = 3 defective components in a sample of n=10n = 10 and want to know which pp fits this observation.

For every pp the pmf gives us a probability for exactly this observation:

P(X=3)=(103)p3(1−p)7.P(X = 3) = \binom{10}{3}p^3(1-p)^{7}.

Let us compare two candidates, p=0.2p = 0.2 and p=0.5p = 0.5. The binomial coefficient is the same in both cases and cancels, so it is enough to compare p3(1−p)7p^3(1-p)^7:

0.23⋅0.87=0.008⋅0.2097…≈0.00168,0.53⋅0.57=0.510≈0.00098.0.2^3 \cdot 0.8^7 = 0.008 \cdot 0.2097\ldots \approx 0.00168, \qquad 0.5^3 \cdot 0.5^7 = 0.5^{10} \approx 0.00098.

So our observation is about 1.7 times as likely under p=0.2p = 0.2 as under p=0.5p = 0.5. In this sense p=0.2p = 0.2 fits better.

The natural next question is which pp fits best of all. We are looking for the maximum of p↦pk(1−p)n−kp \mapsto p^k(1-p)^{n-k}, so we differentiate:

ddp(pk(1−p)n−k)=k pk−1(1−p)n−k−(n−k) pk(1−p)n−k−1=pk−1(1−p)n−k−1(k(1−p)−(n−k)p).\frac{d}{dp}\left( p^k(1-p)^{n-k} \right) = k\,p^{k-1}(1-p)^{n-k} - (n-k)\,p^k(1-p)^{n-k-1} = p^{k-1}(1-p)^{n-k-1}\Big( k(1-p) - (n-k)p \Big).

For 0<p<10 < p < 1 the leading factor is nonzero, so what remains is the condition k(1−p)=(n−k)pk(1-p) = (n-k)p. Solving it: k−kp=np−kpk - kp = np - kp, so k=npk = np and therefore

p=kn.p = \frac{k}{n}.

For our observation that is p=310=0.3p = \frac{3}{10} = 0.3 – simply the proportion of successes in the sample. That is exactly what you would have guessed without any calculation; what is remarkable is that here it drops out of a calculation, from a clearly stated criterion: choose the parameter that makes the observation you actually made as likely as possible.

<Figure size 800x400 with 1 Axes>
P(X=3) at p=0.2: 0.20132659199999992
P(X=3) at p=0.5: 0.1171875
P(X=3) at p=0.3: 0.2668279319999998   <- the maximum

This procedure – choosing the parameter that makes the observation most likely – is called maximum likelihood estimation. The chapter Statistical models treats it in general and for other distributions.

Digression: the Poisson binomial distribution

  • Parameters p1,…,pn∈[0,1]p_1,\dots,p_n \in [0,1], one probability per trial

  • pmf p(k)=∑A∈Fk∏i∈Api∏i∉A(1−pi)p(k) = \displaystyle\sum_{A \in F_k} \prod_{i \in A} p_i \prod_{i \notin A} (1-p_i), where FkF_k is the set of subsets of {1,…,n}\{1,\dots,n\} with kk elements

  • Expected value ∑i=1npi\sum_{i=1}^n p_i

  • Variance ∑i=1npi(1−pi)\sum_{i=1}^n p_i(1-p_i)

  • Example application: nn components from different batches, each with its own failure probability – how many of them fail?

  • In scipy.stats: no object of its own; the pmf is easy enough to compute yourself.

We are no longer tossing the same coin nn times but nn different coins, each with its own success probability pip_i. The Xi∼Ber⁡(pi)X_i \sim \operatorname{Ber}(p_i) are still independent, just no longer identically distributed. Expected value and variance therefore follow exactly as for the binomial distribution, from linearity and from the variance rule for independent summands; when all the pip_i are equal, as for the binomial distribution, we get npnp and np(1−p)np(1-p) back.

The pmf itself is unwieldy – the sum runs over (nk)\binom{n}{k} subsets. In practice it is computed iteratively: start with “no coins, no successes, with probability 1” and work the coins in one at a time.

The interesting case is many small pip_i. If we set every pi=λnp_i = \frac{\lambda}{n} and let nn grow, the expected value stays fixed at λ\lambda while each individual trial becomes ever less likely. This is the passage to the Poisson distribution:

sum of the pmf = 1.0
E(X) = 1.9500000000000004  expected: 1.95
V(X) = 0.7174999999999998  expected: 0.7174999999999999
<Figure size 700x400 with 1 Axes>
<Figure size 800x400 with 1 Axes>

Poisson distribution

  • Parameter λ>0\lambda > 0, notation Pois⁡(λ)\operatorname{Pois}(\lambda)

  • pmf p(k)=e−λλkk!p(k) = e^{-\lambda}\dfrac{\lambda^k}{k!} for k∈N0k \in \mathbb{N}_0

  • Expected value λ\lambda

  • Variance λ\lambda

  • Example applications:

    • How many requests reach the server in one second?

    • How many typos are there on one page?

    • How many atomic nuclei in a specimen decay in one minute?

  • In scipy.stats: st.poisson(mu=λ)

The guiding question: how many events occur in a fixed period of time, if they occur independently of one another and at a constant mean rate? In contrast to every distribution in this chapter so far, this one does not have finite support – in principle there could be arbitrarily many events, even if large numbers become very unlikely.

That the pmf sums to 1 follows from the exponential series ∑k≥0λkk!=eλ\sum_{k \geq 0} \frac{\lambda^k}{k!} = e^{\lambda}:

∑k=0∞e−λλkk!=e−λeλ=1.\sum_{k=0}^\infty e^{-\lambda}\frac{\lambda^k}{k!} = e^{-\lambda}e^{\lambda} = 1.

The expected value follows with an index shift – the summand for k=0k=0 is 0 and drops out:

E(X)=∑k=1∞k e−λλkk!=λe−λ∑k=1∞λk−1(k−1)!=λe−λeλ=λ.\mathbb{E}(X) = \sum_{k=1}^\infty k\,e^{-\lambda}\frac{\lambda^k}{k!} = \lambda e^{-\lambda}\sum_{k=1}^\infty \frac{\lambda^{k-1}}{(k-1)!} = \lambda e^{-\lambda}e^{\lambda} = \lambda.

For the variance, compute E(X(X−1))=λ2\mathbb{E}(X(X-1)) = \lambda^2 in just the same way, which gives V(X)=E(X2)−λ2=λ2+λ−λ2=λ\mathbb{V}(X) = \mathbb{E}(X^2) - \lambda^2 = \lambda^2 + \lambda - \lambda^2 = \lambda. So here expected value and variance coincide – a practical test of whether a Poisson distribution can fit a dataset at all.

E(X) = 2.5  V(X) = 2.5  (both equal to lambda)
P(X > 2) = 0.45618688411667035  by hand: 0.4561868841166704
<Figure size 1200x400 with 2 Axes>

For completeness: the geometric distribution

  • Parameter p∈(0,1]p \in (0,1], notation Geom⁡(p)\operatorname{Geom}(p)

  • pmf p(k)=(1−p)k−1pp(k) = (1-p)^{k-1}p for k∈{1,2,3,… }k \in \{1,2,3,\dots\}

  • Expected value 1p\dfrac{1}{p}

  • Variance 1−pp2\dfrac{1-p}{p^2}

  • Example applications:

    • How many times do I have to throw a die until the first six comes up? (p=16p = \frac16, on average 6 throws)

    • How many connection attempts does it take until the first success?

  • In scipy.stats: st.geom(p)

We repeat a Bernoulli experiment until the first success and count the trials. For the first k−1k-1 trials to fail and the kk-th to succeed has probability (1−p)k−1p(1-p)^{k-1}p – hence the pmf.

The geometric distribution is memoryless: if you have already thrown the die ten times in vain, the probability of needing another kk throws is the same as it was at the start. The die does not remember. It is thus the discrete counterpart of the exponential distribution, the only memoryless distribution in the continuous case.

E(X) = 6.0  expected: 6.0
V(X) = 30.000000000000007  expected: 30.000000000000004
<Figure size 1200x400 with 2 Axes>

Looking back

All the distributions in this chapter hang together:

  • The categorical distribution is the most general distribution on a finite set; its parameter space is the simplex Δn\Delta_n.

  • The discrete uniform distribution is the centre of that simplex and has maximal entropy.

  • The Bernoulli distribution is the case n=2n=2.

  • The binomial distribution counts the successes in nn independent Bernoulli trials with the same pp, the Poisson binomial distribution allows different pip_i.

  • The Poisson distribution is their limiting case for many trials, each of them very unlikely.

  • The geometric distribution turns the question around: not how many successes in a fixed number of trials, but how many trials up to the first success.

Next we leave countable sets of outcomes behind. Look at the binomial distribution not for small pp but for large nn, and the passage to the limit leads not to the Poisson distribution but to the normal distribution – that is the de Moivre–Laplace theorem, and the chapter The normal distribution opens with it.