Sub topic 1
Counting Refresher: Permutations and Combinations
Prerequisites
This chapter assumes you can simplify fractions, work with basic algebra, and recognize factorials such as 5! = 120. You should also know a standard die has six faces and a deck has 52 cards.
Counting Refresher
A permutation counts ordered arrangements. A combination counts unordered selections. Choosing 2 people from 5 for a President and Secretary is a permutation: 5 × 4 = 20. Choosing 2 people for a committee is a combination: 10 ways, because order does not matter.
from math import comb, perm
print(perm(5, 2)) # 20 ordered picks
print(comb(5, 2)) # 10 unordered picksSample Space and Events
The sample space is the set of all possible outcomes. The event is the subset you care about. For two dice, the sample space has 36 outcomes. The event "sum is 7" contains 6 of them: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1).
Classical Probability: Favourable over Total
When every outcome is equally likely, the probability of an event is:
P(event) = favourable outcomes / total outcomes
For the dice event above, P(sum = 7) = 6/36 = 1/6.
from itertools import product
from fractions import Fraction
outcomes = list(product(range(1, 7), repeat=2))
favourable = [o for o in outcomes if sum(o) == 7]
print(Fraction(len(favourable), len(outcomes))) # 1/6The Complement Rule and "At Least One"
The phrase "at least one" usually means many cases. The complement of an event is "it does not happen." Then:
P(at least one) = 1 − P(none)
Consider rolling a fair die 3 times. The chance of at least one six is 1 minus the chance of no sixes, which is (5/6)³ = 125/216. So P(at least one six) = 91/216.
from fractions import Fraction
print(1 - Fraction(5, 6) ** 3) # 91/216Exercise
A fair coin is tossed 4 times. What is the probability of at least one head?
Show answer
The complement is no heads, which has probability (1/2)^4 = 1/16. So P(at least one head) = 1 − 1/16 = 15/16.