LogExpFunctions

Various special functions based on log and exp moved from StatsFuns.jl into a separate package, to minimize dependencies. These functions only use native Julia code, so there is no need to depend on librmath or similar libraries. See the discussion at StatsFuns.jl#46.

The original authors of these functions are the StatsFuns.jl contributors.

LogExpFunctions.xlogxFunction
xlogx(x)

Return x * log(x) for x ≥ 0, handling $x = 0$ by taking the downward limit.

julia> xlogx(0)
0.0
LogExpFunctions.xlogyFunction
xlogy(x, y)

Return x * log(y) for y > 0 with correct limit at $x = 0$.

julia> xlogy(0, 0)
0.0
LogExpFunctions.logisticFunction
logistic(x)

The logistic sigmoid function mapping a real number to a value in the interval $[0,1]$,

\[\sigma(x) = \frac{1}{e^{-x} + 1} = \frac{e^x}{1+e^x}.\]

Its inverse is the logit function.

LogExpFunctions.logitFunction
logit(x)

The logit or log-odds transformation,

\[\log\left(\frac{x}{1-x}\right), \text{where} 0 < x < 1\]

Its inverse is the logistic function.

LogExpFunctions.log1psqFunction
log1psq(x)

Return log(1+x^2) evaluated carefully for abs(x) very small or very large.

LogExpFunctions.log1pexpFunction
log1pexp(x)

Return log(1+exp(x)) evaluated carefully for largish x.

This is also called the "softplus" transformation, being a smooth approximation to max(0,x). Its inverse is logexpm1.

LogExpFunctions.log1pmxFunction
log1pmx(x)

Return log(1 + x) - x.

Use naive calculation or range reduction outside kernel range. Accurate ~2ulps for all x.

LogExpFunctions.logaddexpFunction
logaddexp(x, y)

Return log(exp(x) + exp(y)), avoiding intermediate overflow/undeflow, and handling non-finite values.

LogExpFunctions.logsumexpFunction
logsumexp(X)

Compute log(sum(exp, X)) in a numerically stable way that avoids intermediate over- and underflow.

X should be an iterator of real numbers. The result is computed using a single pass over the data.

References

Sebastian Nowozin: Streaming Log-sum-exp Computation

logsumexp(X; dims)

Compute log.(sum(exp.(X); dims=dims)) in a numerically stable way that avoids intermediate over- and underflow.

The result is computed using a single pass over the data.

References

Sebastian Nowozin: Streaming Log-sum-exp Computation