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.CallbackType

Supertype of all callbacks. They specify additional actions that can be performed before or after each MCMC step or at the very end of sampling.

source

This package implements two types of callbacks:

  • SavingCallback—for saving intermediate states of the sampler to CSV files
  • REPLCallback—for printing progress messages to REPL
Tip

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.

Note

To pass a list of Callbacks to an mcmc sampler pass them in a list to a run! function.

Callback for printing progress messages to REPL


ExtensibleMCMC.REPLCallbackType
struct 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.

source

Callback for saving intermediate results to CSV files


ExtensibleMCMC.SavingCallbackType
struct 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 sampling
  • save_at_iters: specifies at which additional intermediate iterations the chain should be saved to a file
  • overwrite: set to true to overwrite any already existing file that shares the name with the one passed to this function
  • filename: the main stem of the file's name
  • add_datestamps: will add the date and time at the time of creating the callback to the filename if set to true
  • path: specifies the directory path to save to
source
Tip

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!Method
init!(::Callback, ws::GlobalWorkspace)

Initialization actions for a callback. By default nothing to be done.

source
ExtensibleMCMC.check_if_executeMethod
check_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.

source
ExtensibleMCMC.execute!Method
execute!(
    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.

source
ExtensibleMCMC.cleanup!Method
cleanup!(callback::Callback, ws::GlobalWorkspace, step)

The last call to callback, after all MCMC steps and before exiting the function run!.

source