logspace.add | R Documentation |
Compute the log(arithm) of a sum (or difference) from the log of terms without causing overflows and without throwing away large handfuls of accuracy.
logspace.add(lx, ly)
:=\log (\exp (lx) + \exp (ly))
logspace.sub(lx, ly)
:=\log (\exp (lx) - \exp (ly))
logspace.add(lx, ly)
logspace.sub(lx, ly)
lx , ly |
numeric vectors, typically of the same
|
a numeric
vector of the same length as x+y
.
This is really from R's C source code for pgamma()
, i.e.,
‘<R>/src/nmath/pgamma.c’
The function definitions are very simple, logspace.sub()
using log1mexp()
.
Morten Welinder (for R's pgamma()
); Martin Maechler
lsum
, lssum
; then pgamma()
set.seed(12)
ly <- rnorm(100, sd= 50)
lx <- ly + abs(rnorm(100, sd=100)) # lx - ly must be positive for *.sub()
stopifnot(exprs = {
all.equal(logspace.add(lx,ly),
log(exp(lx) + exp(ly)), tol=1e-14)
all.equal(logspace.sub(lx,ly),
log(exp(lx) - exp(ly)), tol=1e-14)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.