Point-wise log-likelihood for black-box model in PyMC v4

In PyMC3, pm.Deterministic was sufficient to make sure that the point-wise log-likelihood is stored in the output InferenceData (because this only happens automatically for “observed” variables):

with pm.Model() as my_model:
    ...
    # Store the model log-likelihood as well
    loglik = pm.Deterministic('log_likelihood', model.logpt)

i.e., The goal is to get InferenceData that looks like this:

Inference data with groups:
	> posterior
	> log_likelihood
	> sample_stats
	> observed_data

Whereas without adding pm.Deterministic it looks like this:

Inference data with groups:
	> posterior
	> sample_stats

However, in PyMC v4.0.0 (pre-release) this generates:

  File "/usr/local/dev/mod17/lib/python/mod17/calibration.py", line 751, in compile_npp_model
    loglik = pm.Deterministic('log_likelihood', model.logpt)
  File "/usr/local/python-env/mod17/lib/python3.10/site-packages/pymc/model.py", line 1886, in Deterministic
    var = var.copy(model.name_for(name))
AttributeError: 'function' object has no attribute 'copy'

I think the suggested approach now is to use pm.DensityDist but it’s unclear how to do so from the currently limited API documentation.

Does anyone have an example of using pm.DensityDist for this purpose?

Does the log_likelihood not show in the posterior group?

Ah you need to add parenthesis: loglik = pm.Deterministic('log_likelihood', model.logpt()). But that will be the scalar logp, if you want point-wise you can do loglik = pm.Deterministic('log_likelihood', model.logpt(sum=False)) but that will be a list of different dimensions, so you will have to find a way to spread the output into multiple pm.Deterministics or somehow make it square.