Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF012 · Algorithm

No-U-Turn Sampler (NUTS)

Chooses HMC trajectory lengths dynamically using a no-U-turn criterion and a valid candidate-selection scheme, reducing the need to hand-select a leapfrog count.

Partly runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method covers part of this entry; some options or variants aren't built yet. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Partly runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Chooses HMC trajectory lengths dynamically using a no-U-turn criterion and a valid candidate-selection scheme, reducing the need to hand-select a leapfrog count. It is a distinct transition construction within HMC rather than another posterior model.

Question / hypotheses

How far should a Hamiltonian trajectory grow before it begins retracing its path, while preserving the desired invariant distribution?

Architecture / computational structure

Build a trajectory by recursively doubling forward or backward, check U-turn/divergence conditions, and select a candidate from the permitted states using a balance-preserving rule. Warmup typically adapts step size and metric. The example isolates the original identity-metric endpoint dot products, not the tree algorithm.

Assumptions

The continuous differentiable target and geometry requirements of HMC still apply. Correct tree expansion, stopping, and candidate selection are essential. A naive rule that simply stops an arbitrary trajectory at its first turn and keeps its endpoint is not the NUTS algorithm.

Formula / computational definition

Δq=q+−q−;ΔqTp−≥0 ∧ ΔqTp+≥0(original identity-metric endpoint criterion).\Delta q=q^+-q^-;\qquad \Delta q^Tp^-\ge0\ \land\ \Delta q^Tp^+\ge0\quad\text{(original identity-metric endpoint criterion)}.

Parameters and notation

States q and momenta p have the same roles as HMC. Algorithm choices include target acceptance during step-size adaptation, metric type, maximum tree depth, warmup, and chain count. Modern implementations use generalized criteria and slice or multinomial candidate rules; specify the implementation rather than assuming all variants are identical.

Fitting, fine-tuning, and adaptation

Repeat warmup after substantive target changes, saving sampler configuration and diagnostics with results. Raising maximum depth increases the permitted work but does not fix funnel geometry. Reparameterize, improve priors, or adjust the metric when needed. Sampling a neural head with a frozen backbone conditions on that backbone.

How to use and calibrate

Inspect divergences, depth saturation, leapfrog counts, effective samples per second, and multiple-chain diagnostics together. Compare posterior moments to a small analytic or quadrature example. Plot geometry and energy diagnostics rather than using a single acceptance statistic as a quality label.

Example application

Fit a hierarchical latent Gaussian model with different posterior scales where choosing a useful fixed trajectory length would require substantial manual tuning.

Interpretation / pitfalls

Watch out

Automatic path length does not mean automatic reliable inference. Modern Stan’s generalized criterion is not exactly the simple endpoint illustration. Warmup adaptation must not be conflated with unrestricted test-time changes to an invariant sampling kernel.

Python and R code examples

Scope: A two-dimensional identity-metric U-turn predicate only; not a NUTS transition, tree builder, candidate selector, or adaptation routine.

Implementation route: NumPy/base R compute the geometric predicate. For an actual NUTS chain, use a validated implementation such as Stan through CmdStanPy/CmdStanR; the complete transition is substantially more than this diagnostic.

Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — A two-dimensional identity-metric U-turn predicate only; not a NUTS transition, tree builder, candidate selector, or adaptation routine. · needs numpy

import numpy as np
qminus=np.array([-1.,0.]); qplus=np.array([1.,1.])
pminus=np.array([.5,.2]); pplus=np.array([-.3,.1]); delta=qplus-qminus
dots=np.array([delta@pminus,delta@pplus]); continue_tree=bool(np.all(dots>=0))
assert not continue_tree
print('endpoint dot products',dots,'continue by this predicate',continue_tree)
R

Component demo, not a trained model — A two-dimensional identity-metric U-turn predicate only; not a NUTS transition, tree builder, candidate selector, or adaptation routine.

qminus <- c(-1,0); qplus <- c(1,1); pminus <- c(.5,.2); pplus <- c(-.3,.1)
delta <- qplus-qminus; dots <- c(sum(delta*pminus),sum(delta*pplus))
continue.tree <- all(dots>=0); stopifnot(!continue.tree)
print(list(endpoint.dots=dots,continue.by.predicate=continue.tree))

Sources

From the CodeSplash reference, entry INF012.