# NUTS differences in Stan vs paper

**URL:** <https://discourse.mc-stan.org/t/nuts-differences-in-stan-vs-paper/221>\
**Category:** Algorithms\
**Created:** [January 25, 2017, 11:45am UTC](https://discourse.mc-stan.org/t/nuts-differences-in-stan-vs-paper/221 "2017-01-25T11:45:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![idontgetoutmuch](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/idontgetoutmuch/32/47_2.png) [@idontgetoutmuch](https://discourse.mc-stan.org/u/idontgetoutmuch)\
**Post date:** [January 25, 2017, 11:45am UTC](https://discourse.mc-stan.org/t/nuts-differences-in-stan-vs-paper/221/1 "2017-01-25T11:45:47Z")

</div>

I don’t seem to be able to post in the developer section :( so forgive me for posting here.

I couldn’t find this in the new (discourse) discussion forum [Redirecting to Google Groups](https://groups.google.com/forum/#!topic/stan-dev/gPHJdAns6rc) from which I quote:

> > I’m wondering if there have been any changes to the NUTS algorithm since the paper was published.

> Yes. See [[1601.00225] Identifying the Optimal Integration Time in Hamiltonian Monte Carlo](https://arxiv.org/abs/1601.00225), Section 2.

I have read through Section 2 and two termination criteria are given: exhaustive and no u-turn. As far as I can see the latter is the same as in the original paper (assuming the metric is Euclidean). Can someone spell out the difference explicitly?

BTW my intention is to reproduce the results in Section 4 of the referenced paper.

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [January 25, 2017, 1:42pm UTC](https://discourse.mc-stan.org/t/nuts-differences-in-stan-vs-paper/221/2 "2017-01-25T13:42:21Z")

</div>

Section 2 covered both the termination criterion and how to sample from a trajectory. We no longer use a slice sampler to sample a state from a trajectory, but rather sample directly from the  
marginal probabilities as discussed in the paper.

If you want to know the exact implementation then please read the code.

---

<div class="post-metadata">

**Author:** ![idontgetoutmuch](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/idontgetoutmuch/32/47_2.png) [@idontgetoutmuch](https://discourse.mc-stan.org/u/idontgetoutmuch)\
**Post date:** [January 26, 2017, 8:29am UTC](https://discourse.mc-stan.org/t/nuts-differences-in-stan-vs-paper/221/3 "2017-01-26T08:29:00Z")

</div>

I have never read C++ before so I hope you forgive anything that would be obvious to someone who knows the language.

I can see you changed from slice sampling to multinomial sampling in 05dcaa7. And (some of? all of?) the variable names match the paper e.g. z\_minus I assume matches $z\_-$. That is very helpful :)

And I have found the check for the u-turn in `base_nuts.hpp`:

```
      // Break when NUTS criterion is no longer satisfied
      rho += rho_subtree;
      if (!compute_criterion(p_sharp_minus, p_sharp_plus, rho))
        break;

```

So I think I can see what is going on. Thanks very much :)

Anything else I should look at or be aware of?

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [February 2, 2017, 12:58am UTC](https://discourse.mc-stan.org/t/nuts-differences-in-stan-vs-paper/221/4 "2017-02-02T00:58:15Z")

</div>

I do think you found the essentials.
