Log probabilities trick

It is often the case in calculating likelihoods, that the probabilities can be too small for computational stability. Then it makes sense to work with the log of probabilities. However here one will immediately encounter another problem: how to get log(p + q) from log(p) and log(q)?

Richard Durbin et al. explain a trick for this in Biological sequence analysis: Probabilistic models of proteins and nucleic acids (1998), section 3.6.

\log(p + q) = \log(p ( 1 + \frac{q}{p}))
= \log(p) + \log(1 + \exp(\log(\frac{q}{p})))
= \log(p) + \log(1 + \exp(\log(q) - \log(p)))

Then if p is chosen as the larger of p and q, Durbin et al. argue that using a table of interpolations for calculating log(1 + exp(x)) gives very close estimates for log(p + q).

Advertisements

2 thoughts on “Log probabilities trick”

    1. I just noticed there is a function logspace_add in the R API, which probably uses this trick, as they say “without causing overflows or throwing away too much accuracy”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s