AI Hints
Statistics & Math for ML · Lesson 34 of 138

Counting: Permutations

When order matters.

⏱ 2 min read▶ Video at 38:18🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

Many probability questions come down to counting. How many ways can five runners finish first, second, and third? When the order matters, we're counting permutations.

Counting: Permutations
From the video: Counting: Permutations
  • Probability often means counting
  • First, second, third
  • Order matters: permutations

Arrangements

Three letters, A, B, and C, can be arranged in six ways. Iter tools lists them all. Five runners can finish in five factorial ways, which is a hundred and twenty. And math dot perm counts ways to fill just the top three places. Run it. Sixty ways.

perms.pypython
import math
from itertools import permutations

print(["".join(p) for p in permutations("ABC")])
print("5 runners, all places:", math.factorial(5))
print("5 runners, top 3:", math.perm(5, 3))
Output
['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
5 runners, all places: 120
5 runners, top 3: 60

Why it multiplies

For first place, there are five choices. Then four runners are left for second, and three for third. So five times four times three, which is sixty. Factorial, written with an exclamation mark, multiplies all the way down to one. Five factorial is five times four times three times two times one.

keyvalue
Top 3 of 55 × 4 × 3 = 60
n!n × (n−1) × … × 1
math.factorialall arrangements
math.perm(n, k)ordered picks

Summary

Permutations count arrangements where order matters. Multiply the choices at each step. Next, combinations, when order doesn't matter.

Key takeaways
  • permutations()
  • math.factorial
  • math.perm(n, k)
Next up: Combinations — when order doesn't