DifferentialEvolutionMCMC.DE
— Typefunction DE(;
n_groups = 4,
priors = nothing,
Np,
burnin = 1000,
discard_burnin = true,
α = .1,
β = .1,
ϵ = .001,
σ = .05,
κ = 1.0,
θsnooker = 0.0,
bounds,
n_initial = 0,
generate_proposal = random_gamma,
update_particle! = mh_update!,
evaluate_fitness! = compute_posterior!,
sample = sample,
blocking_on = x -> false,
blocks = [false])
Differential Evolution MCMC object.
Keywords
n_groups=4
: number of groups of particles.Np
: number of particles per group.burnin=1000
: number of burnin iterationsdiscard_burnin
: indicates whether burnin samples are discarded. Default is true.α=.1
: migration probability.β=.1
: mutation probability.ϵ=.001
: noise in crossover step.σ=.05
: standard deviation of noise added to parameters for mutation.κ=1.0
: recombination with probability (1-κ) during crossover.θsnooker=0
: sample along line x_i - z. 0.1 is recommended if > 0.n_initial
: initial number of samples from the prior distribution whensample=resample
. 10 times the number of parameters is a typical valuebounds
: a vector of tuples for lower and upper bounds of each parameteriter
: current iterationgenerate_proposal
: a function that generates proposals. Default is the two mode proposal described in Turner et al. 2012. You can also choosefixed_gamma
,variable_gamma
(see help) or pass a custom functionupdate_particle!
: a function for updating the particle with a proposal value. Default:mh_update!
, which uses the Metropolis-Hastings rule.evaluate_fitness!
: a function for evaluating the fitness of a posterior. The default is to compute the posterior loglikelihood withcompute_posterior!
. Selectevaluate_fun!
for optimization rather than MCMC sampling.sample
: a function for sampling particles during the crossover step. The defaultsample
uses current particle parameter values whereasresample
samples from the history of accepted values for each particle. Np must 3 or greater when usingresample
.blocking_on = x -> false
: a function that indicates whether block updating is used on each iteration. The function requires optimization_tests arguement for the DE sampler object and must return a true or false value.blocks
: a vector of boolean vectors indicating which parameters to update. Each sub-vector represents a block and each element in the sub-vector indicates which parameters are updated within the block. For example, [[true,false],[false,true]] indicates that the parameter in the first position is updated on the first block and the parameter in the second position is updated on the second block. If a parameter is a vector or matrix, they are nested within the block sub-vector.
References
Ter Braak, C. J. A Markov Chain Monte Carlo version of the genetic algorithm Differential Evolution: easy Bayesian computing for real parameter spaces.
Ter Braak, Cajo JF, and Jasper A. Vrugt. Differential evolution Markov chain with snooker updater and fewer chains. Statistics and Computing 18.4 (2008): 435-446
Turner, B. M., Sederberg, P. B., Brown, S. D., & Steyvers, M. (2013). A method for efficiently sampling from distributions with correlated dimensions. Psychological methods, 18(3), 368.
Turner, B. M., & Sederberg, P. B. (2012). Approximate Bayesian computation with differential evolution. Journal of Mathematical Psychology, 56(5), 375-385.
DifferentialEvolutionMCMC.DEModel
— TypeDEModel{F,L,T,S} <: AbstractModel where {F <: Function,L,T,S}
A model object containing the log likelihood function and prior distributions.
Fields
prior_loglike::L
: log likelihood of posterior sample. A function must be defined forsample
, but not foroptimize
.loglike::F
: a log likelihood function for Bayesian parameter estimation or an objective function for optimization.sample_prior::S
: a function for initial values. Typically, a prior distribution is ideal.names::T
: parameter names
Constructor
function DEModel(
args...;
prior_loglike = nothing,
loglike,
names,
sample_prior,
data,
kwargs...)
Arguments
args...
: optional positional arguments forloglike
Keywords
prior_loglike=nothing
: log likelihood of posterior sample. A function must be defined forsample
, but not foroptimize
.loglike
: a log likelihood function for Bayesian parameter estimation or an objective function for optimization.sample_prior
: a function for initial values. Typically, a prior distribution is ideal.names
: parameter nameskwargs...
: optional keyword arguments forloglike
DifferentialEvolutionMCMC.Particle
— TypeParticle{T}
Fields
Θ::Vector{T}
: a vector of parametersaccept::Vector{Bool}
: proposal acceptance. 1: accept, 0: rejectweight::Float64
: particle weight based on model fit (currently posterior log likelihood)lp::Vector{Float64}
: a vector of log posterior probabilities associated with each accepted proposalid::Int
: particle id
AbstractMCMC.bundle_samples
— Methodbundle_samples(model::DEModel, de::DE, groups, n_iter)
Converts group particles to a chain object capable of generating convergence diagnostics and posterior summaries.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroups
: a vector of groups of particlesn_iter
: number of iterations
DifferentialEvolutionMCMC.accept
— Functionaccept(proposal, current, log_adj=0.0)
Metropolis-Hastings proposal selection Note: assumes weights are posterior log likelihoods
Arguments
proposal
: weight of proposal e.g. posterior log likelihoodcurrent
: weight of current value e.g. posterior log likelihoodadj
: an adjustment term for the snooker update
DifferentialEvolutionMCMC.adjust_loglike
— Methodadjust_loglike(Pt, proposal, Pz)
The adjusted log likelihood component for a snooker update.
Pt
: the target particle from iteration n - 1proposal
: the proposal particle on iteration nPz
: the particle formed by the projection of particles m and n
DifferentialEvolutionMCMC.compute_posterior!
— Methodcompute_posterior!(de, model, proposal)
Computes posterior log likelihood of proposal particle. The value -Inf is returned if the proposal is out of bounds.
Arguments
de
: differential evolution objectmodel
: model containing a likelihood function with data and priorsproposal
: the proposed particle
DifferentialEvolutionMCMC.crossover!
— Methodcrossover!(model, de, group)
Performs crossover step for each particle pt in the chain
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroup
: a group of particlesblock
: a vector of boolean values indicating which parameters to update
DifferentialEvolutionMCMC.crossover!
— Methodcrossover!(model, de, group, pt::Particle, block)
Performs crossover step for a given particle pt.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroup
: a group of particlesblock
: a vector of boolean values indicating which parameters to update
DifferentialEvolutionMCMC.crossover!
— Methodcrossover!(model, de, group, pt::Particle)
Performs crossover step for for a given particle pt
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectpt
: a target particle to be updated
DifferentialEvolutionMCMC.crossover!
— Methodcrossover!(model, de, group)
Performs crossover step for each particle pt in the chain
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroup
: a group of particles
DifferentialEvolutionMCMC.evaluate_fun!
— Methodevaluate_fun!(de, model, proposal))
Evaluates the fitness of an arbitrary function called loglike
. This is used for point estimation as it does not use a prior distribution.
Arguments
de
: differential evolution objectmodel
: model containing a likelihood function with data and priorsproposal
: the proposed particle
DifferentialEvolutionMCMC.fixed_gamma
— Methodfixed_gamma(de, Pt, group)
Generate proposal according to θ' = θt + γ(θm − θn) + b where γ = 2.38.
Arguments
de
: differential evolution objectPt
: current particlegroup
: a group of particles
DifferentialEvolutionMCMC.get_names
— Methodget_names(model, p)
Returns parameters names.
Arguments
p
: a particlemodel
: model containing a likelihood function with data and priors
DifferentialEvolutionMCMC.in_bounds
— Methodin_bounds(b, θ::Real)
Checks whether parameter is within lower and upper bounds.
Arguments
b
: boundary (lowerbound,upperbound)θ
: a parameter value
DifferentialEvolutionMCMC.init_particle
— Methodinit_particle!(model, de, p, n_iter)
Initializes values for a particle.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectp
: a particlen_iter
: the number of iterations
DifferentialEvolutionMCMC.mh_update!
— Functionmh_update!(de, current, proposal, log_adj=0.0)
Update particle based on Metropolis-Hastings rule.
Arguments
de
: differential evolution objectcurrent
: current particleproposal
: proposal particlelog_adj
: an adjustment term for snooker update
DifferentialEvolutionMCMC.migration!
— Methodmigration!(de, groups)
Select a subset of groups and swap one poorly particle from each selected groups.
Arguments
de
: differential evolution objectgroups
: groups of particles
DifferentialEvolutionMCMC.mutate_or_crossover!
— Methodmutate_or_crossover!(model, de, group, seed::Number)
Selects between mutation and crossover step with probability β.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroup
: a vector of interacting particles (e.g. chains)seed::Number
: RNG seed
DifferentialEvolutionMCMC.mutation!
— Methodmutation!(model, de, group)
Mutates each particle by adding random noise y ~ normal(0, σ), where σ is a small number.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroup
: a group of particles
DifferentialEvolutionMCMC.optimize
— Methodoptimize(model::DEModel, de::DE, n_iter::Int; progress=false, kwargs...)
Finds optimal set of parameters.
Arguments
model
: a model containing likelihood function with data and priorsde
: differential evolution objectn_iter
: number of iterations or samples
Keywords
progress=false
: show progress of algorithmkwargs...
: optional keyword arguments
DifferentialEvolutionMCMC.optimize
— Methodoptimize(model::DEModel, de::DE, ::MCMCThreads, n_iter::Int; progress=false, kwargs...)
Finds optimal set of parameters.
Arguments
model
: a model containing likelihood function with data and priorsde
: differential evolution objectMCMCThreads
: pass MCMCThreads() object to run on multiple threadsn_iter
: number of iterations or samples
Keywords
progress=false
: show progress of algorithmkwargs...
: optional keyword arguments
DifferentialEvolutionMCMC.project
— Methodproject(p1::Particle, p2::Particle)
Returns the projection of particle p1
onto particle p2
. This method works for vectors and nested arrays.
Arguments
p1::Particle
: base particlep2::Particle
: particle projected ontop1
DifferentialEvolutionMCMC.pstep!
— Methodpstep!(model::DEModel, de::DE, groups)
Perform a single step for DE-MCMC with each particle group on a separate thread.
Arguments
model
: model containing a likelihood function with data and priorsde
: DE-MCMC sampler objectgroups
: Array of vectors of particles
DifferentialEvolutionMCMC.random_gamma
— Methodrandom_gamma(de, Pt, group)
Generate proposal according to θ' = θt + γ1(θm − θn) + γ2(θb − θt) + b. γ2=0 after burnin
Arguments
de
: differential evolution objectPt
: current particlegroup
: a group of particles
DifferentialEvolutionMCMC.recombination!
— Methodrecombination!(de, pt::Particle, pp::Particle)
Resets parameters of proposal to previous value with probability (1-κ).
de
: differential evolution objectpt
: current particalpp
: proposal particle
DifferentialEvolutionMCMC.resample
— Methodresample(de, group, n, replace)
Sample a random particle from previously accepted values for snooker update.
Arguments
de
: differential evolution objectgroup
: a group of particlesn
: number of particles to samplereplace
: sample with replacement if true
DifferentialEvolutionMCMC.reset!
— Methodreset!(p1::Particle, p2::Particle, idx)
During block updates, all parameters are updated. This function resets parameters in the proposal p1
with values from the previous particle.
Arguments
p1
: proposal particle which will have values reset p2
: previous particle idx
: boolean vector indicating which values are updated in block. False values are reset.
DifferentialEvolutionMCMC.sample_init
— Methodsample_init(model::DEModel, de::DE, n_iter)
Creates vectors of particles and samples initial parameter values from priors.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectn_iter
: number of iterations
DifferentialEvolutionMCMC.select_base
— Methodselect_base(group)
Selects base particle θb with probability proportional to weight.
group
: a group of particles
DifferentialEvolutionMCMC.select_groups
— Methodselect_groups(de, groups)
Select a subset of groups for migration and return their indices.
Arguments
de
: differential evolution objectgroups
: groups of particles
DifferentialEvolutionMCMC.select_particle
— Methodselect_particle(group)
Select particle from a single chain inversely proportional to its weight.
Arguments
group
: a group of particles
DifferentialEvolutionMCMC.select_particles
— Methodselect_particles(sub_group)
Select particles from groups for migration. Returns particle index and particles.
Arguments
sub_group
: vector of particles
DifferentialEvolutionMCMC.shift_particles!
— Methodshift_particles!(sub_group, p_idx, particles)
Swap the particles so that p1->pn, p2 -> p1,..., pn -> pn-1 where pi is the particle belonging to the ith group.
Arguments
sub_group
: group of particlesp_idx
: particle indexparticles
: particle objects representing position in parameter space
DifferentialEvolutionMCMC.snooker_update!
— Methodsnooker_update!(de, Pt, group)
Performs snooker update during crossover
Arguments
de
: differential evolution objectPt
: current particlegroup
: a group of particles
DifferentialEvolutionMCMC.step!
— Methodstep!(model::DEModel, de::DE, groups)
Perform a single step for DE-MCMC.
Arguments
model
: model containing a likelihood function with data and priorsde
: DE-MCMC sampler objectgroups
: Array of vectors of particles
DifferentialEvolutionMCMC.store_samples!
— Methodstore_samples!(de, groups)
Store samples after burnin period.
Arguments
de
: differential evolution objectgroups
: groups of particles
DifferentialEvolutionMCMC.update!
— Methodupdate!(model, de, groups)(model, de, groups)
Particles are updated by block or simultaneously on a single thread.
Arguments
model
: model containing a likelihood function with data and priorsde
: differential evolution objectgroup
: a vector of interacting particles (e.g. chains)
DifferentialEvolutionMCMC.variable_gamma
— Methodvariable_gamma(de, Pt, group)
Generate proposal according to θ' = θt + γ(θm − θn) + b where γ = 2.38/√(2d) where d is the number of parameters
Arguments
de
: differential evolution objectPt
: current particlegroup
: a group of particles
StatsBase.sample
— Methodsample(de::DE, group_diff, n, replace)
Sample a random particle.
Arguments
de
: differential evolution objectgroup
: a group of particlesn
: number of particles to samplereplace
: sample with replacement if true
StatsBase.sample
— Methodsample(model::DEModel, de::DE, n_iter::Int; progress=false, kwargs...)
Samples from the posterior distribution.
Arguments
model
: a model containing likelihood function with data and priorsde
: differential evolution objectn_iter
: number of iterations or samples
Keywords
progress=false
: show progress of samplerkwargs...
: optional keyword arguments
StatsBase.sample
— Methodsample(model::DEModel, de::DE, ::MCMCThreads, n_iter::Int; progress=false, kwargs...)
Samples from the posterior distribution with each group of particles on a seperarate thread for the mutation and crossover steps.
Arguments
model
: a model containing likelihood function with data and priorsde
: differential evolution objectMCMCThreads
: pass MCMCThreads() object to run on multiple threadsn_iter
: number of iterations or samples
Keywords
progress=false
: show progress of samplerkwargs...
: optional keyword arguments