LogExpFunctions.jl icon indicating copy to clipboard operation
LogExpFunctions.jl copied to clipboard

[FR] smoothed log sum implementation

Open floswald opened this issue 4 years ago • 1 comments

thanks for this cool little package. I have a feature request/question. In discrete choice models (for example in this paper equation 14 ) we often have a smoothed version of the log sum function. that is, instead of

alpha + log( sum (exp( x - alpha ) ) )

we'd have

alpha + σ log( sum (exp( (x - alpha)/σ ) ) )

I was trying to think how to add this to the package (maybe in my own fork if this turns out non of interest here), but I'm not totally sure where. My best guess would have been to do the division by sigma in places like here, but not totally certain. thanks for any hints!

floswald avatar May 02 '21 14:05 floswald

I've encountered this smoothed version in OT (e.g., https://papers.nips.cc/paper/2016/file/2a27b8144ac02f67687f76782a3b5d8f-Paper.pdf) but just used logsumexp with an iterator to implement it (https://github.com/devmotion/StochasticOptimalTransport.jl/blob/7960dc8668c27236da3b0429a86fb2ad57f9c382/src/utils.jl#L111-L113, seems I should switch to LogExpFunctions now it's reexported from StatsFuns).

Unfortunately, the implementation of logsumexp is not as simple and straightforward to read as I would like it to be, mainly because it is supposed to cover a variety of different use cases in an optimal way, e.g., regular arrays, iterators, and GPU arrays (https://github.com/JuliaStats/StatsFuns.jl/pull/97). It's an implementation of the one-pass algorithm. To me it seems one would have to change https://github.com/JuliaStats/LogExpFunctions.jl/blob/9e5de8108a264fc2c1695edbad1657db0b46ee40/src/logsumexp.jl#L35 and https://github.com/JuliaStats/LogExpFunctions.jl/blob/9e5de8108a264fc2c1695edbad1657db0b46ee40/src/logsumexp.jl#L46 (there the final result is computed in a numerically stable way from alpha and sum(exp(x - alpha)) - 1) and one would have to modify the reductions in https://github.com/JuliaStats/LogExpFunctions.jl/blob/9e5de8108a264fc2c1695edbad1657db0b46ee40/src/logsumexp.jl#L64, https://github.com/JuliaStats/LogExpFunctions.jl/blob/9e5de8108a264fc2c1695edbad1657db0b46ee40/src/logsumexp.jl#L72, and https://github.com/JuliaStats/LogExpFunctions.jl/blob/9e5de8108a264fc2c1695edbad1657db0b46ee40/src/logsumexp.jl#L86.

devmotion avatar May 02 '21 15:05 devmotion