---
title: Counting outcomes
heading: Permutations, combinations and error patterns
eyebrow: A11 · Lesson 3 · Counting
xp: 40
---

::: hook
How many 8-bit bytes have exactly two 1s? How many 6-digit PINs are there? If a reel of 50 chips has 2 bad ones and you test 3 at random, how likely are you to miss both? When outcomes are equally likely, every one of these is a counting problem, and the sample spaces are far too big to list. A few counting rules handle them all, and one of them, the binomial coefficient, will run through the rest of the course: it counts the ways errors can land in a code word.
:::

::: play
- The bars show what share of all $2^n$ words of $n$ bits have exactly $k$ ones: $\binom{n}{k}/2^n$.
- Change $n$. Where is the peak? Watch the readouts: how fast do the counts grow?
- The $r$ slider moves the marker; the readout gives the count for $k = r$. Check that $\binom{n}{k} = \binom{n}{n - k}$.
:::
:::

```sim plot
view: graph
x: k = 0..16.99
y: share = 0..0.55
slider: n = 2..16 step 1 = 8
slider: r = 0..16 step 1 = 2
plot: step(n + 0.5 - k)*n!/(floor(k)!*(abs(n - floor(k)))!)/2^n | label: share of words with k ones | samples: 1700 | fill: 0..17
marker: x = r + 0.5 | label: k = r
readout: words with r ones = step(n + 0.5 - r)*n!/(r!*(abs(n - r))!) | digits: 6
readout: all n-bit words = 2^n | digits: 6
```

::: predict
## How many different 8-bit bytes contain exactly two 1s?

- [ ] 16
- [x] 28
- [ ] 56
- [ ] 64

::: reveal
28. Choose which two of the eight positions hold the 1s. There are $8 \times 7 = 56$ ways to pick them in order, but each pair is picked twice (first 3 then 5, or first 5 then 3), so $\frac{56}{2} = 28$. That's $\binom{8}{2}$.
:::
:::

::: nameit
## The multiplication rule

If a choice is made in stages, with $n_1$ options at the first stage, $n_2$ at the second and so on, the number of outcomes is $n_1n_2\cdots$. A 6-digit PIN has $10^6$ possibilities; an $n$-bit word has $2^n$.

## Ordered choices: permutations

Choosing $k$ things from $n$ in order, without repeats:

$$n(n - 1)\cdots(n - k + 1) = \frac{n!}{(n - k)!}$$

All $n$ of them in order: $n!$.

## Unordered choices: combinations

Choosing $k$ from $n$ when order doesn't matter, the **binomial coefficient**:

$$\binom{n}{k} = \frac{n!}{k!\,(n - k)!}$$

Each unordered set appears $k!$ times among the ordered choices, so divide by $k!$. Two facts worth knowing: $\binom{n}{k} = \binom{n}{n - k}$ (choosing the ones is choosing the zeros), and $\sum_k \binom{n}{k} = 2^n$.

## Error patterns

If each bit of an $n$-bit word flips independently with probability $p$, a particular pattern with $k$ errors has chance $p^k(1 - p)^{n - k}$, and there are $\binom{n}{k}$ such patterns:

$$P(k\text{ errors}) = \binom{n}{k}p^k(1 - p)^{n - k}$$

This is the **binomial distribution**, lesson 9's first named distribution.

## Sampling without replacement

Draw $k$ items from $N$, of which $D$ are defective. Every set of $k$ is equally likely, so

$$P(\text{no defective}) = \frac{\binom{N - D}{k}}{\binom{N}{k}}$$
:::

::: example
## An 8-bit word goes over a link where each bit flips with probability 0.01. A single-error-correcting code fixes the word if at most one bit is wrong. How often does it succeed?

::: solution
1. **No errors.**
   $$\begin{aligned} P(0) &= 0.99^8 \\ &= 0.9227 \end{aligned}$$
2. **Exactly one error.**
   $$\begin{aligned} P(1) &= \binom{8}{1}(0.01)(0.99)^7 \\ &= 0.0746 \end{aligned}$$
3. **The code succeeds in either case.**
   $$0.9227 + 0.0746 = 0.9973$$
4. **Compare.** Uncorrectable words drop from 7.7% to 0.27%.

**Answer:** the code succeeds with probability 0.9973.
:::
:::

::: example
## A reel of 50 chips has 2 bad ones. You test 3 at random. What's the chance of finding neither bad chip?

::: solution
1. **Count the samples.** All 3 chips must come from the 48 good ones:
   $$\begin{aligned} P &= \frac{\binom{48}{3}}{\binom{50}{3}} \\ &= \frac{48 \cdot 47 \cdot 46}{50 \cdot 49 \cdot 48} \\ &= \frac{47 \times 46}{50 \times 49} \\ &= 0.882 \end{aligned}$$
2. **What it means.** Sampling only 3 chips misses the bad ones 88% of the time, which is why acceptance sampling uses larger samples.
3. **With replacement.** Drawing with replacement would give
   $$\left(\frac{48}{50}\right)^3 = 0.885$$
   nearly the same, because the sample is small.

**Answer:** the chance of finding neither bad chip is 0.882.
:::
:::

::: example
## A random 4-bit word has each bit equally likely to be 0 or 1. What is the chance it has exactly two 1s?

::: solution
1. **Count the words.** There are $\binom42 = 6$ ways to choose which two bits are 1, out of $2^4 = 16$ equally likely words:
   $$\frac{6}{16} = 0.375$$
2. **The whole distribution.** The full distribution of the count is 1, 4, 6, 4, 1 out of 16: a row of Pascal's triangle, the binomial distribution with $p = \frac12$.

**Answer:** the chance of exactly two 1s is 0.375.
:::
:::

::: check
given: n = 6 | 8 | 10; k = 2 | 3
answer: n!/(k!*(n - k)!)
tolerance: 0.01%

## How many {n}-bit words have exactly {k} ones?

::: right
Right. $\binom{n}{k}$ = {answer}.
:::

::: misses
- [n!/(n - k)!] That counts ordered choices; each set of positions appears $k!$ times.
- [n^k] Positions can't repeat, and order doesn't matter.
- [any] Not yet. $\binom{n}{k} = \frac{n!}{k!(n - k)!}$.
:::
:::

::: check
given: d = 4 | 6 | 8
answer: 10^d
tolerance: 0.01%

## How many {d}-digit PINs are there, if digits may repeat?

::: right
Right. Ten choices at each of {d} places: $10^d$ = {answer}.
:::

::: misses
- [10!/(10 - d)!] That forbids repeats.
- [any] Not yet. The multiplication rule.
:::
:::

::: check
given: n = 5 | 8; k = 2 | 3
answer: n!/(n - k)!
tolerance: 0.01%

## {k} different test jobs go to {k} of {n} machines, one each. In how many ways can they be assigned?

::: right
Right. The first job has $n$ choices, the next $n - 1$, and so on: $\frac{n!}{(n - k)!}$ = {answer}.
:::

::: misses
- [n!/(k!*(n - k)!)] The jobs are different, so order matters: don't divide by $k!$.
- [n^k] Each machine takes only one job.
- [any] Not yet. Count in stages.
:::
:::

::: check
given: n = 8 | 16; k = 1 | 2; p = 0.01 | 0.05
answer: n!/(k!*(n - k)!)*p^k*(1 - p)^(n - k)
tolerance: 0.5%

## Each bit of an {n}-bit word flips with probability {p}, independently. What is the chance of exactly {k} errors?

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

::: misses
- [p^k*(1 - p)^(n - k)] That's one pattern. Multiply by the number of patterns, $\binom{n}{k}$.
- [n!/(k!*(n - k)!)*p^k] The other bits must be right: include $(1 - p)^{n - k}$.
- [any] Not yet. Count the patterns, then multiply by each one's chance.
:::
:::

::: check
given: N = 20 | 50; D = 2 | 3; k = 3 | 5
answer: (N - D)!*(N - k)!/((N - D - k)!*N!)
tolerance: 0.1%

## A batch of {N} parts has {D} defective. You test {k} at random. What is the chance none of them is defective?

::: right
Right. $\binom{N - D}{k}\big/\binom{N}{k}$ = {answer}.
:::

::: misses
- [(1 - D/N)^k] That's sampling with replacement. Without it, each good draw makes the next slightly less likely to be good.
- [any] Not yet. Count the samples with no defective, over all samples.
:::
:::

::: check
given: n = 6 | 10 | 12
answer: 2^n
tolerance: 0.01%

## What is $\sum_{k=0}^{\given{n}}\binom{\given{n}}{k}$?

::: right
Right. Every one of the $2^n$ words has some number of ones, so the counts add to {answer}.
:::

::: misses
- [n!] That counts orderings of $n$ things.
- [any] Not yet. Each word is counted once, in the term for its number of ones.
:::
:::

::: check
kind: parts
given: n = 8 | 10; k = 2 | 3

## A word has {n} bits, each flipped independently with probability 0.01.

::: part
answer: n!/(k!*(n - k)!)
tolerance: 0

### In how many ways can exactly {k} bits flip?

::: misses
- [n^k] Order doesn't matter: divide out $k!$ arrangements.
- [any] $\binom nk$.
:::
:::

::: part
answer: n!/(k!*(n - k)!)*0.01^k*0.99^(n - k)
tolerance: 0.5%

### What is the chance of exactly {k} flips?

::: misses
- [any] $\binom nk p^k(1 - p)^{n-k}$.
:::
:::
:::

::: check
kind: derivation
vars: N = 20..100, D = 1..5

## A reel of $N$ chips has $D$ bad ones; you test 2 at random.

::: step
answer: (N - D)/N*(N - D - 1)/(N - 1)

### What is the chance both are good?
:::

::: step
answer: 1 - (N - D)/N*(N - D - 1)/(N - 1)

### What is the chance of finding at least one bad chip?
:::

::: right
Right. Drawing without replacement changes the odds for the second draw.
:::
:::

::: explain
## Why do you divide by $k!$ when order doesn't matter?

::: model
Counting ordered choices treats picking positions 3 then 5 as different from 5 then 3. Any particular set of $k$ positions can be listed in $k!$ orders, and the ordered count includes every one of them. So the ordered count is $k!$ times too big for the question "which positions", and dividing by $k!$ leaves each set counted once.
:::
:::

::: practice

::: check
given: n = 7 | 9 | 12; k = 2 | 4
answer: n!/(k!*(n - k)!)
tolerance: 0.01%

## What is $\binom{\given{n}}{\given{k}}$?

::: misses
- [n!/(n - k)!] Divide by $k!$.
- [any] $\frac{n!}{k!(n - k)!}$.
:::
:::

::: check
given: n = 3..10 int
answer: n!
tolerance: 0.01%

## In how many orders can {n} different resistors be placed in a row?

::: misses
- [2^n] That's yes-or-no choices; here every order counts.
- [any] $n!$.
:::
:::

::: check
given: n = 8 | 12; p = 0.01 | 0.02
answer: (1 - p)^n
tolerance: 0.1%

## What is the chance that an {n}-bit word arrives with no errors, if each bit flips with probability {p}?

::: misses
- [1 - n*p] That's only an approximation; multiply the chances.
- [any] $(1 - p)^n$.
:::
:::
:::
