Implement One IRLS Step for Logistic Regression via the Working Response

Quick Overview

Implement one iteratively reweighted least squares step for logistic regression in NumPy by solving the weighted normal equations for the working response. It tests GLM fitting mechanics, the link to Newton's method, and memory-aware linear algebra.

Implement One IRLS Step for Logistic Regression via the Working Response

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement one iteratively reweighted least squares (IRLS) update for logistic regression from scratch (no `sklearn` or `statsmodels`). Express it as a weighted least-squares fit of the working response, not as an explicit Newton update. ```python def irls_step(X: np.ndarray, y: np.ndarray, w: np.ndarray) -> np.ndarray: ... ``` `X` is `(n_samples, d)` with the first column all ones, `w` is `(d,)` with the intercept at `w[0]`, and `y` holds the floats 0.0 and 1.0. Return `w_new` of shape `(d,)`. The step must follow these definitions: - `eta = X @ w`, `p = 1/(1+np.exp(-eta))`, and weights `s = p * (1 - p)`. - Working response: `z = eta + (y - p) / s`. - Solve `(X.T @ np.diag(s) @ X) @ w_new = X.T @ np.diag(s) @ z` with `np.linalg.solve`, forming `A = (X.T * s) @ X` and `rhs = (X.T * s) @ z` (do not build `np.diag(s)`). - No learning rate and no `/n_samples` scaling anywhere. - Exactly one step: no loop and no convergence check. ```hint Broadcasting the weights Work out what `X.T * s` does to each column of `X.T`, and why that gives the same result as multiplying by a diagonal matrix. ``` ```hint Checking your answer Substitute `z` into the right-hand side and simplify. You should be able to show that the result is one Newton-Raphson step on the log-likelihood. ``` ### Constraints & Assumptions - `X` has shape `(n_samples, d)` with a leading column of ones, `y` has shape `(n_samples,)`, and `w` has shape `(d,)`. - Assume `s` stays positive (no perfectly separated points) and `X` has full column rank, so the system has a unique solution. ### What a Strong Answer Covers - The sequence `eta`, `p`, `s`, `z`, and what the working response represents. - Forming the weighted normal equations by broadcasting, without an n by n diagonal matrix. - Why the weighted least-squares solve equals one Newton-Raphson step, with no learning rate or 1/n factor. - Time and memory cost in terms of n and d. - What goes wrong when `s` approaches zero or the system is close to singular. ### Follow-up Questions - How would you guard against a near-singular `XᵀSX` or `s` underflowing to zero? - How does adding L2 regularization change this IRLS step? - How would you turn this step into a full fitting loop, and what stopping rule would you use?

Overview: Implement one iteratively reweighted least squares step for logistic regression in NumPy by solving the weighted normal equations for the working response. It tests GLM fitting mechanics, the link to Newton's method, and memory-aware linear algebra.

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

Implement one iteratively reweighted least squares (IRLS) update for logistic regression from scratch (no sklearn or statsmodels). Express it as a weighted least-squares fit of the working response, not as an explicit Newton update.

def irls_step(X: np.ndarray, y: np.ndarray, w: np.ndarray) -> np.ndarray:
    ...

X is (n_samples, d) with the first column all ones, w is (d,) with the intercept at w[0], and y holds the floats 0.0 and 1.0. Return w_new of shape (d,).

The step must follow these definitions:

  • eta = X @ w , p = 1/(1+np.exp(-eta)) , and weights s = p * (1 - p) .
  • Working response: z = eta + (y - p) / s .
  • Solve (X.T @ np.diag(s) @ X) @ w_new = X.T @ np.diag(s) @ z with np.linalg.solve , forming A = (X.T * s) @ X and rhs = (X.T * s) @ z (do not build np.diag(s) ).
  • No learning rate and no /n_samples scaling anywhere.
  • Exactly one step: no loop and no convergence check.

Constraints & Assumptions

  • X has shape (n_samples, d) with a leading column of ones, y has shape (n_samples,) , and w has shape (d,) .
  • Assume s stays positive (no perfectly separated points) and X has full column rank, so the system has a unique solution.

What a Strong Answer Covers Guidance

  • The sequence eta , p , s , z , and what the working response represents.
  • Forming the weighted normal equations by broadcasting, without an n by n diagonal matrix.
  • Why the weighted least-squares solve equals one Newton-Raphson step, with no learning rate or 1/n factor.
  • Time and memory cost in terms of n and d.
  • What goes wrong when s approaches zero or the system is close to singular.

Follow-up Questions Guidance

  • How would you guard against a near-singular XᵀSX or s underflowing to zero?
  • How does adding L2 regularization change this IRLS step?
  • How would you turn this step into a full fitting loop, and what stopping rule would you use?