Callbacks
Callbacks are instructions that allow for certain interactions with the state of the sampler while the sampler is still working.
Each concrete implementation of a callback inherits from
ExtensibleMCMC.Callback
— TypeSupertype of all callbacks. They specify additional actions that can be performed before or after each MCMC step or at the very end of sampling.
This package implements two types of callbacks:
SavingCallback
—for saving intermediate states of the sampler to CSV filesREPLCallback
—for printing progress messages to REPL
See a companion package ExtensibleMCMCPlots.jl where we additionally implemented a PlottingCallback
that does online diagnostic plots that are automatically updated as the chain is being updated.
To pass a list of Callback
s to an mcmc sampler pass them in a list to a run!
function.
Callback for printing progress messages to REPL
ExtensibleMCMC.REPLCallback
— Typestruct REPLCallback <: Callback
print_every_k_iter::Int64
show_all_updates::Bool
basic_info_only::Bool
end
Struct with instructions for printing progress messages to REPL.
REPLCallback(;
print_every_k_iter=100,
show_all_upates=true,
basic_info_only=true,
)
Base constructor that uses named arguments.
Callback for saving intermediate results to CSV files
ExtensibleMCMC.SavingCallback
— Typestruct SavingCallback <: Callback
save_at_the_end::Bool
save_intermediate::Bool
save_at_iters::Vector{Int64}
filename::String
end
Struct for saving the intermediate or final states of the sampled chain to a hard drive.
SavingCallback(;
save_at_the_end=true,
save_at_iters=[],
overwrite_at_save=false,
filename="mcmc_results",
add_datestamp=false,
path=".",
)
The main constructor for SavingCallback
.
Arguments
save_at_the_end
: indicates whether to save to a file at the end of mcmc samplingsave_at_iters
: specifies at which additional intermediate iterations the chain should be saved to a fileoverwrite
: set to true to overwrite any already existing file that shares the name with the one passed to this functionfilename
: the main stem of the file's nameadd_datestamps
: will add the date and time at the time of creating the callback to the filename if set totrue
path
: specifies the directory path to save to
If each iteration of your MCMC sampler is very fast then you should avoid appending to CSV files at each iteration of the MCMC chain to prevent slow-downs. Use save_at_iters
to specify the iterations at which to save to CSV files.
Writing custom callbacks
You may write your own, custom callback. For each such callback you may provide the following methods. If these are not provided they will fall on defaults that do nothing.
ExtensibleMCMC.init!
— Methodinit!(::Callback, ws::GlobalWorkspace)
Initialization actions for a callback. By default nothing to be done.
ExtensibleMCMC.check_if_execute
— Methodcheck_if_execute(callback::Callback, step, flag)
Check if callback
is supposed to be executed at the mcmc step step
with pre- post- update flag flag
.
ExtensibleMCMC.execute!
— Methodexecute!(
callback::Callback,
global_ws::GlobalWorkspace,
local_wss,
step,
flag
)
Perform actions as specified by the callback
. local_wss
is a vector of all local workspaces, step
is an iterator from the MCMCSchedule
and flag
is either ::PreMCMCStep
or ::PostMCMCStep
.
ExtensibleMCMC.cleanup!
— Methodcleanup!(callback::Callback, ws::GlobalWorkspace, step)
The last call to callback, after all MCMC steps and before exiting the function run!
.