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

**URL:** <https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600>\
**Category:** General\
**Created:** [June 2, 2023, 12:38pm UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600 "2023-06-02T12:38:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [June 2, 2023, 12:38pm UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/1 "2023-06-02T12:38:29Z")

</div>

I am programming some custom samplers (specifically [adaptive MALT](https://arxiv.org/abs/2210.12200) 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?

---

<div class="post-metadata">

**Author:** ![hhau](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@hhau](https://discourse.mc-stan.org/u/hhau)\
**Post date:** [June 2, 2023, 12:43pm UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/2 "2023-06-02T12:43:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [June 2, 2023, 1:26pm UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/3 "2023-06-02T13:26:45Z")

</div>

And `cmdstan` output csvs likewise have a `n_leapfrog__` column.

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [July 20, 2023, 12:42pm UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/4 "2023-07-20T12:42:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [July 21, 2023, 4:18am UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/5 "2023-07-21T04:18:55Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [July 25, 2023, 11:17am UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/6 "2023-07-25T11:17:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [July 25, 2023, 11:22am UTC](https://discourse.mc-stan.org/t/how-to-extract-number-of-leapfrog-steps-per-iteration-per-chain/31600/7 "2023-07-25T11:22:06Z")

</div>

Yup
