Calculates log(1+exp(x)) in a numerically stable fashion.

log1pexp(x)

Arguments

x

a numeric vector.

Value

A numeric vector where the i-th entry is equal to log(1+exp(x[i])), but computed more stably.

Details

We follow the recipe of Machler (2012), that is formula (10) page 7.

References

Machler, M. (2012). Accurately computing log(1-exp(-|a|)). URL: https://cran.r-project.org/package=Rmpfr/vignettes/log1mexp-note.pdf.

Examples

set.seed(141) library(qgam); x <- rnorm(100, 0, 100) log1pexp(x) - log1p(exp(x))
#> [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #> [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #> [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0