jax.lax.zeta gives wrong results for negative non-integer q (Euler-Maclaurin breakdown)
Description
jax.lax.zeta(s, q) (Hurwitz zeta function) returns incorrect values for non-integer q < 0 when |q| ≳ 8. The error grows catastrophically as q becomes more negative.
This is a numerical correctness bug that silently corrupts any computation that uses lax.zeta directly with negative non-integer arguments.
Reproducer
import jax, jax.numpy as jnp
from scipy.special import zeta
jax.config.update("jax_enable_x64", True)
for q in [-3.3, -8.5, -9.5, -16.5]:
jax_v = float(jax.lax.zeta(jnp.float64(2), jnp.float64(q)))
sp_v = float(zeta(2, q))
print(f"zeta(2, {q:>6}): JAX = {jax_v:>20.4f} scipy = {sp_v:.4f}")
zeta(2, -3.3): JAX = 14.8177 scipy = 14.8177 ✓
zeta(2, -8.5): JAX = 6.7950 scipy = 9.7586 ✗ (30 % error)
zeta(2, -9.5): JAX = -2854289648037.6890 scipy = 9.7697 ✗ (catastrophic)
zeta(2, -16.5): JAX = -0.0588 scipy = 9.8108 ✗ (wrong sign)
(Note: q = integer ≤ 0 correctly returns inf — those are genuine poles.)
Root cause
XLA's Zeta implementation (xla/hlo/builder/lib/math.cc) uses an Euler-Maclaurin expansion with a fixed N = 9 initial terms. The paper it cites (Johansson 2015, eq. 5) requires ℜ(a + N) > 0 for convergence, i.e. q > -N. For q ≲ -9 the expansion diverges and produces garbage.
This is the same root cause as the lax.polygamma / digamma gradient issue tracked in #37635 and #11481. In those bugs polygamma(n, x) calls zeta(n+1, x) with a negative x, which is why the polygamma fix in openxla/xla#43545 works: it reflects x to a positive argument before calling Zeta. However, direct lax.zeta calls are not fixed by that PR.
Expected fix
Apply the Hurwitz zeta recurrence relation:
ζ(s, q) = q^{-s} + ζ(s, q+1)
repeatedly until q + k > 0, then call the Euler-Maclaurin expansion on the positive argument. This is the same strategy used in the Polygamma fix.
Alternatively, applying the Hurwitz reflection formula would work for integer s, but the recurrence is simpler and general.
The fix belongs in XLA (xla/hlo/builder/lib/math.cc) in the Zeta function, or as a JAX-level workaround analogous to the approach we used for polygamma (see #38085 and openxla/xla#43545).
Impact
Any JAX code computing lax.zeta(s, q) for negative non-integer q gets silently wrong results. This includes downstream computations in statistical models using the Hurwitz zeta function (e.g. Lerch transcendent, log-likelihood of certain distributions).
I’m interested in looking into this.
My plan is to first reproduce the mismatch on current main, then add a focused regression test for negative non-integer q values. After that I’ll investigate whether a minimal fix can live in JAX or whether this needs to be addressed closer to the XLA implementation.
If anyone is already working on this, let me know; otherwise I’m happy to take a pass at it.
This falls under #38651; please note that it may take a while for this to receive attention from the team.