Extracting pointwise log likelihood from a STAN model

I’m starting to work on an adaption of ADVI in STAN where to calculate a loss function similar to the ELBO, I’ll need to access a pointwise log likelihood of the model, but I can’t tell if that’s possible. Open to any help or advice.

You can look at all theses of approximate leave-one-out cross-validation (LOO). In Stan, you have to do it in a loop. So if y is a vector and you have y ~ normal(mu, sigma) in Stan, that adds up the log densities. You can do the same thing and save the intermediate vector of log likelihoods like this:

transformed parameters {
  vector[N] log_lik;
  for (n in 1:N)
    log_lik[n] = normal_lpdf(y | mu, sigma);
  ...
}
model {
  target += log_lik;  // automatically sums
  ...

Then you can add them to the target this way