Skip to content



# AdvancedHMC.AMethod.

A single Hamiltonian integration step.

NOTE: this function is intended to be used in find_good_stepsize only.

# AdvancedHMC.build_treeMethod.

Recursivly build a tree for a given depth j.

# AdvancedHMC.combineMethod.

combine(treeleft::BinaryTree, treeright::BinaryTree)

Merge a left tree treeleft and a right tree treeright under given Hamiltonian h, then draw a new candidate sample and update related statistics for the resulting tree.

# AdvancedHMC.find_good_stepsizeMethod.

Find a good initial leap-frog step-size via heuristic search.

# AdvancedHMC.isterminatedMethod.

isterminated(h::Hamiltonian, t::BinaryTree{<:ClassicNoUTurn})

Detect U turn for two phase points (zleft and zright) under given Hamiltonian h using the (original) no-U-turn cirterion.


# AdvancedHMC.isterminatedMethod.

isterminated(h::Hamiltonian, t::BinaryTree{<:GeneralisedNoUTurn})

Detect U turn for two phase points (zleft and zright) under given Hamiltonian h using the generalised no-U-turn criterion.


# AdvancedHMC.maxabsMethod.

maxabs(a, b)

Return the value with the largest absolute value.

# AdvancedHMC.mh_accept_ratioMethod.

Perform MH acceptance based on energy, i.e. negative log probability.

# AdvancedHMC.nom_step_sizeMethod.


Get the nominal integration step size. The current integration step size may differ from this, for example if the step size is jittered. Nominal step size is usually used in adaptation.

# AdvancedHMC.pm_next!Method.

Progress meter update with all trajectory stats, iteration number and metric shown.

# AdvancedHMC.randcatMethod.

randcat(rng, P::AbstractMatrix)

Generating Categorical random variables in a vectorized mode. P is supposed to be a matrix of (D, N) where each column is a probability vector.


P = [
    0.5 0.3;
    0.4 0.6;
    0.1 0.1
u = [0.3, 0.4]
C = [
    0.5 0.3
    0.9 0.9
    1.0 1.0

Then C .< u' is

    0 1
    0 0
    0 0

thus convert.(Int, vec(sum(C .< u'; dims=1))) .+ 1 equals [1, 2].

# AdvancedHMC.simple_pm_next!Method.

Simple progress meter update without any show values.

# AdvancedHMC.statMethod.

Returns the statistics for transition t.

# AdvancedHMC.step_sizeFunction.


Get the current integration step size.

# AdvancedHMC.temperMethod.

temper(lf::TemperedLeapfrog, r, step::NamedTuple{(:i, :is_half),<:Tuple{Integer,Bool}}, n_steps::Int)

Tempering step. step is a named tuple with

  • i being the current leapfrog iteration and
  • is_half indicating whether or not it’s (the first) half momentum/tempering step

# AdvancedHMC.transitionMethod.

transition(τ::AbstractTrajectory{I}, h::Hamiltonian, z::PhasePoint)

Make a MCMC transition from phase point z using the trajectory τ under Hamiltonian h.

NOTE: This is a RNG-implicit fallback function for transition(GLOBAL_RNG, τ, h, z)

# StatsBase.sampleMethod.

    n_adapts::Int=min(div(n_samples, 10), 1_000);

Sample n_samples samples using the proposal τ under Hamiltonian h.

  • The randomness is controlled by rng.

    • If rng is not provided, GLOBAL_RNG will be used.
  • The initial point is given by θ.
  • The adaptor is set by adaptor, for which the default is no adaptation.

    • It will perform n_adapts steps of adaptation, for which the default is the minimum of 1_000 and 10% of n_samples
  • drop_warmup controls to drop the samples during adaptation phase or not
  • verbose controls the verbosity
  • progress controls whether to show the progress meter or not


# AdvancedHMC.AbstractIntegratorType.

abstract type AbstractIntegrator

Represents an integrator used to simulate the Hamiltonian system.


A AbstractIntegrator is expected to have the following implementations:

  • stat(@ref)
  • nom_step_size(@ref)
  • step_size(@ref)

# AdvancedHMC.AbstractProposalType.

Abstract Markov chain Monte Carlo proposal.

# AdvancedHMC.AbstractTrajectoryType.

Hamiltonian dynamics numerical simulation trajectories.

# AdvancedHMC.AbstractTrajectorySamplerType.

Defines how to sample a phase-point from the simulated trajectory.

# AdvancedHMC.BinaryTreeType.

A full binary tree trajectory with only necessary leaves and information stored.

# AdvancedHMC.ClassicNoUTurnType.

struct ClassicNoUTurn <: AdvancedHMC.AbstractTerminationCriterion

Classic No-U-Turn criterion as described in Eq. (9) in [1].

Informally, this will terminate the trajectory expansion if continuing the simulation either forwards or backwards in time will decrease the distance between the left-most and right-most positions.


  1. Hoffman, M. D., & Gelman, A. (2014). The No-U-Turn Sampler: adaptively setting path lengths in Hamiltonian Monte Carlo. Journal of Machine Learning Research, 15(1), 1593-1623. (arXiv)

# AdvancedHMC.EndPointTSType.

struct EndPointTS <: AdvancedHMC.AbstractTrajectorySampler

Samples the end-point of the trajectory.

# AdvancedHMC.GeneralisedNoUTurnType.

struct GeneralisedNoUTurn{T<:(AbstractArray{var"#s58",1} where var"#s58"<:Real)} <: AdvancedHMC.AbstractTerminationCriterion

Generalised No-U-Turn criterion as described in Section A.4.2 in [1].


  • rho::AbstractArray{var"#s58",1} where var"#s58"<:Real

    Integral or sum of momenta along the integration path.


  1. Betancourt, M. (2017). A Conceptual Introduction to Hamiltonian Monte Carlo. arXiv preprint arXiv:1701.02434.

# AdvancedHMC.HMCDAType.

struct HMCDA{S<:AdvancedHMC.AbstractTrajectorySampler, I<:AdvancedHMC.AbstractIntegrator} <: AdvancedHMC.DynamicTrajectory{I<:AdvancedHMC.AbstractIntegrator}

Standard HMC implementation with fixed total trajectory length.


  • integrator::AdvancedHMC.AbstractIntegrator

    Integrator used to simulate trajectory.

  • λ::AbstractFloat

    Total length of the trajectory, i.e. take floor(λ / integrator_step) number of leapfrog steps.


  1. Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)

# AdvancedHMC.JitteredLeapfrogType.

struct JitteredLeapfrog{FT<:AbstractFloat, T<:Union{AbstractArray{FT<:AbstractFloat,1}, FT<:AbstractFloat}} <: AdvancedHMC.AbstractLeapfrog{T<:Union{AbstractArray{FT<:AbstractFloat,1}, FT<:AbstractFloat}}

Leapfrog integrator with randomly “jittered” step size ϵ for every trajectory.


  • ϵ0::Union{AbstractArray{FT,1}, FT} where FT<:AbstractFloat

    Nominal (non-jittered) step size.

  • jitter::AbstractFloat

    The proportion of the nominal step size ϵ0 that may be added or subtracted.

  • ϵ::Union{AbstractArray{FT,1}, FT} where FT<:AbstractFloat

    Current (jittered) step size.


This is the same as LeapFrog(@ref) but with a “jittered” step size. This means that at the beginning of each trajectory we sample a step size ϵ by adding or subtracting from the nominal/base step size ϵ0 some random proportion of ϵ0, with the proportion specified by jitter, i.e. ϵ = ϵ0 - jitter * ϵ0 * rand(). p Jittering might help alleviate issues related to poor interactions with a fixed step size:

  • In regions with high “curvature” the current choice of step size might mean over-shoot leading to almost all steps being rejected. Randomly sampling the step size at the beginning of the trajectories can therefore increase the probability of escaping such high-curvature regions.
  • Exact periodicity of the simulated trajectories might occur, i.e. you might be so unlucky as to simulate the trajectory forwards in time L ϵ and ending up at the same point (which results in non-ergodicity; see Section 3.2 in [1]). If momentum is refreshed before each trajectory, then this should not happen exactly but it can still be an issue in practice. Randomly choosing the step-size ϵ might help alleviate such problems.


  1. Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)

# AdvancedHMC.LeapfrogType.

struct Leapfrog{T<:(Union{AbstractArray{var"#s58",1}, var"#s58"} where var"#s58"<:AbstractFloat)} <: AdvancedHMC.AbstractLeapfrog{T<:(Union{AbstractArray{var"#s58",1}, var"#s58"} where var"#s58"<:AbstractFloat)}

Leapfrog integrator with fixed step size ϵ.


  • ϵ::Union{AbstractArray{var"#s58",1}, var"#s58"} where var"#s58"<:AbstractFloat

    Step size.

# AdvancedHMC.MultinomialTSType.

struct MultinomialTS{F<:AbstractFloat} <: AdvancedHMC.AbstractTrajectorySampler

Multinomial trajectory sampler carried during the building of the tree. It contains the weight of the tree, defined as the total probabilities of the leaves.


  • zcand::AdvancedHMC.PhasePoint

    Sampled candidate PhasePoint.

  • ℓw::AbstractFloat

    Total energy for the given tree, i.e. the sum of energies of all leaves.

# AdvancedHMC.MultinomialTSMethod.

MultinomialTS(s::MultinomialTS, H0::AbstractFloat, zcand::PhasePoint)

Multinomial sampler for a trajectory consisting only a leaf node.

  • tree weight is the (unnormalised) energy of the leaf.

# AdvancedHMC.MultinomialTSMethod.

MultinomialTS(rng::AbstractRNG, z0::PhasePoint)

Multinomial sampler for the starting single leaf tree. (Log) weights for leaf nodes are their (unnormalised) Hamiltonian energies.


# AdvancedHMC.NUTSType.

Dynamic trajectory HMC using the no-U-turn termination criteria algorithm.

# AdvancedHMC.NUTSMethod.

NUTS(args...) = NUTS{MultinomialTS,GeneralisedNoUTurn}(args...)

Create an instance for the No-U-Turn sampling algorithm with multinomial sampling and original no U-turn criterion.

Below is the doc for NUTS{S,C}.

) where {I<:AbstractIntegrator,F<:AbstractFloat,S<:AbstractTrajectorySampler,C<:AbstractTerminationCriterion}

Create an instance for the No-U-Turn sampling algorithm.

# AdvancedHMC.NUTSMethod.

) where {I<:AbstractIntegrator,F<:AbstractFloat,S<:AbstractTrajectorySampler,C<:AbstractTerminationCriterion}

Create an instance for the No-U-Turn sampling algorithm.

# AdvancedHMC.SliceTSType.

struct SliceTS{F<:AbstractFloat} <: AdvancedHMC.AbstractTrajectorySampler

Trajectory slice sampler carried during the building of the tree. It contains the slice variable and the number of acceptable condidates in the tree.


  • zcand::AdvancedHMC.PhasePoint

    Sampled candidate PhasePoint.

  • ℓu::AbstractFloat

    Slice variable in log-space.

  • n::Int64

    Number of acceptable candidates, i.e. those with probability larger than slice variable u.

# AdvancedHMC.SliceTSMethod.

SliceTS(rng::AbstractRNG, z0::PhasePoint)

Slice sampler for the starting single leaf tree. Slice variable is initialized.

# AdvancedHMC.SliceTSMethod.

SliceTS(s::SliceTS, H0::AbstractFloat, zcand::PhasePoint)

Create a slice sampler for a single leaf tree:

  • the slice variable is copied from the passed-in sampler s and
  • the number of acceptable candicates is computed by comparing the slice variable against the current energy.

# AdvancedHMC.StaticTrajectoryType.

struct StaticTrajectory{S<:AdvancedHMC.AbstractTrajectorySampler, I<:AdvancedHMC.AbstractIntegrator} <: AdvancedHMC.AbstractTrajectory{I<:AdvancedHMC.AbstractIntegrator}

Static HMC with a fixed number of leapfrog steps.


  • integrator::AdvancedHMC.AbstractIntegrator

    Integrator used to simulate trajectory.

  • n_steps::Int64

    Number of steps to simulate, i.e. length of trajectory will be n_steps + 1.


  1. Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)

# AdvancedHMC.TemperedLeapfrogType.

struct TemperedLeapfrog{FT<:AbstractFloat, T<:Union{AbstractArray{FT<:AbstractFloat,1}, FT<:AbstractFloat}} <: AdvancedHMC.AbstractLeapfrog{T<:Union{AbstractArray{FT<:AbstractFloat,1}, FT<:AbstractFloat}}

Tempered leapfrog integrator with fixed step size ϵ and “temperature” α.


  • ϵ::Union{AbstractArray{FT,1}, FT} where FT<:AbstractFloat

    Step size.

  • α::AbstractFloat

    Temperature parameter.


Tempering can potentially allow greater exploration of the posterior, e.g. in a multi-modal posterior jumps between the modes can be more likely to occur.

# AdvancedHMC.TerminationType.


Termination reasons

  • dynamic: due to stoping criteria
  • numerical: due to large energy deviation from starting (possibly numerical errors)

# AdvancedHMC.TerminationMethod.

Termination(s::MultinomialTS, nt::NUTS, H0::F, H′::F) where {F<:AbstractFloat}

Check termination of a Hamiltonian trajectory.

# AdvancedHMC.TerminationMethod.

Termination(s::SliceTS, nt::NUTS, H0::F, H′::F) where {F<:AbstractFloat}

Check termination of a Hamiltonian trajectory.

# AdvancedHMC.TransitionType.

struct Transition{P<:AdvancedHMC.PhasePoint, NT<:NamedTuple}

A transition that contains the phase point and other statistics of the transition.


  • z::AdvancedHMC.PhasePoint

    Phase-point for the transition.

  • stat::NamedTuple

    Statistics related to the transition, e.g. energy.