DifferentialEvolutionMetropolis Documentation
Tools for sampling from log-densities using differential evolution algorithms.
See Sampling from multimodal distributions and Customizing your sampler to get started.
This package is built upon AbstractMCMC.jl so log-densities should be constructed using that package, and can be used with TransformVariables.jl or Bijectors.jl to control the parameter space.
The other key dependency is Distributions.jl. Almost every parameter in proposals given here are defined via customizable univariate distributions. Values that are fixed are specified via a Dirac distribution, though in the API these can be specified with any real value. As a warning there are some checks on the given distributions, but in the interest of flexibility it is up to the user to ensure that they are suitable for the given parameter. You can disable any checking of your provided distributions with ; check_args = false if you really want to ruin your sampler efficiency. Distributions can optionally be used to define your log-density, as in the examples given here.
As far as I am aware, there is one other package that implements differential evolution MCMC in Julia, DifferentialEvolutionMCMC.jl. I opted to implement my own version as I wanted a more flexible API and the subsampling scheme from DREAM. That's not to discredit DifferentialEvolutionMCMC.jl, it has many features this package does not, such as being able to work on optimization problems and parameter blocking.
Main features
- Original differential evolution, snooker, and adaptive subspace sampling (i.e. from DREAM) updates
- Optional parallel tempering (no swaps yet, information is shared by the DE updates!) and annealing
- Composite samplers, can combine any of the implemented updates (in future I'll wrap other abstractMCMC based samplers)
- Easy to implement your own updates!
- Can output in
MCMCChainsformat, though you use multiple sampling chains (i.e. chains of the DE-chains) these will all be appended together
Next Steps
A few plans for this package, feel free to suggest features or improvements via issues:
- Implement multi-try and delayed rejection DREAM, I avoided these so far since I have been using these samplers for costly log-densities with relatively few parameters, such as one that solve an ODE.
- Additional diagnostic checks and adaptive schemes.
Contents
- Customizing your sampler
- DifferentialEvolutionMetropolis Documentation
- Sampling from multimodal distributions
Functions
Implemented Sampling Schemes
DifferentialEvolutionMetropolis.deMC — Function
deMC(model_wrapper, n_its; kwargs...)Run the Differential Evolution Markov Chain (DE-MC) sampler proposed by ter Braak (2006).
This sampler uses differential evolution updates with optional switching between two scaling factors (γ₁ and γ₂) to enable mode switching.
This implementation varies slightly from the original: updates within a population occur based on the previous positions to enable easy parallelization.
See doi.org/10.1007/s11222-006-8769-1 for more information.
The algorithm runs for a fixed number of iterations with optional burn-in.
Arguments
model_wrapper: LogDensityModel containing the target log-density functionn_its: Number of sampling iterations per chain
Keyword Arguments
rng: Random number generator. Defaults todefault_rng().n_burnin: Number of burn-in iterations. Defaults ton_its * 5.save_burnt: Save burn-in samples in output. Defaults tofalse.γ₁: Primary scaling factor. Defaults to2.38 / sqrt(2 * dim).γ₂: Secondary scaling factor for mode switching. Defaults to 1.0.p_γ₂: Probability of usingγ₂. Defaults to 0.1.chain_type: Type of chain to return (e.g.,Any,DifferentialEvolutionOutput,MCMCChains.Chains, orFlexiChains.VNChain). Defaults toDifferentialEvolutionOutput.save_final_state: Whether to return the final state along with samples, if true the output will be (samples::chaintype, finalstate). Defaults tofalse.
Generic DE Arguments
n_chains: Number of parallel chains. Defaults tomax(2 * dimension, 3)for adequate mixing.adapt: Whether to enable adaptive behavior during warm-up (if the sampler supports it). Defaults totrue.initial_position: Starting positions for chains. Can benothing(random initialization), or a vector of parameter vectors. If the provided vector is smaller thann_chains + n_hot_chains, it will be expanded; if larger andmemory=true, excess positions become initial memory. Defaults tonothing.parallel: Whether to evaluate initial log-densities in parallel. Useful for expensive models. Defaults tofalse.n_preallocated_indices: This package provides fast sampling-without-replacement by pre-allocating indices, defaults to 3 (which the most asked for by the implemented samplers). Consider increasing it if you implement your own proposal that callspick_chainswithn_chains > 3.silent: Suppress informational logging during initialization (e.g., initial position adjustments and memory setup) whentrue. Defaults tofalse.
Memory-based Sampling Arguments
memory: Whether to use memory-based sampling that stores past positions. Memory-based samplers can be more efficient for high-dimensional problems. Defaults tofalse.N₀: Initial memory size for memory-based samplers. Should be ≥n_chains + n_hot_chains. Defaults to2 * n_chains + n_hot_chains.update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Overwrites memory options given at initialization, generally should only be of use if callingstepdirectly.memory_refill: Whether to refill memory when full instead of extending the memory, will replace from the start. Defaults tofalse.memory_size: Maximum number of positions preallocated per chain in memory. The effective number stored positions ismemory_size * (n_chains + n_hot_chains). Defaults to1001or2*num_warmupif that is provided here or viasample. Ifmemory_refill = truethis is the maximum number stored before refilling, ifmemory_refill = falseonce the memory is full, the array is extended by anothermemory_sizeworth of positions. Set with consideration of available RAM and expected run length.memory_thin_interval: Thinning interval for memory updates. If > 0, only everymemory_thin_interval-th position is stored in memory.
Parallel Tempering and Simulated Annealing Arguments
n_hot_chains: Number of hot chains for parallel tempering. Defaults to 0 (no parallel tempering).max_temp_pt: Maximum temperature for parallel tempering. Defaults to 2*sqrt(dimension).max_temp_sa: Maximum temperature for simulated annealing. Defaults tomax_temp_pt.α: Temperature ladder spacing parameter. Controls the geometric spacing between temperatures. Defaults to 1.0.annealing: Whether to use simulated annealing (temperature decreases over time). Defaults tofalse.annealing_steps: Number of annealing steps. Defaults toannealing ? num_warmup : 0.temperature_ladder: Pre-defined temperature ladder as a vector of vectors. If provided, overrides automatic temperature ladder creation. Defaults tocreate_temperature_ladder(n_chains, n_hot_chains, α, max_temp_pt, max_temp_sa, annealing_steps).
kwargs...: Additional keyword arguments passed toAbstractMCMC.sample(e.g.,memory_refill,memory_thin_interval,silent). See AbstractMCMC documentation.
Returns
- depends on
chain_type, andsave_final_state
Example
using DifferentialEvolutionMetropolis, Random, Distributions
# Define a simple log-density function
model_wrapper(θ) = logpdf(MvNormal([0.0, 0.0], I), θ)
# Run differential evolution MCMC
result = deMC(model_wrapper, 1000; n_chains = 10, parallel = false)Notes
- For non-memory samplers,
n_chainsshould typically be ≥ dimension for good mixing - Memory-based samplers can work effectively with fewer chains than the problem dimension
- Initial log-densities are computed automatically for all starting positions
- When using parallel tempering (
n_hot_chains > 0), only the cold chains (firstn_chains) are returned in the sample, but all chains participate in the sampling process - Memory-based samplers with parallel tempering will issue warnings since hot chains typically aren't necessary when using memory
See also deMCzs, DREAMz, setup_de_update.
DifferentialEvolutionMetropolis.deMCzs — Function
deMCzs(model_wrapper, n_its; kwargs...)Run the Differential Evolution Markov Chain with snooker update and historic sampling (DE-MCzs) sampler.
It combines DE updates with optional snooker moves and uses memory-based sampling to efficiently handle high-dimensional problems with fewer chains.
Proposed by ter Braak and Vrugt (2008), see doi.org/10.1007/s11222-008-9104-9.
The algorithm runs for a fixed number of iterations with optional burn-in.
Arguments
model_wrapper: LogDensityModel containing the target log-density functionn_its: Number of sampling iterations per chain
Keyword Arguments
rng: Random number generator. Defaults todefault_rng().n_burnin: Number of burn-in iterations. Defaults ton_its * 5.γ: Scaling factor for DE updates. Defaults to2.38 / sqrt(2 * dim).γₛ: Scaling factor for snooker updates. Defaults to2.38 / sqrt(2).p_snooker: Probability of snooker moves. Defaults to 0.1.β: Noise distribution for DE updates. Defaults toUniform(-1e-4, 1e-4).chain_type: Type of chain to return (e.g.,Any,DifferentialEvolutionOutput,MCMCChains.Chains, orFlexiChains.VNChain). Defaults toDifferentialEvolutionOutput.save_final_state: Whether to return the final state along with samples, if true the output will be (samples::chaintype, finalstate). Defaults tofalse.
Generic DE Arguments
n_chains: Number of parallel chains. Defaults tomax(2 * dimension, 3)for adequate mixing.adapt: Whether to enable adaptive behavior during warm-up (if the sampler supports it). Defaults totrue.initial_position: Starting positions for chains. Can benothing(random initialization), or a vector of parameter vectors. If the provided vector is smaller thann_chains + n_hot_chains, it will be expanded; if larger andmemory=true, excess positions become initial memory. Defaults tonothing.parallel: Whether to evaluate initial log-densities in parallel. Useful for expensive models. Defaults tofalse.n_preallocated_indices: This package provides fast sampling-without-replacement by pre-allocating indices, defaults to 3 (which the most asked for by the implemented samplers). Consider increasing it if you implement your own proposal that callspick_chainswithn_chains > 3.silent: Suppress informational logging during initialization (e.g., initial position adjustments and memory setup) whentrue. Defaults tofalse.
Memory-based Sampling Arguments
memory: Whether to use memory-based sampling that stores past positions. Memory-based samplers can be more efficient for high-dimensional problems. Defaults totrue.N₀: Initial memory size for memory-based samplers. Should be ≥n_chains + n_hot_chains. Defaults to2 * n_chains + n_hot_chains.update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Overwrites memory options given at initialization, generally should only be of use if callingstepdirectly.memory_refill: Whether to refill memory when full instead of extending the memory, will replace from the start. Defaults tofalse.memory_size: Maximum number of positions preallocated per chain in memory. The effective number stored positions ismemory_size * (n_chains + n_hot_chains). Defaults to1001or2*num_warmupif that is provided here or viasample. Ifmemory_refill = truethis is the maximum number stored before refilling, ifmemory_refill = falseonce the memory is full, the array is extended by anothermemory_sizeworth of positions. Set with consideration of available RAM and expected run length.memory_thin_interval: Thinning interval for memory updates. If > 0, only everymemory_thin_interval-th position is stored in memory.
Parallel Tempering and Simulated Annealing Arguments
n_hot_chains: Number of hot chains for parallel tempering. Defaults to 0 (no parallel tempering).max_temp_pt: Maximum temperature for parallel tempering. Defaults to 2*sqrt(dimension).max_temp_sa: Maximum temperature for simulated annealing. Defaults tomax_temp_pt.α: Temperature ladder spacing parameter. Controls the geometric spacing between temperatures. Defaults to 1.0.annealing: Whether to use simulated annealing (temperature decreases over time). Defaults tofalse.annealing_steps: Number of annealing steps. Defaults toannealing ? num_warmup : 0.temperature_ladder: Pre-defined temperature ladder as a vector of vectors. If provided, overrides automatic temperature ladder creation. Defaults tocreate_temperature_ladder(n_chains, n_hot_chains, α, max_temp_pt, max_temp_sa, annealing_steps).
kwargs...: Additional keyword arguments passed toAbstractMCMC.sample(e.g.,memory_refill,memory_thin_interval,silent). See AbstractMCMC documentation.
Returns
- depends on
chain_type, andsave_final_state
Example
using DifferentialEvolutionMetropolis, Random, Distributions
# Define a simple log-density function
model_wrapper(θ) = logpdf(MvNormal([0.0, 0.0], I), θ)
# Run differential evolution MCMC
result = deMCzs(model_wrapper, 1000; n_chains = 3)Notes
- For non-memory samplers,
n_chainsshould typically be ≥ dimension for good mixing - Memory-based samplers can work effectively with fewer chains than the problem dimension
- Initial log-densities are computed automatically for all starting positions
- When using parallel tempering (
n_hot_chains > 0), only the cold chains (firstn_chains) are returned in the sample, but all chains participate in the sampling process - Memory-based samplers with parallel tempering will issue warnings since hot chains typically aren't necessary when using memory
DifferentialEvolutionMetropolis.DREAMz — Function
DREAMz(model_wrapper, n_its; kwargs...)Run the Differential Evolution Adaptive Metropolis (DREAMz) sampler.
This advanced adaptive sampler uses subspace sampling with adaptive crossover probabilities. It can switch between scaling factors and includes outlier chain detection/replacement. The algorithm adapts during warm-up and can use memory-based sampling for efficiency.
Based on Vrugt et al. (2009), see doi.org/10.1515/IJNSNS.2009.10.3.273.
The algorithm runs for a fixed number of iterations with optional burn-in.
Arguments
model_wrapper: LogDensityModel containing the target log-density functionn_its: Number of sampling iterations per chain
Keyword Arguments
rng: Random number generator. Defaults todefault_rng().n_burnin: Number of burn-in iterations. Defaults ton_its * 5.γ₁: Primary scaling factor for subspace updates. Defaults to adaptive.γ₂: Secondary scaling factor. Defaults to 1.0.p_γ₂: Probability of usingγ₂. Defaults to 0.2.n_cr: Number of crossover probabilities for adaptation. Defaults to 3.cr₁: Crossover probability forγ₁. Defaults to adaptive.cr₂: Crossover probability forγ₂. Defaults to adaptive.ϵ: Additive noise distribution. Defaults toUniform(-1e-4, 1e-4).e: Multiplicative noise distribution. Defaults toNormal(0.0, 1e-2).chain_type: Type of chain to return (e.g.,Any,DifferentialEvolutionOutput,MCMCChains.Chains, orFlexiChains.VNChain). Defaults toDifferentialEvolutionOutput.save_final_state: Whether to return the final state along with samples, if true the output will be (samples::chaintype, finalstate). Defaults tofalse.
Generic DE Arguments
n_chains: Number of parallel chains. Defaults tomax(2 * dimension, 3)for adequate mixing.adapt: Whether to enable adaptive behavior during warm-up (if the sampler supports it). Defaults totrue.initial_position: Starting positions for chains. Can benothing(random initialization), or a vector of parameter vectors. If the provided vector is smaller thann_chains + n_hot_chains, it will be expanded; if larger andmemory=true, excess positions become initial memory. Defaults tonothing.parallel: Whether to evaluate initial log-densities in parallel. Useful for expensive models. Defaults tofalse.n_preallocated_indices: This package provides fast sampling-without-replacement by pre-allocating indices, defaults to 3 (which the most asked for by the implemented samplers). Consider increasing it if you implement your own proposal that callspick_chainswithn_chains > 3.silent: Suppress informational logging during initialization (e.g., initial position adjustments and memory setup) whentrue. Defaults tofalse.
Memory-based Sampling Arguments
memory: Whether to use memory-based sampling that stores past positions. Memory-based samplers can be more efficient for high-dimensional problems. Defaults totrue.N₀: Initial memory size for memory-based samplers. Should be ≥n_chains + n_hot_chains. Defaults to2 * n_chains + n_hot_chains.update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Overwrites memory options given at initialization, generally should only be of use if callingstepdirectly.memory_refill: Whether to refill memory when full instead of extending the memory, will replace from the start. Defaults tofalse.memory_size: Maximum number of positions preallocated per chain in memory. The effective number stored positions ismemory_size * (n_chains + n_hot_chains). Defaults to1001or2*num_warmupif that is provided here or viasample. Ifmemory_refill = truethis is the maximum number stored before refilling, ifmemory_refill = falseonce the memory is full, the array is extended by anothermemory_sizeworth of positions. Set with consideration of available RAM and expected run length.memory_thin_interval: Thinning interval for memory updates. If > 0, only everymemory_thin_interval-th position is stored in memory.
Parallel Tempering and Simulated Annealing Arguments
n_hot_chains: Number of hot chains for parallel tempering. Defaults to 0 (no parallel tempering).max_temp_pt: Maximum temperature for parallel tempering. Defaults to 2*sqrt(dimension).max_temp_sa: Maximum temperature for simulated annealing. Defaults tomax_temp_pt.α: Temperature ladder spacing parameter. Controls the geometric spacing between temperatures. Defaults to 1.0.annealing: Whether to use simulated annealing (temperature decreases over time). Defaults tofalse.annealing_steps: Number of annealing steps. Defaults toannealing ? num_warmup : 0.temperature_ladder: Pre-defined temperature ladder as a vector of vectors. If provided, overrides automatic temperature ladder creation. Defaults tocreate_temperature_ladder(n_chains, n_hot_chains, α, max_temp_pt, max_temp_sa, annealing_steps).
kwargs...: Additional keyword arguments passed toAbstractMCMC.sample(e.g.,memory_refill,memory_thin_interval,silent). See AbstractMCMC documentation.
Returns
- depends on
chain_type, andsave_final_state
Example
using DifferentialEvolutionMetropolis, Random, Distributions
# Define a simple log-density function
model_wrapper(θ) = logpdf(MvNormal([0.0, 0.0], I), θ)
# Run DREAM with subspace sampling
result = DREAMz(model_wrapper, 1000; n_chains = 10, memory = false)Notes
- For non-memory samplers,
n_chainsshould typically be ≥ dimension for good mixing - Memory-based samplers can work effectively with fewer chains than the problem dimension
- Initial log-densities are computed automatically for all starting positions
- When using parallel tempering (
n_hot_chains > 0), only the cold chains (firstn_chains) are returned in the sample, but all chains participate in the sampling process - Memory-based samplers with parallel tempering will issue warnings since hot chains typically aren't necessary when using memory
See also deMC, deMCzs, setup_subspace_sampling.
Setup Functions
DifferentialEvolutionMetropolis.setup_sampler_scheme — Function
Create a composite sampler scheme from multiple differential evolution update steps.
The update method used in each iteration for each chain is randomly selected from the provided update steps according to their weights. This allows combining different sampling strategies (e.g., DE updates with snooker updates) in a single sampler.
Arguments
updates...: One or more differential evolution sampler objects created by functions likesetup_de_update,setup_snooker_update,setup_subspace_sampling.
Keyword Arguments
w: Vector of weights for each update step. If not provided, all updates are chosen with equal probability. Weights must be non-negative and will be automatically normalized.
Returns
- A
DifferentialEvolutionCompositeSamplerthat can be used withAbstractMCMC.sample.
Examples
using DifferentialEvolutionMetropolis
# Only snooker updates
sampler1 = setup_sampler_scheme(setup_snooker_update())
# DE and Snooker with equal probability
sampler2 = setup_sampler_scheme(setup_de_update(), setup_snooker_update())
# Snooker 10% of the time, DE 90% of the time
sampler3 = setup_sampler_scheme(setup_de_update(), setup_snooker_update(); w = [0.9, 0.1])See also setup_de_update, setup_snooker_update, setup_subspace_sampling.
DifferentialEvolutionMetropolis.setup_de_update — Function
Set up a Differential Evolution (DE) update step for MCMC sampling.
Creates a sampler that proposes new states by adding scaled difference vectors between randomly selected chains plus small noise. This is the core update mechanism from the original DE-MC algorithm by ter Braak (2006).
See doi.org/10.1007/s11222-006-8769-1 for more information.
Keyword Arguments
γ: Scaling factor for the difference vector. Can be aReal(fixed value), aUnivariateDistribution(random scaling), ornothing(automatic based onn_dims). Defaults tonothing.β: Distribution for small noise added to proposals. Must be a univariate continuous distribution. Defaults toUniform(-1e-4, 1e-4).n_dims: Problem dimension used for automaticγselection. If > 0 andγisnothing, setsγto the theoretically optimal2.38 / sqrt(2 * n_dims). If ≤ 0, usesUniform(0.8, 1.2). Defaults to 0.check_args: Whether to validate input distributions. Defaults totrue.
Returns
- A
DifferentialEvolutionSamplerthat can be used withsetup_sampler_schemeorsteporsamplefrom AbstractMCMC.
Example
using DifferentialEvolutionMetropolis, Distributions
# Setup differential evolution update with custom parameters
de_update = setup_de_update(γ = 1.0, β = Normal(0.0, 0.01))See also setup_snooker_update, setup_subspace_sampling, setup_sampler_scheme.
DifferentialEvolutionMetropolis.setup_snooker_update — Function
Set up a Snooker update step for MCMC sampling.
Creates a sampler that proposes moves along the line connecting the current position to a projection point, scaled by the difference between two other randomly selected chains. This update can help with sampling from distributions with complex geometries by making larger moves in effective directions.
See doi.org/10.1007/s11222-008-9104-9 for more information.
Keyword Arguments
γ: Scaling factor for the projection. Can be aReal(fixed value), aUnivariateDistribution(random scaling), ornothing(automatic based ondeterministic_γ). Defaults tonothing.deterministic_γ: Whenγisnothing, determines the automatic value. Iftrue, uses the theoretically optimal2.38 / sqrt(2). Iffalse, usesUniform(0.8, 1.2). Defaults totrue.check_args: Whether to validate input distributions. Defaults totrue.
Returns
- A
DifferentialEvolutionSnookerSamplerthat can be used withsetup_sampler_schemeorsteporsamplefrom AbstractMCMC.
Example
using DifferentialEvolutionMetropolis, Distributions
# Setup snooker update with custom gamma distribution
snooker_update = setup_snooker_update(γ = Uniform(0.1, 2.0))See also setup_de_update, setup_subspace_sampling, setup_sampler_scheme.
DifferentialEvolutionMetropolis.setup_subspace_sampling — Function
Set up a Subspace Sampling (DREAM-like) update step for MCMC sampling.
Creates a sampler that updates only a random subset of parameters in each iteration, using multiple scaled difference vectors. The crossover probability determines which parameters to update and can be adapted during warm-up for improved efficiency.
See doi.org/10.1515/IJNSNS.2009.10.3.273 for more information.
Keyword Arguments
γ: Scaling factor for the difference vector sum. Ifnothing(default), uses the adaptive formula2.38 / sqrt(2 * δ * d)wheredis the number of updated dimensions. If aRealis provided, uses that fixed value throughout sampling.cr: Crossover probability for parameter selection. Can be aReal(fixed probability),nothing(adaptive usingn_crvalues), or aUnivariateDistribution. If cr is aDiscreteNonParametricthen it those values can also be adapted. Defaults tonothing.n_cr: Number of crossover probabilities to adapt between whencrisnothing. Higher values allow more fine-tuned adaptation. Defaults to 3.δ: Number of difference vectors to sum. Can be anInteger(fixed) or aDiscreteUnivariateDistribution(random). Defaults toDiscreteUniform(1, 3).ϵ: Distribution for small additive noise in the selected subspace. Defaults toUniform(-1e-4, 1e-4).e: Distribution for multiplicative noise(1 + e)applied to the difference vector sum. Defaults toNormal(0.0, 1e-2).check_args: Whether to validate input distributions. Defaults totrue.
Returns
- A subspace sampler that can be used with
setup_sampler_schemeorsteporsamplefrom AbstractMCMC.
Example
using DifferentialEvolutionMetropolis, Distributions
# Setup subspace sampling with custom crossover rate and delta
subspace_config = setup_subspace_sampling(cr = Beta(1, 2), δ = 2)See also setup_de_update, setup_snooker_update, setup_sampler_scheme.
Core Sampling Functions
AbstractMCMC.step — Function
step(rng, model_wrapper, sampler, state; parallel=false, update_memory=true, kwargs...)Perform a single MCMC step using differential evolution sampling.
This is the core sampling function that proposes new states for all chains and accepts or rejects them according to the Metropolis criterion. For adaptive samplers, the function automatically fixes adaptive parameters before sampling.
Arguments
rng: Random number generatormodel_wrapper: LogDensityModel containing the target log-density functionsampler: Differential evolution sampler (any AbstractDifferentialEvolutionSampler)state: Current state of all chains
Keyword Arguments
parallel: Whether to run chains in parallel using threading. Defaults tofalse. Advisable for slow models.update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Over writes memory options given at initialization.kwargs...: Additional keyword arguments passed to update functions (see https://turinglang.org/AbstractMCMC.jl/stable/api/#Common-keyword-arguments)
Returns
sample: DifferentialEvolutionSample containing new positions and log-densitiesnew_state: Updated state for the next iteration
Example
sample, new_state = step(rng, model, sampler, state; parallel=true)See also step_warmup, sample from AbstractMCMC.
step(rng, model_wrapper, sampler; n_chains, memory=true, N₀, adapt=true, initial_position=nothing, parallel=false, kwargs...)Initialize differential evolution sampling by setting up chains and computing initial state.
This function serves as the entry point for differential evolution MCMC sampling. It handles chain initialization, memory setup for memory-based samplers, adaptive state initialization, and returns the initial sample and state that can be used with AbstractMCMC.sample.
Arguments
rng: Random number generatormodel_wrapper: LogDensityModel containing the target log-density functionsampler: Differential evolution sampler to use
Keyword Arguments
Generic DE Arguments
n_chains: Number of parallel chains. Defaults tomax(2 * dimension, 3)for adequate mixing.adapt: Whether to enable adaptive behavior during warm-up (if the sampler supports it). Defaults totrue.initial_position: Starting positions for chains. Can benothing(random initialization), or a vector of parameter vectors. If the provided vector is smaller thann_chains + n_hot_chains, it will be expanded; if larger andmemory=true, excess positions become initial memory. Defaults tonothing.parallel: Whether to evaluate initial log-densities in parallel. Useful for expensive models. Defaults tofalse.n_preallocated_indices: This package provides fast sampling-without-replacement by pre-allocating indices, defaults to 3 (which the most asked for by the implemented samplers). Consider increasing it if you implement your own proposal that callspick_chainswithn_chains > 3.silent: Suppress informational logging during initialization (e.g., initial position adjustments and memory setup) whentrue. Defaults tofalse.
Memory-based Sampling Arguments
memory: Whether to use memory-based sampling that stores past positions. Memory-based samplers can be more efficient for high-dimensional problems. Defaults totrue.N₀: Initial memory size for memory-based samplers. Should be ≥n_chains + n_hot_chains. Defaults to2 * n_chains + n_hot_chains.update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Overwrites memory options given at initialization, generally should only be of use if callingstepdirectly.memory_refill: Whether to refill memory when full instead of extending the memory, will replace from the start. Defaults tofalse.memory_size: Maximum number of positions preallocated per chain in memory. The effective number stored positions ismemory_size * (n_chains + n_hot_chains). Defaults to1001or2*num_warmupif that is provided here or viasample. Ifmemory_refill = truethis is the maximum number stored before refilling, ifmemory_refill = falseonce the memory is full, the array is extended by anothermemory_sizeworth of positions. Set with consideration of available RAM and expected run length.memory_thin_interval: Thinning interval for memory updates. If > 0, only everymemory_thin_interval-th position is stored in memory.
Parallel Tempering and Simulated Annealing Arguments
n_hot_chains: Number of hot chains for parallel tempering. Defaults to 0 (no parallel tempering).max_temp_pt: Maximum temperature for parallel tempering. Defaults to 2*sqrt(dimension).max_temp_sa: Maximum temperature for simulated annealing. Defaults tomax_temp_pt.α: Temperature ladder spacing parameter. Controls the geometric spacing between temperatures. Defaults to 1.0.annealing: Whether to use simulated annealing (temperature decreases over time). Defaults tofalse.annealing_steps: Number of annealing steps. Defaults toannealing ? num_warmup : 0.temperature_ladder: Pre-defined temperature ladder as a vector of vectors. If provided, overrides automatic temperature ladder creation. Defaults tocreate_temperature_ladder(n_chains, n_hot_chains, α, max_temp_pt, max_temp_sa, annealing_steps).
Returns
sample: DifferentialEvolutionSample containing initial positions and log-densitiesstate: Initial state (DifferentialEvolutionState) ready for sampling
Examples
using DifferentialEvolutionMetropolis, Random, Distributions
# Setup
rng = Random.default_rng()
model_wrapper(θ) = logpdf(MvNormal([0.0, 0.0], I), θ)
sampler = deMCzs()
# Basic initialization with default settings
sample, state = step(rng, model_wrapper, sampler)
# Custom number of chains with memory disabled
sample2, state2 = step(rng, model_wrapper, sampler; n_chains=10, memory=false)
# With custom initial positions
init_pos = [randn(2) for _ in 1:8]
sample3, state3 = step(rng, model_wrapper, sampler; initial_position=init_pos)Notes
- For non-memory samplers,
n_chainsshould typically be ≥ dimension for good mixing - Memory-based samplers can work effectively with fewer chains than the problem dimension
- Initial log-densities are computed automatically for all starting positions
- When using parallel tempering (
n_hot_chains > 0), only the cold chains (firstn_chains) are returned in the sample, but all chains participate in the sampling process - Memory-based samplers with parallel tempering will issue warnings since hot chains typically aren't necessary when using memory
See also sample from AbstractMCMC, deMC, deMCzs, DREAMz.
AbstractMCMC.step_warmup — Function
step_warmup(rng, model_wrapper, sampler, state; parallel=false, kwargs...)Perform a single MCMC step during the warm-up (adaptive) phase.
During warm-up, this function performs the same sampling as step but also updates adaptive parameters. For subspace samplers, it adapts crossover probabilities based on the effectiveness of different parameter subsets.
Arguments
rng: Random number generatormodel_wrapper: LogDensityModel containing the target log-density functionsampler: Adaptive differential evolution samplerstate: Current state including adaptive parameters
Keyword Arguments
update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Useful if memory has grown too large.parallel: Whether to run chains in parallel using threading. Defaults tofalse.kwargs...: Additional keyword arguments passed to update functions
Returns
sample: DifferentialEvolutionSample containing new positions and log-densitiesnew_state: Updated state with adapted parameters for the next iteration
Example
using DifferentialEvolutionMetropolis, Random, Distributions
# Setup for warmup step example
rng = Random.default_rng()
model_wrapper(θ) = logpdf(MvNormal([0.0, 0.0], I), θ)
sampler = DREAMz()
# Initialize state (this would typically be done by AbstractMCMC.sample)
# sample, new_state = step_warmup(rng, model_wrapper, sampler, state; parallel=false)See also step, fix_sampler.
step_warmup(rng, model_wrapper, sampler, state; kwargs...)Perform a single MCMC step during warm-up for composite samplers.
For composite samplers, this function randomly selects one of the component update methods, performs a warm-up step with that method, and updates the corresponding adaptive state while preserving other component states.
Arguments
rng: Random number generatormodel_wrapper: LogDensityModel containing the target log-density functionsampler: Composite differential evolution samplerstate: Current state with composite adaptive parameters
Keyword Arguments
update_memory: Whether to update the memory with new positions (for memory-based samplers). Defaults totrue. Useful if memory has grown too large.kwargs...: Additional keyword arguments passed to component update functions
Returns
sample: DifferentialEvolutionSample containing new positions and log-densitiesnew_state: Updated state with adapted parameters for the selected component
See also step_warmup, setup_sampler_scheme.
DifferentialEvolutionMetropolis.fix_sampler — Function
fix_sampler(sampler::AbstractDifferentialEvolutionSampler, adaptive_state::AbstractDifferentialEvolutionAdaptiveState)Fix adaptive parameters of a sampler to their current adapted values.
For non-adaptive samplers, returns the sampler unchanged. For adaptive samplers, returns a new sampler with the adaptive parameters fixed to their current values in the adaptive_state.
Arguments
sampler: The differential evolution sampler to fixadaptive_state: The adaptive state containing current parameter values
Returns
- A sampler with fixed (non-adaptive) parameters
Example
using DifferentialEvolutionMetropolis, Random, Distributions
# This function is typically used after warmup/adaptation phase
# fixed_sampler = fix_sampler(adaptive_sampler, state.adaptive_state)See also fix_sampler_state.
DifferentialEvolutionMetropolis.fix_sampler_state — Function
fix_sampler_state(sampler::AbstractDifferentialEvolutionSampler, state::DifferentialEvolutionState)Fix adaptive sampler parameters and return a corresponding non-adaptive state.
Takes an adaptive sampler and state, fixes the sampler's adaptive parameters to their current values, and returns both the fixed sampler and a simplified state without adaptive components.
Arguments
sampler: The differential evolution sampler (potentially adaptive)state: The current sampler state (DifferentialEvolutionState)
Returns
fixed_sampler: Sampler with adaptive parameters fixed to current valuesfixed_state: State without adaptive components
Example
using DifferentialEvolutionMetropolis, Random, Distributions
# This function is typically used after warmup/adaptation phase
# fixed_sampler, fixed_state = fix_sampler_state(sampler, state)See also fix_sampler.
Convergence and Stopping Criteria
DifferentialEvolutionMetropolis.r̂_stopping_criteria — Function
r̂_stopping_criteria(rng, model, sampler, samples, state, iteration; kwargs...)Stopping criterion based on the Gelman-Rubin diagnostic (R̂).
Sampling continues until the R̂ value for all parameters falls below maximum_R̂, indicating convergence across chains. This function is designed to be used as the N_or_isdone argument in AbstractMCMC.sample for adaptive stopping.
The diagnostic is computed on the last half of the collected samples to focus on the stationary portion of the chains.
Arguments
rng: Random number generator (unused but required by AbstractMCMC interface)model: The model being sampled (unused but required by interface)sampler: The differential evolution sampler (unused but required by interface)samples: Vector of collected samples from all chainsstate: Current sampler state (unused but required by interface)iteration: Current iteration number
Keyword Arguments
check_every: Frequency (in iterations) for checking R̂ values. Defaults to 1000.maximum_R̂: Maximum acceptable R̂ value for convergence. Defaults to 1.2.maximum_iterations: Maximum number of iterations before forced stopping. Defaults to 100000.minimum_iterations: Minimum iterations before convergence checking begins. Defaults to 0.
Returns
trueif sampling should stop (converged or maximum iterations reached)falseif sampling should continue
Example
using DifferentialEvolutionMetropolis, AbstractMCMC, Random, Distributions
# Create a simple model
model_wrapper(θ) = logpdf(MvNormal([0.0, 0.0], I), θ)
# Setup sampler
sampler = deMCzs()
# Use with adaptive stopping criterion
rng = Random.default_rng()
chains = sample(rng, model_wrapper, sampler, r̂_stopping_criteria;
n_chains=4, check_every=500, maximum_R̂=1.1)See also MCMCDiagnosticTools.rhat, deMCzs, DREAMz.
Output
The output format can be modified with chain_type, the supported options are Chains from MCMCChains, VNChain from FlexiChains, Any which returns the basic DifferentialEvolutionMetropolis.DifferentialEvolutionSample, and the default option DifferentialEvolutionOutput. If save_final_state = true the format will be (sample::requested format, final_state). If run in parallel using step(model, sampler, parallel_option, n_its, n_meta_chains; n_chains = n_chains) the meta chains and DE chains will be merged into one dimension for both Chains and DifferentialEvolutionOutput, if the final state is saved it will be a vector of length n_meta_chains containing the final state for each.
Note that support for FlexiChains is a bit underutilized as the all samplers currently require all of your parameters to have one type.
DifferentialEvolutionMetropolis.DifferentialEvolutionOutput — Type
DifferentialEvolutionOutput{T <: Real}Container for differential evolution MCMC sampling results.
Fields
samples::Array{T, 3}: Three-dimensional array of parameter samples with dimensions (iterations, chains, parameters). Each sample represents a point in parameter space from the MCMC chain.ld::Matrix{T}: Two-dimensional matrix of log-density values with dimensions (iterations, chains). Contains the log-probability density evaluated at each corresponding sample point.
Type Parameters
T <: Real: Numeric type for the samples and log-density values (typicallyFloat64).
Examples
# Access samples from the output
output = sample(model, sampler, n_samples)
parameter_samples = output.samples # Shape: (n_samples, n_chains, n_params)
log_densities = output.ld # Shape: (n_samples, n_chains)
# Extract samples for a specific chain
chain_1_samples = output.samples[:, 1, :] # All samples from chain 1Index
DifferentialEvolutionMetropolis.DifferentialEvolutionOutputAbstractMCMC.stepAbstractMCMC.step_warmupDifferentialEvolutionMetropolis.DREAMzDifferentialEvolutionMetropolis.deMCDifferentialEvolutionMetropolis.deMCzsDifferentialEvolutionMetropolis.fix_samplerDifferentialEvolutionMetropolis.fix_sampler_stateDifferentialEvolutionMetropolis.r̂_stopping_criteriaDifferentialEvolutionMetropolis.setup_de_updateDifferentialEvolutionMetropolis.setup_sampler_schemeDifferentialEvolutionMetropolis.setup_snooker_updateDifferentialEvolutionMetropolis.setup_subspace_sampling