TExcelTAD
Home › Probability Theory › Conditional Probability

Conditional Probability

New information changes probabilities. If you know a die roll was even, the chance it is a 6 rises from 1/61/6 to 1/31/3. Conditional probability formalises this update: restrict attention to the outcomes consistent with what you know, and renormalise.

Definition

For events EE and FF with Pr⁡[F]>0\Pr[F] > 0, the probability of EE given FF is

Pr⁡[E∣F]=Pr⁡[E∩F]Pr⁡[F]\Pr[E \mid F] = \frac{\Pr[E \cap F]}{\Pr[F]}

FF becomes the new sample space; within it, outcomes keep their relative weights. For equally likely outcomes, Pr⁡[E∣F]=∣E∩F∣/∣F∣\Pr[E \mid F] = \lvert E \cap F \rvert / \lvert F \rvert.

python · try it yourself
from fractions import Fraction
from itertools import product

omega = list(product(range(1, 7), repeat=2))

def P(event, given=lambda w: True):
    F = [w for w in omega if given(w)]
    return Fraction(sum(1 for w in F if event(w)), len(F))

sum8 = lambda w: w[0] + w[1] == 8
first_is_3 = lambda w: w[0] == 3
double = lambda w: w[0] == w[1]
print("P(sum 8)                 =", P(sum8))
print("P(sum 8 | first die 3)   =", P(sum8, first_is_3))
print("P(sum 8 | double)        =", P(sum8, double))
print("P(double | sum 8)        =", P(double, sum8))
P(sum 8)                 = 5/36
P(sum 8 | first die 3)   = 1/6
P(sum 8 | double)        = 1/6
P(double | sum 8)        = 1/5

Note that Pr⁡[E∣F]\Pr[E \mid F] and Pr⁡[F∣E]\Pr[F \mid E] are different numbers: Pr⁡[sum 8∣double]=1/6\Pr[\text{sum } 8 \mid \text{double}] = 1/6 but Pr⁡[double∣sum 8]=1/5\Pr[\text{double} \mid \text{sum } 8] = 1/5. Confusing the two is the most common error in probability (see Paradoxes).

The multiplication rule and trees

Rearranging the definition gives Pr⁡[E∩F]=Pr⁡[F]⋅Pr⁡[E∣F]\Pr[E \cap F] = \Pr[F] \cdot \Pr[E \mid F], and for a chain of events

Pr⁡[A1∩A2∩⋯∩An]=Pr⁡[A1]⋅Pr⁡[A2∣A1]⋯Pr⁡[An∣A1∩⋯∩An−1]\Pr[A_1 \cap A_2 \cap \cdots \cap A_n] = \Pr[A_1] \cdot \Pr[A_2 \mid A_1] \cdots \Pr[A_n \mid A_1 \cap \cdots \cap A_{n-1}]

This is the logic of tree diagrams: each branch is labelled with a conditional probability, and the probability of a path is the product along it. Drawing two cards without replacement, Pr⁡[both aces]=452⋅351=1221\Pr[\text{both aces}] = \frac{4}{52} \cdot \frac{3}{51} = \frac{1}{221}.

The law of total probability

If B1,…,BkB_1, \ldots, B_k split the sample space into disjoint cases, then

Pr⁡[E]=∑i=1kPr⁡[Bi] Pr⁡[E∣Bi]\Pr[E] = \sum_{i=1}^{k} \Pr[B_i] \, \Pr[E \mid B_i]

Example: 60% of a company's servers run in region A, where a request fails with probability 0.01, and 40% in region B, where it fails with probability 0.03. Overall failure probability: 0.6⋅0.01+0.4⋅0.03=0.0180.6 \cdot 0.01 + 0.4 \cdot 0.03 = 0.018.

python · try it yourself
import random

rng = random.Random(2)
n = 200_000
fails_total = fails_B = in_B = 0
for _ in range(n):
    region_B = rng.random() < 0.4
    fail = rng.random() < (0.03 if region_B else 0.01)
    fails_total += fail
    in_B += region_B
    fails_B += fail and region_B
print("P(fail) ~", round(fails_total / n, 4), " exact 0.018")
print("P(region B | fail) ~", round(fails_B / fails_total, 3), " exact", round(0.012 / 0.018, 3))
P(fail) ~ 0.0179  exact 0.018
P(region B | fail) ~ 0.675  exact 0.667

The second line runs the logic backwards: given a failure, which region was it? That reversal is Bayes' theorem.

In software

Conditional probabilities are what dashboards actually show when they filter: "error rate for mobile users" is Pr⁡[error∣mobile]\Pr[\text{error} \mid \text{mobile}]. Recommendation systems estimate Pr⁡[click∣user,item]\Pr[\text{click} \mid \text{user}, \text{item}], and language models compute Pr⁡[next token∣previous tokens]\Pr[\text{next token} \mid \text{previous tokens}] (Sampling from Language Models).

Exercises

Exercise 1: Two children

A family has two children, and at least one is a girl. What is the probability that both are girls (each child independently a girl with probability 1/2)?

Answer

1/31/3.

Solution

The equally likely cases GG, GB, BG, BB; "at least one girl" leaves GG, GB, BG, of which one is GG.

Exercise 2: Cards in a row

Three cards are drawn without replacement. What is the probability that all three are hearts?

Answer

1352⋅1251⋅1150=11850≈0.0129\frac{13}{52} \cdot \frac{12}{51} \cdot \frac{11}{50} = \frac{11}{850} \approx 0.0129.

Solution

Multiplication rule along one branch of the tree: each factor is the probability of a heart given the previous cards were hearts.

Exercise 3: Total probability (code)

Urn 1 has 3 red and 2 blue balls, urn 2 has 1 red and 4 blue. Pick an urn by a fair coin, then a ball. Find Pr⁡[red]\Pr[\text{red}] exactly and by simulation.

Answer

12⋅35+12⋅15=25\frac{1}{2} \cdot \frac{3}{5} + \frac{1}{2} \cdot \frac{1}{5} = \frac{2}{5}.

Solution
python · try it yourself
import random

rng = random.Random(3)
urns = [["r"] * 3 + ["b"] * 2, ["r"] + ["b"] * 4]
n = 100_000
red = sum(rng.choice(urns[rng.randint(0, 1)]) == "r" for _ in range(n))
print(red / n, 2 / 5)
0.39987 0.4

Test yourself

What is P(E∣F)P(E \mid F) by definition?

  1. P(E)P(F)P(E) P(F)
  2. P(E∩F)/P(F)P(E \cap F) / P(F)
  3. P(E∩F)/P(E)P(E \cap F) / P(E)
  4. P(F∣E)P(F \mid E)

If P(E∣F)=0.9P(E \mid F) = 0.9, then P(F∣E)P(F \mid E) is…

  1. 0.9
  2. 0.1
  3. not determined without more information
  4. 1