# \[AdvancedHMC\] Implementation of Custom HMCKernel

**URL:** https://discourse.julialang.org/t/advancedhmc-implementation-of-custom-hmckernel/132664
**Category:** Statistics
**Tags:** package
**Created:** [September 25, 2025, 5:51pm UTC](https://discourse.julialang.org/t/advancedhmc-implementation-of-custom-hmckernel/132664 "2025-09-25T17:51:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sample\_slice\_of\_life](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@sample\_slice\_of\_life](https://discourse.julialang.org/u/sample_slice_of_life)
#### Post date: [September 25, 2025, 5:51pm UTC](https://discourse.julialang.org/t/advancedhmc-implementation-of-custom-hmckernel/132664/1 "2025-09-25T17:51:21Z")

</div>

Hello all,

I am a student researching possible extensions of HMC-type samplers for settings in which posteriors exhibit high correlation between parameters.

I have a working implementation of a code for a toy problem in Julia using the AdvancedHMC.jl package. The results (posterior traceplots, convergence diagnostics, etc.) are _textbook_ – could not ask for better.

My issue: I have been attempting to replicate the same behavior in R. I am implementing both forward and adjoint sensitivity approaches to help obtain gradients w.r.t. parameters of the system, along with trying to implement NUTS, dual-averaging, and mass matrix adaptation (beyond diagonal). **Nothing I do gives results as efficient as Julia’s AdvancedHMC.jl or Stan in R.** Julia having access to AD methods obviously means the wall-clock time is better.

This issue is not ‘surprising’ – per se – but really dampens the exploration of any ideas in research because a suboptimal NUTS sampler means that any convergence diagnostics from testing an idea shroud any potential efficacy.

My Question: Is there any way to implement your _own_ HMCKernel instead of

> ϵ = find\_good\_stepsize(hamiltonian, collect(Float64, initial\_θ))  
> integrator = JitteredLeapfrog(ϵ, 0.1)  
> kernel = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn()))  
> adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, integrator))

and still be able to take advantage of, say, the StanHMCAdaptor and the like?

---

<div class="post-metadata">

### Author: ![Red-Portal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/red-portal/32/9102_2.png) [@Red-Portal](https://discourse.julialang.org/u/Red-Portal)
#### Post date: [October 15, 2025, 7:37am UTC](https://discourse.julialang.org/t/advancedhmc-implementation-of-custom-hmckernel/132664/2 "2025-10-15T07:37:31Z")

</div>

Short answer is yes, AdvancedHMC is highly modular and fairly easy to customize. But of course, how you would do that depends on what you want to customize specifically.
