Sometimes, you want to calculate the logarithm of a sum of exponentials, like:
(1) 
The problem to directly calculate this equation is that if one
gets sufficient large, it will overflow when you calculate the exponential of the number. We can get more arithmetic stability with a little algebra. First, find the maximum value
in
:
(2) 
Then, we have:
(3) 
So the largest value passed to the exponential function is 0. If there are really tiny values after subtraction, they’ll become zero and drop out, as they should with limited precision arithmetic.
By using the technique introduced here, we can also calculate:
(4) 
where
.
Reference
[1] Log Sum of Exponentials
[2] log-sum-exp trick