jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.scipy.stats.expon.logcdf returns -inf for small positive x (log1p(-sf) cancellation)

Open the-Shallow opened this issue 2 months ago • 2 comments

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

the-Shallow avatar Sep 25 '26 03:09 the-Shallow

#38651

jakevdp avatar Sep 25 '26 12:09 jakevdp

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

abhinav-phi avatar Sep 27 '26 03:09 abhinav-phi