How to extract number of leapfrog steps per iteration per chain?

I am programming some custom samplers (specifically adaptive MALT with manual gradients) for a model in R/C++ and comparing efficiency to Stan. Comparing min(ESS)/second is easy but I also want to compare min(ESS) per gradient evaluation, for which I need to be able to extract the number of leapfrog steps per transition/iteration for each chain for the (Stan variation of) the NUTS sampler.

Is there a way to do this in rstan or cmdstanr?

From memory rstan::get_sampler_params will give you back an array including n_leapfrog__ per-iteration.

2 Likes

And cmdstan output csvs likewise have a n_leapfrog__ column.

2 Likes

Hi - how can I do this for cmdstanr output? (without having to convert to rstan CSV??)

If the result from the call to $sample() was stored in a variable fit, then type fit$ and rstudio will auto-suggest elements/functions, one of which is hmc_diagnostics(), which acts like draws() in returning the draw by draw HMC diagnostic quantities, including number of leapfrog steps, with options for the return format (df, array, rvar, etc)

2 Likes

Thanks @mike-lawrence - do you need to use save_warmup = 1 to get the leapfrog steps during the burn-in phase or will they be saved regardless?

Yup

2 Likes