Skip to content
DoingCamp CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

Lesson 3 of 20 · about 15 min

Conditional probability

New information changes the odds. A friend peeks at two dice and says "there's at least one six". What's the chance of double six now? You'll watch the grid shrink as you learn things, discover why P(A given B) and P(B given A) can be wildly different, and build the rule behind every "people who bought this also bought" feature.

🧭 What you need first: The rules of probability

The story

Your friend rolls two dice behind a book, peeks, and says: “I can tell you there’s at least one six.” What’s the chance it’s a double six?

Before she spoke, double six had a 136\frac{1}{36} chance. Now it must be higher: you’ve learned something, and the boring outcomes with no six are gone. But how much higher? Many people say 16\frac{1}{6} (“one die is a six, the other has a 1 in 6 chance”). The real answer is smaller. You’ll find it on the grid in a moment.

This is conditional probability: updating chances when you learn something. It’s how a spam filter reacts to the word “lottery”, how your phone suggests the next word, and how doctors read test results (that one is lesson 5).

Another way: a guessing game

“I’m thinking of someone in this school.” Your chance of guessing right is tiny. “They’re in grade 6.” Better. “They play the trumpet.” Now only three people fit. Each clue throws out everyone who doesn’t match, and the chances get spread over who’s left.

Play with it

Shrink the world

Same 36-square grid as last lesson, but now you’re told something first. Squares that can’t have happened get crossed out. Pick the outcomes asked for and watch the fraction: it’s out of the squares still in play.

🎯 1/4: You know the red die shows 6. Pick the outcomes where the sum is 7.

Each square is one way the two dice can land: red die first, blue die second. All 36 are equally likely. Tap squares to pick them (tap again to unpick). We already know the red die shows 6, so the crossed-out squares can't happen any more.

123456
1
2
3
4
5
6

rows: red die · columns: blue die

P(picked | given) = 0/6

Only 6 outcomes are still possible, so they become the whole world: the fraction is out of 6, not 36.

The friend’s hint leaves 11 possible squares, and only one of them is (6, 6), so the answer is 111≈9%\frac{1}{11} \approx 9\%, not 16\frac{1}{6}. “At least one six” doesn’t tell you which die is the six, so there are more ways to have exactly one six than you’d guess.

And the last two challenges show something important: given an even sum, a double has chance 618=13\frac{6}{18} = \frac{1}{3}. Given a double, an even sum is certain (66=1\frac{6}{6} = 1). Flipping the condition changed the answer completely.

Name it

The real words

  • Conditional probability P(A | B) is the probability of AA given that BB happened. The vertical bar is read “given”.

    P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}

    (This needs P(B)>0P(B) > 0: you can’t condition on something impossible.)

    • Why: BB is the new world, so only the part of AA inside BB counts (A∩BA \cap B), and we divide by the size of the new world, P(B)P(B), so that the new world adds up to 1 again.
    • With equally likely outcomes this is just counting: ∣A∩B∣∣B∣=111\frac{|A \cap B|}{|B|} = \frac{1}{11} for the friend’s hint.
  • Multiplication rule: multiply both sides by P(B)P(B):

    P(A∩B)=P(B)⋅P(A∣B)P(A \cap B) = P(B) \cdot P(A \mid B)
    • Example: draw 2 cards without putting the first back. P(both aces)=452⋅351=1221P(\text{both aces}) = \frac{4}{52} \cdot \frac{3}{51} = \frac{1}{221}: the second fraction is “ace given the first was an ace”, with 3 aces left in 51 cards.

    • It chains, one event at a time:

      P(A∩B∩C)=P(A)⋅P(B∣A)⋅P(C∣A∩B)\begin{aligned} &P(A \cap B \cap C) \\ &= P(A) \cdot P(B \mid A) \cdot P(C \mid A \cap B) \end{aligned}

      Drawing pictures of these chains gives a probability tree (lesson 6).

  • Don’t flip the bar: P(A∣B)≠P(B∣A)P(A \mid B) \ne P(B \mid A) in general. Almost every professional basketball player is tall, but very few tall people are professional basketball players. Mixing these two up is so common it has a name in courtrooms, the prosecutor’s fallacy, and lesson 5 shows how to get from one to the other.

Build it

Conditional probability by counting

Write cond(a, b): P(A | B) for two events given as sets of outcomes, where every outcome is equally likely. Return a Fraction: the outcomes in both, divided by the outcomes in B.

Challenge

Two cards, no putting back

A deck has 52 cards: 13 ranks × 4 suits, with 4 aces. Make draws, the list of all ordered (first, second) pairs of two different cards. Then use counting to write p_second_ace_given_first_ace() and p_both_aces(), both as Fractions. Finally write p_both_aces_rule() using the multiplication rule instead of counting.

Remember it

99% of spam emails contain a link. Does that mean an email with a link is 99% likely to be spam?

Got it?

Finish the lesson to color it in on your map and collect its concept cards.

Did this lesson click?