---
title: Discrete distributions
heading: Counting successes and waiting for one
eyebrow: A11 · Lesson 10 · Discrete distributions
xp: 40
---

::: hook
Send a 1000-bit packet over a link with a bit error rate of one in a thousand. How many errors will it have? Not exactly one: sometimes none, sometimes three. And if a corrupted packet is resent until one gets through, how many tries does that take? Two distributions answer these: the binomial, which counts successes in a fixed number of tries, and the geometric, which counts tries until the first success. Between them they describe most of the yes-or-no randomness in digital systems.
:::

::: play
- Each draw counts the successes in $n$ independent tries, each succeeding with probability $p$. The histogram builds the distribution; the line is the binomial formula.
- Where is the peak? Compare the measured mean and standard deviation with $np$ and $\sqrt{np(1 - p)}$.
- Make $p$ small and $n$ large. The shape leans against zero; lesson 12 explains what it becomes.
:::
:::

```sim plot
view: random
x: k = -0.5..20.5
slider: n = 1..20 step 1 = 10
slider: p = 0.02..0.98 step 0.02 = 0.3
sample: coin(p)*step(n - 0.5) + coin(p)*step(n - 1.5) + coin(p)*step(n - 2.5) + coin(p)*step(n - 3.5) + coin(p)*step(n - 4.5) + coin(p)*step(n - 5.5) + coin(p)*step(n - 6.5) + coin(p)*step(n - 7.5) + coin(p)*step(n - 8.5) + coin(p)*step(n - 9.5) + coin(p)*step(n - 10.5) + coin(p)*step(n - 11.5) + coin(p)*step(n - 12.5) + coin(p)*step(n - 13.5) + coin(p)*step(n - 14.5) + coin(p)*step(n - 15.5) + coin(p)*step(n - 16.5) + coin(p)*step(n - 17.5) + coin(p)*step(n - 18.5) + coin(p)*step(n - 19.5) | mean: M | sd: S
trials: 20000
bins: 21
density: step(n + 0.5 - k)*n!/(round(k)!*(abs(n - round(k)))!)*p^round(k)*(1 - p)^(abs(n - round(k)))
readout: mean, measured = M | digits: 3
readout: np = n*p | digits: 3
readout: standard deviation, measured = S | digits: 3
readout: square root of np(1 − p) = sqrt(n*p*(1 - p)) | digits: 3
```

::: predict
## A 1000-bit packet crosses a link where each bit is wrong with probability 0.001, independently. What is the chance the packet has no errors?

- [ ] 99.9%
- [ ] 0%: it will have exactly one error
- [x] About 37%
- [ ] 50%

::: reveal
About 37%: $0.999^{1000} = 0.368$. The expected number of errors is one, but the count varies from packet to packet: about 37% have none, 37% have one, 18% have two, and so on. That number, very close to $e^{-1}$, isn't a coincidence; lesson 12 explains it.
:::
:::

::: nameit
## Probability mass functions

A discrete random variable takes values from a list. Its **probability mass function** (PMF) gives the chance of each value, $p_X(k) = P(X = k)$, and the chances add to 1. Its **cumulative distribution function** (CDF) adds them up: $F_X(k) = P(X \le k)$.

## Bernoulli

One try that succeeds ($X = 1$) with probability $p$: mean $p$, variance $p(1 - p)$ (lesson 9).

## Binomial

The number of successes in $n$ independent tries, each with success probability $p$:

$$P(X = k) = \binom{n}{k}p^k(1 - p)^{n - k}, \qquad k = 0, 1, \ldots, n$$

It's a sum of $n$ independent Bernoulli variables, so means and variances add:

$$E[X] = np \qquad \text{Var}(X) = np(1 - p)$$

Bit errors in a word, defective parts in a lot, and packets lost out of a burst are all binomial when the tries are independent.

## Geometric

The number of tries up to and including the first success:

$$P(X = k) = (1 - p)^{k - 1}p, \qquad k = 1, 2, \ldots$$

It needs more than $k$ tries only if the first $k$ all fail: $P(X > k) = (1 - p)^k$. Its mean and variance are

$$E[X] = \frac{1}{p} \qquad \text{Var}(X) = \frac{1 - p}{p^2}$$

A link that resends a packet until it arrives (an ARQ scheme) needs $\frac{1}{p}$ transmissions on average.

**No memory.** $P(X > m + k \mid X > m) = (1 - p)^k = P(X > k)$. After any number of failures, the count of tries still needed has the same distribution as at the start: the link doesn't remember its bad luck.
:::

::: example
## Each transmission of a packet succeeds with probability 0.8, and failed packets are resent. How many transmissions does a packet need on average, and how often does it need more than three?

::: solution
1. **Mean.** The number of transmissions is geometric with $p = 0.8$:
   $$\frac{1}{0.8} = 1.25$$
2. **More than three.** That means the first three all failed:
   $$0.2^3 = 0.008$$
   less than 1% of packets.

**Answer:** a packet needs 1.25 transmissions on average, and more than three for 0.8% of packets.
:::
:::

::: example
## A 1000-bit packet has bit error rate $10^{-3}$. Find the mean and standard deviation of the number of errors, and the chance of no errors.

::: solution
1. **The model.** The error count is binomial with $n = 1000$ and $p = 10^{-3}$.
2. **Mean.**
   $$np = 1$$
3. **Standard deviation.**
   $$\sqrt{np(1 - p)} = 0.9995$$
4. **No errors.**
   $$\begin{aligned} P(0) &= 0.999^{1000} \\ &= 0.368 \end{aligned}$$
5. **What it means.** The spread is as large as the mean, so a code that corrects only one error per packet would still fail often (lesson 12 computes how often).

**Answer:** the mean is 1 error, the standard deviation 0.9995, and the chance of no errors 0.368.
:::
:::

::: example
## A packet gets through with probability 0.9, and failures are resent. How many transmissions does it take on average, and how often more than three?

::: solution
1. **Mean.** The number of transmissions is geometric with $p = 0.9$:
   $$\frac1p = 1.11$$
2. **More than three.** It takes more than three only if the first three all fail:
   $$0.1^3 = 0.001$$
   one packet in a thousand.
3. **What it means.** A protocol that gives up after three tries loses that fraction.

**Answer:** it takes 1.11 transmissions on average, and more than three for one packet in a thousand.
:::
:::

::: check
given: n = 100 | 1000; p = 0.01 | 0.05
answer: n*p
tolerance: 0.5%

## A batch of {n} parts each fails with probability {p}, independently. How many failures do you expect?

::: right
Right. $np$ = {answer}.
:::

::: misses
- [p] That's the chance for one part; multiply by the number of parts.
- [any] Not yet. The binomial mean, $np$.
:::
:::

::: check
given: n = 100 | 400; p = 0.1 | 0.5
answer: sqrt(n*p*(1 - p))
tolerance: 0.5%

## A count is binomial with $n$ = {n} and $p$ = {p}. What is its standard deviation?

::: right
Right. $\sqrt{np(1 - p)}$ = {answer}.
:::

::: misses
- [n*p*(1 - p)] That's the variance; take its square root.
- [sqrt(n*p)] Include the factor $1 - p$.
- [any] Not yet. $\sqrt{np(1 - p)}$.
:::
:::

::: check
given: n = 5 | 10; k = 2 | 3; p = 0.1 | 0.3
answer: n!/(k!*(n - k)!)*p^k*(1 - p)^(n - k)
tolerance: 0.5%

## $X$ is binomial with $n$ = {n} and $p$ = {p}. What is $P(X = \given{k})$?

::: right
Right. $\binom{n}{k}p^k(1 - p)^{n - k}$ = {answer}.
:::

::: misses
- [p^k*(1 - p)^(n - k)] Multiply by the number of arrangements, $\binom{n}{k}$.
- [any] Not yet. The binomial PMF.
:::
:::

::: check
given: p = 0.5 | 0.8 | 0.9
answer: 1/p
tolerance: 0.5%

## Each transmission succeeds with probability {p}, and failures are resent. On average, how many transmissions does a packet need?

::: right
Right. The geometric mean, $\frac{1}{p}$ = {answer}.
:::

::: misses
- [p] That's the chance per try. The average number of tries is $1/p$.
- [(1 - p)/p] That counts only the failed tries before the success.
- [any] Not yet. $E[X] = 1/p$.
:::
:::

::: check
given: p = 0.7 | 0.9; k = 2 | 3
answer: (1 - p)^k
tolerance: 0.5%

## Each try succeeds with probability {p}. What is the chance that more than {k} tries are needed?

::: right
Right. The first {k} must all fail: $(1 - p)^k$ = {answer}.
:::

::: misses
- [(1 - p)^(k - 1)] That's the chance of needing at least {k} tries, which allows success on try {k}.
- [1 - p^k] All {k} must fail, so multiply the failure chances.
- [any] Not yet. $P(X > k) = (1 - p)^k$.
:::
:::

::: check
given: p = 0.6 | 0.75; m = 3 | 5
answer: p
tolerance: 0.5%

## A packet has already failed {m} times; each try succeeds with probability {p}, independently. What is the chance the next try succeeds?

::: right
Right. {answer}: the tries are independent, so past failures change nothing.
:::

::: misses
- [1 - (1 - p)^(m + 1)] That's the chance of success somewhere in the first $m + 1$ tries, judged before any were made.
- [any] Not yet. Independent tries have no memory.
:::
:::

::: check
kind: parts
given: p = 0.8 | 0.95; k = 2 | 3

## Each transmission succeeds with probability {p}, and failures are resent.

::: part
answer: 1/p
tolerance: 0.5%

### How many transmissions does a packet take on average?

::: misses
- [p] It's $1/p$.
- [any] The mean of a geometric distribution.
:::
:::

::: part
answer: (1 - p)^k
tolerance: 0.5%

### What is the chance it needs more than {k}?

::: misses
- [any] The first {k} must all fail.
:::
:::
:::

::: check
kind: derivation
vars: n = 100..10000, p = 0.0001..0.01

## A packet of $n$ bits has bit error rate $p$.

::: step
answer: n*p

### What is the mean number of errors?
:::

::: step
answer: sqrt(n*p*(1 - p))

### What is its standard deviation?
:::

::: right
Right. A binomial count: mean $np$, standard deviation $\sqrt{np(1 - p)}$.
:::
:::

::: check
kind: python

## Simulate 10⁵ packets, each retried until it succeeds with probability 0.8 per try (`np.random.default_rng(4)`; `rng.geometric(0.8, n)` gives the tries). Set `mean_tries` and `over3`, the fraction needing more than 3.

```python starter
import numpy as np

mean_tries = over3 = 0.0
print(mean_tries, over3)
```

```python solution
import numpy as np

rng = np.random.default_rng(4)
tries = rng.geometric(0.8, 10**5)
mean_tries = tries.mean()
over3 = np.mean(tries > 3)
print(mean_tries, over3)
```

```python tests
assert abs(mean_tries - 1.25) < 0.01, f"The mean is 1/p = 1.25; got {mean_tries}."
assert abs(over3 - 0.008) < 0.0015, f"More than three tries happens with 0.2^3 = 0.008; got {over3}."
```

::: right
Right. 1.25 tries on average, and 0.8% need more than three.
:::
:::

::: explain
## Why is the mean of a binomial $np$?

::: model
A binomial count is the sum of $n$ indicator variables, one per try, each 1 with probability $p$ and 0 otherwise. Each indicator has mean $p$, and expected values add for any variables, independent or not. So the total has mean $p + p + \cdots = np$. The variance also adds, but only because the tries are independent, giving $np(1 - p)$.
:::
:::

::: practice

::: check
given: n = 8 | 20; p = 0.25 | 0.5
answer: n*p*(1 - p)
tolerance: 0.5%

## What is the variance of a binomial count with $n$ = {n} and $p$ = {p}?

::: misses
- [sqrt(n*p*(1 - p))] That's the standard deviation.
- [any] $np(1 - p)$.
:::
:::

::: check
given: p = 0.1 | 0.2 | 0.25 | 0.4 | 0.5 | 0.6
answer: (1 - p)/p^2
tolerance: 0.5%

## What is the variance of a geometric count with $p$ = {p}?

::: misses
- [any] $\frac{1 - p}{p^2}$.
:::
:::

::: check
given: n = 6 | 10; p = 0.05 | 0.1
answer: 1 - (1 - p)^n
tolerance: 0.1%

## In {n} independent tries with success probability {p}, what is the chance of at least one success?

::: misses
- [n*p] Use the complement.
- [any] $1 - (1 - p)^n$.
:::
:::
:::
