Log probability

From HandWiki
Short description: Logarithm of probabilities, useful for calculations

In probability theory and computer science, a log probability is simply a logarithm of a probability.[1] The use of log probabilities means representing probabilities on a logarithmic scale [math]\displaystyle{ (-\inf, 0] }[/math], instead of the standard [math]\displaystyle{ [0, 1] }[/math] unit interval.

Since the probabilities of independent events multiply, and logarithms convert multiplication to addition, log probabilities of independent events add. Log probabilities are thus practical for computations, and have an intuitive interpretation in terms of information theory: the negative expected value of the log probabilities is the information entropy of an event. Similarly, likelihoods are often transformed to the log scale, and the corresponding log-likelihood can be interpreted as the degree to which an event supports a statistical model. The log probability is widely used in implementations of computations with probability, and is studied as a concept in its own right in some applications of information theory, such as natural language processing.

Motivation

Representing probabilities in this way has several practical advantages:

  1. Speed. Since multiplication is more expensive than addition, taking the product of a high number of probabilities is often faster if they are represented in log form. (The conversion to log form is expensive, but is only incurred once.) Multiplication arises from calculating the probability that multiple independent events occur: the probability that all independent events of interest occur is the product of all these events' probabilities.
  2. Accuracy. The use of log probabilities improves numerical stability, when the probabilities are very small, because of the way in which computers approximate real numbers.[1]
  3. Simplicity. Many probability distributions have an exponential form. Taking the log of these distributions eliminates the exponential function, unwrapping the exponent. For example, the log probability of the normal distribution's probability density function is [math]\displaystyle{ -((x-m_x)/\sigma_m)^2+C }[/math] instead of [math]\displaystyle{ C_2 \exp\left(-((x-m_x)/\sigma_m)^2\right) }[/math]. Log probabilities make some mathematical manipulations easier to perform.
  4. Optimization. Since most common probability distributions—notably the exponential family—are only logarithmically concave,[2][3] and concavity of the objective function plays a key role in the maximization of a function such as probability. Optimizers work better with log probabilities.

Representation issues

The logarithm function is not defined for zero, so log probabilities can only represent non-zero probabilities. Since the logarithm of a number in [math]\displaystyle{ (0, 1) }[/math] interval is negative, often the negative log probabilities are used. In that case the log probabilities in the following formulas would be inverted.

Any base can be selected for the logarithm.

Basic manipulations

In this section we would name probabilities in logarithmic space [math]\displaystyle{ x' }[/math] and [math]\displaystyle{ y' }[/math] for short:

[math]\displaystyle{ x' = \log(x) \in \mathbb{R} }[/math]
[math]\displaystyle{ y' = \log(y) \in \mathbb{R} }[/math]

The product of probabilities [math]\displaystyle{ x \cdot y }[/math] corresponds to addition in logarithmic space.

[math]\displaystyle{ \log(x \cdot y) = \log(x) + \log(y) = x' + y' . }[/math]

The sum of probabilities [math]\displaystyle{ x + y }[/math] 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

[math]\displaystyle{ \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} }[/math]

The formula above is more accurate than [math]\displaystyle{ \log\left(e^{x'} + e^{y'}\right) }[/math], provided one takes advantage of the asymmetry in the addition formula. [math]\displaystyle{ {x'} }[/math] 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.

[math]\displaystyle{ -\infty + \log\left(1 + \exp\left(y' - (-\infty)\right)\right) = -\infty + \infty }[/math] This quantity is indeterminate, and will result in NaN.
[math]\displaystyle{ x' + \log\left(1 + \exp\left(-\infty - x'\right)\right) = x' + 0 }[/math] This is the desired answer.

The above formula alone will incorrectly produce an indeterminate result in the case where both arguments are [math]\displaystyle{ -\infty }[/math]. This should be checked for separately to return [math]\displaystyle{ -\infty }[/math].

For numerical reasons, one should use a function that computes [math]\displaystyle{ \log(1+x) }[/math] (log1p) directly.

See also

References