jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.scipy.special.betaln returns NaN for infinite arguments where infinities are expected

Open ALinrunrun opened this issue 4 months ago • 4 comments

Description

jax.scipy.special.betaln appears to return NaN for several inputs involving inf, where infinite limiting values are expected.

For the log-beta function, these cases have well-defined limiting behavior. For example, if beta(inf, b) tends to 0.0 for positive finite b, then betaln(inf, b) should tend to -inf. Similarly, betaln(a, inf) should tend to -inf for positive finite a.

However, JAX returns NaN for all tested cases below.

Minimal reproducible example

#!/usr/bin/env python3
import os
import sys
import warnings

warnings.filterwarnings("ignore")
os.environ.setdefault("JAX_PLATFORMS", "cpu")

try:
    import numpy as np
    import jax.numpy as jnp
    import jax.scipy.special as jsp
except ImportError as e:
    print(f"missing dep: {e}")
    sys.exit(2)

a = np.array([0.0, np.inf, np.inf, 10.0, 0.25], dtype=np.float32)
b = np.array([0.0, 0.25, 2.0, np.inf, np.inf], dtype=np.float32)

expected = np.array([np.inf, -np.inf, -np.inf, -np.inf, -np.inf], dtype=np.float32)
out = np.asarray(jsp.betaln(jnp.asarray(a), jnp.asarray(b)))

print("jax:", out.tolist())
print("expected:", expected.tolist())

if not np.array_equal(out, expected, equal_nan=True):
    sys.exit(0)
sys.exit(1)

Actual result

jax: [nan, nan, nan, nan, nan]
expected: [inf, -inf, -inf, -inf, -inf]

Expected result

[inf, -inf, -inf, -inf, -inf]

The tested cases are:

betaln(0.0, 0.0)      -> inf
betaln(inf, 0.25)     -> -inf
betaln(inf, 2.0)      -> -inf
betaln(10.0, inf)     -> -inf
betaln(0.25, inf)     -> -inf

JAX instead returns NaN for all of these cases.

System info (python version, jaxlib version, accelerator, etc.)

jax: 0.10.1 jaxlib: 0.10.1 numpy: 2.4.6 python: 3.13.13 platform: Ubuntu 24.04.3 LTS, Linux x86_64 accelerator: CPU backend

Note: GPU is present, but CUDA-enabled jaxlib is not installed, so JAX falls back to CPU.

ALinrunrun avatar Jul 12 '26 22:07 ALinrunrun

This falls under https://github.com/jax-ml/jax/issues/38651; please note that it may take a while for this to receive attention from the team.

jakevdp avatar Jul 12 '26 23:07 jakevdp

Reproduced on 0.10.2: betaln(inf, 2.0) and betaln(2.0, inf) return nan, where SciPy returns -inf (the betaln(inf, inf) case does correctly match SciPy at nan). The implementation (jax/_src/third_party/scipy/betaln.py:59-63) computes min/max, then large_b = lgamma(a) + algdiv(a, b) for b >= 8, with no handling of infinite arguments — algdiv(a, inf) is undefined and yields nan rather than the limiting value. Mathematically B(a, inf) -> 0 for finite a > 0, so betaln(a, inf) should be -inf. A guard returning -inf when exactly one argument is +inf and the other is finite and positive (and leaving the inf, inf and non-positive cases to fall through to the current nan) would match SciPy for the reported cases. @jakevdp

CodersAcademy006 avatar Jul 16 '26 09:07 CodersAcademy006

Opened PR #39682 with a fix. Root cause: algdiv computes u = d * log1p(a/b) where d=inf and log1p(a/inf)=0, giving inf * 0 = NaN. Fixed by setting u=0 when b is infinite.

AloysJehwin avatar Aug 03 '26 16:08 AloysJehwin

Thanks for the context on #38651 and the broader situation. Understood on the triage burden — that makes sense.

One thing I'd genuinely like to do is contribute something useful to JAX rather than add to the pile. Is there a specific area or issue type where external contributions are currently welcome and would make it through review? Happy to look at something more carefully scoped if you can point me in a direction.

AloysJehwin avatar Aug 03 '26 18:08 AloysJehwin