A spam filter uses two yes/no features: O = “contains the word offer” and L = “contains a link”. The training set has 10 emails: 4 spam and 6 ham (not spam). Among spam, O=1 in 3 emails and L=1 in 3. Among ham, O=1 in 1 email and L=1 in 2. Using Bernoulli naive Bayes, how is a new email with O=1,L=0 classified (a) with maximum-likelihood estimates and (b) with Laplace (add-one) smoothing, and what is the posterior P(spam∣x) in each case?

Supervised learning, Naive Bayes
Level L2 Elementary. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can apply naive Bayes by hand, turn the scores into a posterior probability, and apply Laplace smoothing correctly.

Key idea

Naive Bayes assumes the features are independent once the class is known, so P(c∣x)∝P(c)∏jP(xj∣c), where c is the class and xj the j-th feature. Laplace smoothing for a yes/no feature adds 1 to each count: P^(xj=1∣c)=nj,c+1nc+2, where nj,c is the number of class-c emails with xj=1 and nc is the number of class-c emails.

Steps

  1. Priors are the class frequencies: P(S)=4/10=0.4 for spam and P(H)=6/10=0.6 for ham.
  2. (a) Maximum-likelihood estimates are plain fractions of counts: P(O=1∣S)=3/4 and P(L=0∣S)=1−3/4=1/4; P(O=1∣H)=1/6 and P(L=0∣H)=1−2/6=4/6.
  3. Multiply each prior by its likelihoods. Spam: 0.4⋅34⋅14=0.075. Ham: 0.6⋅16⋅46≈0.0667. These are only proportional to the posteriors, so divide by their sum: P(S∣x)=0.0750.075+0.0667≈0.53. Predict spam.
  4. (b) Laplace smoothing adds 1 to each count and 2 to each total; it is 2 because the feature has two possible values. So P(O=1∣S)=3+14+2=46, P(L=0∣S)=1+14+2=26, P(O=1∣H)=1+16+2=28 and P(L=0∣H)=4+16+2=58.
  5. New scores: spam 0.4⋅46⋅26≈0.0889; ham 0.6⋅28⋅58=0.09375. So P(S∣x)≈0.08890.0889+0.0938≈0.49. Predict ham.
  6. Comment on the flip. With only 10 emails the counts are tiny, so smoothing changes the decision. Its main job is to stop one zero count from making the whole product zero.

Common mistakes

How long your answer should be

Half a page of arithmetic; about 4 minutes.

Last checked on 4 December 2026.

Read this

Previous in Supervised learning: A logistic regression model gives P(y=1∣x)=σ(w⊤x+b), with w=(2,−1), b=−1 and σ(z)=1/(1+e−z). (a) What is the decision boundary for threshold 0.5? (b) What is it for threshold 0.8? (c) How is the point x=(1,0) classified under each threshold?
Next in Supervised learning: A node holds 16 samples, 8 positive and 8 negative. Split A sends 6 samples (5 positive, 1 negative) left and 10 samples (3 positive, 7 negative) right. Split B sends 8 samples (6 positive, 2 negative) left and 8 samples (2 positive, 6 negative) right. What is the information gain (using base-2 entropy) of each split, and which split is chosen?