Created
August 28, 2012 22:36
-
-
Save longouyang/3504979 to your computer and use it in GitHub Desktop.
Adding log probabilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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