log_of_sum_of_exp_x: Compute the Log of the Sum of the Exponent of Log-Values

View source: R/S02_Math.R

log_of_sum_of_exp_xR Documentation

Compute the Log of the Sum of the Exponent of Log-Values

Description

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

Usage

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.

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]) ).

Value

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

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) )
log_of_sum_of_exp_x( x )


rettopnivek/arfpam documentation built on Oct. 20, 2024, 7:24 p.m.