Function to compute the log of the sum of exponent of a set of log-values.

log_of_sum_of_exp_x(x, na.rm = FALSE)

Arguments

x

A numeric vector of log-values.

na.rm

Logical; if TRUE removes NA values.

Value

The log of the sum of the exponentiated values of x.

Details

It is often useful to take the log of very small or large values (e.g., likelihoods). Working with log-values makes exponentiation, multiplication, and division easier. However, addition and subtraction become harder.

Assume a = log(a.) and b = log(b.) . The identity log(a. + b.) = a + log(1 + exp(b - a)) allows us to compute the log of the sum of the exponentiated values of a and b. This can be further extended to a series of log-values x = {x[1], x[2], ..., x[n]} as x[1] + log( 1 + exp(x[2] - x[1]) + ... + exp(x[n] - x[1]) ).

References

http://bayesfactor.blogspot.com/2016/05/numerical-pitfalls-in-computing-variance.html

https://stackoverflow.com/questions/65233445/how-to-calculate-sums-in-log-space-without-underflow

Examples

exp_x <- 1:4
x <- log( exp_x )
log( sum( exp_x) )
#> [1] 2.302585
log_of_sum_of_exp_x( x )
#> [1] 2.302585