Week 8 · Generative models: Naive Bayes
Read the words of the video
- Imagine you're teaching a friend to spot spam emails. You describe what spam looks like, like words such as 'free' or 'money'. You also describe normal emails with words like 'meeting' or 'lunch'. When a new email arrives, you simply ask: "Which kind of email would more likely have written this message?"
- This way of thinking is called a 'generative model'. We learn a story for how spam emails are *made*, and a story for how normal emails are *made*. Then, we find which story best explains a new email. It's 'naive' because it assumes words appear independently, even if they often come together, like 'free' and 'money'. This simplifies the model.
- Let's try an example. Imagine we have some known spam and normal emails. We count how often words appear in each. Now, a new email "free money" arrives. We use these counts to find its score for being spam, and its score for being normal. The spam score is much higher, so we predict this email is spam.
- For your exam, remember to classify emails using our method, and count model parameters. Always recall that Naive Bayes assumes features are independent *given the class*. And remember, it's a *generative* model. Keep practising, and you'll easily spot spam, and ace your quiz! You got this!
The one-minute story
How would you teach someone to spot spam? One way: describe what spam looks like. "Spam emails often say free, win and money. Normal emails talk about meeting, today, lunch." Then, when a new email arrives, ask: "Which kind of email would more likely have written this?"
That is a generative model: for each class you learn a little story of how its members are produced, and you classify by asking which story explains the new example best.
The "naive" part: to keep the stories simple, we pretend the words in an email are chosen independently once we know whether it is spam. That is not really true (free and money often come together), but the shortcut makes the model cheap, quick and, surprisingly, very good at text.
By the end of this week you can:
- explain generative vs discriminative models;
- apply Bayes' rule and the naive independence assumption to classify;
- estimate Naive Bayes parameters from counts with Laplace smoothing;
- handle numeric features with Gaussian Naive Bayes;
- count the parameters of a Naive Bayes model.
Words you need
- Generative model - models how the data is produced for each class: and .
- Discriminative model - models the answer directly: (or just a boundary). Logistic regression is next week.
- Prior - how common each class is.
- Class-conditional likelihood - how likely this input is, if the class were .
- Naive Bayes assumption - features are independent given the class.
- Laplace smoothing - adding a "1" to every count so nothing gets probability exactly zero.
Idea 1: Generative or discriminative?
Bayes' rule gives the quantity we want, the probability of each class given the data:
- A generative model learns the right-hand side pieces, and , and then uses Bayes' rule. It can even generate fake data. (Naive Bayes, Gaussian mixtures.)
- A discriminative model learns directly and never bothers modelling . (Logistic regression, SVM, perceptron.)
The prediction is the class with the largest posterior. The denominator is the same for every class, so we can ignore it when comparing.
Idea 2: The naive assumption
With features, modelling the full joint needs a huge table (for yes/no features, numbers per class). The naive shortcut:
so we only need one small distribution per feature per class. The classifier becomes
Counting parameters (binary features, classes, features):
- Naive Bayes: numbers plus class priors . For two classes: .
- Full joint model: . For two classes: .
For : naive needs numbers, the full model . For : versus . The naive assumption turns an exponential into a linear number of parameters.
Idea 3: Learning from counts, and smoothing
Estimating by counting is MLE:
Problem. If the word lottery never appeared in any training spam, its estimated probability given spam is , and because the probabilities are multiplied, one zero wipes out the entire product, no matter how spammy the rest of the email looks.
Laplace smoothing (add-one): pretend you saw every word one extra time:
For a yes/no feature with two values: . This is exactly the posterior mean with a uniform Beta(1,1) prior (MLT week 4), i.e. "Laplace's rule of succession".
In practice multiply many tiny probabilities and the computer rounds to zero, so we add logs instead: .
Training data (word lists).
- Spam (3 emails): "free win", "free money", "win money". Word counts: free 2, win 2, money 2 (total 6).
- Ham (3 emails): "meeting today", "free today", "lunch today". Counts: meeting 1, today 3, free 1, lunch 1 (total 6).
Vocabulary: free, win, money, meeting, today, lunch, so . Priors: .
Question. Classify the email "free money" with Laplace smoothing.
Solution.
- , .
- , .
- Unnormalised scores: spam ; ham .
- Normalise: .
Predict spam. Without smoothing, would be and ham would score exactly , which would be too confident.
Idea 4: Numeric features, Gaussian Naive Bayes
When a feature is a number (height, income), model as a Gaussian with its own mean and variance for each class and feature, both fitted by MLE from the class's examples ( = sample mean, = sample variance). Then multiply the Gaussian densities across features, times the prior, as before.
Question. Class has feature mean 2 and variance 1; class has mean 5 and variance 1; equal priors. Classify and give the posterior.
Solution. ; . Posterior of is . Predict . With equal priors and equal variances the decision boundary is the midpoint .
Idea 5: When does it work, and why "naive" still wins?
- Pros: very fast to train (just counting), few parameters, works with little data, handles many features (text with thousands of words).
- Cons: correlated features are double counted (if free and offer nearly always appear together, the model counts them as two separate pieces of evidence); the probabilities it outputs are often over-confident, though the ranking of classes is usually still right.
- For binary/count features the log-odds are a linear function of the features, so Naive Bayes ends up as a linear classifier, like logistic regression, but fitted by counting instead of optimisation.
Your turn
. , ; , . An email contains "win" and "free". Find .
Need a hint?
Multiply prior times the two likelihoods for each class.
Show the full solution
Spam: . Ham: . Posterior .
There are 20 words in the spam class, a vocabulary of 100 words, and the word "lottery" never appears in spam. Give its smoothed probability given spam.
Need a hint?
.
Show the full solution
.
A two-class problem has 10 binary features. Count the parameters for Naive Bayes and for the full joint model.
Need a hint?
Naive: . Full: .
Show the full solution
Naive: . Full: .
(a) Naive Bayes assumes features are independent given the class. (b) Naive Bayes is a discriminative model. (c) Laplace smoothing prevents zero probabilities. (d) Because of the independence assumption, the predicted probabilities are always correct.
Need a hint?
Re-read the idea boxes.
Show the full solution
(a) True. (b) False: it is generative. (c) True. (d) False: the assumption is usually violated, and probabilities can be over-confident, though the predicted class is often right.
Class A: mean 0, variance 1. Class B: mean 4, variance 1. Equal priors. Classify and give the decision boundary.
Need a hint?
Which mean is closer to? With equal variances and priors the boundary is halfway.
Show the full solution
and . Predict A. Boundary at (the midpoint).
Exam radar
MLT Quiz 2: (of the 21 summarised Quiz 2 papers) Naive Bayes in 18, generative and discriminative models in 9, Laplace smoothing in 6, number of parameters in 3. End Term: Naive Bayes in 19, Prior 13, Posterior 14, Assumption 19.
Typical: classify an email with Laplace smoothing (a fraction calculation like Example 1), count parameters, state the independence assumption exactly ("given the class"), "which of these is a generative model?".
- The independence is conditional on the class, not unconditional.
- Don't forget the prior .
- With Laplace smoothing the denominator gets (the vocabulary size) for word counts, or for a yes/no feature.
- Use the same vocabulary for every class.
- Compare unnormalised scores; you only need to normalise if asked for a probability.
Quick quiz
Naive Bayes assumes that the features are...
- all equal
- independent given the class
- independent of the class
- Gaussian
Laplace smoothing is used to...
- speed up training
- avoid zero probabilities
- reduce the number of classes
- scale the features
Which of these is a generative model?
- logistic regression
- SVM
- Naive Bayes
- perceptron
Select all TRUE statements. (select all that apply)
- Naive Bayes needs far fewer parameters than a full joint model.
- We can compare classes using unnormalised scores.
- Naive Bayes can never be used on text.
- The prior p(y) is ignored in Naive Bayes.
Recap
- Generative: model and , classify with Bayes' rule; discriminative: model directly.
- Naive Bayes: . Parameters: for binary features.
- Estimate by counting; Laplace smoothing adds 1 (and to the denominator). Work in logs.
- Numeric features: Gaussian per class per feature. Correlated features are double counted.
- Laplace: (words) or (yes/no)
- Parameters (binary): naive ; full
- Gaussian NB: with class-wise MLE
- MLF week 10 - Probability for machine learning: Naive Bayes is Bayes' rule plus the probability tools from MLF week 10.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 12 real questions on this week All Quiz 2 papers