liveBook · Manning

Combination

As explained in Statistics Every Programmer Needs

A combination is a selection of items in which order does not matter.

A combination selects items when their order does not matter. It supports probability calculations and contrasts with a permutation, where arrangement order matters. section 2.2.3 section 2.0

Choose the formula according to whether selected items can be reused:

  • Without replacement: C(n, r) = n! / [r!(n − r)!]. For n = 5 and r = 3, the result is 10. section 2.2.3
  • With replacement: C(n + r − 1, r) = (n + r − 1)! / [r!(n − 1)!]. For n = 5 and r = 3, the result is 35. section 2.2.3
  • Python provides math.comb(n, r) for these integer calculations; factorial expressions expose the underlying formulas. section 2.2.3

Starting lineup and Pascal’s triangle

A basketball starting lineup is a combination because the introduction order does not change the selected group. Pascal’s triangle also gives C(5, 3) = 10 at the sixth row and fourth position. 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

Inspect executable Python for factorial and math.comb implementations of combinations without replacement. section 2.2.3

How Statistics Every Programmer Needs covers it

How deep it goes
Explains order, replacement conditions, formulas, Pascal’s triangle, and Python implementations through worked small examples.
Examples
Uses a basketball lineup, Pascal’s triangle, and Python calculations with n = 5 and r = 3.
What it assumes
Assumes basic probability and counting context while distinguishing replacement from nonreplacement selection.
What you can do after
Apply it via Python’s math module to count order-independent selections with or without replacement.
How it connects
Connects combinations to permutations, replacement, factorials, Pascal’s triangle, probability, and counting rules.

Also explained in

Compare how 3 books explain combination →

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.