Skip to content

P8: maximum likelihood with autodiff, by Newton–Raphson on the mle lectures' Poisson regression #81

Open

Description

@mmcky

#5's Phase 2 lists P8, gradient-based estimation: maximum likelihood with autodiff, "where JAX's autodiff should matter". This proposes P8's design, from QuantEcon's maximum likelihood lectures, lecture-jax's mle_jax and lecture-python's mle. The model, the comparison, the sweeps, the precisions and JAX's loop below were agreed with Matt, and he approved the whole design on 2026-11-20.

Why

  • It is the estimation loop the lectures teach. Both lectures fit a Poisson regression by Newton–Raphson, with the gradient from jax.grad and the Hessian from jax.jacfwd. Until 2025-09-10 (lecture-python.myst d695a10), lecture-python's mle was NumPy, with the gradient and Hessian written by hand. So the lectures hold both sides of the comparison: derivatives by hand, as a NumPy or Numba author writes them, and by autodiff, as a JAX author does.
  • It can separate autodiff's cost from JAX's. JAX with autodiff against NumPy by hand mixes two things, the stack and how the derivatives are found. So a companion problem, mle_hand, runs JAX with the same hand-written derivatives as NumPy and Numba. JAX against JAX measures what autodiff costs.
  • Its answer can be checked. The data are simulated from a known β, and every stack follows the same Newton steps, so the estimates agree closely across stacks and come near the truth.

The problem

Model Poisson regression: y_i ~ Poisson(μ_i), with μ_i = exp(x_i′β)
The data Drawn once by NumPy from a fixed seed, outside the timed cell, and shared by every stack. obs: lecture-jax's exercise, x ~ N(0, 1), X = [1, x, x²] and β = (−2.5, 0.25, 0.5). params: X = [1, z_1, …, z_{k−1}] with z standard normal, β_0 = −2, and slopes in a fixed random direction of length 0.75, so that log μ has the obs model's mean (−2) and spread (0.75)
Newton–Raphson From β = 0.1 in every coordinate, the lectures' start: β ← β − H⁻¹G, until no coordinate changes by more than 1e-5 (the exercise's tolerance), or 100 iterations (the lectures' cap). The step is a linear solve, where the lectures invert H: the same step, more accurately, and at k = 1,000 more cheaply
The derivatives NumPy and Numba: G = Xᵀ(y − μ) and H = −Xᵀ diag(μ) X, as lecture-python's mle had them. JAX in P8: jax.grad of the lectures' log-likelihood, and jax.jacfwd of that, as both lectures do now. JAX in mle_hand: the hand-written G and H
What is timed The whole Newton loop, from the start to the estimate
Sweeps obs: n = 10,000, 100,000, 500,000 (the exercise's), 1 million, 3 million and 10 million observations, with k = 3 (3 million added in #82). params: k = 3, 10, 30, 100, 300 and 1,000 parameters, with n = 100,000
Precision float64 and float32
Recorded The estimate β̂, and the iteration count

Why these sweeps. An iteration costs about n·k for μ and the gradient, n·k² for the Hessian, and k³ for the solve. obs keeps the lecture's k = 3, so every step is a few passes over the data and is limited by memory bandwidth: this is where a GPU might start to pay. params makes the Hessian dominate. Forward-mode autodiff builds the Hessian by differentiating the gradient once per parameter. For this model that means a product of X with a k × k identity, as much work as the hand-written Hessian's own product. A check on qe-mini1 with JAX 0.12.0.1 found that product in the jaxpr and in the compiled program. XLA's own count of the work is twice the hand-written Hessian's: 4.1e9 against 2.1e9 flops at n = 100,000 and k = 100. Timed alone at k = 1,000, the autodiff Hessian took 2.5 times as long as the hand-written one in float64, and 1.5 times in float32. But the extra product doesn't depend on β, so inside lax.while_loop XLA moves it out of the loop and computes it once per solve. In the whole Newton loop the two took about the same time (below). mle_hand measures this at every size. The lectures' form of the log-likelihood, with y·log(exp(Xβ)), compiles to the same products as y·Xβ − exp(Xβ).

Why obs starts at 10,000. A NumPy prototype on qe-mini1, on one core, ran Newton from the lectures' start. From 10,000 to 10 million observations it took 10 to 20 iterations, in float64 and float32 alike, varying with n. At 1,000 it took 41 in float64 and overflowed in float32, so a cell there would measure the start rather than the stacks. An intercept-only start, the usual one for a Poisson regression, was worse: 60 to 100 iterations. params took 7 to 9 iterations at every k tried up to 300, at n = 10,000 and from either start, and the prototype below took 15 at k = 1,000.

NumPy in float32. The products and μ stay in float32. np.linalg.solve computes in float64 for float32 inputs, as #73 found for P7, but here that is only the k × k solve, a small share of the work. The README will say so.

Memory. The data X take 240 MB at the largest obs cell and 800 MB at the largest params cell, in float64. Forward-mode autodiff holds a few arrays of n × k beside them, the product moved out of the loop among them, so the largest JAX cell needs a few GB.

A prototype across the stacks. A script on qe-mini1's CPU ran every stack in both precisions. It used the data above but not the harness, and its timings are development runs, not measurements: warm times, the best of two.

  • Iterations: every stack took NumPy's number of Newton iterations at every size. In obs that was 10, 12 and 12 at n = 10,000, 100,000 and 1 million; in params, 7, 9 and 15 at k = 30, 300 and 1,000. So params converges at k = 1,000, and the sweep can stay as proposed.
  • Estimates: every estimate was within 1.2e-7 of NumPy's in float64, at most 6e-8 relative to its largest coordinate, which is how the checks below measure it.
  • Compilation: Numba's first call, with compilation, took about 1.4 s, and JAX's about 0.1 s.
Newton loop, warm (s) NumPy Numba Numba, parallel JAX, hand JAX, autodiff
obs, n = 1 million, float64 0.14 0.12 0.10 0.072 0.086
obs, n = 1 million, float32 0.11 0.097 0.081 0.066 0.074
params, k = 1,000, float64 5.8 6.2 5.7 9.8 9.9
params, k = 1,000, float32 1.7 2.2 1.7 4.3 4.2

At k = 1,000, NumPy and Numba hand the Hessian's product to Accelerate, which beat XLA's CPU backend here in both precisions. #1 found the same for float64 products on an M1 Pro.

Stacks

The derivatives The loop
NumPy By hand, with NumPy's matrix products A Python loop
Numba By hand: μ and y − μ in one prange pass over the observations, and the matrix products with @, which Numba hands to BLAS An njit loop
JAX, P8 (mle) jax.grad and jax.jacfwd of the lectures' log-likelihood lax.while_loop
JAX, mle_hand By hand, as NumPy's, in jax.numpy lax.while_loop
Numba, mle_onepass (added in #82) By hand: μ, G and H in one prange pass, one observation at a time, in blocks of 4,096 An njit loop
Numba, mle_blocked (added in #82) By hand: μ, G and H in one prange pass, each block of observations (256 rows in float64, 1,024 in float32) forming its own with BLAS, summed in 64 fixed runs An njit loop

JAX's Newton loop is lax.while_loop, as every JAX stack in the suite has it, where the lectures call G and H from a Python loop. Inside it, XLA computes autodiff's extra product once per solve rather than once an iteration, as above, so mle against mle_hand measures autodiff's cost after XLA's optimisation. The README and the finding will say so.

mle_hand runs on jax-cpu and jax-gpu only. It needs two small changes to the harness: a problem may list the backends it runs on, and it may name the problem whose NumPy rows its answers are checked against, here mle. mle_onepass and mle_blocked, added in #82, run on numba and numba-parallel only, checked the same way. With mle's Numba, which forms G and H through BLAS over all of X, they show how to structure the pass over the observations as the parameters grow. mle_onepass runs obs and params to 300 parameters, mle_blocked both sweeps.

Checks

  • The estimate at fixed positions, against NumPy's in float64 at the same size, within a relative 1e-8 in float64 and 1e-5 in float32 (changed in Problems: P8, maximum likelihood with autodiff, and three companions (#81) #82 from the suite's 1e-3, since the intercept sets the scale). Each companion's are checked against mle's NumPy rows.
  • The iteration count, against NumPy's in the same precision.
  • A cell fails if it reached 100 iterations or its estimate isn't finite.

Activity

  1. mmcky commented on Nov 21, 2026

    @mmcky
    ContributorAuthor

    Four departures from this design, made in #82 after its reviews, and merged with Matt's agreement as 11c1c59. The issue body now reflects them. This comment records them. Each was Matt's answer to a question from the independent review, on 2026-11-20.

    1. mle_onepass, a Numba-only companion (Q22, Q23). The review found that a loop accumulating G and H in Numba's pass over the observations, rather than through BLAS over all of X, was much faster with few parameters. Matt kept mle's Numba column as designed (Q20), and asked for the one-pass loop's numbers across sizes, to show where it pays. It adds one observation at a time, in blocks of 4,096, and runs obs and params to 300 parameters.
    2. mle_blocked, a second Numba-only companion (Q24). The review then found that what loses to BLAS from 100 parameters is adding one observation at a time, not reading X once. A pass in which each block of observations forms its own G and H with BLAS beat BLAS over all of X up to 300 parameters. It runs both sweeps, with blocks of 256 rows in float64 and 1,024 in float32, summed in 64 fixed runs.
    3. obs gained 3 million observations (Q22), between 1 million and 10 million, for every stack.
    4. P8's float32 check is a relative 1e-5, not the suite's 1e-3 (Q21). The check divides by the largest value, the intercept, −2.5 or −2, where most slopes are far smaller: the median is 0.03 at 300 parameters. At 1e-3, a cell that stopped two iterations early would pass, and so would TF32's truncation of G's and H's products. Correct float32 cells differed from NumPy's float64 by 3.3e-7 at most.

    In development runs on the M6, in parallel and in float64, each Numba form was the fastest somewhere:

    Cell BLAS over all of X (mle) mle_onepass mle_blocked
    obs, 3 parameters, 10 million observations 1.27 s 0.20 s 0.22 s
    params, 300 parameters, 100,000 observations 0.38 s 1.07 s 0.31 s
    params, 1,000 parameters, 100,000 observations 5.7 s — 7.0 s

    The measured runs will give the same comparison on each machine.

    One more change tightens the implementation rather than the design. mle_hand writes G as (y − μ)X, since X′(y − μ) made XLA's CPU backend copy X transposed every iteration. That would have charged the hand-written loop for a copy, rather than charging autodiff for its Hessian.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions