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.