sumlog {snipEM} | R Documentation |
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))).
sumlog(x,lower=-745,upper=709)
x |
Vector of log-values |
lower |
Value such that exp(lower-epsilon)=0 |
upper |
Value such that exp(upper+epsilon)=Inf |
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).
A scalar equal to log(sum(exp(x)))
.
Alessio Farcomeni alessio.farcomeni@uniroma1.it, Andy Leung andy.leung@stat.ubc.ca
Farcomeni, A. (2012) Quantile Regression for longitudinal data based on latent Markov subject-specific parameters. Statistics and Computing, 22, 141-152
# 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)