How to Determine Acceptance Rate in Turing.jl with Metropolis-Hastings Algorithm

I am working on a project with Turing.jl using the Metropolis-Hastings (MH) algorithm. I’m interested in tracking the acceptance rate of the sampler in real-time and obtaining the final acceptance rate after the inference is complete.

While exploring the Turing.jl, I noticed that it offers functionality for tracking various diagnostics during sampling, and the documentation includes a TensorBoard showing the acceptance rate for HMC.

Could anyone provide insights or examples on how to use TuringCallbacks.jl to monitor the acceptance rate for MH in a similar way?

How to set up TuringCallbacks to track the acceptance rate during sampling with the MH algorithm?

How can I obtain the acceptance rate after the inference from the chain?

Do I need to write an internal function for that? If it is, how it might be?

Thank you for your support!

Example code:

using Turing, TuringCallbacks

@model function demo(x)
    s ~ InverseGamma(2, 3)
    m ~ Normal(0, sqrt(s))
    for i in eachindex(x)
        x[i] ~ Normal(m, sqrt(s))
    end
end

xs = randn(100) .+ 1;
model = demo(xs);

# Number of MCMC samples/steps
num_samples = 10_000

# Sampling algorithm to use
alg = MH()

# Create the callback
callback = TensorBoardCallback("tensorboard_logs/run")

# Sample
chain = sample(model, alg, num_samples; callback = callback)

...