In this section we would name probabilities in logarithmic space x' and y' for short: \begin{align} x' &= \log(x) \in \mathbb{R} \\ y' &= \log(y) \in \mathbb{R} \end{align} The product of probabilities x \cdot y corresponds to addition in logarithmic space. \log(x \cdot y) = \log(x) + \log(y) = x' + y' . The
sum of probabilities x + y is a bit more involved to compute in logarithmic space, requiring the computation of one exponent and one logarithm. However, in many applications a multiplication of probabilities (giving the probability of all independent events occurring) is used more often than their addition (giving the probability of at least one of mutually exclusive events occurring). Additionally, the cost of computing the addition can be avoided in some situations by simply using the highest probability as an approximation. Since probabilities are non-negative this gives a lower bound. This approximation is used in reverse to get a
continuous approximation of the max function.
Addition in log space \begin{align} &\log(x + y) \\ = {}& \log(x + x \cdot y / x) \\ = {}& \log(x + x \cdot \exp(\log(y / x))) \\ = {}& \log(x \cdot (1 + \exp(\log(y) - \log(x)))) \\ = {}& \log(x) + \log(1 + \exp(\log(y) - \log(x))) \\ = {}& x' + \log\left(1 + \exp\left(y' - x'\right)\right) \end{align} The formula above is more accurate than \log\left(e^{x'} + e^{y'}\right), provided one takes advantage of the asymmetry in the addition formula. {x'} should be the larger (least negative) of the two operands. This also produces the correct behavior if one of the operands is
floating-point negative infinity, which corresponds to a probability of zero. -\infty + \log\left(1 + \exp\left(y' - (-\infty)\right)\right) = -\infty + \infty This quantity is
indeterminate, and will result in
NaN. x' + \log\left(1 + \exp\left(-\infty - x'\right)\right) = x' + 0 This is the desired answer. The above formula alone will incorrectly produce an indeterminate result in the case where both arguments are -\infty . This should be checked for separately to return -\infty . For numerical reasons, one should use a function that computes \log(1+x) (
log1p) directly. ==See also==