MarkovChainMonteCarlo
Top-level class and methods
CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCWrapper — Type
struct MCMCWrapper{VV1<:(AbstractVector), VV2<:(AbstractVector), VV3<:(AbstractVector)}Top-level class holding all configuration information needed for MCMC sampling: the prior, emulated likelihood and sampling algorithm (DensityModel and Sampler, respectively, in AbstractMCMC's terminology).
Fields
prior::EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution:ParameterDistributionobject describing the prior distribution on parameter values.encoded_prior::EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution:ParameterDistributionobject describing the encoded prior distribution on the encoded parameter values.observations::AbstractVector: [outputdim x Nsamples] matrix, of given observation data.encoded_observations::AbstractVector: Vector of observations describing the data samples to actually used during MCMC sampling (that have been transformed into a space consistent with emulator outputs).log_posterior_map::AbstractMCMC.AbstractModel:AdvancedMH.DensityModelobject, used to evaluate the posterior density being sampled from.mh_proposal_sampler::AbstractMCMC.AbstractSampler: Object describing a MCMC sampling algorithm and its settings.sample_kwargs::NamedTuple: NamedTuple of other arguments to be passed toAbstractMCMC.sample().encoder_schedule::AbstractVector: Vector of encoders dictating how to encode/decode data.
CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCWrapper — Method
MCMCWrapper(
mcmc_alg::CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCProtocol,
observation::Union{AbstractMatrix, AbstractVector},
prior::EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution,
em_or_fmw::Union{CalibrateEmulateSample.Emulators.Emulator, CalibrateEmulateSample.Emulators.ForwardMapWrapper};
init_params,
burnin,
kwargs...
) -> CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCWrapper
Constructor for MCMCWrapper that will perform an MCMC sampling in the encoded space given by em_or_fmw (Emulator or ForwardMapWrapper). It creates and wraps an instance of EmulatorPosteriorModel, for sampling from the Emulator (predicting means and covariances), and MetropolisHastingsSampler, for generating the MC proposals.
mcmc_alg:MCMCProtocoldescribing the MCMC sampling algorithm to use. Currently implemented algorithms are:RWMHSampling: Metropolis-Hastings sampling from a vanilla random walk with fixed stepsize.pCNMHSampling: Metropolis-Hastings sampling using the preconditioned Crank-Nicholson algorithm, which has a well-behaved small-stepsize limit.BarkerSampling: Metropolis-Hastings sampling using the Barker proposal, which has a robustness to choosing step-size parameters.
observation: Vector (for one sample) or matrix with columns as samples from the observation. Can, e.g., be picked from anObservationstruct usingget_obs_sample.prior:ParameterDistributionobject containing the parameters' prior distributions.em_or_fmw:EmulatororForwardMapWrapperto sample from.init_params: Starting parameter values for MCMC sampling. (defined in unconstrained parameter coordinates)burnin: Initial number of MCMC steps to discard from output (pre-convergence).
StatsBase.sample — Function
sample([rng,] mcmc::MCMCWrapper, args...; kwargs...)
Extends the sample methods of AbstractMCMC (which extends StatsBase) to sample from the emulated posterior, using the MCMC sampling algorithm and Emulator configured in MCMCWrapper. Returns a MCMCChains.Chains object containing the samples.
Supported methods are:
sample([rng, ]mcmc, N; kwargs...)Return a
MCMCChains.Chainsobject containingNsamples from the emulated posterior.sample([rng, ]mcmc, isdone; kwargs...)Sample from the
modelwith the Markov chain Monte Carlosampleruntil a convergence criterionisdonereturnstrue, and return the samples. The functionisdonehas the signatureisdone(rng, model, sampler, samples, state, iteration; kwargs...)where
stateanditerationare the current state and iteration of the sampler, respectively. It should returntruewhen sampling should end, andfalseotherwise.sample([rng, ]mcmc, parallel_type, N, nchains; kwargs...)Sample
nchainsMonte Carlo Markov chains in parallel according toparallel_type, which may beMCMCThreads()orMCMCDistributed()for thread and parallel sampling, respectively.
CalibrateEmulateSample.MarkovChainMonteCarlo.get_posterior — Function
get_posterior(
mcmc::CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCWrapper,
chain::MCMCChains.Chains;
noise_injector_threshold,
noise_injector_scaling
) -> EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution
Returns a ParameterDistribution object corresponding to the empirical distribution of the samples in chain.
keyword args
noise_injector_threshold[=0.001]: If the encoded space is lossy, and the lost variability due to encoding exceeds a thresholdnoise_injector_threshold, then in place of decoding posterior samples, additional noise consistent with the prior is injected into the null space of the encoder. Seedecode_and_add_noise()for more detail.noise_injector_scaling[=1.0]: Scales the injected noise; though 1.0 is the only "consistent" value, reduction may be necessary if noise injection causes posterior samples to be unstable in simulations.
CalibrateEmulateSample.MarkovChainMonteCarlo.optimize_stepsize — Function
optimize_stepsize(
rng::Random.AbstractRNG,
mcmc::CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCWrapper;
init_stepsize,
N,
max_iter,
target_acc,
sample_kwargs...
) -> Float64
Uses a heuristic to return a stepsize for the mh_proposal_sampler element of MCMCWrapper which yields fast convergence of the Markov chain.
The criterion used is that Metropolis-Hastings proposals should be accepted between 15% and 35% of the time.
sourceSee AbstractMCMC sampling API for background on our use of Turing.jl's AbstractMCMC API for MCMC sampling.
Sampler algorithms
CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCProtocol — Type
abstract type MCMCProtocolType used to dispatch different methods of the MetropolisHastingsSampler constructor, corresponding to different sampling algorithms.
CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocol — Type
abstract type AutodiffProtocolType used to dispatch different autodifferentiation methods where different emulators have a different compatability with autodiff packages
sourceCalibrateEmulateSample.MarkovChainMonteCarlo.ForwardDiffProtocol — Type
abstract type ForwardDiffProtocol <: CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocolType to construct samplers for emulators compatible with ForwardDiff.jl autodifferentiation
CalibrateEmulateSample.MarkovChainMonteCarlo.ReverseDiffProtocol — Type
abstract type ReverseDiffProtocol <: CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocolType to construct samplers for emulators compatible with ReverseDiff.jl autodifferentiation
CalibrateEmulateSample.MarkovChainMonteCarlo.RWMHSampling — Type
struct RWMHSampling{T<:CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocol} <: CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCProtocolMCMCProtocol which uses Metropolis-Hastings sampling that generates proposals for new parameters as as vanilla random walk, based on the covariance of prior.
CalibrateEmulateSample.MarkovChainMonteCarlo.pCNMHSampling — Type
struct pCNMHSampling{T<:CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocol} <: CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCProtocolMCMCProtocol which uses Metropolis-Hastings sampling that generates proposals for new parameters according to the preconditioned Crank-Nicholson (pCN) algorithm, which is usable for MCMC in the stepsize → 0 limit, unlike the vanilla random walk. Steps are based on the covariance of prior.
CalibrateEmulateSample.MarkovChainMonteCarlo.BarkerSampling — Type
struct BarkerSampling{T<:CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocol} <: CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCProtocolMCMCProtocol which uses Metropolis-Hastings sampling that generates proposals for new parameters according to the Barker proposal.
CalibrateEmulateSample.MarkovChainMonteCarlo.MetropolisHastingsSampler — Function
MetropolisHastingsSampler(
_::CalibrateEmulateSample.MarkovChainMonteCarlo.RWMHSampling{T<:CalibrateEmulateSample.MarkovChainMonteCarlo.AutodiffProtocol},
encoded_prior::EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution,
encoder_schedule::AbstractVector
) -> CalibrateEmulateSample.MarkovChainMonteCarlo.RWMetropolisHastings{AdvancedMH.RandomWalkProposal{false, _A}} where _A
Constructor for all Sampler objects, with one method for each supported MCMC algorithm.
Both currently implemented Samplers use a Gaussian approximation to the prior: specifically, new Metropolis-Hastings proposals are a scaled combination of the old parameter values and a random shift distributed as a Gaussian with the same covariance as the prior.
This suffices for our current use case, in which our priors are Gaussian (possibly in a transformed space) and we assume enough fidelity in the Emulator that inference isn't prior-dominated.
Emulated posterior (Model)
CalibrateEmulateSample.MarkovChainMonteCarlo.EmulatorPosteriorModel — Function
EmulatorPosteriorModel(
encoded_prior::EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution,
em_or_fmw::Union{CalibrateEmulateSample.Emulators.Emulator, CalibrateEmulateSample.Emulators.ForwardMapWrapper},
obs_vec::AbstractVector
) -> AdvancedMH.DensityModel{F} where F<:(CalibrateEmulateSample.MarkovChainMonteCarlo.var"#13#14"{EnsembleKalmanProcesses.ParameterDistributions.ParameterDistribution{PDType, CType, ST}, _A, <:AbstractVector{T}} where {PDType<:EnsembleKalmanProcesses.ParameterDistributions.ParameterDistributionType, CType<:EnsembleKalmanProcesses.ParameterDistributions.ConstraintType, ST<:AbstractString, _A, T})
Factory which constructs AdvancedMH.DensityModel objects given an (Encoded) prior on the model parameters (encoded_prior) and an Emulator of the log-likelihood of the data given parameters. Together these yield the log posterior density we're attempting to sample from with the MCMC, which is the role of the DensityModel class in the AbstractMCMC interface.
Internals - MCMC State
CalibrateEmulateSample.MarkovChainMonteCarlo.MCMCState — Type
struct MCMCState{T, L<:Real} <: AdvancedMH.AbstractTransitionExtends the AdvancedMH.Transition (which encodes the current state of the MC during sampling) with a boolean flag to record whether this state is new (arising from accepting a Metropolis-Hastings proposal) or old (from rejecting a proposal).
Fields
params::Any: Sampled value of the parameters at the current state of the MCMC chain.log_density::Real: Log probability ofparams, as computed by the model using the prior.accepted::Bool: Whether this state resulted from accepting a new MH proposal.
CalibrateEmulateSample.MarkovChainMonteCarlo.accept_ratio — Function
accept_ratio(chain::MCMCChains.Chains) -> Any
Fraction of MC proposals in chain which were accepted (according to Metropolis-Hastings.)