jax.scipy.stats.expon.logcdf returns -inf for small positive x (log1p(-sf) cancellation)
Description
Description
expon.logcdf is computed as log1p(-sf(x)). For standardized x below about 6e-8 (float32, JAX's
default) or 1.1e-16 (float64), sf(x) rounds to exactly 1 and logcdf returns -inf instead of the
finite log(x)-sized value, and gradients through it become inf. expon.cdf in the same module computes
the value accurately.
Steps to reproduce
import numpy as np
from jax.scipy.stats import expon
print(expon.logcdf(np.float32(1e-8))) # -inf, exact log(-expm1(-1e-8)) = -18.4207
Expected
logcdf(x) = log(-expm1(-x)), finite for every x > 0.
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
#38651
opened a pr for this #41011.. fix uses log1mexp so the small x cancellation is gone, logcdf(np.float32(1e-8)) now returns -18.420681 instead of -inf and the gradient stays finite. tests added in tests/scipy_stats_test.py