TuringLang / AdvancedHMC.jl

Robust, modular and efficient implementation of advanced Hamiltonian Monte Carlo algorithms
https://turinglang.org/AdvancedHMC.jl/
MIT License
228 stars 39 forks source link

Progress message `ratio_divergent_transitions_during_adaption` bug #373

Open sefffal opened 1 month ago

sefffal commented 1 month ago

This is difficult to demonstrate, but I believe the value of ratio_divergent_transitions_during_adaption printed during sampling when progress=true is currently incorrect.

The behaviour I've seen is:

  1. During adaptation, numerical errors are encountered causing ratio_divergent_transitions_during_adaption to increase
  2. By the end of adaptation, the numerical errors stop
  3. Sampling continues after adaptation and ratio_divergent_transitions stays zero.
  4. ratio_divergent_transitions_during_adaption starts to drop (even though adaptation is now finished!) until it reaches a lower value, eg 50% lower if there are equal numbers of samples during and after adaptation.

I couldn't find the right location in the code, but I believe that ratio_divergent_transitions_during_adaption might be calculating the ratio using the total number of samples so far, and not stopping updating after adaptation completes.

sefffal commented 1 month ago

I think I see the issue: https://github.com/TuringLang/AdvancedHMC.jl/blob/2b3814ccbb36806eb0f21c2c937146606a873884/src/sampler.jl#L198C1-L199C62

percentage_divergent_transitions = num_divergent_transitions / I
percentage_divergent_transitions_during_adaption =
    num_divergent_transitions_during_adaption / i

should instead be:

percentage_divergent_transitions = num_divergent_transitions / I
percentage_divergent_transitions_during_adaption =
    num_divergent_transitions_during_adaption / min(i, n_adapts)