API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
AbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
Sampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
Sampling a single chain
StatsBase.sample
— Methodsample(
+API · AbstractMCMC
+
+
+
+
+
+API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
@@ -72,3 +536,4 @@
nchains::Integer;
kwargs...,
)
Wrap the logdensity
function in a LogDensityModel
, and call sample
with the resulting model instead of logdensity
.
The logdensity
function has to support the LogDensityProblems.jl interface.
sourceTwo algorithms are provided for parallel sampling with multiple threads and multiple processes, and one allows for the user to sample multiple chains in serial (no parallelization):
AbstractMCMC.MCMCThreads
— TypeMCMCThreads
The MCMCThreads
algorithm allows users to sample MCMC chains in parallel using multiple threads.
sourceAbstractMCMC.MCMCDistributed
— TypeMCMCDistributed
The MCMCDistributed
algorithm allows users to sample MCMC chains in parallel using multiple processes.
sourceAbstractMCMC.MCMCSerial
— TypeMCMCSerial
The MCMCSerial
algorithm allows users to sample serially, with no thread or process parallelism.
sourceCommon keyword arguments
Common keyword arguments for regular and parallel sampling are:
progress
(default: AbstractMCMC.PROGRESS[]
which is true
initially): toggles progress loggingchain_type
(default: Any
): determines the type of the returned chaincallback
(default: nothing
): if callback !== nothing
, then callback(rng, model, sampler, sample, state, iteration)
is called after every sampling step, where sample
is the most recent sample of the Markov chain and state
and iteration
are the current state and iteration of the samplerdiscard_initial
(default: 0
): number of initial samples that are discardedthinning
(default: 1
): factor by which to thin samples.initial_state
(default: nothing
): if initial_state !== nothing
, the first call to AbstractMCMC.step
is passed initial_state
as the state
argument.
Info The common keyword arguments progress
, chain_type
, and callback
are not supported by the iterator AbstractMCMC.steps
and the transducer AbstractMCMC.Sample
.
There is no "official" way for providing initial parameter values yet. However, multiple packages such as EllipticalSliceSampling.jl and AdvancedMH.jl support an initial_params
keyword argument for setting the initial values when sampling a single chain. To ensure that sampling multiple chains "just works" when sampling of a single chain is implemented, we decided to support initial_params
in the default implementations of the ensemble methods:
initial_params
(default: nothing
): if initial_params isa AbstractArray
, then the i
th element of initial_params
is used as initial parameters of the i
th chain. If one wants to use the same initial parameters x
for every chain, one can specify e.g. initial_params = FillArrays.Fill(x, N)
.
Progress logging can be enabled and disabled globally with AbstractMCMC.setprogress!(progress)
.
AbstractMCMC.setprogress!
— Functionsetprogress!(progress::Bool; silent::Bool=false)
Enable progress logging globally if progress
is true
, and disable it otherwise. Optionally disable informational message if silent
is true
.
sourceChains
The chain_type
keyword argument allows to set the type of the returned chain. A common choice is to return chains of type Chains
from MCMCChains.jl.
AbstractMCMC defines the abstract type AbstractChains
for Markov chains.
AbstractMCMC.AbstractChains
— TypeAbstractChains
AbstractChains
is an abstract type for an object that stores parameter samples generated through a MCMC process.
sourceFor chains of this type, AbstractMCMC defines the following two methods.
AbstractMCMC.chainscat
— Functionchainscat(c::AbstractChains...)
Concatenate multiple chains.
By default, the chains are concatenated along the third dimension by calling cat(c...; dims=3)
.
sourceAbstractMCMC.chainsstack
— Functionchainsstack(c::AbstractVector)
Stack chains in c
.
By default, the vector of chains is returned unmodified. If eltype(c) <: AbstractChains
, then reduce(chainscat, c)
is called.
sourceSettings
This document was generated with Documenter.jl version 1.4.1 on Monday 24 June 2024. Using Julia version 1.10.4.
+
diff --git a/dev/design/index.html b/dev/design/index.html
index 1d20dc97..f8f67dd5 100644
--- a/dev/design/index.html
+++ b/dev/design/index.html
@@ -1,5 +1,469 @@
-Design · AbstractMCMC Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
+Design · AbstractMCMC
+
+
+
+
+
+Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractMCMC.AbstractModel,
sampler::AbstractMCMC.AbstractSampler,
@@ -25,3 +489,4 @@
return AbstractMCMC.bundle_samples(samples, model, sampler, state, chain_type; kwargs...)
end
All other default implementations make use of the same structure and in particular call the same methods.
Sampling step
The only method for which no default implementation is provided (and hence which downstream packages have to implement) is AbstractMCMC.step
. It defines the sampling step of the inference method.
AbstractMCMC.step
— Functionstep(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
Samples describe the results of a single step of the sampler
. As an example, a sample might include a vector of parameters sampled from a prior distribution.
When sampling using sample
, every step
call after the first has access to the current state
of the sampler.
sourceCollecting samples
Note This section does not apply to the iterator and transducer interface.
After the initial sample is obtained, the default implementations for regular and parallel sampling (not for the iterator and the transducer since it is not needed there) create a container for all samples (the initial one and all subsequent samples) using AbstractMCMC.samples
.
AbstractMCMC.samples
— Functionsamples(sample, model, sampler[, N; kwargs...])
Generate a container for the samples of the MCMC sampler
for the model
, whose first sample is sample
.
The method can be called with and without a predefined number N
of samples.
sourceIn each step, the sample is saved in the container by AbstractMCMC.save!!
. The notation !!
follows the convention of the package BangBang.jl which is used in the default implementation of AbstractMCMC.save!!
. It indicates that the sample is pushed to the container but a "widening" fallback is used if the container type does not allow to save the sample. Therefore AbstractMCMC.save!!
always has to return the container.
AbstractMCMC.save!!
— Functionsave!!(samples, sample, iteration, model, sampler[, N; kwargs...])
Save the sample
of the MCMC sampler
at the current iteration
in the container of samples
.
The function can be called with and without a predefined number N
of samples. By default, AbstractMCMC uses push!!
from the Julia package BangBang to append to the container, and widen its type if needed.
sourceFor most use cases the default implementation of AbstractMCMC.samples
and AbstractMCMC.save!!
should work out of the box and hence need not to be overloaded in downstream code.
Creating chains
Note This section does not apply to the iterator and transducer interface.
At the end of the sampling procedure for regular and paralle sampling we transform the collection of samples to the desired output type by calling AbstractMCMC.bundle_samples
.
AbstractMCMC.bundle_samples
— Functionbundle_samples(samples, model, sampler, state, chain_type[; kwargs...])
Bundle all samples
that were sampled from the model
with the given sampler
in a chain.
The final state
of the sampler
can be included in the chain. The type of the chain can be specified with the chain_type
argument.
By default, this method returns samples
.
sourceThe default implementation should be fine in most use cases, but downstream packages could, e.g., save the final state of the sampler as well if they overload AbstractMCMC.bundle_samples
.
Settings
This document was generated with Documenter.jl version 1.4.1 on Monday 24 June 2024. Using Julia version 1.10.4.
+
diff --git a/dev/index.html b/dev/index.html
index 6e6e2b91..c96a1010 100644
--- a/dev/index.html
+++ b/dev/index.html
@@ -1,2 +1,467 @@
-Home · AbstractMCMC AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 1.4.1 on Monday 24 June 2024. Using Julia version 1.10.4.
+Home · AbstractMCMC
+
+
+
+
+
+AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 1.4.1 on Monday 24 June 2024. Using Julia version 1.10.4.
+
diff --git a/index.html b/index.html
index 1a6cc116..83aa121b 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
+
diff --git a/previews/PR117/api/index.html b/previews/PR117/api/index.html
index 6d1d858c..3e60542b 100644
--- a/previews/PR117/api/index.html
+++ b/previews/PR117/api/index.html
@@ -1,5 +1,469 @@
-API · AbstractMCMC API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
+API · AbstractMCMC
+
+
+
+
+
+API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
@@ -72,3 +536,4 @@
nchains::Integer;
kwargs...,
)
Wrap the logdensity
function in a LogDensityModel
, and call sample
with the resulting model instead of logdensity
.
The logdensity
function has to support the LogDensityProblems.jl interface.
sourceTwo algorithms are provided for parallel sampling with multiple threads and multiple processes, and one allows for the user to sample multiple chains in serial (no parallelization):
AbstractMCMC.MCMCThreads
— TypeMCMCThreads
The MCMCThreads
algorithm allows users to sample MCMC chains in parallel using multiple threads.
sourceAbstractMCMC.MCMCDistributed
— TypeMCMCDistributed
The MCMCDistributed
algorithm allows users to sample MCMC chains in parallel using multiple processes.
sourceAbstractMCMC.MCMCSerial
— TypeMCMCSerial
The MCMCSerial
algorithm allows users to sample serially, with no thread or process parallelism.
sourceCommon keyword arguments
Common keyword arguments for regular and parallel sampling are:
progress
(default: AbstractMCMC.PROGRESS[]
which is true
initially): toggles progress loggingchain_type
(default: Any
): determines the type of the returned chaincallback
(default: nothing
): if callback !== nothing
, then callback(rng, model, sampler, sample, iteration)
is called after every sampling step, where sample
is the most recent sample of the Markov chain and iteration
is the current iterationnum_warmup
(default: 0
): number of "warm-up" steps to take before the first "regular" step, i.e. number of times to call AbstractMCMC.step_warmup
before the first call to AbstractMCMC.step
.discard_initial
(default: num_warmup
): number of initial samples that are discarded. Note that if discard_initial < num_warmup
, warm-up samples will also be included in the resulting samples.thinning
(default: 1
): factor by which to thin samples.initial_state
(default: nothing
): if initial_state !== nothing
, the first call to AbstractMCMC.step
is passed initial_state
as the state
argument.
Info The common keyword arguments progress
, chain_type
, and callback
are not supported by the iterator AbstractMCMC.steps
and the transducer AbstractMCMC.Sample
.
There is no "official" way for providing initial parameter values yet. However, multiple packages such as EllipticalSliceSampling.jl and AdvancedMH.jl support an initial_params
keyword argument for setting the initial values when sampling a single chain. To ensure that sampling multiple chains "just works" when sampling of a single chain is implemented, we decided to support initial_params
in the default implementations of the ensemble methods:
initial_params
(default: nothing
): if initial_params isa AbstractArray
, then the i
th element of initial_params
is used as initial parameters of the i
th chain. If one wants to use the same initial parameters x
for every chain, one can specify e.g. initial_params = FillArrays.Fill(x, N)
.
Progress logging can be enabled and disabled globally with AbstractMCMC.setprogress!(progress)
.
AbstractMCMC.setprogress!
— Functionsetprogress!(progress::Bool)
Enable progress logging globally if progress
is true
, and disable it otherwise.
sourceChains
The chain_type
keyword argument allows to set the type of the returned chain. A common choice is to return chains of type Chains
from MCMCChains.jl.
AbstractMCMC defines the abstract type AbstractChains
for Markov chains.
AbstractMCMC.AbstractChains
— TypeAbstractChains
AbstractChains
is an abstract type for an object that stores parameter samples generated through a MCMC process.
sourceFor chains of this type, AbstractMCMC defines the following two methods.
AbstractMCMC.chainscat
— Functionchainscat(c::AbstractChains...)
Concatenate multiple chains.
By default, the chains are concatenated along the third dimension by calling cat(c...; dims=3)
.
sourceAbstractMCMC.chainsstack
— Functionchainsstack(c::AbstractVector)
Stack chains in c
.
By default, the vector of chains is returned unmodified. If eltype(c) <: AbstractChains
, then reduce(chainscat, c)
is called.
sourceSettings
This document was generated with Documenter.jl version 1.1.2 on Thursday 26 October 2023. Using Julia version 1.9.3.
+
diff --git a/previews/PR117/design/index.html b/previews/PR117/design/index.html
index 6336ba0b..938bddad 100644
--- a/previews/PR117/design/index.html
+++ b/previews/PR117/design/index.html
@@ -1,5 +1,469 @@
-Design · AbstractMCMC Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
+Design · AbstractMCMC
+
+
+
+
+
+Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractMCMC.AbstractModel,
sampler::AbstractMCMC.AbstractSampler,
@@ -25,3 +489,4 @@
return AbstractMCMC.bundle_samples(samples, model, sampler, state, chain_type; kwargs...)
end
All other default implementations make use of the same structure and in particular call the same methods.
Sampling step
The only method for which no default implementation is provided (and hence which downstream packages have to implement) is AbstractMCMC.step
. It defines the sampling step of the inference method.
AbstractMCMC.step
— Functionstep(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
Samples describe the results of a single step of the sampler
. As an example, a sample might include a vector of parameters sampled from a prior distribution.
When sampling using sample
, every step
call after the first has access to the current state
of the sampler.
sourceIf one also has some special handling of the warmup-stage of sampling, then this can be specified by overloading
AbstractMCMC.step_warmup
— Functionstep_warmup(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
When sampling using sample
, this takes the place of AbstractMCMC.step
in the first num_warmup
number of iterations, as specified by the num_warmup
keyword to sample
. This is useful if the sampler has an initial "warmup"-stage that is different from the standard iteration.
By default, this simply calls AbstractMCMC.step
.
sourcewhich will be used for the first num_warmup
iterations, as specified as a keyword argument to AbstractMCMC.sample
. Note that this is optional; by default it simply calls AbstractMCMC.step
from above.
Collecting samples
Note This section does not apply to the iterator and transducer interface.
After the initial sample is obtained, the default implementations for regular and parallel sampling (not for the iterator and the transducer since it is not needed there) create a container for all samples (the initial one and all subsequent samples) using AbstractMCMC.samples
.
AbstractMCMC.samples
— Functionsamples(sample, model, sampler[, N; kwargs...])
Generate a container for the samples of the MCMC sampler
for the model
, whose first sample is sample
.
The method can be called with and without a predefined number N
of samples.
sourceIn each step, the sample is saved in the container by AbstractMCMC.save!!
. The notation !!
follows the convention of the package BangBang.jl which is used in the default implementation of AbstractMCMC.save!!
. It indicates that the sample is pushed to the container but a "widening" fallback is used if the container type does not allow to save the sample. Therefore AbstractMCMC.save!!
always has to return the container.
AbstractMCMC.save!!
— Functionsave!!(samples, sample, iteration, model, sampler[, N; kwargs...])
Save the sample
of the MCMC sampler
at the current iteration
in the container of samples
.
The function can be called with and without a predefined number N
of samples. By default, AbstractMCMC uses push!!
from the Julia package BangBang to append to the container, and widen its type if needed.
sourceFor most use cases the default implementation of AbstractMCMC.samples
and AbstractMCMC.save!!
should work out of the box and hence need not to be overloaded in downstream code.
Creating chains
Note This section does not apply to the iterator and transducer interface.
At the end of the sampling procedure for regular and paralle sampling we transform the collection of samples to the desired output type by calling AbstractMCMC.bundle_samples
.
AbstractMCMC.bundle_samples
— Functionbundle_samples(samples, model, sampler, state, chain_type[; kwargs...])
Bundle all samples
that were sampled from the model
with the given sampler
in a chain.
The final state
of the sampler
can be included in the chain. The type of the chain can be specified with the chain_type
argument.
By default, this method returns samples
.
sourceThe default implementation should be fine in most use cases, but downstream packages could, e.g., save the final state of the sampler as well if they overload AbstractMCMC.bundle_samples
.
Settings
This document was generated with Documenter.jl version 1.1.2 on Thursday 26 October 2023. Using Julia version 1.9.3.
+
diff --git a/previews/PR117/index.html b/previews/PR117/index.html
index 1deb3115..1e6772e8 100644
--- a/previews/PR117/index.html
+++ b/previews/PR117/index.html
@@ -1,2 +1,467 @@
-Home · AbstractMCMC AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 1.1.2 on Thursday 26 October 2023. Using Julia version 1.9.3.
+Home · AbstractMCMC
+
+
+
+
+
+AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 1.1.2 on Thursday 26 October 2023. Using Julia version 1.9.3.
+
diff --git a/previews/PR120/api/index.html b/previews/PR120/api/index.html
index 628fa55c..dda64ed4 100644
--- a/previews/PR120/api/index.html
+++ b/previews/PR120/api/index.html
@@ -1,5 +1,469 @@
-API · AbstractMCMC API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
+API · AbstractMCMC
+
+
+
+
+
+API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
@@ -72,3 +536,4 @@
nchains::Integer;
kwargs...,
)
Wrap the logdensity
function in a LogDensityModel
, and call sample
with the resulting model instead of logdensity
.
The logdensity
function has to support the LogDensityProblems.jl interface.
sourceTwo algorithms are provided for parallel sampling with multiple threads and multiple processes, and one allows for the user to sample multiple chains in serial (no parallelization):
AbstractMCMC.MCMCThreads
— TypeMCMCThreads
The MCMCThreads
algorithm allows users to sample MCMC chains in parallel using multiple threads.
sourceAbstractMCMC.MCMCDistributed
— TypeMCMCDistributed
The MCMCDistributed
algorithm allows users to sample MCMC chains in parallel using multiple processes.
sourceAbstractMCMC.MCMCSerial
— TypeMCMCSerial
The MCMCSerial
algorithm allows users to sample serially, with no thread or process parallelism.
sourceCommon keyword arguments
Common keyword arguments for regular and parallel sampling are:
progress
(default: AbstractMCMC.PROGRESS[]
which is true
initially): toggles progress loggingchain_type
(default: Any
): determines the type of the returned chaincallback
(default: nothing
): if callback !== nothing
, then callback(rng, model, sampler, sample, iteration)
is called after every sampling step, where sample
is the most recent sample of the Markov chain and iteration
is the current iterationdiscard_initial
(default: 0
): number of initial samples that are discardedthinning
(default: 1
): factor by which to thin samples.
Info The common keyword arguments progress
, chain_type
, and callback
are not supported by the iterator AbstractMCMC.steps
and the transducer AbstractMCMC.Sample
.
There is no "official" way for providing initial parameter values yet. However, multiple packages such as EllipticalSliceSampling.jl and AdvancedMH.jl support an init_params
keyword argument for setting the initial values when sampling a single chain. To ensure that sampling multiple chains "just works" when sampling of a single chain is implemented, we decided to support init_params
in the default implementations of the ensemble methods:
init_params
(default: nothing
): if set to init_params !== nothing
, then the i
th element of init_params
is used as initial parameters of the i
th chain. If one wants to use the same initial parameters x
for every chain, one can specify e.g. init_params = Iterators.repeated(x)
or init_params = FillArrays.Fill(x, N)
.
Progress logging can be enabled and disabled globally with AbstractMCMC.setprogress!(progress)
.
AbstractMCMC.setprogress!
— Functionsetprogress!(progress::Bool)
Enable progress logging globally if progress
is true
, and disable it otherwise.
sourceChains
The chain_type
keyword argument allows to set the type of the returned chain. A common choice is to return chains of type Chains
from MCMCChains.jl.
AbstractMCMC defines the abstract type AbstractChains
for Markov chains.
AbstractMCMC.AbstractChains
— TypeAbstractChains
AbstractChains
is an abstract type for an object that stores parameter samples generated through a MCMC process.
sourceFor chains of this type, AbstractMCMC defines the following two methods.
AbstractMCMC.chainscat
— Functionchainscat(c::AbstractChains...)
Concatenate multiple chains.
By default, the chains are concatenated along the third dimension by calling cat(c...; dims=3)
.
sourceAbstractMCMC.chainsstack
— Functionchainsstack(c::AbstractVector)
Stack chains in c
.
By default, the vector of chains is returned unmodified. If eltype(c) <: AbstractChains
, then reduce(chainscat, c)
is called.
sourceSettings
This document was generated with Documenter.jl version 0.27.24 on Monday 13 March 2023. Using Julia version 1.8.5.
+
diff --git a/previews/PR120/design/index.html b/previews/PR120/design/index.html
index 16ba8773..d2c324e5 100644
--- a/previews/PR120/design/index.html
+++ b/previews/PR120/design/index.html
@@ -1,5 +1,469 @@
-Design · AbstractMCMC Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
+Design · AbstractMCMC
+
+
+
+
+
+Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractMCMC.AbstractModel,
sampler::AbstractMCMC.AbstractSampler,
@@ -25,3 +489,4 @@
return AbstractMCMC.bundle_samples(samples, model, sampler, state, chain_type; kwargs...)
end
All other default implementations make use of the same structure and in particular call the same methods.
Sampling step
The only method for which no default implementation is provided (and hence which downstream packages have to implement) is AbstractMCMC.step
. It defines the sampling step of the inference method.
AbstractMCMC.step
— Functionstep(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
Samples describe the results of a single step of the sampler
. As an example, a sample might include a vector of parameters sampled from a prior distribution.
When sampling using sample
, every step
call after the first has access to the current state
of the sampler.
sourceCollecting samples
Note This section does not apply to the iterator and transducer interface.
After the initial sample is obtained, the default implementations for regular and parallel sampling (not for the iterator and the transducer since it is not needed there) create a container for all samples (the initial one and all subsequent samples) using AbstractMCMC.samples
.
AbstractMCMC.samples
— Functionsamples(sample, model, sampler[, N; kwargs...])
Generate a container for the samples of the MCMC sampler
for the model
, whose first sample is sample
.
The method can be called with and without a predefined number N
of samples.
sourceIn each step, the sample is saved in the container by AbstractMCMC.save!!
. The notation !!
follows the convention of the package BangBang.jl which is used in the default implementation of AbstractMCMC.save!!
. It indicates that the sample is pushed to the container but a "widening" fallback is used if the container type does not allow to save the sample. Therefore AbstractMCMC.save!!
always has to return the container.
AbstractMCMC.save!!
— Functionsave!!(samples, sample, iteration, model, sampler[, N; kwargs...])
Save the sample
of the MCMC sampler
at the current iteration
in the container of samples
.
The function can be called with and without a predefined number N
of samples. By default, AbstractMCMC uses push!!
from the Julia package BangBang to append to the container, and widen its type if needed.
sourceFor most use cases the default implementation of AbstractMCMC.samples
and AbstractMCMC.save!!
should work out of the box and hence need not to be overloaded in downstream code.
Creating chains
Note This section does not apply to the iterator and transducer interface.
At the end of the sampling procedure for regular and paralle sampling we transform the collection of samples to the desired output type by calling AbstractMCMC.bundle_samples
.
AbstractMCMC.bundle_samples
— Functionbundle_samples(samples, model, sampler, state, chain_type[; kwargs...])
Bundle all samples
that were sampled from the model
with the given sampler
in a chain.
The final state
of the sampler
can be included in the chain. The type of the chain can be specified with the chain_type
argument.
By default, this method returns samples
.
sourceThe default implementation should be fine in most use cases, but downstream packages could, e.g., save the final state of the sampler as well if they overload AbstractMCMC.bundle_samples
.
Settings
This document was generated with Documenter.jl version 0.27.24 on Monday 13 March 2023. Using Julia version 1.8.5.
+
diff --git a/previews/PR120/index.html b/previews/PR120/index.html
index 259f3bb6..ca1078c1 100644
--- a/previews/PR120/index.html
+++ b/previews/PR120/index.html
@@ -1,2 +1,467 @@
-Home · AbstractMCMC AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 0.27.24 on Monday 13 March 2023. Using Julia version 1.8.5.
+Home · AbstractMCMC
+
+
+
+
+
+AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 0.27.24 on Monday 13 March 2023. Using Julia version 1.8.5.
+
diff --git a/previews/PR120/search/index.html b/previews/PR120/search/index.html
index 719cfded..52919329 100644
--- a/previews/PR120/search/index.html
+++ b/previews/PR120/search/index.html
@@ -1,2 +1,467 @@
-Search · AbstractMCMC Settings
This document was generated with Documenter.jl version 0.27.24 on Monday 13 March 2023. Using Julia version 1.8.5.
+Search · AbstractMCMC
+
+
+
+
+
+Settings
This document was generated with Documenter.jl version 0.27.24 on Monday 13 March 2023. Using Julia version 1.8.5.
+
diff --git a/previews/PR124/api/index.html b/previews/PR124/api/index.html
index ce3b5fb3..e634a483 100644
--- a/previews/PR124/api/index.html
+++ b/previews/PR124/api/index.html
@@ -1,5 +1,469 @@
-API · AbstractMCMC API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
+API · AbstractMCMC
+
+
+
+
+
+API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
@@ -72,3 +536,4 @@
nchains::Integer;
kwargs...,
)
Wrap the logdensity
function in a LogDensityModel
, and call sample
with the resulting model instead of logdensity
.
The logdensity
function has to support the LogDensityProblems.jl interface.
sourceTwo algorithms are provided for parallel sampling with multiple threads and multiple processes, and one allows for the user to sample multiple chains in serial (no parallelization):
AbstractMCMC.MCMCThreads
— TypeMCMCThreads
The MCMCThreads
algorithm allows users to sample MCMC chains in parallel using multiple threads.
sourceAbstractMCMC.MCMCDistributed
— TypeMCMCDistributed
The MCMCDistributed
algorithm allows users to sample MCMC chains in parallel using multiple processes.
sourceAbstractMCMC.MCMCSerial
— TypeMCMCSerial
The MCMCSerial
algorithm allows users to sample serially, with no thread or process parallelism.
sourceCommon keyword arguments
Common keyword arguments for regular and parallel sampling are:
progress
(default: AbstractMCMC.PROGRESS[]
which is true
initially): toggles progress loggingchain_type
(default: Any
): determines the type of the returned chaincallback
(default: nothing
): if callback !== nothing
, then callback(rng, model, sampler, sample, iteration)
is called after every sampling step, where sample
is the most recent sample of the Markov chain and iteration
is the current iterationdiscard_initial
(default: 0
): number of initial samples that are discardedthinning
(default: 1
): factor by which to thin samples.
Info The common keyword arguments progress
, chain_type
, and callback
are not supported by the iterator AbstractMCMC.steps
and the transducer AbstractMCMC.Sample
.
There is no "official" way for providing initial parameter values yet. However, multiple packages such as EllipticalSliceSampling.jl and AdvancedMH.jl support an init_params
keyword argument for setting the initial values when sampling a single chain. To ensure that sampling multiple chains "just works" when sampling of a single chain is implemented, we decided to support init_params
in the default implementations of the ensemble methods:
init_params
(default: nothing
): if set to init_params !== nothing
, then the i
th element of init_params
is used as initial parameters of the i
th chain. If one wants to use the same initial parameters x
for every chain, one can specify e.g. init_params = Iterators.repeated(x)
or init_params = FillArrays.Fill(x, N)
.
Progress logging can be enabled and disabled globally with AbstractMCMC.setprogress!(progress)
.
AbstractMCMC.setprogress!
— Functionsetprogress!(progress::Bool)
Enable progress logging globally if progress
is true
, and disable it otherwise.
sourceChains
The chain_type
keyword argument allows to set the type of the returned chain. A common choice is to return chains of type Chains
from MCMCChains.jl.
AbstractMCMC defines the abstract type AbstractChains
for Markov chains.
AbstractMCMC.AbstractChains
— TypeAbstractChains
AbstractChains
is an abstract type for an object that stores parameter samples generated through a MCMC process.
sourceFor chains of this type, AbstractMCMC defines the following two methods.
AbstractMCMC.chainscat
— Functionchainscat(c::AbstractChains...)
Concatenate multiple chains.
By default, the chains are concatenated along the third dimension by calling cat(c...; dims=3)
.
sourceAbstractMCMC.chainsstack
— Functionchainsstack(c::AbstractVector)
Stack chains in c
.
By default, the vector of chains is returned unmodified. If eltype(c) <: AbstractChains
, then reduce(chainscat, c)
is called.
sourceSettings
This document was generated with Documenter.jl version 0.27.25 on Saturday 29 July 2023. Using Julia version 1.9.2.
+
diff --git a/previews/PR124/design/index.html b/previews/PR124/design/index.html
index 91662321..cc412447 100644
--- a/previews/PR124/design/index.html
+++ b/previews/PR124/design/index.html
@@ -1,5 +1,469 @@
-Design · AbstractMCMC Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
+Design · AbstractMCMC
+
+
+
+
+
+Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractMCMC.AbstractModel,
sampler::AbstractMCMC.AbstractSampler,
@@ -25,3 +489,4 @@
return AbstractMCMC.bundle_samples(samples, model, sampler, state, chain_type; kwargs...)
end
All other default implementations make use of the same structure and in particular call the same methods.
Sampling step
The only method for which no default implementation is provided (and hence which downstream packages have to implement) is AbstractMCMC.step
. It defines the sampling step of the inference method.
AbstractMCMC.step
— Functionstep(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
Samples describe the results of a single step of the sampler
. As an example, a sample might include a vector of parameters sampled from a prior distribution.
When sampling using sample
, every step
call after the first has access to the current state
of the sampler.
sourceCollecting samples
Note This section does not apply to the iterator and transducer interface.
After the initial sample is obtained, the default implementations for regular and parallel sampling (not for the iterator and the transducer since it is not needed there) create a container for all samples (the initial one and all subsequent samples) using AbstractMCMC.samples
.
AbstractMCMC.samples
— Functionsamples(sample, model, sampler[, N; kwargs...])
Generate a container for the samples of the MCMC sampler
for the model
, whose first sample is sample
.
The method can be called with and without a predefined number N
of samples.
sourceIn each step, the sample is saved in the container by AbstractMCMC.save!!
. The notation !!
follows the convention of the package BangBang.jl which is used in the default implementation of AbstractMCMC.save!!
. It indicates that the sample is pushed to the container but a "widening" fallback is used if the container type does not allow to save the sample. Therefore AbstractMCMC.save!!
always has to return the container.
AbstractMCMC.save!!
— Functionsave!!(samples, sample, iteration, model, sampler[, N; kwargs...])
Save the sample
of the MCMC sampler
at the current iteration
in the container of samples
.
The function can be called with and without a predefined number N
of samples. By default, AbstractMCMC uses push!!
from the Julia package BangBang to append to the container, and widen its type if needed.
sourceFor most use cases the default implementation of AbstractMCMC.samples
and AbstractMCMC.save!!
should work out of the box and hence need not to be overloaded in downstream code.
Creating chains
Note This section does not apply to the iterator and transducer interface.
At the end of the sampling procedure for regular and paralle sampling we transform the collection of samples to the desired output type by calling AbstractMCMC.bundle_samples
.
AbstractMCMC.bundle_samples
— Functionbundle_samples(samples, model, sampler, state, chain_type[; kwargs...])
Bundle all samples
that were sampled from the model
with the given sampler
in a chain.
The final state
of the sampler
can be included in the chain. The type of the chain can be specified with the chain_type
argument.
By default, this method returns samples
.
sourceThe default implementation should be fine in most use cases, but downstream packages could, e.g., save the final state of the sampler as well if they overload AbstractMCMC.bundle_samples
.
Settings
This document was generated with Documenter.jl version 0.27.25 on Saturday 29 July 2023. Using Julia version 1.9.2.
+
diff --git a/previews/PR124/index.html b/previews/PR124/index.html
index 2c955a6e..9e3a4244 100644
--- a/previews/PR124/index.html
+++ b/previews/PR124/index.html
@@ -1,2 +1,467 @@
-Home · AbstractMCMC AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 0.27.25 on Saturday 29 July 2023. Using Julia version 1.9.2.
+Home · AbstractMCMC
+
+
+
+
+
+AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 0.27.25 on Saturday 29 July 2023. Using Julia version 1.9.2.
+
diff --git a/previews/PR124/search/index.html b/previews/PR124/search/index.html
index 6dc9d650..d4b8e358 100644
--- a/previews/PR124/search/index.html
+++ b/previews/PR124/search/index.html
@@ -1,2 +1,467 @@
-Search · AbstractMCMC Settings
This document was generated with Documenter.jl version 0.27.25 on Saturday 29 July 2023. Using Julia version 1.9.2.
+Search · AbstractMCMC
+
+
+
+
+
+Settings
This document was generated with Documenter.jl version 0.27.25 on Saturday 29 July 2023. Using Julia version 1.9.2.
+
diff --git a/previews/PR137/api/index.html b/previews/PR137/api/index.html
index 4cf6822d..3914e958 100644
--- a/previews/PR137/api/index.html
+++ b/previews/PR137/api/index.html
@@ -1,5 +1,469 @@
-API · AbstractMCMC API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
+API · AbstractMCMC
+
+
+
+
+
+API
AbstractMCMC defines an interface for sampling Markov chains.
Model
AbstractMCMC.AbstractModel
— TypeAbstractModel
An AbstractModel
represents a generic model type that can be used to perform inference.
sourceAbstractMCMC.LogDensityModel
— TypeLogDensityModel <: AbstractMCMC.AbstractModel
Wrapper around something that implements the LogDensityProblem.jl interface.
Note that this does not implement the LogDensityProblems.jl interface itself, but it simply useful for indicating to the sample
and other AbstractMCMC
methods that the wrapped object implements the LogDensityProblems.jl interface.
Fields
logdensity
: The object that implements the LogDensityProblems.jl interface.
sourceSampler
AbstractMCMC.AbstractSampler
— TypeAbstractSampler
The AbstractSampler
type is intended to be inherited from when implementing a custom sampler. Any persistent state information should be saved in a subtype of AbstractSampler
.
When defining a new sampler, you should also overload the function transition_type
, which tells the sample
function what type of parameter it should expect to receive.
sourceSampling a single chain
StatsBase.sample
— Methodsample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
@@ -72,3 +536,4 @@
nchains::Integer;
kwargs...,
)
Wrap the logdensity
function in a LogDensityModel
, and call sample
with the resulting model instead of logdensity
.
The logdensity
function has to support the LogDensityProblems.jl interface.
sourceTwo algorithms are provided for parallel sampling with multiple threads and multiple processes, and one allows for the user to sample multiple chains in serial (no parallelization):
AbstractMCMC.MCMCThreads
— TypeMCMCThreads
The MCMCThreads
algorithm allows users to sample MCMC chains in parallel using multiple threads.
sourceAbstractMCMC.MCMCDistributed
— TypeMCMCDistributed
The MCMCDistributed
algorithm allows users to sample MCMC chains in parallel using multiple processes.
sourceAbstractMCMC.MCMCSerial
— TypeMCMCSerial
The MCMCSerial
algorithm allows users to sample serially, with no thread or process parallelism.
sourceCommon keyword arguments
Common keyword arguments for regular and parallel sampling are:
progress
(default: AbstractMCMC.PROGRESS[]
which is true
initially): toggles progress loggingchain_type
(default: Any
): determines the type of the returned chaincallback
(default: nothing
): if callback !== nothing
, then callback(rng, model, sampler, sample, state, iteration)
is called after every sampling step, where sample
is the most recent sample of the Markov chain and state
and iteration
are the current state and iteration of the samplerdiscard_initial
(default: 0
): number of initial samples that are discardedthinning
(default: 1
): factor by which to thin samples.initial_state
(default: nothing
): if initial_state !== nothing
, the first call to AbstractMCMC.step
is passed initial_state
as the state
argument.
Info The common keyword arguments progress
, chain_type
, and callback
are not supported by the iterator AbstractMCMC.steps
and the transducer AbstractMCMC.Sample
.
There is no "official" way for providing initial parameter values yet. However, multiple packages such as EllipticalSliceSampling.jl and AdvancedMH.jl support an initial_params
keyword argument for setting the initial values when sampling a single chain. To ensure that sampling multiple chains "just works" when sampling of a single chain is implemented, we decided to support initial_params
in the default implementations of the ensemble methods:
initial_params
(default: nothing
): if initial_params isa AbstractArray
, then the i
th element of initial_params
is used as initial parameters of the i
th chain. If one wants to use the same initial parameters x
for every chain, one can specify e.g. initial_params = FillArrays.Fill(x, N)
.
Progress logging can be enabled and disabled globally with AbstractMCMC.setprogress!(progress)
.
AbstractMCMC.setprogress!
— Functionsetprogress!(progress::Bool)
Enable progress logging globally if progress
is true
, and disable it otherwise.
sourceChains
The chain_type
keyword argument allows to set the type of the returned chain. A common choice is to return chains of type Chains
from MCMCChains.jl.
AbstractMCMC defines the abstract type AbstractChains
for Markov chains.
AbstractMCMC.AbstractChains
— TypeAbstractChains
AbstractChains
is an abstract type for an object that stores parameter samples generated through a MCMC process.
sourceFor chains of this type, AbstractMCMC defines the following two methods.
AbstractMCMC.chainscat
— Functionchainscat(c::AbstractChains...)
Concatenate multiple chains.
By default, the chains are concatenated along the third dimension by calling cat(c...; dims=3)
.
sourceAbstractMCMC.chainsstack
— Functionchainsstack(c::AbstractVector)
Stack chains in c
.
By default, the vector of chains is returned unmodified. If eltype(c) <: AbstractChains
, then reduce(chainscat, c)
is called.
sourceSettings
This document was generated with Documenter.jl version 1.2.1 on Wednesday 14 February 2024. Using Julia version 1.10.0.
+
diff --git a/previews/PR137/design/index.html b/previews/PR137/design/index.html
index dbcadafc..d9557f02 100644
--- a/previews/PR137/design/index.html
+++ b/previews/PR137/design/index.html
@@ -1,5 +1,469 @@
-Design · AbstractMCMC Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
+Design · AbstractMCMC
+
+
+
+
+
+Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractMCMC.AbstractModel,
sampler::AbstractMCMC.AbstractSampler,
@@ -25,3 +489,4 @@
return AbstractMCMC.bundle_samples(samples, model, sampler, state, chain_type; kwargs...)
end
All other default implementations make use of the same structure and in particular call the same methods.
Sampling step
The only method for which no default implementation is provided (and hence which downstream packages have to implement) is AbstractMCMC.step
. It defines the sampling step of the inference method.
AbstractMCMC.step
— Functionstep(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
Samples describe the results of a single step of the sampler
. As an example, a sample might include a vector of parameters sampled from a prior distribution.
When sampling using sample
, every step
call after the first has access to the current state
of the sampler.
sourceCollecting samples
Note This section does not apply to the iterator and transducer interface.
After the initial sample is obtained, the default implementations for regular and parallel sampling (not for the iterator and the transducer since it is not needed there) create a container for all samples (the initial one and all subsequent samples) using AbstractMCMC.samples
.
AbstractMCMC.samples
— Functionsamples(sample, model, sampler[, N; kwargs...])
Generate a container for the samples of the MCMC sampler
for the model
, whose first sample is sample
.
The method can be called with and without a predefined number N
of samples.
sourceIn each step, the sample is saved in the container by AbstractMCMC.save!!
. The notation !!
follows the convention of the package BangBang.jl which is used in the default implementation of AbstractMCMC.save!!
. It indicates that the sample is pushed to the container but a "widening" fallback is used if the container type does not allow to save the sample. Therefore AbstractMCMC.save!!
always has to return the container.
AbstractMCMC.save!!
— Functionsave!!(samples, sample, iteration, model, sampler[, N; kwargs...])
Save the sample
of the MCMC sampler
at the current iteration
in the container of samples
.
The function can be called with and without a predefined number N
of samples. By default, AbstractMCMC uses push!!
from the Julia package BangBang to append to the container, and widen its type if needed.
sourceFor most use cases the default implementation of AbstractMCMC.samples
and AbstractMCMC.save!!
should work out of the box and hence need not to be overloaded in downstream code.
Creating chains
Note This section does not apply to the iterator and transducer interface.
At the end of the sampling procedure for regular and paralle sampling we transform the collection of samples to the desired output type by calling AbstractMCMC.bundle_samples
.
AbstractMCMC.bundle_samples
— Functionbundle_samples(samples, model, sampler, state, chain_type[; kwargs...])
Bundle all samples
that were sampled from the model
with the given sampler
in a chain.
The final state
of the sampler
can be included in the chain. The type of the chain can be specified with the chain_type
argument.
By default, this method returns samples
.
sourceThe default implementation should be fine in most use cases, but downstream packages could, e.g., save the final state of the sampler as well if they overload AbstractMCMC.bundle_samples
.
Settings
This document was generated with Documenter.jl version 1.2.1 on Wednesday 14 February 2024. Using Julia version 1.10.0.
+
diff --git a/previews/PR137/index.html b/previews/PR137/index.html
index 1acfcc48..90a02547 100644
--- a/previews/PR137/index.html
+++ b/previews/PR137/index.html
@@ -1,2 +1,467 @@
-Home · AbstractMCMC AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 1.2.1 on Wednesday 14 February 2024. Using Julia version 1.10.0.
+Home · AbstractMCMC
+
+
+
+
+
+AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 1.2.1 on Wednesday 14 February 2024. Using Julia version 1.10.0.
+
diff --git a/previews/PR86/api/index.html b/previews/PR86/api/index.html
index f3afb89d..4a942655 100644
--- a/previews/PR86/api/index.html
+++ b/previews/PR86/api/index.html
@@ -1,5 +1,469 @@
-API · AbstractMCMC API
AbstractMCMC defines an interface for sampling Markov chains.
Sampling a single chain
StatsBase.sample
— Methodsample([rng, ]model, sampler, N; kwargs...)
Return N
samples from the model
with the Markov chain Monte Carlo sampler
.
sourceStatsBase.sample
— Methodsample([rng, ]model, sampler, isdone; kwargs...)
Sample from the model
with the Markov chain Monte Carlo sampler
until a convergence criterion isdone
returns true
, and return the samples.
The function isdone
has the signature
isdone(rng, model, sampler, samples, iteration; kwargs...)
and should return true
when sampling should end, and false
otherwise.
sourceIterator
AbstractMCMC.steps
— Methodsteps([rng, ]model, sampler; kwargs...)
Create an iterator that returns samples from the model
with the Markov chain Monte Carlo sampler
.
Examples
julia> struct MyModel <: AbstractMCMC.AbstractModel end
+API · AbstractMCMC
+
+
+
+
+
+API
AbstractMCMC defines an interface for sampling Markov chains.
Sampling a single chain
StatsBase.sample
— Methodsample([rng, ]model, sampler, N; kwargs...)
Return N
samples from the model
with the Markov chain Monte Carlo sampler
.
sourceStatsBase.sample
— Methodsample([rng, ]model, sampler, isdone; kwargs...)
Sample from the model
with the Markov chain Monte Carlo sampler
until a convergence criterion isdone
returns true
, and return the samples.
The function isdone
has the signature
isdone(rng, model, sampler, samples, iteration; kwargs...)
and should return true
when sampling should end, and false
otherwise.
sourceIterator
AbstractMCMC.steps
— Methodsteps([rng, ]model, sampler; kwargs...)
Create an iterator that returns samples from the model
with the Markov chain Monte Carlo sampler
.
Examples
julia> struct MyModel <: AbstractMCMC.AbstractModel end
julia> struct MySampler <: AbstractMCMC.AbstractSampler end
@@ -112,3 +576,4 @@
rng, model[i], sampler_current, state_current;
kwargs...
)
This issue should eventually disappear as the community moves towards a unified approach to implement AbstractMCMC.AbstractModel
.
Settings
This document was generated with Documenter.jl version 0.27.3 on Tuesday 7 December 2021. Using Julia version 1.7.0.
+
diff --git a/previews/PR86/design/index.html b/previews/PR86/design/index.html
index c31f7d2b..c225922c 100644
--- a/previews/PR86/design/index.html
+++ b/previews/PR86/design/index.html
@@ -1,5 +1,469 @@
-Design · AbstractMCMC Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
+Design · AbstractMCMC
+
+
+
+
+
+Design
This page explains the default implementations and design choices of AbstractMCMC. It is not intended for users but for developers that want to implement the AbstractMCMC interface for Markov chain Monte Carlo sampling. The user-facing API is explained in API.
Overview
AbstractMCMC provides a default implementation of the user-facing interface described in API. You can completely neglect these and define your own implementation of the interface. However, as described below, in most use cases the default implementation allows you to obtain support of parallel sampling, progress logging, callbacks, iterators, and transducers for free by just defining the sampling step of your inference algorithm, drastically reducing the amount of code you have to write. In general, the docstrings of the functions described below might be helpful if you intend to make use of the default implementations.
Basic structure
The simplified structure for regular sampling (the actual implementation contains some additional error checks and support for progress logging and callbacks) is
StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractMCMC.AbstractModel,
sampler::AbstractMCMC.AbstractSampler,
@@ -25,3 +489,4 @@
return AbstractMCMC.bundle_samples(samples, model, sampler, state, chain_type; kwargs...)
end
All other default implementations make use of the same structure and in particular call the same methods.
Sampling step
The only method for which no default implementation is provided (and hence which downstream packages have to implement) is AbstractMCMC.step
. It defines the sampling step of the inference method.
AbstractMCMC.step
— Functionstep(rng, model, sampler[, state; kwargs...])
Return a 2-tuple of the next sample and the next state of the MCMC sampler
for model
.
Samples describe the results of a single step of the sampler
. As an example, a sample might include a vector of parameters sampled from a prior distribution.
When sampling using sample
, every step
call after the first has access to the current state
of the sampler.
sourceCollecting samples
Note This section does not apply to the iterator and transducer interface.
After the initial sample is obtained, the default implementations for regular and parallel sampling (not for the iterator and the transducer since it is not needed there) create a container for all samples (the initial one and all subsequent samples) using AbstractMCMC.samples
.
AbstractMCMC.samples
— Functionsamples(sample, model, sampler[, N; kwargs...])
Generate a container for the samples of the MCMC sampler
for the model
, whose first sample is sample
.
The method can be called with and without a predefined number N
of samples.
sourceIn each step, the sample is saved in the container by AbstractMCMC.save!!
. The notation !!
follows the convention of the package BangBang.jl which is used in the default implementation of AbstractMCMC.save!!
. It indicates that the sample is pushed to the container but a "widening" fallback is used if the container type does not allow to save the sample. Therefore AbstractMCMC.save!!
always has to return the container.
AbstractMCMC.save!!
— Functionsave!!(samples, sample, iteration, model, sampler[, N; kwargs...])
Save the sample
of the MCMC sampler
at the current iteration
in the container of samples
.
The function can be called with and without a predefined number N
of samples. By default, AbstractMCMC uses push!!
from the Julia package BangBang to append to the container, and widen its type if needed.
sourceFor most use cases the default implementation of AbstractMCMC.samples
and AbstractMCMC.save!!
should work out of the box and hence need not to be overloaded in downstream code.
Creating chains
Note This section does not apply to the iterator and transducer interface.
At the end of the sampling procedure for regular and paralle sampling we transform the collection of samples to the desired output type by calling AbstractMCMC.bundle_samples
.
AbstractMCMC.bundle_samples
— Functionbundle_samples(samples, model, sampler, state, chain_type[; kwargs...])
Bundle all samples
that were sampled from the model
with the given sampler
in a chain.
The final state
of the sampler
can be included in the chain. The type of the chain can be specified with the chain_type
argument.
By default, this method returns samples
.
sourceThe default implementation should be fine in most use cases, but downstream packages could, e.g., save the final state of the sampler as well if they overload AbstractMCMC.bundle_samples
.
Settings
This document was generated with Documenter.jl version 0.27.3 on Tuesday 7 December 2021. Using Julia version 1.7.0.
+
diff --git a/previews/PR86/index.html b/previews/PR86/index.html
index ee575643..5f0f7a6b 100644
--- a/previews/PR86/index.html
+++ b/previews/PR86/index.html
@@ -1,2 +1,467 @@
-Home · AbstractMCMC AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 0.27.3 on Tuesday 7 December 2021. Using Julia version 1.7.0.
+Home · AbstractMCMC
+
+
+
+
+
+AbstractMCMC.jl
Abstract types and interfaces for Markov chain Monte Carlo methods.
AbstractMCMC defines an interface for sampling and combining Markov chains. It comes with a default sampling algorithm that provides support of progress bars, parallel sampling (multithreaded and multicore), and user-provided callbacks out of the box. Typically developers only have to define the sampling step of their inference method in an iterator-like fashion to make use of this functionality. Additionally, the package defines an iterator and a transducer for sampling Markov chains based on the interface.
Settings
This document was generated with Documenter.jl version 0.27.3 on Tuesday 7 December 2021. Using Julia version 1.7.0.
+
diff --git a/previews/PR86/search/index.html b/previews/PR86/search/index.html
index f1faf2a9..68466cf6 100644
--- a/previews/PR86/search/index.html
+++ b/previews/PR86/search/index.html
@@ -1,2 +1,467 @@
-Search · AbstractMCMC Settings
This document was generated with Documenter.jl version 0.27.3 on Tuesday 7 December 2021. Using Julia version 1.7.0.
+Search · AbstractMCMC
+
+
+
+
+
+Settings
This document was generated with Documenter.jl version 0.27.3 on Tuesday 7 December 2021. Using Julia version 1.7.0.
+