Storing log probability values while running the optimizer in rstan

I am using the optimizing function in rstan to get a point estimate of my parameters. However, I want to store the log probability of my model for each iteration the optimizer takes. Basically in the end I want to create a plot of the log probabilities vs iteration number.

I know we have the verbose flag in the optimizing function but I can’t seem to find any way to store log probabilities of the model.

Is there a function in rstan that does that or is there some other way to get this?

Any suggestions or pointers would be really helpful.


PS: I have edited the model code to remove the unnecessary complexities which aren’t relevant to the question. But if it would be required to answer the question, I would be happy to share it here.

model {
  target += calculate_lpdf(X| X0);
  for(i in 1:N){
    Y[,i] ~ multinomial(pi[i]);

stan_model <- stan_model(file = "mymodel.stan")
optimization_result <- optimizing(stan_model, data = stan_data, verbose=TRUE, hessian = TRUE)

I’m not sure about RStan, but you can get this information out of CmdStanR. It returns an MLE object, one aspect of which is this:

An alternative is to use RStan to recompute the log densities after the fact. You can also use BridgeStan for that.

1 Like