# Log probability

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Log_probability
> Markdown URL: https://mediated.wiki/source/Log_probability.md
> Source: https://en.wikipedia.org/wiki/Log_probability
> Source revision: 1340826122
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Logarithm of probabilities, useful for calculations

Not to be confused with [log odds](/source/Log_odds).

In [probability theory](/source/Probability_theory) and [computer science](/source/Computer_science), a **log probability** is simply a [logarithm](/source/Logarithm) of a [probability](/source/Probability).[1] The use of log probabilities means representing probabilities on a [logarithmic scale](/source/Logarithmic_scale) ( − ∞ , 0 ] {\displaystyle (-\infty ,0]} , instead of the standard [ 0 , 1 ] {\displaystyle [0,1]} [unit interval](/source/Unit_interval).

Since the probabilities of [independent](/source/Independence_(probability_theory)) [events](/source/Event_(probability_theory)) 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](/source/Information_theory): the negative [expected value](/source/Expected_value) of the log probabilities is the [information entropy](/source/Information_entropy) of an event. Similarly, [likelihoods](/source/Likelihood) are often transformed to the log scale, and the corresponding [log-likelihood](/source/Log-likelihood) can be interpreted as the degree to which an event supports a [statistical model](/source/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](/source/Natural_language_processing).

## Motivation

Representing probabilities in this way has several practical advantages:

1. **Speed.** Since multiplication is more [expensive](/source/Time_complexity) 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.

1. **Accuracy.** The use of log probabilities improves [numerical stability](/source/Numerical_stability), when the probabilities are very small, because of the way in which computers [approximate real numbers](/source/Floating_point).[1]

1. **Simplicity.** Many [probability distributions](/source/Probability_distribution) 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](/source/Probability_density_function) is − ( ( x − m x ) / σ m ) 2 + C {\displaystyle -((x-m_{x})/\sigma _{m})^{2}+C} instead of C 2 exp ⁡ ( − ( ( x − m x ) / σ m ) 2 ) {\displaystyle C_{2}\exp \left(-((x-m_{x})/\sigma _{m})^{2}\right)} . Log probabilities make some mathematical manipulations easier to perform.

1. **Optimization.** Since most common [probability distributions](/source/Probability_distribution)—notably the [exponential family](/source/Exponential_family)—are only [logarithmically concave](/source/Logarithmically_concave_function),[2][3] and [concavity](/source/Concave_function) of the [objective function](/source/Objective_function) plays a key role in the [maximization](/source/Mathematical_optimization) 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 ( 0 , 1 ) {\displaystyle (0,1)} interval is negative, often the negative log probabilities are used. In that case the log probabilities in the following formulas would be [inverted](/source/Additive_inverse).

Any base can be selected for the logarithm.

## Basic manipulations

Further information: [Log semiring](/source/Log_semiring)

In this section we would name probabilities in logarithmic space x ′ {\displaystyle x'} and y ′ {\displaystyle y'} for short: x ′ = log ⁡ ( x ) ∈ R y ′ = log ⁡ ( y ) ∈ R {\displaystyle {\begin{aligned}x'&=\log(x)\in \mathbb {R} \\y'&=\log(y)\in \mathbb {R} \end{aligned}}}

The product of probabilities x ⋅ y {\displaystyle x\cdot y} corresponds to addition in logarithmic space.

log ⁡ ( x ⋅ y ) = log ⁡ ( x ) + log ⁡ ( y ) = x ′ + y ′ . {\displaystyle \log(x\cdot y)=\log(x)+\log(y)=x'+y'.}

The [sum of probabilities](#Addition_in_log_space) x + y {\displaystyle 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](/source/LogSumExp).

### Addition in log space

log ⁡ ( x + y ) = log ⁡ ( x + x ⋅ y / x ) = log ⁡ ( x + x ⋅ exp ⁡ ( log ⁡ ( y / x ) ) ) = log ⁡ ( x ⋅ ( 1 + exp ⁡ ( log ⁡ ( y ) − log ⁡ ( x ) ) ) ) = log ⁡ ( x ) + log ⁡ ( 1 + exp ⁡ ( log ⁡ ( y ) − log ⁡ ( x ) ) ) = x ′ + log ⁡ ( 1 + exp ⁡ ( y ′ − x ′ ) ) {\displaystyle {\begin{aligned}&\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{aligned}}}

The formula above is more accurate than log ⁡ ( e x ′ + e y ′ ) {\displaystyle \log \left(e^{x'}+e^{y'}\right)} , provided one takes advantage of the asymmetry in the addition formula. x ′ {\displaystyle {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](/source/Floating-point_arithmetic) [negative infinity](/source/Negative_infinity), which corresponds to a probability of zero.

− ∞ + log ⁡ ( 1 + exp ⁡ ( y ′ − ( − ∞ ) ) ) = − ∞ + ∞ {\displaystyle -\infty +\log \left(1+\exp \left(y'-(-\infty )\right)\right)=-\infty +\infty } This quantity is [indeterminate](/source/Indeterminate_form), and will result in [NaN](/source/NaN). x ′ + log ⁡ ( 1 + exp ⁡ ( − ∞ − x ′ ) ) = x ′ + 0 {\displaystyle 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 − ∞ {\displaystyle -\infty } . This should be checked for separately to return − ∞ {\displaystyle -\infty } .

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

## See also

- [Information content](/source/Information_content)

- [Log-likelihood](/source/Log-likelihood)

## References

1. ^ [***a***](#cite_ref-chrispiech_1-0) [***b***](#cite_ref-chrispiech_1-1) Piech, Chris. ["Probability for Computer scientists - Log probabilities"](https://chrispiech.github.io/probabilityForComputerScientists/en/part1/log_probabilities/). Retrieved 20 July 2023.

1. **[^](#cite_ref-2)** Kass, Robert E.; Vos, Paul W. (1997). [*Geometrical Foundations of Asymptotic Inference*](https://books.google.com/books?id=e43EAIfUPCwC&pg=PA14). New York: John Wiley & Sons. p. 14. [ISBN](/source/ISBN_(identifier)) [0-471-82668-5](https://en.wikipedia.org/wiki/Special:BookSources/0-471-82668-5).

1. **[^](#cite_ref-3)** Papadopoulos, Alecos (September 25, 2013). ["Why we always put log() before the joint pdf when we use MLE (Maximum likelihood Estimation)?"](https://stats.stackexchange.com/q/70975). *[Stack Exchange](/source/Stack_Exchange)*.

---
Adapted from the Wikipedia article [Log probability](https://en.wikipedia.org/wiki/Log_probability) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Log_probability?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
