Learn R Programming

snipEM (version 1.0.1)

sumlog: Log-sum from log data

Description

Obtain log(sum(x)) from log(x), without passing to exponentials. It is based on the fact that log(a + b) = log(a) + log (1 + exp(log(b) - log(a))).

Usage

sumlog(x,lower=-745,upper=709)

Arguments

x

Vector of log-values

lower

Value such that exp(lower-epsilon)=0

upper

Value such that exp(upper+epsilon)=Inf

Value

A scalar equal to log(sum(exp(x))).

Details

This function computes the logarithm of the sum of exp(x), without passing through exponentials. It shall be used to avoid under/over flow. It has proven useful in computing the likelihood of finite mixture models, normalization constants, importance sampling, etc. It is described in the appendix of Farcomeni (2012).

References

Farcomeni, A. (2012) Quantile Regression for longitudinal data based on latent Markov subject-specific parameters. Statistics and Computing, 22, 141-152

Examples

Run this code
# NOT RUN {
# complete underflow without sumlog 
x <- c(-750,-752)
log(sum(exp(x)))
sumlog(x)

# imprecise sum 
x <- c(-745,-752)
log(sum(exp(x)))
sumlog(x)

# no issues 
x <- c(log(3),log(2))
log(5)
log(sum(exp(x)))
sumlog(x)
# }

Run the code above in your browser using DataLab