Probability, lesson 3
Conditional probability
How the odds change once you know something.
A chance changes when you learn something, even though nothing about the dice or the cards has changed. This lesson is about updating a chance on news. You need the last lesson's rule: a chance is a share of equally likely outcomes.
By the end, you can:
Part 1. The chance given what you know
Update a chance on news by counting what's left.
Roll a red die and a blue die. The chance of a total of 7 is 6/36 = 1/6. Now someone peeks and tells you at least one die shows a 6. Every square where that's false can't be the roll any more. Cross those out, and count again among the squares left.
Example 1.1
A total of 7, given at least one 6
Roll a red die and a blue die. You learn that at least one of them shows a 6. What's the chance the total is 7?
- Keep the squares the news allows6 + 6 − 1 = 11
A 6 on the red die is 6 squares, a 6 on the blue die 6 more. Six and six is in both, so take 1 away.
- Count the ones you want2
1 + 6 and 6 + 1 are the only squares left that total 7.
- Divide by what's left2 / 11
The squares are still equally likely. Only the bottom of the fraction changed, from 36 to 11.
Answer2/11, about 18%, up from 1/6.
Why is this right? The news doesn't change the dice. It only tells you which squares are still possible. Those 11 were equally likely before the news, so they are equally likely after it, and the chance is a share of them.
Conditional probability
P(A | B), read "the probability of A given B", is the chance of A once you know B happened. Count A inside B. With chances instead of counts, divide. For the dice, (2/36) / (11/36) = 2/11.
P(A | B) = P(A and B) / P(B)
The common trap is swapping the two sides. Every lottery winner bought a ticket, so P(bought a ticket | won) = 1. Almost no ticket buyer wins, so P(won | bought a ticket) is tiny. The same two events give very different chances, because the bottoms of the fractions differ.
Your turn
A family has two children, and you learn at least one is a girl. What's the chance both are girls? (Each child is a girl or a boy with chance 1/2, independently.)
Part 2. One card, then another
Multiply chances for cards drawn one after another.
Cards are where this matters most, because every card you draw changes the deck. Draw two cards without putting the first back, and the second card's chances depend on what the first one was. So take the cards in order, and condition each one on the cards before it.
Example 2.1
Two aces in a row
Draw two cards from a shuffled deck, without putting the first back. What's the chance both are aces?
- First card4/52
4 aces among 52 cards.
- Second card, given the first3/51
After an ace, 3 aces are left among 51 cards.
- Multiply4/52 × 3/51 = 12/2,652 = 1/221
Of the deals that start with an ace, 3/51 go on to a second ace.
Answer1/221, about 0.45%.
Why multiply? Picture many deals. The first card is an ace in 4/52 of them, and of those, the second is an ace 3/51 of the time. A share of a share is a product. Counting agrees: C(4, 2) = 6 pairs of aces among C(52, 2) = 1,326 pairs, and 6/1,326 = 1/221.
The multiplication rule for chances
The chance of both is the chance of the first, times the chance of the second given the first. It's the formula for P(B | A), turned around:
P(A and B) = P(A) × P(B | A)
Leave out the condition and you'd write 4/52 × 4/52 = 1/169, as if the first ace went back in the deck. That's right when you replace the card. Without replacement, the second card comes from what the first one left behind.
The rule follows the cards as they come, and counting looks at the finished hand. Both give 1/221. When one way gets messy, try the other.
Your turn
Draw two cards from a shuffled deck, without putting the first back. What's the chance both are hearts?
Part 3. Splitting into cases
Find a chance by splitting on what you don't know.
Guess first
Shuffle a deck and deal two cards face down. What's the chance the second one is an ace?
It's tempting to want the first card before you answer. You don't need it. The first card was either an ace or not, never both, so split on it and work out each case.
Example 3.1
The second card
Deal two cards face down from a shuffled deck. What's the chance the second one is an ace?
- First card an ace4/52 × 3/51 = 12/2,652
Then 3 aces are left among 51 cards.
- First card not an ace48/52 × 4/51 = 192/2,652
Then all 4 aces are left among 51 cards.
- Add the cases12/2,652 + 192/2,652 = 204/2,652
The cases can't both happen, and one of them must, so their chances add.
- Simplify204/2,652 = 1/13
Exactly 4/52, the same as for the first card.
Answer1/13, the same as for the first card.
The law of total probability
Split on what you don't know, into cases that can't overlap and that together cover everything. The chance of A is its chance in each case, weighted by how likely that case is:
P(A) = P(case 1) × P(A | case 1) + P(case 2) × P(A | case 2)
There's also a shortcut. Before anyone looks, nothing makes the second place in a shuffled deck different from the first, so each is an ace with chance 4/52. The trap is reasoning as if you knew the first card. A condition changes a chance only once you've learned it.
Your turn
A strategy trades every day. 80% of days are calm, and it loses money on 30% of those. 20% are volatile, and it loses on 60% of those. On what share of days does it lose?
Where you'll meet it
Cards drawn without replacement, "given at least one" questions and the two-child puzzle come up often in quant interviews. Write down the equally likely outcomes, cross out the ones the news rules out, and count what's left. Say what you're conditioning on before you compute. Many wrong answers are right answers to a slightly different question.
Testing a trading signal is conditional probability. Say a stock rose on 520 of the last 1,000 days, so P(up) = 52%. A signal fired on 200 of those days, and the next day was up 116 times: P(up next day | signal) = 116 / 200 = 58%. If the two numbers matched, the signal would tell you nothing.
from fractions import Fraction as F
from itertools import product
def chance(outcomes, event, given=lambda o: True):
left = [o for o in outcomes if given(o)] # cross out what the news rules out
return F(sum(1 for o in left if event(o)), len(left))
dice = list(product(range(1, 7), repeat=2))
seven, six = (lambda s: sum(s) == 7), (lambda s: 6 in s)
print(chance(dice, seven, given=six), chance(dice, six, given=seven)) # 2/11 1/3
kids = list(product("GB", repeat=2))
print(chance(kids, lambda k: k == ("G", "G"), given=lambda k: "G" in k)) # 1/3
print(F(4, 52) * F(3, 51), F(13, 52) * F(12, 51)) # 1/221 1/17
print(F(4, 52) * F(3, 51) + F(48, 52) * F(4, 51)) # 1/13chance does by brute force what the lesson does by hand: keep the outcomes that fit the news, then count. It's a quick way to settle a tricky wording on a small case.
Check yourself
Four questions from across the lesson. Try each one before you look back.
1
Which is bigger: the chance of at least one 6 given the total is 7, or the chance the total is 7 given at least one 6?
2
Box 1 holds 1 red ball and 1 blue. Box 2 holds 3 red and 1 blue. Flip a coin to pick a box, then take a ball from it. What's the chance it's red?
3
Draw two cards from a shuffled deck, without putting the first back. What's the chance at least one is an ace?
4
Roll a red die and a blue die. You learn the red one shows 3. What's the chance the total is 7?
What to remember
1The chance given what you know
News crosses out outcomes, so count what's left: given at least one 6, a total of 7 is 2 of the 11 squares left, or 2/11.
1The chance given what you know
P(A | B) and P(B | A) share a top and have different bottoms: for the same 2 squares, 2/11 against 1/3.
2One card, then another
For draws one after another, multiply each chance given the ones before: two aces is 4/52 × 3/51 = 1/221.
3Splitting into cases
Split on what you don't know, weight each case by its chance and add: the second card is an ace with chance 12/2,652 + 192/2,652 = 1/13.
Probability overviewSpotted a mistake? Tell us.