Blackbox likelihood -- find_MAP works but pm.sample returns the prior

I am following the pymc3 page on how to use a “blackbox” function and think that it is working properly, because when I use find_MAP() it gives me back a pretty accurate value based on the simulated data I put in. But when I swap out pm.sample() instead of pm.find_MAP(), then my posterior simply returns the uniform prior I put in. I have tried changing ‘target_accept’ and ‘step_size’ and increasing ‘tune’, but I keep getting back the uniform prior. Could someone let me know how I could fix this problem?

with pm.Model():
    # uniform prior
    sh = pm.Uniform('sh', lower=0., upper=30.)

    # convert to a tensor vector
    theta = tt.as_tensor_variable([sh])

    # use a custom blackbox function (use a lamdba function to "call" the Op)
    pm.DensityDist('likelihood', lambda v: logl(v), observed={'v': theta})
    map_est = pm.find_MAP()

    trace = pm.sample(500, tune=1000, chains=2, 
                      nuts_kwargs={'target_accept': 0.95})

print(map_est['sh']) # correctly returns a value of 21.9 (ground truth = 22)
pm.summary(trace) # returns a mean of 15, with HPD betw 0 and 28

Did you validate the gradient is outputted correctly? also you don need to do

# convert to a tensor vector
    theta = tt.as_tensor_variable([sh])

as sh is already a tensor.

1 Like