Created
November 1, 2012 05:08
-
-
Save harrymvr/3991982 to your computer and use it in GitHub Desktop.
Python code to sample from a probability distribution described by the logarithms of the probabilities (log probabilities). Includes code for log sum (log_add)
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
def log_add(x, y): | |
maximum = max(x,y) | |
minimum = min(x,y) | |
if(abs(maximum - minimum) > 30): | |
# the difference is too small, return the just the maximum | |
return maximum | |
return maximum + log(1 + pow(2, minimum - maximum) ,2) |
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
import numpy as np | |
import random | |
from math import log | |
import log_add | |
# Takes as input a log probability vector. The base of the logarithm is 2. | |
# Returns a sampled position according to the corresponding log probabilities | |
# Uses the formula from | |
# http://blog.smola.org/post/987977550/log-probabilities-semirings-and-floating-point-numbers | |
def sample_from_log_prob(A): | |
max_log_prob = max(A) | |
C = [A[0]] | |
for a in A[1:]: | |
C.append(log_add(C[-1], a)) | |
C_pos = [ -c for c in reversed(C)] | |
r = log(random.random(),2) | |
pos = np.searchsorted(C_pos,-r) | |
return len(C) - pos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment