JuliaStats / StatsFuns.jl

Mathematical functions related to statistics.
Other
235 stars 40 forks source link

weighted logsumexp #101

Closed mileslucas closed 2 years ago

mileslucas commented 4 years ago

I ran into a use case where I needed a weighted sum in the logsumexp function when copying some python code.

There are no performance regressions in the new version, I also removed the TODO regarding some special-casing on the value u because without it the performance is much worse (30 ns -> 1 us) for cases where dims=:.

Here are the benchmarks I did to make sure there's still a super-fast no allocation for the simplest case

julia> @benchmark logsumexp($([1.0, 2.0]))
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     32.608 ns (0.00% GC)
  median time:      33.277 ns (0.00% GC)
  mean time:        34.576 ns (0.00% GC)
  maximum time:     101.738 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     993

julia> @benchmark logsumexp($([1.0, 2.0]), $([-0.5, 0.5]))
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     36.436 ns (0.00% GC)
  median time:      37.050 ns (0.00% GC)
  mean time:        38.228 ns (0.00% GC)
  maximum time:     150.541 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     992
andreasnoack commented 2 years ago

Should now be against https://github.com/JuliaStats/LogExpFunctions.jl since logsumexp has moved there.