BUG: icdf loses precision in the far tails for Cauchy, Exponential and Weibull
Describe the issue:
Three icdf implementations degrade for quantiles close to 0, and two of them fail outright.
Cauchy.icdf computes alpha + beta * tan(pi * (value - 0.5)). Once value drops below roughly 5.5e-17, value - 0.5 loses it completely, so the argument sits exactly on tan's pole and the result saturates at a constant:
| q | pm.icdf |
scipy | rel err |
|---|---|---|---|
| 1e-10 | -3183097229.94 | -3183098861.84 | 5.13e-07 |
| 1e-12 | -318314379920 | -318309886184 | 1.41e-05 |
| 1e-14 | -3.18780761675e+13 | -3.18309886184e+13 | 1.48e-03 |
| 1e-16 | -1.9789379661e+15 | -3.18309886184e+15 | 3.78e-01 |
| 1e-18 | -1.63312393532e+16 | -3.18309886184e+17 | 9.49e-01 |
| 1e-20 | -1.63312393532e+16 | -3.18309886184e+19 | 9.99e-01 |
Below 1e-17 it returns the same number however small the quantile gets, so the derivative goes flat along with it.
Exponential.icdf computes -mu * log(1 - value) and Weibull.icdf computes beta * (-log(1 - value)) ** (1 / alpha). 1 - value rounds to exactly 1 below machine epsilon, so both return -0:
| q | Exponential | scipy | Weibull | scipy |
|---|---|---|---|---|
| 1e-16 | 1.11022302463e-16 | 1e-16 | 1.05367121277e-08 | 1e-08 |
| 1e-17 | -0 | 1e-17 | -0 | 3.16227766e-09 |
| 1e-20 | -0 | 1e-20 | -0 | 1e-10 |
Normal, Laplace, Logistic, Gumbel, Pareto, Uniform, LogNormal, Triangular, HalfNormal and HalfCauchy are all accurate over the same range, so this is specific to these three.
It matters beyond precision: Truncated distributions call icdf, and it gets differentiated, so a saturated value means a flat gradient where it should be steep.
Reproduceable code example:
import numpy as np
import pymc as pm
from pymc.logprob.basic import icdf
from scipy import stats
qs = np.array([1e-12, 1e-14, 1e-16, 1e-17, 1e-18, 1e-20])
for name, rv, ref in [
("Cauchy", pm.Cauchy.dist(alpha=0.0, beta=1.0), stats.cauchy()),
("Exponential", pm.Exponential.dist(lam=1.0), stats.expon()),
("Weibull", pm.Weibull.dist(alpha=2.0, beta=1.0), stats.weibull_min(2.0)),
]:
print(name)
print(" pymc :", np.asarray(icdf(rv, qs).eval(), dtype=float))
print(" scipy:", ref.ppf(qs))Error message:
No exception is raised. icdf returns a saturated constant (Cauchy) or -0
(Exponential, Weibull).PyMC version information:
- PyMC version: 6.3.2+3.g3f3b1082e
- PyTensor version: 3.3.1
- Python version: 3.12.13
- OS: macOS 26.6.2 (arm64)
- Install method: pip, editable install from main
Context for the issue:
All three have exact cancellation-free equivalents.
For Cauchy, tan(pi * (v - 1/2)) equals -1 / tan(pi * v) for v < 1/2 and
1 / tan(pi * (1 - v)) for v > 1/2. Neither form cancels, and neither evaluates tan
near its pole. The original expression is worth keeping around the median, where it
cannot cancel and is exactly alpha at v = 1/2, which the reciprocal form is not.
Exponential and Weibull want log1p(-value).
I have a patch with regression tests ready and will open a PR.
Source: pymc-devs/pymc