Lesson 4 of 15

Conditional Probability

Updating on Evidence

Conditional probability P(AB)P(A \mid B) is the probability of AA given that BB has occurred:

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

Conditioning on BB reduces the sample space to only outcomes where BB holds.

# Roll two dice. Given the sum > 7, what is P(first die = 6)?
# Outcomes where sum > 7: (2,6),(3,5),(3,6),(4,4),(4,5),(4,6),(5,3)...(6,6) → 15 outcomes
# Of these, first die = 6: (6,2),(6,3),(6,4),(6,5),(6,6) → 5 outcomes
# P(first=6 | sum>7) = 5/15 = 1/3

p_ab = 5/36   # P(first=6 AND sum>7)
p_b  = 15/36  # P(sum > 7)
print(round(p_ab / p_b, 4))  # 0.3333

Chain Rule

P(AB)=P(AB)P(B)=P(BA)P(A)P(A \cap B) = P(A \mid B) \cdot P(B) = P(B \mid A) \cdot P(A)

This extends to any number of events: P(A1A2An)=P(A1)P(A2A1)P(A3A1,A2)P(A_1 \cap A_2 \cap \cdots \cap A_n) = P(A_1) \cdot P(A_2 \mid A_1) \cdot P(A_3 \mid A_1, A_2) \cdots

Your Task

Implement conditional(p_ab, p_b) that returns P(AB)=P(AB)/P(B)P(A \mid B) = P(A \cap B) / P(B), rounded to 4 decimal places.

Pyodide loading...
Loading...
Click "Run" to execute your code.