Conditional Probability#
Probability describes uncertainty given what we know. When we learn that event has occurred, probabilities of other events should update. The conditional probability of given is:
Think of as the "new universe": we restrict attention to outcomes in , then ask what fraction of those also satisfy .
Worked example — cards. Draw one card from a standard 52-card deck. Let = "card is an Ace", = "card is a spade". Then , but
— interesting: knowing the suit doesn't change the probability of an Ace. This is independence (defined below).
Multiplication rule (rearranging the definition):
This is how we factor joint probabilities into a chain of conditionals.
Worked example — two draws without replacement. Draw two cards from a deck. What is the probability both are Aces? Let = first is Ace, = second is Ace.
After drawing the first Ace, only 3 Aces remain among the 51 remaining cards — conditioning captures this.
Total Probability#
The law of total probability partitions the sample space and weights conditional probabilities:
If form a partition of (mutually exclusive and exhaustive), then
This is the weighted average of conditional probabilities, where the weights are the probabilities of the conditioning events.
Worked example — factory lines. A factory has three production lines. Line 1 produces 50% of units with a 1% defect rate. Line 2 produces 30% at 2%. Line 3 produces 20% at 5%. If we pick a unit at random, what is ?
So 2.1% of all units are defective. The law of total probability will combine with Bayes' rule in Week 5 for the reverse inference: given a defective unit, which line did it come from?
Independence#
Events and are independent if learning about one gives no information about the other:
Equivalently: (when ), and (when ).
Worked example — coin and die. Toss a fair coin and roll a fair die. Let = "coin is Heads", = "die shows 6". Since the outcomes occur independently:
Pairwise vs mutual independence. For three events, pairwise independence (, , ) does not guarantee mutual independence. Here is a classic counterexample:
Toss two fair coins. Let = first coin heads, = second coin heads, = both coins match (both heads or both tails). Then . Check pairwise:
- — independent.
- — independent.
- — independent.
But , while . The events are pairwise independent but not mutually independent. Mutual independence requires and the pairwise conditions.
Chain Rule#
The chain rule (also called the product rule) factors a joint probability over variables into a product of conditionals:
Each step conditions on all previous variables. This is always valid — no independence assumptions are needed. The order of variables can be chosen arbitrarily; different orderings give different but equivalent factorisations.
Worked example — three draws. Draw three cards without replacement from a standard deck. Probability all three are spades:
The chain rule is the backbone of autoregressive models in ML (e.g., language models predict token given tokens ) and of Bayesian networks (Week 13), where conditional independence prunes the conditioning sets.
Conditional Independence#
and are conditionally independent given (written ) if, once you know , learning gives no additional information about :
Equivalently: .
Conditional independence is different from (marginal) independence. Variables can be:
- Marginally dependent but conditionally independent.
- Marginally independent but conditionally dependent.
Story — Naive Bayes preview. In document classification, the words in a document are not independent (seeing "gradient" increases the probability of seeing "descent"). But if we know the document topic (say, "machine learning"), the appearance of individual words becomes approximately independent: .
This is the naive Bayes assumption: features are conditionally independent given the class label. It is "naive" because it is rarely exactly true, yet it works surprisingly well in practice because the probability rankings of classes are often correct even when the absolute probabilities are off.
Why this matters for graphical models (Week 13): conditional independence statements are compactly encoded by graph structure. A missing edge between two nodes means they are conditionally independent given their neighbours.
From a two-way table: P(A ∩ B)=0.15, P(B)=0.3. Compute P(A | B) as a decimal.
Knowledge check#
The definition of conditional probability P(A | B) is:
Browser lab#
Simulate pairs of variables and compute empirical conditional probabilities, comparing to the true conditional table.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
n = 10000
p_x = np.array([0.3, 0.7])
p_y_given_x = np.array([[0.2, 0.5, 0.3],
[0.6, 0.3, 0.1]])
xs = rng.choice([0, 1], size=n, p=p_x)
ys = np.array([rng.choice([0, 1, 2], p=p_y_given_x[x]) for x in xs])
joint_emp = np.zeros((2, 3))
for xv in range(2):
for yv in range(3):
joint_emp[xv, yv] = np.mean((xs == xv) & (ys == yv))
p_y_given_x_emp = joint_emp / joint_emp.sum(axis=1, keepdims=True)
print(" True P(Y | X):")
print(p_y_given_x)
print(" Empirical P(Y | X):")
print(np.round(p_y_given_x_emp, 4))
print(f"\n P(Y=0): true={np.sum(p_y_given_x * p_x[:, None], axis=0)[0]:.3f}")
print(f" emp={np.mean(ys == 0):.3f}")
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
for xv, ax, label in zip([0, 1], axes, ["X=0", "X=1"]):
mask = xs == xv
ax.hist(ys[mask], bins=np.arange(-0.5, 3.5, 1), density=True,
alpha=0.5, label="P(Y | X) empirical")
ax.bar([0, 1, 2], p_y_given_x[xv], alpha=0.4, label="P(Y | X) true")
ax.set_title(f"Conditional distribution of Y given {label}")
ax.set_xlabel("Y"); ax.set_ylabel("Probability")
ax.legend()
plt.tight_layout()
plt.show()