Skip to content

Instantly share code, notes, and snippets.

@longouyang
Created August 28, 2012 22:36
Show Gist options
  • Save longouyang/3504979 to your computer and use it in GitHub Desktop.
Save longouyang/3504979 to your computer and use it in GitHub Desktop.
Adding log probabilities
from math import log, exp
"""
sources:
http://windowoffice.tumblr.com/post/33548509/logsum-underflow-trick-re-discovery
https://facwiki.cs.byu.edu/nlp/index.php/Log_Domain_Computations
"""
def log_add(x,y):
logx = log(x)
logy = log(y)
if (logy > logx):
temp = logx
logx = logy
logy = temp
negdiff = logy - logx
if negdiff < -20:
return logx
return logx + log(1.0 + exp(negdiff))
for x in range(1,20):
print log_add(0.5, 2*(10**(-x) ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment