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.xlogx
— Functionxlogx(x)
Return x * log(x)
for x ≥ 0
, handling $x = 0$ by taking the downward limit.
julia> xlogx(0)
0.0
LogExpFunctions.xlogy
— Functionxlogy(x, y)
Return x * log(y)
for y > 0
with correct limit at $x = 0$.
julia> xlogy(0, 0)
0.0
LogExpFunctions.logistic
— FunctionLogExpFunctions.logit
— FunctionLogExpFunctions.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
.
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 as log1p(-expm1(x))
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
.
LogExpFunctions.logmxp1
— Functionlogmxp1(x)
Return log(x) - x + 1
carefully evaluated.
LogExpFunctions.logaddexp
— Functionlogaddexp(x, y)
Return log(exp(x) + exp(y))
, avoiding intermediate overflow/undeflow, and handling non-finite values.
LogExpFunctions.logsumexp
— Functionlogsumexp(X)
Compute log(sum(exp, X))
in a numerically stable way that avoids intermediate over- and underflow.
X
should be an iterator of real or complex numbers. The result is computed using a single pass over the data.
References
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
LogExpFunctions.softmax!
— Functionsoftmax!(r, x)
Overwrite r
with the softmax
(or normalized exponential) transformation of x
That is, r
is overwritten with exp.(x)
, normalized to sum to 1.
See the Wikipedia entry
softmax!(x)
Return the softmax transformation
applied to x
in place.
LogExpFunctions.softmax
— Functionsoftmax(x)
Return the softmax transformation
applied to x
.