LogStatFunctions.jl
Numerically stable logarithms of the mean, variance, and standard deviation of exponentials — a small companion to LogExpFunctions.jl.
Given an array A, these compute log-of-a-statistic-of-exp.(A) without ever forming exp.(A) (which would overflow for large entries):
| function | computes |
|---|---|
logmeanexp(A; dims=:) | log.(mean(exp.(A); dims)) |
logvarexp(A; dims=:, corrected=true) | log.(var(exp.(A); dims, corrected)) |
logstdexp(A; dims=:, corrected=true) | log.(std(exp.(A); dims, corrected)) |
using LogStatFunctions
A = 1000 .* randn(10^4) # exp.(A) would overflow Float64
logmeanexp(A) # finite and accurate
logstdexp(A; corrected=false)logvarexp and logstdexp also accept logmean to reuse a precomputed logmeanexp(A; dims).
Each function has an in-place variant (logmeanexp!, logvarexp!, logstdexp!) that writes the result into a preallocated output array, reducing over the singleton dimensions of out:
A = randn(10, 5)
out = zeros(1, 5)
logmeanexp!(out, A) # same as logmeanexp(A; dims=1)All three functions have ChainRules derivative rules for real arrays (loaded automatically when ChainRulesCore is in the environment), so they can be differentiated with ChainRules-based AD packages such as Zygote. Complex arrays are not covered by these rules.
Reference
LogStatFunctions.logmeanexp — Function
logmeanexp(A; dims=:)Computes log.(mean(exp.(A); dims)), in a numerically stable way.
LogStatFunctions.logvarexp — Function
logvarexp(A; dims=:, corrected=true, logmean=logmeanexp(A; dims))Computes log.(var(exp.(A); dims)), in a numerically stable way.
LogStatFunctions.logstdexp — Function
logstdexp(A; dims=:, corrected=true, logmean=logmeanexp(A; dims))Computes log.(std(exp.(A); dims)), in a numerically stable way.
LogStatFunctions.logmeanexp! — Function
logmeanexp!(out, A)In-place version of logmeanexp: computes log.(mean(exp.(A); dims)) over the singleton dimensions of out, and writes the result to out.
LogStatFunctions.logvarexp! — Function
logvarexp!(out, A; corrected=true, logmean=logmeanexp!(similar(out), A))In-place version of logvarexp: computes log.(var(exp.(A); dims, corrected)) over the singleton dimensions of out, and writes the result to out.
LogStatFunctions.logstdexp! — Function
logstdexp!(out, A; corrected=true, logmean=logmeanexp!(similar(out), A))In-place version of logstdexp: computes log.(std(exp.(A); dims, corrected)) over the singleton dimensions of out, and writes the result to out.