AdvancedHMC.jl
Documentation for AdvancedHMC.jl
Types
AdvancedHMC.ClassicNoUTurn
— Typestruct ClassicNoUTurn{F<:AbstractFloat} <: AdvancedHMC.DynamicTerminationCriterion
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.
Fields
max_depth::Int64
Δ_max::AbstractFloat
References
- 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.HMCSampler
— TypeHMCSampler
An AbstractMCMC.AbstractSampler
for kernels in AdvancedHMC.jl.
Fields
metric
: Choice of initial metricAbstractMetric
. The metric type will be preserved during adaption.adaptor
:AdvancedHMC.Adaptation.AbstractAdaptor
.
Notes
Note that all the fields have the prefix initial_
to indicate that these will not necessarily correspond to the kernel
, metric
, and adaptor
after sampling.
To access the updated fields, use the resulting HMCState
.
AdvancedHMC.HMC
— TypeHMC(ϵ::Real, n_leapfrog::Int)
Hamiltonian Monte Carlo sampler with static trajectory.
Fields
n_leapfrog
: Number of leapfrog steps.integrator
: Choice of integrator, specified either using aSymbol
orAbstractIntegrator
metric
: Choice of initial metric;Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
AdvancedHMC.NUTS
— TypeNUTS(δ::Real; max_depth::Int=10, Δ_max::Real=1000, integrator = :leapfrog, metric = :diagonal)
No-U-Turn Sampler (NUTS) sampler.
Fields
δ
: Target acceptance rate for dual averaging.max_depth
: Maximum doubling tree depth.Δ_max
: Maximum divergence during doubling tree.integrator
: Choice of integrator, specified either using aSymbol
orAbstractIntegrator
metric
: Choice of initial metric;Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
AdvancedHMC.HMCDA
— TypeHMCDA(δ::Real, λ::Real, integrator = :leapfrog, metric = :diagonal)
Hamiltonian Monte Carlo sampler with Dual Averaging algorithm.
Fields
δ
: Target acceptance rate for dual averaging.λ
: Target leapfrog length.integrator
: Choice of integrator, specified either using aSymbol
orAbstractIntegrator
metric
: Choice of initial metric;Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
Notes
For more information, please view the following paper (arXiv link):
- Hoffman, Matthew D., and Andrew Gelman. "The No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte Carlo." Journal of Machine Learning Research 15, no. 1 (2014): 1593-1623.
Functions
StatsBase.sample
— Functionsample([rng], a, [wv::AbstractWeights])
Select a single random element of a
. Sampling probabilities are proportional to the weights given in wv
, if provided.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sample([rng], a, [wv::AbstractWeights], n::Integer; replace=true, ordered=false)
Select a random, optionally weighted sample of size n
from an array a
using a polyalgorithm. Sampling probabilities are proportional to the weights given in wv
, if provided. replace
dictates whether sampling is performed with replacement. ordered
dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a
) should be taken.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sample([rng], a, [wv::AbstractWeights], dims::Dims; replace=true, ordered=false)
Select a random, optionally weighted sample from an array a
specifying the dimensions dims
of the output array. Sampling probabilities are proportional to the weights given in wv
, if provided. replace
dictates whether sampling is performed with replacement. ordered
dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a
) should be taken.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sample([rng], wv::AbstractWeights)
Select a single random integer in 1:length(wv)
with probabilities proportional to the weights given in wv
.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
diff --git a/dev/index.html b/dev/index.html
index d6a51d42..8f1666b9 100644
--- a/dev/index.html
+++ b/dev/index.html
@@ -457,6 +457,7 @@
});
+
AdvancedHMC.jl
AdvancedHMC.jl provides a robust, modular, and efficient implementation of advanced HMC algorithms. An illustrative example of AdvancedHMC's usage is given below. AdvancedHMC.jl is part of Turing.jl, a probabilistic programming library in Julia. If you are interested in using AdvancedHMC.jl through a probabilistic programming language, please check it out!
Interfaces
IMP.hmc
: an experimental Python module for the Integrative Modeling Platform, which uses AdvancedHMC in its backend to sample protein structures.
NEWS
- We presented a paper for AdvancedHMC.jl at AABI in 2019 in Vancouver, Canada. (abs, pdf, OpenReview)
- We presented a poster for AdvancedHMC.jl at StanCon 2019 in Cambridge, UK. (pdf)
API CHANGES
- [v0.5.0] Breaking! Convenience constructors for common samplers changed to:
HMC(leapfrog_stepsize::Real, n_leapfrog::Int)
NUTS(target_acceptance::Real)
HMCDA(target_acceptance::Real, integration_time::Real)
- [v0.2.22] Three functions are renamed.
Preconditioner(metric::AbstractMetric)
-> MassMatrixAdaptor(metric)
and NesterovDualAveraging(δ, integrator::AbstractIntegrator)
-> StepSizeAdaptor(δ, integrator)
find_good_eps
-> find_good_stepsize
- [v0.2.15]
n_adapts
is no longer needed to construct StanHMCAdaptor
; the old constructor is deprecated. - [v0.2.8] Two Hamiltonian trajectory sampling methods are renamed to avoid a name clash with Distributions.
Multinomial
-> MultinomialTS
Slice
-> SliceTS
- [v0.2.0] The gradient function passed to
Hamiltonian
is supposed to return a value-gradient tuple now.
A minimal example - sampling from a multivariate Gaussian using NUTS
This section demonstrates a minimal example of sampling from a multivariate Gaussian (10-dimensional) using the no U-turn sampler (NUTS). Below we describe the major components of the Hamiltonian system which are essential to sample using this approach:
Metric: In many sampling problems the sample space is associated with a metric that allows us to measure the distance between any two points, and other similar quantities. In the example in this section, we use a special metric called the Euclidean Metric, represented with a D × D
matrix from which we can compute distances.[1]
Leapfrog integration: Leapfrog integration is a second-order numerical method for integrating differential equations (In this case they are equations of motion for the relative position of one particle with respect to the other). The order of this integration signifies its rate of convergence. Any algorithm with a finite time step size will have numerical errors, and the order is related to this error. For a second-order algorithm, this error scales as the second power of the time step, hence, the name second-order. High-order integrators are usually complex to code and have a limited region of convergence; hence they do not allow arbitrarily large time steps. A second-order integrator is suitable for our purpose. Hence we opt for the leapfrog integrator. It is called leapfrog
due to the ways this algorithm is written, where the positions and velocities of particles "leap over" each other.[2]
Kernel for trajectories (static or dynamic): Different kernels, which may be static or dynamic, can be used. At each iteration of any variant of the HMC algorithm, there are two main steps - the first step changes the momentum and the second step may change both the position and the momentum of a particle.[3]
using AdvancedHMC, ForwardDiff
using LogDensityProblems
using LinearAlgebra
diff --git a/index.html b/index.html
index 6a5afc30..3ac25969 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
+
diff --git a/v0.6.2/api/index.html b/v0.6.2/api/index.html
index 6cd8119e..57ec78a6 100644
--- a/v0.6.2/api/index.html
+++ b/v0.6.2/api/index.html
@@ -1,5 +1,463 @@
-AdvancedHMC.jl · AdvancedHMC AdvancedHMC.jl
Documentation for AdvancedHMC.jl
Types
AdvancedHMC.ClassicNoUTurn
— Typestruct ClassicNoUTurn{F<:AbstractFloat} <: AdvancedHMC.DynamicTerminationCriterion
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.
Fields
max_depth::Int64
Δ_max::AbstractFloat
References
- 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)
sourceAdvancedHMC.HMCSampler
— TypeHMCSampler
An AbstractMCMC.AbstractSampler
for kernels in AdvancedHMC.jl.
Fields
metric
: Choice of initial metric AbstractMetric
. The metric type will be preserved during adaption.
adaptor
: AdvancedHMC.Adaptation.AbstractAdaptor
.
Notes
Note that all the fields have the prefix initial_
to indicate that these will not necessarily correspond to the kernel
, metric
, and adaptor
after sampling.
To access the updated fields, use the resulting HMCState
.
sourceAdvancedHMC.HMC
— TypeHMC(ϵ::Real, n_leapfrog::Int)
Hamiltonian Monte Carlo sampler with static trajectory.
Fields
n_leapfrog
: Number of leapfrog steps.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
sourceAdvancedHMC.NUTS
— TypeNUTS(δ::Real; max_depth::Int=10, Δ_max::Real=1000, integrator = :leapfrog, metric = :diagonal)
No-U-Turn Sampler (NUTS) sampler.
Fields
δ
: Target acceptance rate for dual averaging.
max_depth
: Maximum doubling tree depth.
Δ_max
: Maximum divergence during doubling tree.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
sourceAdvancedHMC.HMCDA
— TypeHMCDA(δ::Real, λ::Real, integrator = :leapfrog, metric = :diagonal)
Hamiltonian Monte Carlo sampler with Dual Averaging algorithm.
Fields
δ
: Target acceptance rate for dual averaging.
λ
: Target leapfrog length.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
Notes
For more information, please view the following paper (arXiv link):
- Hoffman, Matthew D., and Andrew Gelman. "The No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte Carlo." Journal of Machine Learning Research 15, no. 1 (2014): 1593-1623.
sourceFunctions
StatsBase.sample
— Functionsample([rng], a, [wv::AbstractWeights])
Select a single random element of a
. Sampling probabilities are proportional to the weights given in wv
, if provided.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample([rng], a, [wv::AbstractWeights], n::Integer; replace=true, ordered=false)
Select a random, optionally weighted sample of size n
from an array a
using a polyalgorithm. Sampling probabilities are proportional to the weights given in wv
, if provided. replace
dictates whether sampling is performed with replacement. ordered
dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a
) should be taken.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample([rng], a, [wv::AbstractWeights], dims::Dims; replace=true, ordered=false)
Select a random, optionally weighted sample from an array a
specifying the dimensions dims
of the output array. Sampling probabilities are proportional to the weights given in wv
, if provided. replace
dictates whether sampling is performed with replacement. ordered
dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a
) should be taken.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample([rng], wv::AbstractWeights)
Select a single random integer in 1:length(wv)
with probabilities proportional to the weights given in wv
.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample(
+AdvancedHMC.jl · AdvancedHMC
+
+
+
+
+
+AdvancedHMC.jl
Documentation for AdvancedHMC.jl
Types
AdvancedHMC.ClassicNoUTurn
— Typestruct ClassicNoUTurn{F<:AbstractFloat} <: AdvancedHMC.DynamicTerminationCriterion
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.
Fields
max_depth::Int64
Δ_max::AbstractFloat
References
- 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)
sourceAdvancedHMC.HMCSampler
— TypeHMCSampler
An AbstractMCMC.AbstractSampler
for kernels in AdvancedHMC.jl.
Fields
metric
: Choice of initial metric AbstractMetric
. The metric type will be preserved during adaption.
adaptor
: AdvancedHMC.Adaptation.AbstractAdaptor
.
Notes
Note that all the fields have the prefix initial_
to indicate that these will not necessarily correspond to the kernel
, metric
, and adaptor
after sampling.
To access the updated fields, use the resulting HMCState
.
sourceAdvancedHMC.HMC
— TypeHMC(ϵ::Real, n_leapfrog::Int)
Hamiltonian Monte Carlo sampler with static trajectory.
Fields
n_leapfrog
: Number of leapfrog steps.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
sourceAdvancedHMC.NUTS
— TypeNUTS(δ::Real; max_depth::Int=10, Δ_max::Real=1000, integrator = :leapfrog, metric = :diagonal)
No-U-Turn Sampler (NUTS) sampler.
Fields
δ
: Target acceptance rate for dual averaging.
max_depth
: Maximum doubling tree depth.
Δ_max
: Maximum divergence during doubling tree.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
sourceAdvancedHMC.HMCDA
— TypeHMCDA(δ::Real, λ::Real, integrator = :leapfrog, metric = :diagonal)
Hamiltonian Monte Carlo sampler with Dual Averaging algorithm.
Fields
δ
: Target acceptance rate for dual averaging.
λ
: Target leapfrog length.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
Notes
For more information, please view the following paper (arXiv link):
- Hoffman, Matthew D., and Andrew Gelman. "The No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte Carlo." Journal of Machine Learning Research 15, no. 1 (2014): 1593-1623.
sourceFunctions
StatsBase.sample
— Functionsample([rng], a, [wv::AbstractWeights])
Select a single random element of a
. Sampling probabilities are proportional to the weights given in wv
, if provided.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample([rng], a, [wv::AbstractWeights], n::Integer; replace=true, ordered=false)
Select a random, optionally weighted sample of size n
from an array a
using a polyalgorithm. Sampling probabilities are proportional to the weights given in wv
, if provided. replace
dictates whether sampling is performed with replacement. ordered
dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a
) should be taken.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample([rng], a, [wv::AbstractWeights], dims::Dims; replace=true, ordered=false)
Select a random, optionally weighted sample from an array a
specifying the dimensions dims
of the output array. Sampling probabilities are proportional to the weights given in wv
, if provided. replace
dictates whether sampling is performed with replacement. ordered
dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a
) should be taken.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample([rng], wv::AbstractWeights)
Select a single random integer in 1:length(wv)
with probabilities proportional to the weights given in wv
.
Optionally specify a random number generator rng
as the first argument (defaults to Random.default_rng()
).
sourcesample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
@@ -41,3 +499,4 @@
)
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 notverbose
controls the verbosityprogress
controls whether to show the progress meter or not
sourceMore types
AdvancedHMC.AbstractIntegrator
— Typeabstract type AbstractIntegrator
Represents an integrator used to simulate the Hamiltonian system.
Implementation
A AbstractIntegrator
is expected to have the following implementations:
stat
(@ref)nom_step_size
(@ref)step_size
(@ref)
sourceAdvancedHMC.AbstractMCMCKernel
— Typeabstract type AbstractMCMCKernel
Abstract type for HMC kernels.
sourceAdvancedHMC.AbstractMetric
— Typeabstract type AbstractMetric
Abstract type for preconditioning metrics.
sourceAdvancedHMC.AbstractTerminationCriterion
— Typeabstract type AbstractTerminationCriterion
Abstract type for termination criteria for Hamiltonian trajectories, e.g. no-U-turn and fixed number of leapfrog integration steps.
sourceAdvancedHMC.AbstractTrajectorySampler
— TypeHow to sample a phase-point from the simulated trajectory.
sourceAdvancedHMC.BinaryTree
— TypeA full binary tree trajectory with only necessary leaves and information stored.
sourceAdvancedHMC.ClassicNoUTurn
— Typestruct ClassicNoUTurn{F<:AbstractFloat} <: AdvancedHMC.DynamicTerminationCriterion
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.
Fields
max_depth::Int64
Δ_max::AbstractFloat
References
- 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)
sourceAdvancedHMC.DynamicTerminationCriterion
— Typeabstract type DynamicTerminationCriterion <: AdvancedHMC.AbstractTerminationCriterion
Abstract type for dynamic Hamiltonian trajectory termination criteria.
sourceAdvancedHMC.EndPointTS
— TypeSamples the end-point of the trajectory.
sourceAdvancedHMC.FixedIntegrationTime
— Typestruct FixedIntegrationTime{F<:AbstractFloat} <: AdvancedHMC.StaticTerminationCriterion
Standard HMC implementation with a fixed integration time.
Fields
λ::AbstractFloat
: Total length of the trajectory, i.e. take floor(λ / integrator_step_size)
number of leapfrog steps.
References
- Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)
sourceAdvancedHMC.FixedNSteps
— Typestruct FixedNSteps <: AdvancedHMC.StaticTerminationCriterion
Static HMC with a fixed number of leapfrog steps.
Fields
L::Int64
: Number of steps to simulate, i.e. length of trajectory will be L + 1
.
References
- Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)
sourceAdvancedHMC.FullMomentumRefreshment
— TypeCompletly resample new momentum.
sourceAdvancedHMC.GeneralisedNoUTurn
— Typestruct GeneralisedNoUTurn{F<:AbstractFloat} <: AdvancedHMC.DynamicTerminationCriterion
Generalised No-U-Turn criterion as described in Section A.4.2 in [1].
Fields
max_depth::Int64
Δ_max::AbstractFloat
References
- Betancourt, M. (2017). A Conceptual Introduction to Hamiltonian Monte Carlo. arXiv preprint arXiv:1701.02434.
sourceAdvancedHMC.GeneralizedLeapfrog
— Typestruct GeneralizedLeapfrog{T<:(Union{AbstractVector{var"#s48"}, var"#s48"} where var"#s48"<:AbstractFloat)} <: AdvancedHMC.AbstractLeapfrog{T<:(Union{AbstractVector{var"#s48"}, var"#s48"} where var"#s48"<:AbstractFloat)}
Generalized leapfrog integrator with fixed step size ϵ
.
Fields
ϵ::Union{AbstractVector{var"#s48"}, var"#s48"} where var"#s48"<:AbstractFloat
: Step size.
n::Int64
References
- Girolami, Mark, and Ben Calderhead. "Riemann manifold Langevin and Hamiltonian Monte Carlo methods." Journal of the Royal Statistical Society Series B: Statistical Methodology 73, no. 2 (2011): 123-214.
sourceAdvancedHMC.HMC
— TypeHMC(ϵ::Real, n_leapfrog::Int)
Hamiltonian Monte Carlo sampler with static trajectory.
Fields
n_leapfrog
: Number of leapfrog steps.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
sourceAdvancedHMC.HMCDA
— TypeHMCDA(δ::Real, λ::Real, integrator = :leapfrog, metric = :diagonal)
Hamiltonian Monte Carlo sampler with Dual Averaging algorithm.
Fields
δ
: Target acceptance rate for dual averaging.
λ
: Target leapfrog length.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
Notes
For more information, please view the following paper (arXiv link):
- Hoffman, Matthew D., and Andrew Gelman. "The No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte Carlo." Journal of Machine Learning Research 15, no. 1 (2014): 1593-1623.
sourceAdvancedHMC.HMCProgressCallback
— TypeHMCProgressCallback
A callback to be used with AbstractMCMC.jl's interface, replicating the logging behavior of the non-AbstractMCMC sample
.
Fields
pm
: Progress
meter from ProgressMeters.jl.
progress
: Specifies whether or not to use display a progress bar.
verbose
: If progress
is not specified and this is true
some information will be logged upon completion of adaptation.
num_divergent_transitions
: Number of divergent transitions fo far.
num_divergent_transitions_during_adaption
sourceAdvancedHMC.HMCSampler
— TypeHMCSampler
An AbstractMCMC.AbstractSampler
for kernels in AdvancedHMC.jl.
Fields
metric
: Choice of initial metric AbstractMetric
. The metric type will be preserved during adaption.
adaptor
: AdvancedHMC.Adaptation.AbstractAdaptor
.
Notes
Note that all the fields have the prefix initial_
to indicate that these will not necessarily correspond to the kernel
, metric
, and adaptor
after sampling.
To access the updated fields, use the resulting HMCState
.
sourceAdvancedHMC.HMCState
— TypeHMCState
Represents the state of a HMCSampler
.
Fields
i
: Index of current iteration.
transition
: Current Transition
.
metric
: Current AbstractMetric
, possibly adapted.
κ
: Current AbstractMCMCKernel
.
adaptor
: Current AbstractAdaptor
.
sourceAdvancedHMC.JitteredLeapfrog
— Typestruct 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.
Fields
ϵ0::Union{AbstractVector{FT}, 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{AbstractVector{FT}, FT} where FT<:AbstractFloat
: Current (jittered) step size.
Description
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.
References
- Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)
sourceAdvancedHMC.Leapfrog
— Typestruct Leapfrog{T<:(Union{AbstractVector{var"#s29"}, var"#s29"} where var"#s29"<:AbstractFloat)} <: AdvancedHMC.AbstractLeapfrog{T<:(Union{AbstractVector{var"#s29"}, var"#s29"} where var"#s29"<:AbstractFloat)}
Leapfrog integrator with fixed step size ϵ
.
Fields
ϵ::Union{AbstractVector{var"#s29"}, var"#s29"} where var"#s29"<:AbstractFloat
: Step size.
sourceAdvancedHMC.MultinomialTS
— Methodstruct MultinomialTS{F<:AbstractFloat} <: AdvancedHMC.AbstractTrajectorySampler
Multinomial sampler for a trajectory consisting only a leaf node.
- tree weight is the (unnormalised) energy of the leaf.
sourceAdvancedHMC.MultinomialTS
— Methodstruct MultinomialTS{F<:AbstractFloat} <: AdvancedHMC.AbstractTrajectorySampler
Multinomial sampler for the starting single leaf tree. (Log) weights for leaf nodes are their (unnormalised) Hamiltonian energies.
Ref: https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/hmc/nuts/base_nuts.hpp#L226
sourceAdvancedHMC.MultinomialTS
— Typestruct 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.
Fields
zcand::AdvancedHMC.PhasePoint
: Sampled candidate PhasePoint
.
ℓw::AbstractFloat
: Total energy for the given tree, i.e. the sum of energies of all leaves.
sourceAdvancedHMC.NUTS
— TypeNUTS(δ::Real; max_depth::Int=10, Δ_max::Real=1000, integrator = :leapfrog, metric = :diagonal)
No-U-Turn Sampler (NUTS) sampler.
Fields
δ
: Target acceptance rate for dual averaging.
max_depth
: Maximum doubling tree depth.
Δ_max
: Maximum divergence during doubling tree.
integrator
: Choice of integrator, specified either using a Symbol
or AbstractIntegrator
metric
: Choice of initial metric; Symbol
means it is automatically initialised. The metric type will be preserved during automatic initialisation and adaption.
sourceAdvancedHMC.PartialMomentumRefreshment
— Typestruct PartialMomentumRefreshment{F<:AbstractFloat} <: AdvancedHMC.AbstractMomentumRefreshment
Partial momentum refreshment with refresh rate α
.
Fields
α::AbstractFloat
See equation (5.19) [1]
r' = α⋅r + sqrt(1-α²)⋅G
where r is the momentum and G is a Gaussian random variable.
References
- Neal, Radford M. "MCMC using Hamiltonian dynamics." Handbook of markov chain monte carlo 2.11 (2011): 2.
sourceAdvancedHMC.SliceTS
— Methodstruct SliceTS{F<:AbstractFloat} <: AdvancedHMC.AbstractTrajectorySampler
Slice sampler for the starting single leaf tree. Slice variable is initialized.
sourceAdvancedHMC.SliceTS
— Methodstruct SliceTS{F<:AbstractFloat} <: AdvancedHMC.AbstractTrajectorySampler
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.
sourceAdvancedHMC.SliceTS
— Typestruct 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.
Fields
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
.
sourceAdvancedHMC.StaticTerminationCriterion
— Typeabstract type StaticTerminationCriterion <: AdvancedHMC.AbstractTerminationCriterion
Abstract type for a fixed number of leapfrog integration steps.
sourceAdvancedHMC.StrictGeneralisedNoUTurn
— Typestruct StrictGeneralisedNoUTurn{F<:AbstractFloat} <: AdvancedHMC.DynamicTerminationCriterion
Generalised No-U-Turn criterion as described in Section A.4.2 in [1] with added U-turn check as described in [2].
Fields
max_depth::Int64
Δ_max::AbstractFloat
References
- Betancourt, M. (2017). A Conceptual Introduction to Hamiltonian Monte Carlo. arXiv preprint arXiv:1701.02434.
- https://github.com/stan-dev/stan/pull/2800
sourceAdvancedHMC.TemperedLeapfrog
— Typestruct 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" α
.
Fields
ϵ::Union{AbstractVector{FT}, FT} where FT<:AbstractFloat
: Step size.
α::AbstractFloat
: Temperature parameter.
Description
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.
sourceAdvancedHMC.Termination
— MethodTermination(s, nt, H0, H′)
Check termination of a Hamiltonian trajectory.
sourceAdvancedHMC.Termination
— MethodTermination(s, nt, H0, H′)
Check termination of a Hamiltonian trajectory.
sourceAdvancedHMC.Termination
— TypeTermination
Termination reasons
dynamic
: due to stoping criterianumerical
: due to large energy deviation from starting (possibly numerical errors)
sourceAdvancedHMC.Trajectory
— Typestruct Trajectory{TS<:AdvancedHMC.AbstractTrajectorySampler, I<:AdvancedHMC.AbstractIntegrator, TC<:AdvancedHMC.AbstractTerminationCriterion}
Numerically simulated Hamiltonian trajectories.
sourceAdvancedHMC.Transition
— Typestruct Transition{P<:AdvancedHMC.PhasePoint, NT<:NamedTuple}
A transition that contains the phase point and other statistics of the transition.
Fields
z::AdvancedHMC.PhasePoint
: Phase-point for the transition.
stat::NamedTuple
: Statistics related to the transition, e.g. energy.
sourceAdvancedHMC.Adaptation.AbstractAdaptor
— Typeabstract type AbstractAdaptor
Abstract type for HMC adaptors.
sourceAdvancedHMC.Adaptation.NesterovDualAveraging
— TypeAn implementation of the Nesterov dual averaging algorithm to tune step size.
References
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. Nesterov, Y. (2009). Primal-dual subgradient methods for convex problems. Mathematical programming, 120(1), 221-259.
sourceSettings
This document was generated with Documenter.jl version 1.7.0 on Friday 4 October 2024. Using Julia version 1.7.3.
+
diff --git a/v0.6.2/index.html b/v0.6.2/index.html
index f6594822..3e65a4a2 100644
--- a/v0.6.2/index.html
+++ b/v0.6.2/index.html
@@ -1,5 +1,463 @@
-AdvancedHMC.jl · AdvancedHMC AdvancedHMC.jl
AdvancedHMC.jl provides a robust, modular, and efficient implementation of advanced HMC algorithms. An illustrative example of AdvancedHMC's usage is given below. AdvancedHMC.jl is part of Turing.jl, a probabilistic programming library in Julia. If you are interested in using AdvancedHMC.jl through a probabilistic programming language, please check it out!
Interfaces
IMP.hmc
: an experimental Python module for the Integrative Modeling Platform, which uses AdvancedHMC in its backend to sample protein structures.
NEWS
- We presented a paper for AdvancedHMC.jl at AABI in 2019 in Vancouver, Canada. (abs, pdf, OpenReview)
- We presented a poster for AdvancedHMC.jl at StanCon 2019 in Cambridge, UK. (pdf)
API CHANGES
- [v0.5.0] Breaking! Convenience constructors for common samplers changed to:
HMC(leapfrog_stepsize::Real, n_leapfrog::Int)
NUTS(target_acceptance::Real)
HMCDA(target_acceptance::Real, integration_time::Real)
- [v0.2.22] Three functions are renamed.
Preconditioner(metric::AbstractMetric)
-> MassMatrixAdaptor(metric)
and NesterovDualAveraging(δ, integrator::AbstractIntegrator)
-> StepSizeAdaptor(δ, integrator)
find_good_eps
-> find_good_stepsize
- [v0.2.15]
n_adapts
is no longer needed to construct StanHMCAdaptor
; the old constructor is deprecated. - [v0.2.8] Two Hamiltonian trajectory sampling methods are renamed to avoid a name clash with Distributions.
Multinomial
-> MultinomialTS
Slice
-> SliceTS
- [v0.2.0] The gradient function passed to
Hamiltonian
is supposed to return a value-gradient tuple now.
A minimal example - sampling from a multivariate Gaussian using NUTS
This section demonstrates a minimal example of sampling from a multivariate Gaussian (10-dimensional) using the no U-turn sampler (NUTS). Below we describe the major components of the Hamiltonian system which are essential to sample using this approach:
Metric: In many sampling problems the sample space is associated with a metric that allows us to measure the distance between any two points, and other similar quantities. In the example in this section, we use a special metric called the Euclidean Metric, represented with a D × D
matrix from which we can compute distances.[1]
Leapfrog integration: Leapfrog integration is a second-order numerical method for integrating differential equations (In this case they are equations of motion for the relative position of one particle with respect to the other). The order of this integration signifies its rate of convergence. Any algorithm with a finite time step size will have numerical errors, and the order is related to this error. For a second-order algorithm, this error scales as the second power of the time step, hence, the name second-order. High-order integrators are usually complex to code and have a limited region of convergence; hence they do not allow arbitrarily large time steps. A second-order integrator is suitable for our purpose. Hence we opt for the leapfrog integrator. It is called leapfrog
due to the ways this algorithm is written, where the positions and velocities of particles "leap over" each other.[2]
Kernel for trajectories (static or dynamic): Different kernels, which may be static or dynamic, can be used. At each iteration of any variant of the HMC algorithm, there are two main steps - the first step changes the momentum and the second step may change both the position and the momentum of a particle.[3]
using AdvancedHMC, ForwardDiff
+AdvancedHMC.jl · AdvancedHMC
+
+
+
+
+
+AdvancedHMC.jl
AdvancedHMC.jl provides a robust, modular, and efficient implementation of advanced HMC algorithms. An illustrative example of AdvancedHMC's usage is given below. AdvancedHMC.jl is part of Turing.jl, a probabilistic programming library in Julia. If you are interested in using AdvancedHMC.jl through a probabilistic programming language, please check it out!
Interfaces
IMP.hmc
: an experimental Python module for the Integrative Modeling Platform, which uses AdvancedHMC in its backend to sample protein structures.
NEWS
- We presented a paper for AdvancedHMC.jl at AABI in 2019 in Vancouver, Canada. (abs, pdf, OpenReview)
- We presented a poster for AdvancedHMC.jl at StanCon 2019 in Cambridge, UK. (pdf)
API CHANGES
- [v0.5.0] Breaking! Convenience constructors for common samplers changed to:
HMC(leapfrog_stepsize::Real, n_leapfrog::Int)
NUTS(target_acceptance::Real)
HMCDA(target_acceptance::Real, integration_time::Real)
- [v0.2.22] Three functions are renamed.
Preconditioner(metric::AbstractMetric)
-> MassMatrixAdaptor(metric)
and NesterovDualAveraging(δ, integrator::AbstractIntegrator)
-> StepSizeAdaptor(δ, integrator)
find_good_eps
-> find_good_stepsize
- [v0.2.15]
n_adapts
is no longer needed to construct StanHMCAdaptor
; the old constructor is deprecated. - [v0.2.8] Two Hamiltonian trajectory sampling methods are renamed to avoid a name clash with Distributions.
Multinomial
-> MultinomialTS
Slice
-> SliceTS
- [v0.2.0] The gradient function passed to
Hamiltonian
is supposed to return a value-gradient tuple now.
A minimal example - sampling from a multivariate Gaussian using NUTS
This section demonstrates a minimal example of sampling from a multivariate Gaussian (10-dimensional) using the no U-turn sampler (NUTS). Below we describe the major components of the Hamiltonian system which are essential to sample using this approach:
Metric: In many sampling problems the sample space is associated with a metric that allows us to measure the distance between any two points, and other similar quantities. In the example in this section, we use a special metric called the Euclidean Metric, represented with a D × D
matrix from which we can compute distances.[1]
Leapfrog integration: Leapfrog integration is a second-order numerical method for integrating differential equations (In this case they are equations of motion for the relative position of one particle with respect to the other). The order of this integration signifies its rate of convergence. Any algorithm with a finite time step size will have numerical errors, and the order is related to this error. For a second-order algorithm, this error scales as the second power of the time step, hence, the name second-order. High-order integrators are usually complex to code and have a limited region of convergence; hence they do not allow arbitrarily large time steps. A second-order integrator is suitable for our purpose. Hence we opt for the leapfrog integrator. It is called leapfrog
due to the ways this algorithm is written, where the positions and velocities of particles "leap over" each other.[2]
Kernel for trajectories (static or dynamic): Different kernels, which may be static or dynamic, can be used. At each iteration of any variant of the HMC algorithm, there are two main steps - the first step changes the momentum and the second step may change both the position and the momentum of a particle.[3]
using AdvancedHMC, ForwardDiff
using LogDensityProblems
using LinearAlgebra
@@ -132,3 +590,4 @@
year={2018},
organization={PMLR}
}
References
Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov chain Monte Carlo, 2(11), 2. (arXiv)
Betancourt, M. (2017). A Conceptual Introduction to Hamiltonian Monte Carlo. arXiv preprint arXiv:1701.02434.
Girolami, M., & Calderhead, B. (2011). Riemann manifold Langevin and Hamiltonian Monte Carlo methods. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 73(2), 123-214. (arXiv)
Betancourt, M. J., Byrne, S., & Girolami, M. (2014). Optimizing the integrator step size for Hamiltonian Monte Carlo. arXiv preprint arXiv:1411.6669.
Betancourt, M. (2016). Identifying the optimal integration time in Hamiltonian Monte Carlo. arXiv preprint arXiv:1601.00225.
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)
Footnotes
- 1The Euclidean metric is also known as the mass matrix in the physical perspective. See Hamiltonian mass matrix for available metrics.
- 2About the leapfrog integration scheme: Suppose ${\bf x}$ and ${\bf v}$ are the position and velocity of an individual particle respectively; $i$ and $i+1$ are the indices for time values $t_i$ and $t_{i+1}$ respectively; $dt = t_{i+1} - t_i$ is the time step size (constant and regularly spaced intervals), and ${\bf a}$ is the acceleration induced on a particle by the forces of all other particles. Furthermore, suppose positions are defined at times $t_i, t_{i+1}, t_{i+2}, \dots $, spaced at constant intervals $dt$, the velocities are defined at halfway times in between, denoted by $t_{i-1/2}, t_{i+1/2}, t_{i+3/2}, \dots $, where $t_{i+1} - t_{i + 1/2} = t_{i + 1/2} - t_i = dt / 2$, and the accelerations ${\bf a}$ are defined only on integer times, just like the positions. Then the leapfrog integration scheme is given as: $x_{i} = x_{i-1} + v_{i-1/2} dt; \quad v_{i+1/2} = v_{i-1/2} + a_i dt$. For available integrators refer to Integrator.
- 3On kernels: In the classical HMC approach, during the first step, new values for the momentum variables are randomly drawn from their Gaussian distribution, independently of the current values of the position variables. A Metropolis update is performed during the second step, using Hamiltonian dynamics to provide a new state. For available kernels refer to Kernel.
Settings
This document was generated with Documenter.jl version 1.7.0 on Friday 4 October 2024. Using Julia version 1.7.3.
+