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 supports InverseFunctions.inverse and ChangesOfVariables.test_with_logabsdet_jacobian for log1mexp, log1pexp, log2mexp, logexpm1, logistic, logit, loglogistic, logitexp, log1mlogistic, logit1mexp, and logcosh (no inverse).
LogExpFunctions.xlogx — Functionxlogx(x)
Return x * log(x) for x ≥ 0, handling $x = 0$ by taking the downward limit.
julia> xlogx(0)
0.0LogExpFunctions.xlogy — Functionxlogy(x, y)
Return x * log(y) for y > 0 with correct limit at $x = 0$.
julia> xlogy(0, 0)
0.0LogExpFunctions.xlog1py — Functionxlog1py(x, y)
Return x * log(1 + y) for y ≥ -1 with correct limit at $x = 0$.
julia> xlog1py(0, -1)
0.0LogExpFunctions.xexpx — Functionxexpx(x)
Return x * exp(x) for x > -Inf, or zero if x == -Inf.
julia> xexpx(-Inf)
0.0LogExpFunctions.xexpy — Functionxexpy(x, y)
Return x * exp(y) for y > -Inf, or zero if y == -Inf or if x == 0 and y is finite.
julia> xexpy(1.0, -Inf)
0.0LogExpFunctions.logistic — Functionlogistic(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.logit — Functionlogit(x)
The logit or log-odds transformation, defined as
\[\operatorname{logit}(x) = \log\left(\frac{x}{1-x}\right)\]
for $0 < x < 1$.
Its inverse is the logistic function.
LogExpFunctions.logcosh — Functionlogcosh(x)
Return log(cosh(x)), carefully evaluated without intermediate calculation of cosh(x).
The implementation ensures logcosh(-x) = logcosh(x).
LogExpFunctions.logabssinh — Functionlogabssinh(x)
Return log(abs(sinh(x))), carefully evaluated without intermediate calculation of sinh(x).
The implementation ensures logabssinh(-x) = logabssinh(x).
LogExpFunctions.logabstanh — Functionlogabstanh(x)
Return log(abs(tanh(x))), evaluated carefully.
The implementation ensures logabstanh(-x) = logabstanh(x).
LogExpFunctions.log1psq — Functionlog1psq(x)
Return log(1+x^2) evaluated carefully for abs(x) very small or very large.
LogExpFunctions.log1pexp — Functionlog1pexp(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.
This is also called the "softplus" transformation (in its default parametrization, see softplus), being a smooth approximation to max(0,x).
See:
- Martin Maechler (2012) “Accurately Computing log(1 − exp(− |a|))”
LogExpFunctions.softplus — Functionsoftplus(x)
The generalized softplus function (Wiemann et al., 2024) takes an additional optional parameter a that control the approximation error with respect to the linear spline. It defaults to a=1.0, in which case the softplus is equivalent to log1pexp.
See:
- Wiemann, P. F., Kneib, T., & Hambuckers, J. (2024). Using the softplus function to construct alternative link functions in generalized linear models and beyond. Statistical Papers, 65(5), 3155-3180.
LogExpFunctions.invsoftplus — Functioninvsoftplus(y)
The inverse generalized softplus function (Wiemann et al., 2024). See softplus.
LogExpFunctions.log1mexp — Functionlog1mexp(x)
Return log(1 - exp(x))
See:
- Martin Maechler (2012) “Accurately Computing log(1 − exp(− |a|))”
Note: different than Maechler (2012), no negation inside parentheses
LogExpFunctions.log2mexp — Functionlog2mexp(x)
Return log(2 - exp(x)) evaluated carefully.
LogExpFunctions.logexpm1 — Functionlogexpm1(x)
Return log(exp(x) - 1) or the “invsoftplus” function. It is the inverse of log1pexp (aka “softplus”).
LogExpFunctions.log1pmx — Functionlog1pmx(x)
Return log(1 + x) - x.
Use naive calculation or range reduction outside kernel range. Accurate ~2ulps for all x. This will fall back to the naive calculation for argument types different from Float32, Float64.
LogExpFunctions.logmxp1 — Functionlogmxp1(x)
Return log(x) - x + 1 carefully evaluated. This will fall back to the naive calculation for argument types different from Float64.
LogExpFunctions.logaddexp — Functionlogaddexp(x, y)
Return log(exp(x) + exp(y)), avoiding intermediate overflow/undeflow, and handling non-finite values.
LogExpFunctions.logsubexp — Functionlogsubexp(x, y)
Return log(abs(exp(x) - exp(y))), preserving numerical accuracy.
LogExpFunctions.logsumexp — Functionlogsumexp(X)
Compute log(sum(exp, X)).
X should be an iterator of real or complex numbers. The result is computed in a numerically stable way that avoids intermediate over- and underflow, using a single pass over the data.
See also logsumexp!.
References
logsumexp(X; dims)
Compute log.(sum(exp.(X); dims=dims)).
The result is computed in a numerically stable way that avoids intermediate over- and underflow, using a single pass over the data.
See also logsumexp!.
References
LogExpFunctions.logsumexp! — Functionlogsumexp!(out, X)
Compute logsumexp of X over the singleton dimensions of out, and write results to out.
The result is computed in a numerically stable way that avoids intermediate over- and underflow, using a single pass over the data.
See also logsumexp.
References
LogExpFunctions.softmax! — Functionsoftmax!(r::AbstractArray{<:Real}, x::AbstractArray{<:Real}=r; dims=:)Overwrite r with the softmax transformation of x over dimension dims.
That is, r is overwritten with exp.(x), normalized to sum to 1 over the given dimensions.
See also: softmax
LogExpFunctions.softmax — Functionsoftmax(x::AbstractArray{<:Real}; dims=:)Return the softmax transformation of x over dimension dims.
That is, return exp.(x), normalized to sum to 1 over the given dimensions.
See also: softmax!
LogExpFunctions.cloglog — Functioncloglog(x)
Compute the complementary log-log, log(-log(1 - x)).
LogExpFunctions.cexpexp — Functioncexpexp(x)
Compute the complementary double exponential, 1 - exp(-exp(x)).
LogExpFunctions.loglogistic — Functionloglogistic(x)
Return log(logistic(x)), computed more carefully and with fewer calls than the naive composition of functions.
Its inverse is the logitexp function.
LogExpFunctions.logitexp — Functionlogitexp(x)
Return logit(exp(x)), computed more carefully and with fewer calls than the naive composition of functions.
Its inverse is the loglogistic function.
LogExpFunctions.log1mlogistic — Functionlog1mlogistic(x)
Return log(1 - logistic(x)), computed more carefully and with fewer calls than the naive composition of functions.
Its inverse is the logit1mexp function.
LogExpFunctions.logit1mexp — Functionlogit1mexp(x)
Return logit(1 - exp(x)), computed more carefully and with fewer calls than the naive composition of functions.
Its inverse is the log1mlogistic function.