Stable Naive Bayes Posteriors via Log-Sum-Exp

Quick Overview

Turn Naive Bayes joint log-scores into class posteriors without overflow or underflow, even when scores reach plus or minus 1000. It tests the log-sum-exp max shift, why softmax ignores a per-row constant, handling of negative infinity scores and correct NumPy broadcasting.

Stable Naive Bayes Posteriors via Log-Sum-Exp

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given joint log-scores `jll` of shape `(n_samples, n_classes)`, implement four functions that turn them into posteriors without underflow or overflow, even for scores near ±1000. - `log_sum_exp(jll)`: row-wise `log(sum(exp(jll)))`, shape `(n_samples,)`. - `predict_log_proba(jll)`: log posteriors, same shape as `jll`. - `predict_proba(jll)`: posteriors, each row sums to 1. - `predict_proba_multinomial(X, class_log_prior, feature_log_prob)`: posteriors for `jll = X @ feature_log_prob.T + class_log_prior`. Example: ```python import numpy as np jll = [[1000.0, 1000.0], [-1000.0, -1001.0]] predict_proba(jll) # [[0.5, 0.5], [0.731, 0.269]] log_sum_exp([[0.0, np.log(3), -np.inf]]) # [log(4)], about [1.386] predict_proba([[-np.inf, 0.0, 0.0]]) # [[0.0, 0.5, 0.5]] ``` ```hint Shift the row Find a constant you can subtract from every score in a row without changing the normalized result, and choose it so the largest exponent is exactly zero. Then check what that constant becomes when a row contains -inf. ``` ### Constraints & Assumptions - `jll` is 2-D with shape `(n_samples, n_classes)`. Scores can be near ±1000 and can include `-inf` for a class with zero probability. - For `predict_proba_multinomial`, `X` has shape `(n_samples, n_features)`, `feature_log_prob` has shape `(n_classes, n_features)` and `class_log_prior` has shape `(n_classes,)`. ### What a Strong Answer Covers - Why exponentiating the raw scores fails at ±1000, with the float64 limits involved. - The row-max shift and why it leaves the posteriors unchanged. - Handling of `-inf` entries and of rows whose maximum is not finite. - Shapes and broadcasting: a 1-D log-sum-exp subtracted from `jll` as a column. - Time and space complexity, including the matrix product in the multinomial case. ### Follow-up Questions - What should happen for a row where every score is `-inf`, and how would you report it? - How would you compute a log-sum-exp in one pass when the scores arrive in chunks and cannot all be stored? - `predict_proba` returns exactly 0.0 for a class whose log posterior is -800. When does that matter, and what should downstream code use instead?

Overview: Turn Naive Bayes joint log-scores into class posteriors without overflow or underflow, even when scores reach plus or minus 1000. It tests the log-sum-exp max shift, why softmax ignores a per-row constant, handling of negative infinity scores and correct NumPy broadcasting.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Given joint log-scores jll of shape (n_samples, n_classes), implement four functions that turn them into posteriors without underflow or overflow, even for scores near ±1000.

  • log_sum_exp(jll) : row-wise log(sum(exp(jll))) , shape (n_samples,) .
  • predict_log_proba(jll) : log posteriors, same shape as jll .
  • predict_proba(jll) : posteriors, each row sums to 1.
  • predict_proba_multinomial(X, class_log_prior, feature_log_prob) : posteriors for jll = X @ feature_log_prob.T + class_log_prior .

Example:

import numpy as np

jll = [[1000.0, 1000.0], [-1000.0, -1001.0]]
predict_proba(jll)                         # [[0.5, 0.5], [0.731, 0.269]]

log_sum_exp([[0.0, np.log(3), -np.inf]])   # [log(4)], about [1.386]
predict_proba([[-np.inf, 0.0, 0.0]])       # [[0.0, 0.5, 0.5]]

Constraints & Assumptions

  • jll is 2-D with shape (n_samples, n_classes) . Scores can be near ±1000 and can include -inf for a class with zero probability.
  • For predict_proba_multinomial , X has shape (n_samples, n_features) , feature_log_prob has shape (n_classes, n_features) and class_log_prior has shape (n_classes,) .

What a Strong Answer Covers Guidance

  • Why exponentiating the raw scores fails at ±1000, with the float64 limits involved.
  • The row-max shift and why it leaves the posteriors unchanged.
  • Handling of -inf entries and of rows whose maximum is not finite.
  • Shapes and broadcasting: a 1-D log-sum-exp subtracted from jll as a column.
  • Time and space complexity, including the matrix product in the multinomial case.

Follow-up Questions Guidance

  • What should happen for a row where every score is -inf , and how would you report it?
  • How would you compute a log-sum-exp in one pass when the scores arrive in chunks and cannot all be stored?
  • predict_proba returns exactly 0.0 for a class whose log posterior is -800. When does that matter, and what should downstream code use instead?