liveBook · Manning

Combination without replacement

As explained in Statistics Every Programmer Needs

A combination without replacement is a selection in which order does not matter and selected items cannot be selected again.

A combination without replacement selects items from a set when each item can be chosen at most once and order does not matter. Lottery numbers are a typical case: drawing the same numbers in a different order does not produce a different selection. First check both conditions. If order matters, count permutations instead; if items can be selected again, use combinations with replacement. section 2.2.3 section 2.5

For n distinct items and r selections, start with n!/(n − r)! permutations without replacement. Each unordered selection appears r! times among those ordered arrangements, so divide by r!: C(n, r) = n!/[r!(n − r)!]. With five items and three selections, there are 60 ordered arrangements but only 60/6 = 10 combinations. section 2.2.3

>>> n = 5
>>> r = 3

>>> combinations = (math.factorial(n) / \
>>>                 (math.factorial(r) * math.factorial(n - r)))
>>> print(f'Number of combinations without replacement: '
>>>       f'{combinations}')
Number of combinations without replacement: 10.0

>>> combinations = math.comb(n, r)
>>> print(f'Number of combinations without replacement: '
>>>       f'{combinations}')
Number of combinations without replacement: 10

The Python listing calculates the five-choose-three example using factorials and math.comb(). Both give 10; the factorial expression displays 10.0 because it uses division. section 2.2.3

Exactly two heads in five flips

Choose which two of the five flip positions contain heads. Their order within that choice does not matter, and a position cannot be chosen twice, so math.comb(5, 2) gives 10 favorable sequences. Five independent coin flips have 32 possible sequences; the probability is 10/32, or about 31%. The combination counts favorable outcomes, not the total sequences. section 3.2.3

Figure 2.1 The top of Pascal’s triangle. The triangle is constructed from the top down, where numbers are derived by adding the two numbers immediately above. It so happens that the sum of numbers for each row is twice the sum of numbers from the row above. The number of combinations without replacement can be found at the intersection of the (n + 1)th row and the (r + 1)th position in that row. When n equals 5 (sixth row down) and r equals 3 (fourth position from the left), the number of combinations without replacement equals 10.
figure

Pascal’s triangle provides another way to read the count: for n = 5 and r = 3, the sixth row’s fourth position is 10. section 2.2.3

How Statistics Every Programmer Needs covers it

How deep it goes
Derives the factorial rule by removing order from permutations, then calculates a small case with Python and Pascal’s triangle.
Examples
Uses lottery numbers, five-choose-three calculations, Pascal’s triangle, and the probability of two heads in five flips.
What it assumes
Builds on factorials and permutations without replacement while working through the adjustment needed when order ceases to matter.
What you can do after
Apply it via Python’s math.comb() to count unordered selections and calculate favorable outcomes for a probability.
How it connects
Relates to permutations without replacement, combinations with replacement, factorials, Pascal’s triangle, and probability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.