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

**URL:** https://discourse.julialang.org/t/how-to-determine-acceptance-rate-in-turing-jl-with-metropolis-hastings-algorithm/106689
**Category:** Probabilistic Programming
**Tags:** question, turing
**Created:** [November 24, 2023, 5:31pm UTC](https://discourse.julialang.org/t/how-to-determine-acceptance-rate-in-turing-jl-with-metropolis-hastings-algorithm/106689 "2023-11-24T17:31:29Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Can\_Bozdogan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/can_bozdogan/32/204255_2.png) [@Can\_Bozdogan](https://discourse.julialang.org/u/Can_Bozdogan)
#### Post date: [November 24, 2023, 5:31pm UTC](https://discourse.julialang.org/t/how-to-determine-acceptance-rate-in-turing-jl-with-metropolis-hastings-algorithm/106689/1 "2023-11-24T17:31:29Z")

</div>

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](https://turinglang.org/library/TuringCallbacks/stable/) includes a TensorBoard showing the acceptance rate for HMC.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/9/692e7092cf012dc1eed4eefb57b8895e900f6f2a.jpeg)

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:

```julia
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)

...
```
