Lesson 3.10 · Stage 03: The maths behind machine learning
Maths chapter 8: Probability: measuring chance
Probability is a number from 0 (impossible) to 1 (certain) that says how likely something is. ML models rarely say "yes" or "no"; they say "spam with probability 0.93".
Probability is a number from 0 (impossible) to 1 (certain) that says how likely something is. ML models rarely say “yes” or “no”; they say “spam with probability 0.93”.
8.1 The basic rule
When all outcomes are equally likely:
Worked example. Roll a die. P(even) = 3 favourable (2, 4, 6) ÷ 6 total = 1/2.
- Complement: P(not A) = 1 − P(A). P(not a 6) = 1 − 1/6 = 5/6.
- AND, for independent events (one doesn’t affect the other): multiply. Two coins both heads = 1/2 × 1/2 = 1/4.
- OR: P(A or B) = P(A) + P(B) − P(A and B). Subtract the overlap so it isn’t counted twice. From a pack of 52 cards, P(king or heart) = 4/52 + 13/52 − 1/52 = 16/52 = 4/13.
8.2 Counting
- Factorial: n! = n × (n − 1) × … × 1. The number of ways to arrange 4 books on a shelf is 4! = 24.
- Combinations: the number of ways to choose r items from n, when order doesn’t matter.
Choosing 2 students from 5 for a team: 5! ÷ (2! × 3!) = 120 ÷ 12 = 10 ways.
8.3 Conditional probability
P(A | B) means “the probability of A, given that we know B happened”. The bar | reads “given”.
Worked example: a spam filter. You look at 100 emails.
| Contains “free” | No “free” | Total | |
|---|---|---|---|
| Spam | 24 | 6 | 30 |
| Not spam | 7 | 63 | 70 |
| Total | 31 | 69 | 100 |
- P(spam) = 30/100 = 0.30. Before reading the email, 30% chance.
- P(“free” | spam) = 24/30 = 0.80. Most spam says “free”.
- P(spam | “free”) = 24/31 ≈ 0.77. Once you see “free”, the chance of spam jumps to 77%.
Notice that P(A | B) and P(B | A) are different numbers. Mixing them up is the most common probability mistake.
8.4 Bayes’ rule
Bayes’ rule flips a conditional probability around. It updates a belief when new evidence arrives.
Worked example: a medical test. 1% of people have a disease. The test catches 90% of sick people. It also wrongly flags 5% of healthy people. You test positive. How likely is it that you are sick?
- P(sick) = 0.01, P(positive | sick) = 0.90.
- P(positive) = 0.01 × 0.90 + 0.99 × 0.05 = 0.009 + 0.0495 = 0.0585.
- P(sick | positive) = 0.009 ÷ 0.0585 ≈ 0.15, only about 15%.
The disease is rare, so most positives come from the many healthy people. Our gut feeling says 90%; Bayes says 15%.
Why ML needs it: the Naive Bayes classifier is this rule, and Bayesian methods use it to update a model as data comes in.
Try it
- A bag has 3 red and 5 blue balls. What is P(red)?
- Roll two dice. What is the probability the total is 7?
- How many ways can you choose 2 items from 6?
- Of 200 emails, 50 are spam. “Win” appears in 20 spam and 10 non-spam emails. Find P(spam | “win”).
- A disease affects 2% of people. A test catches 95% of sick people and wrongly flags 10% of healthy people. If you test positive, what is P(sick)?