Why are Naive Bayes posterior computations performed in log space?

Quick Overview

Asks why Naive Bayes classifiers compute posteriors with log probabilities instead of raw products. Tests understanding of floating point limits, why the predicted class is unchanged, how to recover normalized probabilities, and how the log form relates to linear classifiers.

Why are Naive Bayes posterior computations performed in log space?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Naive Bayes scores each class by multiplying the class prior by the likelihood of every feature given that class, for example one factor per word in a text classifier. Why are Naive Bayes posterior computations performed in log space? ```hint Look at the magnitudes Estimate the size of a product of a few hundred probabilities that are each far below 1, and compare it with the range a 64-bit float can represent. ``` ### What a Strong Answer Covers - The numerical problem with multiplying many small probabilities, with a sense of the magnitudes involved - How the log transform changes the computation, written as a formula - Why the predicted class stays the same - How to turn log scores back into normalized probabilities safely - What the log-space form shows about the model, and the pitfalls around zero probabilities ### Follow-up Questions - Why does exponentiating the log scores directly fail, and how does the log-sum-exp trick avoid it? - What happens in log space if a word never appeared with a class in training? - Show that two-class Multinomial Naive Bayes gives log-odds that are linear in the word counts. How does that compare with logistic regression? - Does Gaussian Naive Bayes have the same problem, and how would you compute its log-likelihood?

Overview: Asks why Naive Bayes classifiers compute posteriors with log probabilities instead of raw products. Tests understanding of floating point limits, why the predicted class is unchanged, how to recover normalized probabilities, and how the log form relates to linear classifiers.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Naive Bayes scores each class by multiplying the class prior by the likelihood of every feature given that class, for example one factor per word in a text classifier.

Why are Naive Bayes posterior computations performed in log space?

What a Strong Answer Covers Guidance

  • The numerical problem with multiplying many small probabilities, with a sense of the magnitudes involved
  • How the log transform changes the computation, written as a formula
  • Why the predicted class stays the same
  • How to turn log scores back into normalized probabilities safely
  • What the log-space form shows about the model, and the pitfalls around zero probabilities

Follow-up Questions Guidance

  • Why does exponentiating the log scores directly fail, and how does the log-sum-exp trick avoid it?
  • What happens in log space if a word never appeared with a class in training?
  • Show that two-class Multinomial Naive Bayes gives log-odds that are linear in the word counts. How does that compare with logistic regression?
  • Does Gaussian Naive Bayes have the same problem, and how would you compute its log-likelihood?