signlogsumexp | R Documentation |
sum over signed numbers, with some logsumexp applied to positive and negative values separately
signlogsumexp(logabsv, sign)
logabsv |
a vector; positive or negative; log of abs(v) |
sign |
sign of v, a vector; +1 or -1. |
image
set.seed(123)
x <- rnorm(n = 100, mean = 1000, sd = 10)
comp <- c(log(sum(exp(x))),signlogsumexp(x,rep(1,length(x)))$res,logsumexp(x))
print(comp)
sign_indicators <- c(rep(1,length(x)-50),rep(-1,length(x)-50))
comp2 <- c(log(sum(sign_indicators*exp(x))),signlogsumexp(x,sign_indicators)$res)
print(comp2)
x = c(.5,-.4,-.1)
sum(x) # not zero
signlogsumexp(c(log(abs(x))),sign(x)) # not zero, but much closer to zero.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.