Sample posterior check with pm.Potentials

Hi everybody,

I am trying to do a weighted likelihood following the methods here and @junpenglao. The sampler works but I could not get pm.sample_posterior_check to work. There are couple questions on this forum with no satisfying answer. How to I get a posterior predictive check from a black-box likelihood function like this? Thanks so much.

hr=np.random.normal(60,10,(N,p))
steps=np.random.normal(10,20,(N,p))
t = np.arange(N)
w = np.random.uniform(0,1,N)

with pm.Model() as model_StudentsT_weightedLLH:

     a = pm.Normal('a', mu = 80, sigma = 20) #intercept
     b = pm.HalfNormal('b', sigma = 10) #amplitude
     c = pm.Normal('c', mu = 5, sigma = 1) #phase shift
     d = pm.Normal('d', mu = 0.2, sigma = 0.1) #activity coeff
     σ = pm.HalfNormal("σ", sigma=10) 

     Îź = a - b * pm.math.cos(np.pi/12 * (t - c)) + d*steps 
     llh_w = pm.Potential('llh_w', w * pm.Normal.dist(mu = Ο, sigma = σ).logp(hr))
     pm.sample(10000, return_inferencedata = False, tune = 2000) 
1 Like

Can you provide a working example? I don’t know what N or p are and your code won’t run with arbitrary values. Also, what version of pymc are you using?

1 Like

Any updates on this? I am facing a similar problem. I used weights on the logp of my observations and put that in a pm.Potential just like you did above, and I can sample just fine. But when I try sample_posterior_predictive on my trace after that, I get an empty dictionary.

Potentials are not taken into consideration when doing either prior or posterior predictive, as they only show in the logp graph, not on the random graph. You should see a warning saying as much.

In your case, the model has no “observed” variable so posterior predictive doesn’t do anything. PyMC doesn’t know your Potential corresponds to “observed” variables, and even if it did, it wouldn’t know what to do with that information in prior/posterior predictive.

You can integrate your Potential logp in a CustomDist, in which case you can also provide a custom random function to be used in prior/ posterior predictive. You have to decide what should be done with the weights in that case. pymc.CustomDist — PyMC dev documentation

Thank you so much! This makes sense.