File: logMeanExpLogs.Rd

package info (click to toggle)
r-cran-bayesfactor 0.9.12-4.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,512 kB
  • sloc: cpp: 1,555; sh: 16; makefile: 7
file content (67 lines) | stat: -rw-r--r-- 2,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/logMean.R
\name{logMeanExpLogs}
\alias{logMeanExpLogs}
\alias{logCumMeanExpLogs}
\alias{logSummaryStats}
\title{Functions to compute the logarithm of the mean (and cumulative means) of
vectors of logarithms}
\usage{
logMeanExpLogs(v)
}
\arguments{
\item{v}{A vector of (log) values}
}
\value{
\code{logMeanExpLogs} returns a single value,
\code{logCumMeanExpLogs} returns a vector of values of the same length as
\var{v}, and \code{logSummaryStats} returns a list of the
log mean, log variance, and cumulative log means.
}
\description{
Given a vector of numeric values of real values represented in log form,
\code{logMeanExpLogs} computes the logarithm of the mean of the
(exponentiated) values. \code{logCumMeanExpLogs} computes the logarithm of
the cumulative mean.
}
\details{
Given a vector of values of log values \var{v}, one could compute
\code{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 \deqn{\log(e^a + e^b) = a + \log(1+e^{b-a})}{log(e^a + e^b) = a +
log[ 1 + e^(b-a) ]} and the method of computing \eqn{\log(1+e^x)}{log(1+e^x)}
that avoids overflow (see the references). The code is written in C for very
fast computations.
}
\examples{

# 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)))

}
\references{
For details of the approximation of \eqn{\log(1+e^x)}{log(1+e^x)}
used to prevent loss of precision, see
\url{https://www.codeproject.com/Articles/25294/Avoiding-Overflow-Underflow-and-Loss-of-Precision} and
\url{https://www.johndcook.com/blog/standard_deviation/}.
}
\author{
Richard D. Morey (\email{richarddmorey@gmail.com})
}
\keyword{arith}
\keyword{misc}