Sub topic 1
Refresher: Conditional Probability
Naive Bayes rests on one idea: updating the probability of an event once you know something else happened. This chapter reviews that idea and the small set of rules built on it. Later chapters use these rules to derive the classifier.
Prerequisites
This chapter assumes you know the following:
- Basic probability: an event is a set of outcomes, and its probability is a number between 0 and 1.
- The idea of a joint probability, written P(A, B) or P(A ∩ B), the probability that both A and B happen.
- Supervised classification vocabulary: features, labels, and a train/test split.
- Basic Python. You do not need any library for the examples here.
If any of these is shaky, read the first section slowly. Everything after it builds on it.
Conditional probability
The conditional probability of A given B is the probability that A happens when you already know B happened. It is written P(A | B). The definition is:
P(A | B) = P(A, B) / P(B), defined only when P(B) > 0.
Read it as a restriction of the world. Knowing B happened removes every outcome outside B. Among the outcomes that remain, you ask what fraction also belong to A.
In classification, A is usually a class label and B is something you observed about an example. For instance, P(spam | contains "free") is the probability that an email is spam, given that it contains the word "free".
A worked example with counts
Suppose a mailbox holds 100 emails. You count them:
| Row | Contains "free" | No "free" | Total |
|---|---|---|---|
| Spam | 30 | 10 | 40 |
| Ham (not spam) | 6 | 54 | 60 |
| Total | 36 | 64 | 100 |
From the table:
- P(spam) = 40/100 = 0.40
- P(free) = 36/100 = 0.36
- P(spam, free) = 30/100 = 0.30
- P(spam | free) = 0.30 / 0.36 ≈ 0.833
- P(free | spam) = 0.30 / 0.40 = 0.75
The same count of 30 spam emails with "free" appears in both conditional probabilities. Only the denominator changes, because the group you condition on changes. P(spam | free) divides by the 36 emails that contain "free". P(free | spam) divides by the 40 spam emails.
The direction matters
P(A | B) and P(B | A) are different quantities. Here, 75% of spam contains "free", but about 83% of emails containing "free" are spam. These numbers happen to be close. They can be very far apart. If spam is rare, most emails with "free" can be ham even when almost all spam contains "free".
Confusing the two directions is the most common error with conditional probability. Naive Bayes exists because it is easy to estimate P(word | class) from labeled data, while the quantity you want is P(class | words). Bayes' theorem converts one into the other, and the next chapter covers it.
The product rule
Rearranging the definition gives the product rule:
P(A, B) = P(A | B) · P(B)
By symmetry, P(A, B) = P(B | A) · P(A). The joint probability can be factored in either order.
Check it with the table: P(spam, free) = P(free | spam) · P(spam) = 0.75 × 0.40 = 0.30. This matches the direct count.
For more than two events, the rule chains:
P(A, B, C) = P(A) · P(B | A) · P(C | A, B)
Each factor conditions on everything that came before it. This is the chain rule of probability. A document with many words has a joint probability that expands into a long chain of this kind. The chain becomes impractical to estimate, and that problem motivates the independence assumption later in the tutorial.
Independence
Events A and B are independent if knowing one tells you nothing about the other:
P(A | B) = P(A)
Equivalently, P(A, B) = P(A) · P(B). In the mailbox table, "free" and spam are not independent: P(spam) is 0.40, but P(spam | free) is about 0.83. Knowing the word is present changes the probability.
Conditional independence
Independence can also hold inside a restricted world. A and B are conditionally independent given C if:
P(A, B | C) = P(A | C) · P(B | C)
Once you know C, learning B adds no information about A. The events can still be dependent overall.
Here is a concrete case. Among all emails, the words "free" and "prize" tend to appear together, so they are dependent. Suppose that among spam emails alone, the two words occur independently of each other. Then they are conditionally independent given the class "spam". The dependence in the full data came entirely from the shared cause, which is the class.
This is the assumption that gives naive Bayes its name. A later chapter develops it fully. For now, note the difference in wording: "independent" has no condition, and "conditionally independent given C" holds only after you fix C.
The law of total probability
Sometimes you know the probability of an event within each group but need its overall probability. If the classes C₁, …, C_k are mutually exclusive and cover all possibilities, then:
P(B) = Σᵢ P(B | Cᵢ) · P(Cᵢ)
This is the law of total probability. In the mailbox:
P(free) = P(free | spam) · P(spam) + P(free | ham) · P(ham) = 0.75 × 0.40 + (6/60) × 0.60 = 0.30 + 0.06 = 0.36
This matches the direct count of 36/100. The same sum will appear later as the denominator of Bayes' theorem, where it normalizes the class scores so they add up to 1.
Computing conditional probabilities in code
The following program computes the quantities above from raw counts. It uses only the standard library.
# Counts from the mailbox table: (label, has_free) -> number of emails
counts = {
("spam", True): 30,
("spam", False): 10,
("ham", True): 6,
("ham", False): 54,
}
total = sum(counts.values())
def p_joint(label, has_free):
return counts[(label, has_free)] / total
def p_label(label):
return sum(n for (lab, _), n in counts.items() if lab == label) / total
def p_free(has_free=True):
return sum(n for (_, f), n in counts.items() if f == has_free) / total
p_spam_given_free = p_joint("spam", True) / p_free(True)
p_free_given_spam = p_joint("spam", True) / p_label("spam")
print(f"P(spam | free) = {p_spam_given_free:.3f}")
print(f"P(free | spam) = {p_free_given_spam:.3f}")
# Product rule check: P(spam, free) = P(free | spam) * P(spam)
print(abs(p_joint("spam", True) - p_free_given_spam * p_label("spam")) < 1e-12)
# Law of total probability check
p_free_total = sum(
(counts[(lab, True)] / sum(n for (l, _), n in counts.items() if l == lab))
* p_label(lab)
for lab in ("spam", "ham")
)
print(f"P(free) by total probability = {p_free_total:.3f}")The output is:
P(spam | free) = 0.833
P(free | spam) = 0.750
True
P(free) by total probability = 0.360Common errors
- Swapping the direction. Writing P(A | B) when you computed P(B | A). Always ask which group forms the denominator.
- Conditioning on an impossible event. P(A | B) is undefined when P(B) = 0. This will matter again when a word never appears in a class during training.
- Assuming independence without checking. P(A, B) = P(A) · P(B) holds only for independent events. Multiplying probabilities of dependent events gives the wrong joint probability.
- Mixing up independence and conditional independence. Two events can be dependent overall yet independent given a third, and the reverse can also happen.
Where this fits
The diagram shows how the rules in this chapter feed the naive Bayes derivation.
Exercises
Exercise
In the mailbox table, 6 of the 60 ham emails contain "free". Compute P(free | ham) and P(ham | free). Explain why they differ.
Show answer
P(free | ham) = 6/60 = 0.10. P(ham | free) = 6/36 ≈ 0.167. They differ because the denominators are different groups: the 60 ham emails versus the 36 emails containing "free". The numerator is the same count of 6.
Exercise
Suppose 1% of emails are spam, 90% of spam contains "free", and 5% of ham contains "free". Use the law of total probability to find P(free).
Show answer
P(free) = 0.90 × 0.01 + 0.05 × 0.99 = 0.009 + 0.0495 = 0.0585. About 5.85% of all emails contain "free".
Exercise
Two words are dependent across all emails, but independent among spam emails and independent among ham emails. What is the name of this relationship, and what is the likely source of the overall dependence?
Show answer
They are conditionally independent given the class. The overall dependence comes from the class label: both words are more common in one class, so they tend to appear together when the class is not fixed.