Fit Poisson regression with Newton steps

HardRegressionPro

Why it matters Request-rate and token-count forecasts for capacity planning are classic Poisson GLMs.

Counts (clicks, tokens per request) are modelled with Poisson regression: mu = exp(X w).

Write poisson_newton(X, y, steps). Start from w = 0. Each Newton step: mu = exp(X w), gradient g = X^T (mu - y), Hessian H = X^T diag(mu) X, then w = w - solve(H, g). Return w after the given number of steps.

Signaturedef poisson_newton(X, y, steps)

Example

X = [[1], [1]], y = [2, 4] converges to w = [log 3], about 1.0986.

Understand the idea

Poisson regression models counts, like clicks or tokens per request, with a predicted mean mu = exp(X w). Newton's method fits it fast by using curvature: each step divides the gradient by the Hessian, where each row is weighted by its own mu. Worked example: X = [[1], [1]], y = [1, 3], start w = 0. Step 1: mu = [1, 1], g = (1 - 1) + (1 - 3) = -2, H = 1 + 1 = 2, w = 0 - (-2 / 2) = 1. Step 2: mu = e = 2.718 for both, g = 2 * 2.718 - 4 = 1.437, H = 5.437, w = 1 - 1.437 / 5.437 = 0.736. Further steps approach ln 2 = 0.693, since the best constant rate is the mean count 2. Rule: mu = exp(X w); g = X^T (mu - y); H = X^T diag(mu) X; w = w - solve(H, g); repeat for the given number of steps from w = 0. Dropping the mu weights in H turns it into plain least squares curvature. Used for count data in traffic, demand and event-rate modelling.

Shapes and data flow

X (n, d) @ w (d,) -> eta (n,), mu = exp(eta)
H = X^T (mu * X) -> (d, d)
solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: examplepoisson_newton([[1], [1]], [2, 4], 20)
  • Sample 2: one steppoisson_newton(_X, _y, 1)