rmcgibbo / pyhmc

Hamiltonain Monte Carlo in Python
https://pythonhosted.org/pyhmc
Other
37 stars 17 forks source link

RuntimeWarning: invalid value encountered in log with Beta Distribution. #20

Open dhern023 opened 4 years ago

dhern023 commented 4 years ago

I'm trying to sample a beta distribution with a = n + 44 and b = s + 102, where n and s are constants, scale constant C = 0.01 and starting value p = 0.2 for 1000 iterations.

My beta looks like

g(p) = Cp^(a-1)*(1-p)^(b-1). log(g) = log(C) + (a-1)log(p) + (b-1)log(1-p) d(log(g)) = 0 + (a-1)/p - (b-1)/(1-p)

import pyhmc

sample_size = 31
sum_spacings = 43
starting_value = 0.2
num_iterations = 1000
constant_c = 0.01

def log_posterior_grad(p, n, s):
    log_posterior_beta = numpy.log(constant_c) + (n + 44 - 1)*numpy.log(p) + (s + 102 -1 )*numpy.log(1-p)
    gradient = (n + 44 - 1)/p - (s + 102 -1 )/(1-p)
    return log_posterior_beta, gradient

samples = pyhmc.hmc(fun = log_posterior_grad, x0 = [starting_value ], args = (sample_size, sum_spacings, ), n_steps = 1000)