Learn R Programming

copula (version 0.999-7)

log1mexp: Compute f(a) = $\mathrm{log}$(1 - $\mathrm{exp}$(-a)) Numerically Optimally

Description

Compute f(a) = log(1 - exp(-a)) stably

Usage

log1mexp(a, cutoff = log(2))

Arguments

a
numeric vector of positive values
cutoff
positive number; log(2) is optimal, but the exact value is unimportant, and anything in $[0.5, 1]$ is fine.

Value

  • f(a) == log(1 - exp(-a)) == log1p(-exp(-a)) == log(-expm1(-a))

References

Martin Mächler{Maechler} (2012). Accurately Computing $\log(1-\exp(-|a|))$; http://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf.

Examples

Run this code
a <- 2^seq(-58,10, length = 256)
fExpr <- expression(
          log(1 - exp(-a)),
          log(-expm1(-a)),
          log1p(-exp(-a)),
          log1mexp(a))
names(fExpr) <- c("DEF", "expm1", "log1p", "F")
str(fa <- do.call(cbind, as.list(fExpr)))
matplot(a, fa, type = "l", log = "x")# don't see the big difference
legend("topleft", fExpr, col=1:4, lty=1:4, bty="n")

head(fa)# expm1() works here
tail(fa)# log1p() works here

matplot(a, -fa, type = "l", log = "xy")# don't see the big difference
legend("left", fExpr, col=1:4, lty=1:4, bty="n")

Run the code above in your browser using DataLab