logmeanexp function

The log-mean-exp trick

The log-mean-exp trick

logmeanexp computes [REMOVE_ME]log1ni=1n ⁣exi,log(mean(exp(x))),[REMOVEME2] \log\frac{1}{n}\sum_{i=1}^n\!e^{x_i},log(mean(exp(x))), [REMOVE_ME_2]

avoiding over- and under-flow in doing so. It can optionally return an estimate of the standard error in this quantity.

logmeanexp(x, se = FALSE, ess = FALSE)

Arguments

  • x: numeric
  • se: logical; give approximate standard error?
  • ess: logical; give effective sample size?

Returns

log(mean(exp(x))) computed so as to avoid over- or underflow. If se = TRUE, the approximate standard error is returned as well. If ess = TRUE, the effective sample size is returned also.

Description

logmeanexp computes

log1ni=1n ⁣exi,log(mean(exp(x))), \log\frac{1}{n}\sum_{i=1}^n\!e^{x_i},log(mean(exp(x))),

avoiding over- and under-flow in doing so. It can optionally return an estimate of the standard error in this quantity.

Details

When se = TRUE, logmeanexp uses a jackknife estimate of the variance in log(x)log(x).

When ess = TRUE, logmeanexp returns an estimate of the effective sample size.

Examples

# takes too long for R CMD check ## an estimate of the log likelihood: ricker() |> pfilter(Np=1000) |> logLik() |> replicate(n=5) -> ll logmeanexp(ll) ## with standard error: logmeanexp(ll,se=TRUE) ## with effective sample size logmeanexp(ll,ess=TRUE)

Author(s)

Aaron A. King

  • Maintainer: Aaron A. King
  • License: GPL-3
  • Last published: 2025-04-16