Sub topic 4
The naive assumption: features independent given the class
This chapter assumes you know joint and conditional probability, the product rule, and Bayes' theorem as a way to turn P(x | y) into P(y | x). It also assumes basic NumPy. Here you will learn what the naive assumption says, why the model needs it, and how to tell when it holds.
The problem the assumption solves
A generative classifier needs P(x | y), the probability of seeing a whole feature vector x = (x1, ..., xd) given class y. Estimating that distribution directly is the hard part.
Suppose each of d features is binary. A full joint distribution over the features has 2^d outcomes for each class, so you need 2^d − 1 free parameters per class. With K classes:
| Model of P(x \ | y) | Free likelihood parameters | d = 20, K = 2 |
|---|---|---|---|
| Full joint table | K × (2^d − 1) | 2,097,150 | |
| Naive (one table per feature) | K × d | 40 |
A million parameters cannot be estimated from a few thousand labeled examples. Most feature combinations would never appear in the training set, so most table entries would be zero or noise. A text vocabulary has tens of thousands of features, so the full table is not even possible to store.
The assumption
The naive assumption says that, once you know the class, the features carry no information about each other. Formally, for every pair of features i ≠ j:
P(xi, xj | y) = P(xi | y) * P(xj | y)Extended to all features, this lets the joint likelihood factor into a product of one-dimensional terms:
P(x1, ..., xd | y) = P(x1 | y) * P(x2 | y) * ... * P(xd | y)This is the only modeling assumption naive Bayes makes. Each factor P(xi | y) is a one-dimensional distribution, which you can estimate from data with few examples. The next chapter plugs this product into Bayes' theorem to get the decision rule.
The product rule alone, without any assumption, gives P(x1, x2 | y) = P(x1 | y) P(x2 | x1, y). The assumption replaces P(x2 | x1, y) with P(x2 | y). In words, learning x1 does not change what you believe about x2 once y is known.
Conditional versus marginal independence
These are different properties, and confusing them is the most common mistake with this topic.
- Marginal independence: P(xi, xj) = P(xi) P(xj). The features are unrelated across the whole dataset.
- Conditional independence given y: P(xi, xj | y) = P(xi | y) P(xj | y). The features are unrelated within each class.
Neither implies the other. Features that are strongly correlated across the whole dataset can still be conditionally independent, because the class explains the correlation.
Take spam filtering. The words "free" and "prize" appear together far more often than chance across all emails, because both are common in spam. Within spam alone, suppose they occur independently. Then the correlation exists only because both depend on the class. That is exactly what naive Bayes assumes.
The diagram shows the structure. The class generates every feature, and there are no edges between features. The assumption is exactly the absence of those edges.
Checking the idea with simulated data
The following script builds two binary features that depend only on the class. It then compares the joint frequency of both features being 1 against the product of their individual frequencies, first over all data and then within each class.
import numpy as np
rng = np.random.default_rng(0)
n = 200_000
y = rng.integers(0, 2, size=n)
p_one = np.where(y == 1, 0.8, 0.2) # P(feature = 1 | class)
x1 = rng.random(n) < p_one
x2 = rng.random(n) < p_one
def joint_vs_product(a, b):
return round((a & b).mean(), 3), round(a.mean() * b.mean(), 3)
print("all data P(x1,x2) vs P(x1)P(x2):", joint_vs_product(x1, x2))
for c in (0, 1):
m = y == c
print(f"class {c} P(x1,x2|y) vs P(x1|y)P(x2|y):",
joint_vs_product(x1[m], x2[m]))Expected output is close to these values. Over all data, the joint is about 0.34 while the product is about 0.25, so the features are marginally dependent. Within class 0 the two numbers are both about 0.04, and within class 1 both are about 0.64, so the features are conditionally independent. Naive Bayes fits this data exactly, even though the raw features look strongly correlated.
You can run the same check on real data. Split the training set by class, compute the correlation of feature pairs within each class, and look for large values. This does not prove independence, but it shows where the assumption is weakest.
When the assumption is violated
Real features are rarely independent given the class. Typical cases:
- Phrases and collocations. In text, "new" and "york" depend on each other even within the class "travel".
- Duplicated or derived features. Height in centimeters and height in inches, or a feature and its square, are perfectly dependent.
- Shared hidden causes beyond the class. Sensor readings that all respond to the same temperature are correlated within a class.
Violations cause a specific, predictable error. The model multiplies the likelihood of each feature as if each were new evidence. If two features carry the same information, that evidence is counted twice. With many redundant features the class scores become extreme, which is the root of the overconfident probabilities discussed in a later chapter.
What the assumption does and does not cost
The assumption trades bias for variance. A full joint model has low bias but needs far more data than you have, so its estimates have high variance. The naive model is biased when features interact, but its few parameters are estimated stably. With small datasets and many features, this trade usually favors naive Bayes.
Be precise about what is lost:
- Interactions between features. The model cannot represent "A matters only when B is present".
- Reliable probabilities. Correlated evidence is double counted, so predicted probabilities tend toward 0 or 1.
- Not necessarily the ranking. Class scores can be wrong in magnitude while the highest-scoring class is still correct. A later chapter covers why this happens.
The assumption is a statement about the model, not about the world. You do not need it to be true. You need the errors it causes to be small enough not to change which class scores highest.
Practical checklist
- Prefer features that measure different things. Drop duplicates and near-copies.
- Check within-class correlations, not overall correlations. A high overall correlation is not evidence against the model.
- Treat predicted probabilities with suspicion when many features are correlated.
- If interactions are known to matter, add an explicit interaction feature, or use a model that learns them, such as logistic regression or a tree ensemble.
Exercise
Two features, "free" and "prize", are strongly correlated across a whole email dataset. Does this mean the naive Bayes assumption is violated? What would you measure to decide, and what happens to the model if you add a third feature that is an exact copy of "free"?
Show answer
Not necessarily. The assumption concerns independence given the class, not overall independence. Overall correlation can be fully explained by the class, since both words are common in spam. To check, split the data by class and measure the correlation or the gap between P(free, prize | y) and P(free | y) P(prize | y) within each class. An exact copy of "free" is perfectly dependent on it given any class, so the model multiplies the same likelihood in twice. This overweights that word and pushes class scores and probabilities toward extremes.