jax.random.poisson: samples for large lam (>~2e5) have the wrong variance (+46% at lam=1e8)
Description
Description
For rate parameters above roughly 2e5, jax.random.poisson returns samples whose variance differs from
lam (the mean is correct): +2.4% at lam=1e6, +46% at lam=1e8. poisson casts lam to float32,
and _poisson_rejection computes the acceptance bound t = -lam + k*log(lam) - lgamma(k+1) as a
difference of two terms of size about lam*log(lam); in float32 its absolute error exceeds 1 for large
lam, so the acceptance test is close to random for the proposals that reach it.
Steps to reproduce
import jax, numpy as np
for lam in (1e4, 1e8):
s = np.asarray(jax.random.poisson(jax.random.key(0), lam, (200_000,))).astype(np.float64)
print(lam, s.mean() / lam, s.var() / lam)
# 1e4: 1.0000 0.997 ; 1e8: 1.0000 1.456 (Poisson: variance / lam = 1)
Expected
variance / lam close to 1 for every lam (within sampling error, about 0.003 here).
Suggested fix
Evaluate the acceptance test without the cancellation, e.g. with the log-factorial difference written as k*log(lam/k) + (k - lam) - 0.5*log(2*pi*k) - stirling_correction(k) (as NumPy's PTRS sampler does in double precision), or compute t in float64 when x64 is enabled.
System info (python version, jaxlib version, accelerator, etc.)
Environment
JAX / jaxlib 0.11.2 (the latest release, CPU) and 0.10.1, Python 3.12, Ubuntu 24.04, x86_64
Mathematical and Precision Analysis of the Variance Discrepancy
In _poisson_rejection (jax/_src/random/core.py), the rejection threshold is evaluated as:
t = -lam + k * log_lam - lax_special.lgamma(k + 1)
For proposals $ around $\lambda = 10^8$:
- \ln(\lambda) \approx 10^8 \times \ln(10^8) \approx 1.842 \times 10^9$.
- In single-precision (
float32), the machine epsilon around .84 \times 10^9$ is: 2059\text{spacing}(\text{float32}(1.84 \times 10^9)) = 128.02059 - Meanwhile, the true value of $ for valid candidate proposals (e.g. = \lambda + 0.5\sigma$) is on the order of: 2059t \approx -10.252059 while \approx \ln(v) \in [-5, 0]$.
Because the ULP ($\pm 128.0$) is more than an order of magnitude larger than the entire dynamic range of $ and $, the acceptance condition s <= t is completely dominated by floating-point quantization noise rather than the intended Poisson envelope ratio.
Analytic Reduction
By expanding $\ln(k!) = \text{lgamma}(k+1)$ via Stirling's series: 2059\text{lgamma}(k+1) = k \ln(k) - k + \frac{1}{2}\ln(2\pi k) + \text{stirling_err}(k)2059 the acceptance threshold simplifies analytically to: 2059t = k \ln\left(\frac{\lambda}{k}\right) + (k - \lambda) - \frac{1}{2}\ln(2\pi k) - \text{stirling_err}(k)2059 For = \lambda + \delta \sqrt{\lambda}$, expanding $\ln(\lambda/k) = -\ln(1 + \delta/\sqrt{\lambda})$ yields: 2059k \ln\left(\frac{\lambda}{k}\right) + (k - \lambda) = -\frac{1}{2}\delta^2 + O\left(\frac{1}{\sqrt{\lambda}}\right)2059 Every term in this difference formulation is naturally (1)$ to (\ln \lambda)$, completely avoiding the catastrophic loss of precision from subtracting (10^9)$ terms.