# Issues when using Turing with DynamicHMC

**URL:** <https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127>\
**Category:** Probabilistic Programming\
**Tags:** installation, turing\
**Created:** [November 15, 2019, 2:17pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127 "2019-11-15T14:17:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [November 15, 2019, 2:17pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/1 "2019-11-15T14:17:17Z")

</div>

Hi,

when I try to run the DynamicNUTS example from the documentation upon loading the libraries (and having installed current versions) the below issues occur:

```julia
Warning: Error requiring LogDensityProblems from Turing:
UndefVarError: AbstractLogDensityProblem not defined

```

as well as:

```julia
Warning: Error requiring DynamicHMC from Turing:
UndefVarError: NUTS_init_tune_mcmc not defined

```

This occured on two separate computers (both running windows 10) on a fresh install of julia.

Am I doing something wrong or did something break?

All the best and thanks,  
Jakob

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [November 15, 2019, 2:53pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/2 "2019-11-15T14:53:09Z")

</div>

Tamas updated DynamicHMC a little while ago, and it broke Turing’s interface to it. I thought we had prevented these kinds of errors, though – could you print out the results of `] status` so I can see the version of everything?

As a side note, Turing uses the excellent [AdvancedHMC](https://github.com/TuringLang/AdvancedHMC.jl) as it’s default HMC backend. It provides NUTS, HMCDA, and HMC samplers, which you can use via

```julia
chain = sample(model, HMC(0.01, 7), 1000)
chain = sample(model, NUTS(), 1000)
chain = sample(model, HMCDA(200, 0.65, 0.3), 1000)

```

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [November 15, 2019, 3:05pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/3 "2019-11-15T15:05:49Z")

</div>

Hi Cameron,

thanks for the quick reply.  
I didn’t know about the AdvancedHMC stuff, I think that wasn’t there when I last looked at Turing. I’ll give it a shot right away 🙂

Here’s the status print:

```julia
  [c52e3926] Atom v0.11.3
  [bbc10e6e] DynamicHMC v2.1.0        
  [e5e0dc1b] Juno v0.7.2
  [6fdf6af0] LogDensityProblems v0.9.1
  [ce6b1742] RDatasets v0.6.4
  [f3b207a7] StatsPlots v0.12.0
  [fce5fe82] Turing v0.7.2
  [e88e6eb3] Zygote v0.3.4

```

Best  
Jakob

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 15, 2019, 3:18pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/4 "2019-11-15T15:18:20Z")

</div>

> [@cpfiffer](#):
>
> Tamas updated DynamicHMC a little while ago, and it broke Turing’s interface to it.

All changes to the DynamicHMC API follow SemVer; if there is one that didn’t please open an issue.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [November 16, 2019, 7:55am UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/5 "2019-11-16T07:55:59Z")

</div>

@Tamas_Papp would you be willing to have a look at the current inference implemented between Turing and DynamicHMC? I’m sure you will be much fast than anyone from the Turing team to figure out what has to be done.

[https://github.com/TuringLang/Turing.jl/blob/master/src/contrib/inference/dynamichmc.jl](https://github.com/TuringLang/Turing.jl/blob/master/src/contrib/inference/dynamichmc.jl)

I think it would be great if we could continue supporting DynamicHMC as a backend.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2019, 9:03am UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/6 "2019-11-16T09:03:22Z")

</div>

See

[https://github.com/TuringLang/Turing.jl/pull/960](https://github.com/TuringLang/Turing.jl/pull/960)

but the Turing test suite seems to be broken, so I could not run all the tests. The DynamicHMC part works now.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [November 16, 2019, 9:11am UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/7 "2019-11-16T09:11:07Z")

</div>

Thanks a lot for the PR.

---

<div class="post-metadata">

**Author:** ![herm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herm/32/14268_2.png) [@herm](https://discourse.julialang.org/u/herm)\
**Post date:** [October 12, 2024, 11:39am UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/8 "2024-10-12T11:39:16Z")

</div>

This test:

# Import Turing and DynamicHMC.

# compatible with Julia 1.9.4

# Fails on Julia 10, reason unknown or due to globals

using DynamicHMC, Turing

# Model definition.

@model function gdemo(x, y)  
s² ~ InverseGamma(2, 3)  
m ~ Normal(0, sqrt(s²))  
x ~ Normal(m, sqrt(s²))  
y ~ Normal(m, sqrt(s²))  
end

# Pull 2,000 samples using DynamicNUTS.

chn = sample(gdemo(1.5, 2.0), DynamicNUTS(), 2000)  
Chains MCMC chain (2000×3×1 Array{Float64, 3}):

Iterations = 1:1:2000  
Number of chains = 1  
Samples per chain = 2000  
Wall duration = 12.36 seconds  
Compute duration = 12.36 seconds  
parameters = s², m  
internals = lp

Summary Statistics  
parameters mean std mcse ess\_bulk ess\_tail rhat ess\_per\_sec  
Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64

```
      s² 2.1124 1.9361 0.0791 743.1429 822.0729 0.9999 60.1102
       m 1.1679 0.8720 0.0331 757.1192 605.4845 1.0101 61.2407

```

Quantiles  
parameters 2.5% 25.0% 50.0% 75.0% 97.5%  
Symbol Float64 Float64 Float64 Float64 Float64

```
      s² 0.5705 1.0455 1.5578 2.4618 7.7490
       m -0.5559 0.6403 1.1610 1.6665 2.8760

```

Run fine on Julia 1.9.4 but fails on Julia 1.10

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [October 12, 2024, 12:06pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/9 "2024-10-12T12:06:53Z")

</div>

The same response I got 5 years ago should probably still apply: You could try using the NUTS sampler that comes with the Turing ecosystem instead of the one in `DynamicHMC`. So basically this should do the trick:

```julia
using Turing

@model function gdemo(x, y)
  s² ~ InverseGamma(2, 3)
  m ~ Normal(0, sqrt(s²))
  x ~ Normal(m, sqrt(s²))
  y ~ Normal(m, sqrt(s²))
end

chn = sample(gdemo(1.5, 2.0), NUTS(), 2000)

```

Or is there a specific reason you want to use the one in `DynamicHMC`?

---

<div class="post-metadata">

**Author:** ![herm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herm/32/14268_2.png) [@herm](https://discourse.julialang.org/u/herm)\
**Post date:** [October 14, 2024, 8:35pm UTC](https://discourse.julialang.org/t/issues-when-using-turing-with-dynamichmc/31127/10 "2024-10-14T20:35:16Z")

</div>

Thank you for your suggestion, it works. DynamicHMC is an excellent robust package, too bad that Turing interface to DynamicHMC is not compatible.
