ComradeAHMC.AHMC
— TypeAHMC
Creates a sampler that uses the AdvancedHMC
framework to construct an Hamiltonian Monte Carlo NUTS sampler.
The user must specify the metric
they want to use. Typically we recommend DiagEuclideanMetric
as a reasonable starting place. The other options are chosen to match the Stan
languages defaults and should provide a good starting point. Please see the AdvancedHMC docs for more information.
Notes
For autodiff
the must provide a Val(::Symbol)
that specifies the AD backend. Currently, we use LogDensityProblemsAD
.
Fields
metric
:AdvancedHMC
metric to use
integrator
:AdvancedHMC
integrator Defaults toAdvancedHMC.Leapfrog
trajectory
: HMC trajectory sampler Defaults toAdvancedHMC.MultinomialTS
termination
: HMC termination condition Defaults toAdvancedHMC.StrictGeneralisedNoUTurn
adaptor
: Adaptation strategy for mass matrix and stepsize Defaults toAdvancedHMC.StanHMCAdaptor
targetacc
: Target acceptance rate for all trajectories on the tree Defaults to 0.85
init_buffer
: The number of steps for the initial tuning phase. Defaults to 75 which is the Stan default
term_buffer
: The number of steps for the final fast step size adaptation Default if 50 which is the Stan default
window_size
: The number of steps to tune the covariance before the first doubling Default is 25 which is the Stan default
autodiff
: autodiff backend seeLogDensitProblemsAD.jl
for possible backends. The default isZygote
which is appropriate for high dimensional problems.
ComradeAHMC.DiskStore
— TypeDisk
Type that specifies to save the HMC results to disk.
Fields
name
: Path of the directory where the results will be saved. If the path does not exist it will be automatically created.
stride
: The output stride, i.e. everystride
steps the MCMC output will be dumped to disk.
ComradeAHMC.MemoryStore
— TypeMemory
Stored the HMC samplers in memory or ram.
ComradeAHMC.load_table
— Functionload_table(out::DiskOutput, indices::Union{Base.Colon, UnitRange, StepRange}=Base.Colon(); table="samples")
load_table(out::String, indices::Union{Base.Colon, UnitRange, StepRange}=Base.Colon(); table="samples")
The the results from a HMC run saved to disk. To read in the output the user can either pass the resulting out
object, or the path to the directory that the results were saved, i.e. the path specified in DiskStore
.
Arguments
out::Union{String, DiskOutput}
: Ifout
is a string is must point to the direct that theDiskStore
pointed to. Otherwise it is what is directly returned from sample.indices
: The indices of the that you want to load into memory. The default is to load the entire table.
Keyword Arguments
table
: A string specifying the table you wish to read in. There are two options: "samples" which corresponds to the actual MCMC chain, andstats
which corresponds to additional information about the sampler, e.g., the log density of each sample and tree statistics.
StatsBase.sample
— MethodAbstractMCMC.sample(post::Comrade.Posterior,
sampler::AHMC,
nsamples;
initial_params=nothing,
saveto::Union{Memory, Disk}=Memory(),
kwargs...)
Samples the posterior post
using the AdvancedHMC sampler specified by AHMC
. This will run the sampler for nsamples
.
To initialize the chain the user can set initial_params
to Vector{NamedTuple}
whose elements are the starting locations for each of the nchains
. If no starting location is specified nchains
random samples from the prior will be chosen for the starting locations.
With saveto
the user can optionally specify whether to store the samples in memory MemoryStore
or save directly to disk with DiskStore(filename, stride)
. The stride
controls how often the samples are dumped to disk. In addition is saving to disk the user can also resume a previous run by setting restart=true
.
For possible kwargs
please see the AdvancedHMC.jl docs
This returns a PosteriorSamples
object.
Notes
This will automatically transform the posterior to the flattened unconstrained space.
StatsBase.sample
— MethodAbstractMCMC.sample(post::Comrade.Posterior,
sampler::AHMC,
parallel::AbstractMCMC.AbstractMCMCEnsemble,
nsamples,
nchainsl;
initial_params=nothing,
kwargs...)
Samples the posterior post
using the AdvancedHMC sampler specified by AHMC
. This will sample nchains
copies of the posterior using the parallel
scheme. Each chain will be sampled for nsamples
.
To initialize the chain the user can set initial_params
to Vector{NamedTuple}
whose elements are the starting locations for each of the nchains
. If no starting location is specified nchains
random samples from the prior will be chosen for the starting locations.
For possible kwargs
please see the AdvancedHMC.jl docs
This returns a PosteriorSamples
object indexed as iteration × chain.
Notes
This will automatically transform the posterior to the flattened unconstrained space.