Compute logistic regression log-likelihood and mean log-loss from scratch (MLE equivalence)

Quick Overview

Implement the total Bernoulli log-likelihood and the mean binary cross-entropy of a logistic regression model from scratch in NumPy. It tests the per-sample log-loss derivation, a numerically stable softplus with logaddexp, and why maximizing likelihood and minimizing mean log-loss give the same parameters.

Compute logistic regression log-likelihood and mean log-loss from scratch (MLE equivalence)

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `loglik_and_mean_logloss(X, y, w, b)` from scratch (no `sklearn`). It returns `(loglik, mean_log_loss)`: the total Bernoulli log-likelihood of a logistic regression model and its mean binary cross-entropy, both evaluated at the fixed parameters `w` and `b`. The two values must satisfy `mean_log_loss == -loglik / n_samples`. Requirements: - Compute the logits as `z = X @ w + b`. `X` has no column of ones; the intercept is the separate scalar `b`. - Compute `loglik = np.sum(y * z - np.logaddexp(0.0, z))`, summed over all samples. Use `np.logaddexp(0.0, z)` for the `log(1 + exp(z))` term; do not write it as `np.log(1 + np.exp(z))`. - Set `mean_log_loss = -loglik / n_samples`. - Return both as Python or NumPy floats in the order `(loglik, mean_log_loss)`. Example: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([0.0, 1.0, 1.0]) w = np.array([0.5]) b = -1.0 # z = [-0.5, 0.0, 0.5] loglik, mll = loglik_and_mean_logloss(X, y, w, b) # loglik is about -1.6413, mll is about 0.5471 # mll == -loglik / 3 -> True ``` ```hint Where the identity comes from Compute the summed log-likelihood once and derive the mean from that same number, so the identity holds exactly instead of depending on two separate floating-point paths. ``` ### Constraints & Assumptions - `X` has shape `(n_samples, n_features)`, `w` has shape `(n_features,)`, `y` has shape `(n_samples,)` with labels 0 or 1, and `b` is a scalar. - Only NumPy is available. - Logits can be large in magnitude, so the result must stay finite whenever the true loss is finite. ### What a Strong Answer Covers - Justifies the per-sample term `y * z - log(1 + exp(z))` from the Bernoulli likelihood with a sigmoid link. - Computes the logits with a separate intercept and a numerically stable `log(1 + exp(z))`. - Derives the mean log-loss from the summed log-likelihood so the required identity holds. - Explains why maximizing the log-likelihood and minimizing the mean log-loss give the same parameters. - States the time and space cost and names the shape and empty-input pitfalls. ### Follow-up Questions - Why prefer `np.logaddexp(0.0, z)` over `np.log(1 + np.exp(z))`? For which values of `z` does the naive form fail? - The two objectives differ only by the factor `-1/n_samples`. Does that scaling change the optimum, and does it matter for gradient descent? - What is the gradient of the mean log-loss with respect to `w` and `b`, and how would you compute it without overflow?

Overview: Implement the total Bernoulli log-likelihood and the mean binary cross-entropy of a logistic regression model from scratch in NumPy. It tests the per-sample log-loss derivation, a numerically stable softplus with logaddexp, and why maximizing likelihood and minimizing mean log-loss give the same parameters.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement loglik_and_mean_logloss(X, y, w, b) from scratch (no sklearn). It returns (loglik, mean_log_loss): the total Bernoulli log-likelihood of a logistic regression model and its mean binary cross-entropy, both evaluated at the fixed parameters w and b. The two values must satisfy mean_log_loss == -loglik / n_samples.

Requirements:

  • Compute the logits as z = X @ w + b . X has no column of ones; the intercept is the separate scalar b .
  • Compute loglik = np.sum(y * z - np.logaddexp(0.0, z)) , summed over all samples. Use np.logaddexp(0.0, z) for the log(1 + exp(z)) term; do not write it as np.log(1 + np.exp(z)) .
  • Set mean_log_loss = -loglik / n_samples .
  • Return both as Python or NumPy floats in the order (loglik, mean_log_loss) .

Example:

X = np.array([[1.0], [2.0], [3.0]])
y = np.array([0.0, 1.0, 1.0])
w = np.array([0.5])
b = -1.0
# z = [-0.5, 0.0, 0.5]
loglik, mll = loglik_and_mean_logloss(X, y, w, b)
# loglik is about -1.6413, mll is about 0.5471
# mll == -loglik / 3   -> True

Constraints & Assumptions

  • X has shape (n_samples, n_features) , w has shape (n_features,) , y has shape (n_samples,) with labels 0 or 1, and b is a scalar.
  • Only NumPy is available.
  • Logits can be large in magnitude, so the result must stay finite whenever the true loss is finite.

What a Strong Answer Covers Guidance

  • Justifies the per-sample term y * z - log(1 + exp(z)) from the Bernoulli likelihood with a sigmoid link.
  • Computes the logits with a separate intercept and a numerically stable log(1 + exp(z)) .
  • Derives the mean log-loss from the summed log-likelihood so the required identity holds.
  • Explains why maximizing the log-likelihood and minimizing the mean log-loss give the same parameters.
  • States the time and space cost and names the shape and empty-input pitfalls.

Follow-up Questions Guidance

  • Why prefer np.logaddexp(0.0, z) over np.log(1 + np.exp(z)) ? For which values of z does the naive form fail?
  • The two objectives differ only by the factor -1/n_samples . Does that scaling change the optimum, and does it matter for gradient descent?
  • What is the gradient of the mean log-loss with respect to w and b , and how would you compute it without overflow?