Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use log/exp transformation to make the log likelihood computation of a simple mixture model more robust.
# making mixture models numerically robust
set.seed(4)
rm(list = ls())
nobs = 1000
alpha = 0.1
p = runif(nobs, min = 0.0, max = 1.0)
q = runif(nobs, min = 0.0, max = 1.0)
# naive computation of log-likelihood contribution
log(alpha * prod(p) + (1-alpha) * prod(q))
# sum up the logs
psum = sum(log(c(alpha, p)))
qsum = sum(log(c(1 - alpha, q)))
# use exp/log transformation
const = 1 - max(c(psum, qsum))
# put it back together
log(exp(psum + const) + exp(qsum + const)) - const
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.