1

I want to evaluate the log posterior (ideally separately the log prior and log likelihood) values at sample points and at some manually entered points (e.g. the true parameter value points for synthetic datasets). How can I achieve this in PyMC3?

Update: I've found the logp() method, however it's not very convenient to use for multiple points. Is are there some standard / idiomatic approach?

Update: This [y.logp(trace[i]) for i in range(len(trace))] works, but is superslow.

Update: Slowness was caused by the fact that y is the observed random variable, calling logp method of the model works fast.

Hennadii Madan
  • 1,573
  • 1
  • 14
  • 30
  • Have you found a solution? I've asked a similar question (https://stackoverflow.com/questions/48565385/how-to-calculate-log-posterior-of-a-gp-over-a-trace-in-pymc3) and feel free to post a nice reply to help me out and also claim a point ;) – Josh Albert Feb 01 '18 at 14:46

1 Answers1

1

OK, the reality is that it's best to ask PyMC3 related questions on their discourse forum.

So to get the values of log-posterior after sampling use

logp = mvg_model.logp lnp = np.array([logp(trace.point(i,chain=c)) for c in trace.chains for i in range(len(trace))])

To save them during sampling use

```

with model:
   llk = pm.Deterministic(likelihood_name, model.logpt)

```

Hennadii Madan
  • 1,573
  • 1
  • 14
  • 30
  • I get `AttributeError: 'Model' object has no attribute 'logpt'. Did you mean: 'logp'?`. Maybe something has changed since PyMC3. – Galen Jun 25 '23 at 17:48
  • @Galen Logpt with a "t" is not a typo. Most likely the API has changed since 2018. I haven't been doing MCMC since 2019, so cannot tell. The code was something that was used in PyMC3 code (you can see for yourself if you follow the last link in the answer). – Hennadii Madan Jul 13 '23 at 18:19