Why is this optimization problem so slow in Julia? In R it took 60s

On an side note, it’s a nice little puzzle to compute this function without running into accuracy problems and problems from overflow/underflow. Consider

julia> f(x) = log(1/(1+exp(-x)))

julia> f(100), Float64(f(big"100.0")) # spurious underflow: no correct digits
(0.0, -3.720075976020836e-44)

julia> f(-1000), Float64(f(big"-1000.0")) # spurious overflow
(-Inf, -1000.0)

julia> f(20), Float64(f(big"20.0"))  # only 8 correct digits
(-2.0611536942919273e-9, -2.061153620314381e-9)

One alternative is:

g(x) = x > 0 ? -log1p(exp(-x)) : x - log1p(exp(x))
12 Likes