Skip to main content

Module log_functions

Module log_functions 

Source
Expand description

Functions for computing in the log domain without overflow or cancellation (SciPy’s logsumexp, softmax, log_softmax, logit, expit, log_expit, log_ndtr), and the Box-Cox power transforms (boxcox, boxcox1p, inv_boxcox).

See the book entry Log-domain functions and inverse incomplete integrals.

Functions§

boxcox
The Box-Cox transform (x^λ − 1)/λ, and ln x for λ = 0 (SciPy’s special.boxcox), written with exp_m1 so that it is continuous and accurate near λ = 0.
boxcox1p
The Box-Cox transform of 1 + x, accurate for small x (SciPy’s boxcox1p).
expit
The logistic sigmoid 1 / (1 + e^{−x}), evaluated so that neither branch overflows.
inv_boxcox
The inverse of boxcox: (1 + λy)^{1/λ}, and e^y for λ = 0.
log_expit
ln expit(x) = −ln(1 + e^{−x}), accurate for large |x|.
log_ndtr
The logarithm of the standard normal CDF, ln Φ(x) (SciPy’s log_ndtr), accurate in both tails: for x > −1 as ln(1 − Φ(−x)) with ln_1p; for −20 < x ≤ −1 from erfc, which is accurate relative to its tiny value; below that from the asymptotic series ln Φ(x) = −x²/2 − ln(−x) − ½ ln 2π + ln(1 − 1/x² + 3/x⁴ − 15/x⁶ + …).
log_softmax
aᵢ − logsumexp(a), the logarithm of softmax without underflow.
logit
The log-odds ln(p / (1 − p)), the inverse of expit.
logsumexp
ln Σ exp(aᵢ) without overflow (SciPy’s logsumexp): with m = max aᵢ, the sum is m + ln Σ exp(aᵢ − m), whose terms are at most 1. Returns −∞ for an empty slice or when every term is −∞, and +∞ if any term is +∞.
logsumexp_weighted
ln |Σ bᵢ exp(aᵢ)| and the sign of the sum, for weights of either sign (SciPy’s logsumexp with b and return_sign=True). The log is −∞ with sign 0 when the sum is zero.
softmax
exp(aᵢ) / Σ exp(aⱼ), computed as exp(aᵢ − logsumexp(a)).