Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logstatsexp #77

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ChainRulesCore", "ChainRulesTestUtils", "ChangesOfVariables", "FiniteDifferences", "ForwardDiff", "InverseFunctions", "OffsetArrays", "Random", "Test"]
test = ["ChainRulesCore", "ChainRulesTestUtils", "ChangesOfVariables", "FiniteDifferences", "ForwardDiff", "InverseFunctions", "OffsetArrays", "Random", "Statistics", "Test"]
3 changes: 3 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ softmax!
softmax
cloglog
cexpexp
logmeanexp
logvarexp
logstdexp
```
4 changes: 3 additions & 1 deletion src/LogExpFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import LinearAlgebra

export xlogx, xlogy, xlog1py, xexpx, xexpy, logistic, logit, log1psq, log1pexp, log1mexp, log2mexp, logexpm1,
softplus, invsoftplus, log1pmx, logmxp1, logaddexp, logsubexp, logsumexp, logsumexp!, softmax,
softmax!, logcosh, logabssinh, cloglog, cexpexp
softmax!, logcosh, logabssinh, cloglog, cexpexp,
logmeanexp, logvarexp, logstdexp

# expm1(::Float16) is not defined in older Julia versions,
# hence for better Float16 support we use an internal function instead
Expand All @@ -22,6 +23,7 @@ end

include("basicfuns.jl")
include("logsumexp.jl")
include("logstatsexp.jl")

if !isdefined(Base, :get_extension)
include("../ext/LogExpFunctionsChainRulesCoreExt.jl")
Expand Down
38 changes: 38 additions & 0 deletions src/logstatsexp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
logmeanexp(A::AbstractArray; dims=:)

Computes `log.(mean(exp.(A); dims))`, in a numerically stable way.
"""
function logmeanexp(A::AbstractArray; dims=:)
R = logsumexp(A; dims=dims)
N = convert(eltype(R), length(A) ÷ length(R))
return R .- log(N)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause undesired promotions and allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better now.

Copy link
Member

@devmotion devmotion Nov 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you adressed my comment about promotions but not the allocations.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devmotion you might argue that unless there's a way to modify logsumexp to directly take log(N) into account, an in-place operation might create issues with AD systems which do not support it?

end

"""
logvarexp(A::AbstractArray; dims=:)

Computes `log.(var(exp.(A); dims))`, in a numerically stable way.
"""
function logvarexp(
A::AbstractArray; dims=:, corrected::Bool=true, logmean=logmeanexp(A; dims=dims)
)
R = logsumexp(2logsubexp.(A, logmean); dims=dims)
N = convert(eltype(R), length(A) ÷ length(R))
if corrected
return R .- log(N - 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this causes undesired promotions and allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think it's better now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unnecessary allocations are still present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I'm not sure what you are referring to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R .- log(N - 1)

creates a new array. But if R is e.g. of type Array this causes unnecessary allocations.

else
return R .- log(N)
end
end

"""
logstdexp(A::AbstractArray; dims=:)

Computes `log.(std(exp.(A); dims))`, in a numerically stable way.
"""
function logstdexp(
A::AbstractArray; dims=:, corrected::Bool=true, logmean=logmeanexp(A; dims=dims)
)
return logvarexp(A; dims=dims, corrected=corrected, logmean=logmean) / 2
end
30 changes: 30 additions & 0 deletions test/logstatsexp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Test: @test, @testset, @inferred
using Statistics: mean, var, std
using LogExpFunctions: logmeanexp, logvarexp, logstdexp

@testset "logmeanexp, logvarexp" begin
A = randn(5,3,2)
for dims in (2, (1,2), :)
@test logmeanexp(A; dims=dims) ≈ log.(mean(exp.(A); dims=dims))
for corrected in (true, false)
@test logvarexp(A; dims=dims, corrected=corrected) ≈ log.(var(exp.(A); dims=dims, corrected=corrected))
@test logstdexp(A; dims=dims, corrected=corrected) ≈ log.(std(exp.(A); dims=dims, corrected=corrected))
end
end
@test logvarexp(A; dims=2) ≈ log.(var(exp.(A); dims=2))
@test logstdexp(A; dims=2) ≈ log.(std(exp.(A); dims=2))
@test logmeanexp(A) ≈ log.(mean(exp.(A)))
@test logvarexp(A) ≈ log.(var(exp.(A)))
@test logstdexp(A) ≈ log.(std(exp.(A)))
end

@testset "logmeanexp properties" begin
X = randn(1000, 1000)
@test first(logmeanexp(logmeanexp(X; dims=1); dims=2)) ≈ logmeanexp(X)
@test first(logmeanexp(-logmeanexp(-X; dims=1); dims=2)) ≤ first(-logmeanexp(-logmeanexp(X; dims=1); dims=2))
x = randn()
@test logmeanexp([x]) ≈ x
X = randn(1000, 1000, 1)
@test logmeanexp(X; dims=3) ≈ X
@test -logmeanexp(-X) ≤ logmeanexp(X)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ include("basicfuns.jl")
include("chainrules.jl")
include("inverse.jl")
include("with_logabsdet_jacobian.jl")
include("logstatsexp.jl")
Loading