log1mexp: Compute log(1 - exp(-a)) and log(1 + exp(x)) Numerically...

View source: R/dpq-h.R

log1mexpR Documentation

Compute \mathrm{log}(1 - \mathrm{exp}(-a)) and \log(1 + \exp(x)) Numerically Optimally

Description

Compute f(a) = log(1 - exp(-a)) quickly and numerically accurately.

log1mexp() is simple pure R code;
log1mexpC() is an interface to R C API (‘Mathlib’ / ‘Rmath.h’) function.

log1pexpC() is an interface to R's ‘Mathlibdouble function log1pexp() which computes \log(1 + \exp(x)), accurately, notably for large x, say, x > 720.

Usage

log1mexp (x)
log1mexpC(x)
log1pexpC(x)

Arguments

x

numeric vector of positive values.

Author(s)

Martin Maechler

References

Martin Mächler (2012). Accurately Computing \log(1-\exp(-|a|)); https://CRAN.R-project.org/package=Rmpfr/vignettes/log1mexp-note.pdf.

See Also

The log1mexp() function in CRAN package copula, and the corresponding vignette (in the ‘References’).

Examples

l1m.xy <- curve(log1mexp(x), -10, 10, n=1001)
stopifnot(with(l1m.xy, all.equal(y, log1mexpC(x))))

x <- seq(0, 710, length=1+710*2^4); stopifnot(diff(x) == 1/2^4)
l1pm <- cbind(log1p(exp(x)),
              log1pexpC(x))
matplot(x, l1pm, type="l", log="xy") # both look the same
iF <- is.finite(l1pm[,1])
stopifnot(all.equal(l1pm[iF,2], l1pm[iF,1], tol=1e-15))

DPQ documentation built on Nov. 3, 2024, 3 a.m.