The maths you need · lesson 9 of 12
Parts in this lesson
Amask.mean()the fraction of True values: a probability
B(a & b).mean() / b.mean()a conditional probability
Ctrue_pos / (true_pos + false_pos)Bayes’ rule, for a medical test

Probability

Last updated

A classifier doesn’t just say “Gentoo”; it says “Gentoo, with probability 0.92”. To read that number, and to build models that produce it, you need the language of probability: events, joint and conditional probability, independence, and Bayes’ rule, which turns one conditional probability round into the other. This lesson works all of them out by counting real penguins, then meets the most famous trap in the subject.

By the end of this lesson you can

  • Work out probabilities as fractions of a count, with pandas
  • Tell joint probability from conditional probability, and p(A∣B) from p(B∣A)
  • Check whether two events are independent
  • Use Bayes’ rule, and explain why a rare condition makes most positive tests wrong

Events and probability

An event is something that's either true or false for each case: "this penguin is a Gentoo", "this penguin lives on Biscoe". Its probability, written p(A), is a number from 0 (never) to 1 (always). When every case is equally likely to be picked, it's just a fraction:

p(A)=number of cases where A is truenumber of cases

In pandas an event is a Boolean mask (lesson 0.9), and the mean of a mask is the fraction of Trues, because True counts as 1 and False as 0. So mask.mean() is a probability. Here are the 344 penguins:

penguins.ipynb

Pick a penguin at random and p(Gentoo)=124/344≈0.36, p(Biscoe)=168/344≈0.488.

Joint and conditional

Two kinds of "and" and "given" matter.

The joint probability p(A,B) is the chance that both are true: a Gentoo and from Biscoe. That's (gentoo & biscoe).mean(), 0.36 above. (Every Gentoo in the data lives on Biscoe, so it equals p(Gentoo).)

The conditional probability p(A∣B), said "p of A given B", is the chance of A when you already know B is true. You only look at the cases where B holds:

p(A∣B)=p(A,B)p(B)

In words: of the cases where B is true, the fraction where A is true as well. In pandas: (a & b).mean() / b.mean().

penguins.ipynb

Notice the two are very different. p(Gentoo∣Biscoe)=0.738: about three in four Biscoe penguins are Gentoos. p(Biscoe∣Gentoo)=1.0: every Gentoo is from Biscoe. Swapping the two sides of the bar changes the question, and usually the answer.

A classifier's output is a conditional probability: p(Gentoo∣these measurements). Stage 3's logistic regression is built to produce exactly that.

Independence

Two events are independent when knowing one tells you nothing about the other. Then the joint probability is just the product:

p(A,B)=p(A)p(B)

In words: if they don't affect each other, the chance of both is one chance times the other. Equivalently, p(A∣B)=p(A): knowing B doesn't change the odds of A.

Species and island are clearly not independent:

penguins.ipynb

If they were, p(Gentoo,Biscoe) would be 0.36×0.488≈0.176. It's 0.36, twice as much. Independence is a big assumption that models often make on purpose to keep things simple; the naive Bayes classifier (Stage 4) assumes every feature is independent of the others, given the class.

Bayes’ rule: a medical test

Bayes' rule turns a conditional probability round: from p(B∣A) to p(A∣B).

p(A∣B)=p(B∣A)p(A)p(B)

In words: the chance of A given B is the chance of B given A, times how common A was to begin with, divided by how common B is overall. It comes straight from step 2's formula written both ways round.

The classic use is a medical test. You know:

  • the prevalence p(sick): how common the condition is;
  • the sensitivity p(+∣sick): how often the test catches someone who has it;
  • the specificity p(−∣well): how often it clears someone who doesn't.

You want p(sick∣+): the chance you're sick, given a positive test. The bottom of the fraction, p(+), counts every way to test positive: sick and caught, or well and wrongly flagged. The box below counts it for 1,000 people:

Bayes box1,000 people
has it, tests positive (10)has it, tests negative (0)doesn’t, tests positive (50)doesn’t, tests negative (940)

60 people test positive: 10 who have the condition and 50 who don’t. So if you test positive, the chance you have it is 10 ÷ 60 = 16.7%. The false positives outnumber the true ones, because there are so many more well people to make mistakes on.

The same calculation in code, for a condition 1 in 1,000 people have and a test that's 99% right both ways:

bayes.ipynb

Wrong way: 99% accurate, 9% likely

A test that's right 99% of the time comes back positive. The tempting answer is "99% chance I'm sick". The notebook says 9%. Set the box to 1 in 1,000, with sensitivity and specificity both 99%, and count: about 1 sick person tests positive, but about 10 of the 999 well people do too. So only 1 positive in 11 is real.

The mistake is mixing up p(+∣sick), which is 0.99, with p(sick∣+), which is what you want: swapping the sides of the bar from step 2. It's called the base-rate mistake, because it ignores how rare the condition is (its base rate). When the condition is common, 1 in 10, the same test gives 0.9167 (second cell in step 4).

If the formula feels slippery, count. Simulate a million people and look:

simulate.ipynb

0.0894, close to the formula's 0.0902 (a random sample never lands exactly). The same mistake turns up in machine learning as class imbalance (lesson 2.8): a fraud detector that's "99% accurate" on data where 1 payment in 1,000 is fraud can still be wrong about most of the payments it flags.

Check what you learned

In a class of 40, 10 students play chess. What is p(chess) for a student picked at random?
p(A,B)=0.2 and p(B)=0.5. What is p(A∣B)?
Predict, then check with the Bayes box or p_sick_given_positive: a condition affects 1 in 100 people, and the test is 99% right both ways. Roughly what is the chance you’re sick after a positive test?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning