sumlog: Log-sum from log data

Description Usage Arguments Details Value Author(s) References Examples

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

1
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

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

Value

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

Author(s)

Alessio Farcomeni alessio.farcomeni@uniroma1.it, Andy Leung andy.leung@stat.ubc.ca

References

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 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)

Example output

Loading required package: Rcpp
Loading required package: mvtnorm
Loading required package: MASS
[1] -Inf
[1] -749.8731
[1] -744.4401
[1] -744.9991
[1] 1.609438
[1] 1.609438
[1] 1.609438

snipEM documentation built on May 1, 2019, 8:49 p.m.

Related to sumlog in snipEM...