Given a vector of numeric values of real values
represented in log form, `logMeanExpLogs`

computes
the logarithm of the mean of the (exponentiated) values.
`logCumMeanExpLogs`

computes the logarithm of the
cumulative mean.

1 |

`v` |
A vector of (log) values |

Given a vector of values of log values `v`, one could
compute `log(mean(exp(v)))`

in R. However,
exponentiating and summing will cause a loss of
precision, and possibly an overflow. These functions use
the identity

*log(e^a + e^b) = a + log[ 1 + e^(b-a) ]*

and the method of computing *log(1+e^x)*
that avoids overflow (see the references). The code is
written in C for very fast computations.

`logMeanExpLogs`

returns a single value;
`logCumMeanExpLogs`

returns a vector of values of
the same length as `v`.

Richard D. Morey (richarddmorey@gmail.com)

For details of the approximation of
*log(1+e^x)* used to prevent loss of
precision, see
http://www.codeproject.com/Articles/25294/Avoiding-Overflow-Underflow-and-Loss-of-Precision.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ```
# Sample 100 values
y = log(rexp(100,1))
# These will give the same value,
# since e^y is "small"
logMeanExpLogs(y)
log(mean(exp(y)))
# We can make e^x overflow by multiplying
# e^y by e^1000
largeVals = y + 1000
# This will return 1000 + log(mean(exp(y)))
logMeanExpLogs(largeVals)
# This will overflow
log(mean(exp(largeVals)))
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.