Skip to content
fn

log_ndtr

→Tensor
log_ndtr(input: Tensor)

Numerically stable logarithm of the standard normal CDF.

Computes log⁡Φ(x)\log \Phi(x) without losing precision in the deep left tail, where the direct composition log(ndtr(x)) underflows. Essential for log-likelihood evaluation of probit / Tobit / censored models that need to handle x≪0x \ll 0 cleanly.

Parameters

inputTensor
Input tensor; any floating-point dtype.

Returns

Tensor

log⁡Φ(x)\log \Phi(x) element-wise, same shape and dtype as input; values lie in (−∞,0](-\infty, 0].

Notes

The implementation has three branches; with t=−x/2t = -x/\sqrt{2} in the lower tail and u=x/2u = x/\sqrt{2} in the upper one,

log⁡Φ(x)={log⁡ ⁣(12 erfcx(t))−t2,x<−1log⁡Φ(x),−1≤x≤22log1p⁡ ⁣(−12 erfcx(u) e−u2),x>22.\log \Phi(x) = \begin{cases} \log\!\left(\tfrac{1}{2}\,\mathrm{erfcx}(t)\right) - t^2, & x < -1 \\[2pt] \log \Phi(x), & -1 \le x \le 2\sqrt{2} \\[2pt] \operatorname{log1p}\!\left( -\tfrac{1}{2}\,\mathrm{erfcx}(u)\, e^{-u^2}\right), & x > 2\sqrt{2}. \end{cases}

Between the cuts 0.16≲Φ(x)≲0.9980.16 \lesssim \Phi(x) \lesssim 0.998, so the direct log(ndtr(x)) is safe. Below x=−1x = -1 the identity Φ(x)=12 erfc(t)=12 erfcx(t) e−t2\Phi(x) = \tfrac{1}{2}\,\mathrm{erfc}(t) = \tfrac{1}{2}\,\mathrm{erfcx}(t)\, e^{-t^2} is used: erfc(t) itself underflows to zero near x=−13x = -13 in float32 (and loses its relative precision well before that), while erfcx stays bounded and the Gaussian factor comes out exactly as −t2-t^2.

The upper tail fails the other way round. There log⁡Φ(x)≈−Q\log \Phi(x) \approx -Q with Q=1−Φ(x)Q = 1 - \Phi(x) small, and forming Φ(x)\Phi(x) first leaves QQ to rounding: float32 answered -2.98e-7 at x=5x = 5 for -2.867e-7, and 0 from x≈5.4x \approx 5.4 on. Computing Q=12 erfcx(u) e−u2Q = \tfrac{1}{2}\,\mathrm{erfcx}(u)\, e^{-u^2} directly and taking log1p of its negative keeps the relative precision until QQ itself underflows.

Examples

>>> import lucid
>>> from lucid.special import log_ndtr
>>> log_ndtr(lucid.tensor([-10.0, -1.0, 0.0, 1.0]))
tensor([-53.23, -1.841, -0.6931, -0.1728])