Objects of type LogTargetDensity are not callable

Hi, I’m trying to execute the following example for AdvancedHMC:

``````using AdvancedHMC, ForwardDiff
using LogDensityProblems
using LinearAlgebra

# Define the target distribution using the `LogDensityProblem` interface
struct LogTargetDensity
dim::Int
end
LogDensityProblems.logdensity(p::LogTargetDensity, θ) = -sum(abs2, θ) / 2  # standard multivariate normal
LogDensityProblems.dimension(p::LogTargetDensity) = p.dim
LogDensityProblems.capabilities(::Type{LogTargetDensity}) = LogDensityProblems.LogDensityOrder{0}()

# Choose parameter dimensionality and initial parameter value
D = 10; initial_θ = rand(D)
ℓπ = LogTargetDensity(D)

# Set the number of samples to draw and warmup iterations

# Define a Hamiltonian system
metric = DiagEuclideanMetric(D)
hamiltonian = Hamiltonian(metric, ℓπ, ForwardDiff)

# Define a leapfrog solver, with initial step size chosen heuristically
initial_ϵ = find_good_stepsize(hamiltonian, initial_θ)
integrator = Leapfrog(initial_ϵ)

# Define an HMC sampler, with the following components
#   - multinomial sampling scheme,
#   - generalised No-U-Turn criteria, and
#   - windowed adaption for step-size and diagonal mass matrix
proposal = NUTS{MultinomialTS, GeneralisedNoUTurn}(integrator)

# Run the sampler to draw samples from the specified Gaussian, where
#   - `samples` will store the samples
#   - `stats` will store diagnostic statistics for each sample
``````

However, ForwardDiff does not seem equipped to handle the LogTargetDensity type. Here’s the stacktrace:

``````ERROR: MethodError: objects of type LogTargetDensity are not callable
Stacktrace:
[1] vector_mode_dual_eval!(f::LogTargetDensity, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10}}}, x::Vector{Float64})
@ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/apiutils.jl:24
[2] vector_mode_gradient!(result::DiffResults.MutableDiffResult{1, Float64, Tuple{Vector{Float64}}}, f::LogTargetDensity, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10}}})
[3] gradient!(result::DiffResults.MutableDiffResult{1, Float64, Tuple{Vector{Float64}}}, f::LogTargetDensity, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10}}}, ::Val{true})
[4] gradient!(result::DiffResults.MutableDiffResult{1, Float64, Tuple{Vector{Float64}}}, f::LogTargetDensity, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{LogTargetDensity, Float64}, Float64, 10}}}) (repeats 2 times)
[5] ∂ℓπ∂θ_forwarddiff(ℓπ::LogTargetDensity, θ::Vector{Float64})
[7] ∂H∂θ(h::Hamiltonian{DiagEuclideanMetric{Float64, Vector{Float64}}, LogTargetDensity, AdvancedHMC.var"#∂ℓπ∂θ#66"{LogTargetDensity}}, θ::Vector{Float64})
[8] phasepoint(h::Hamiltonian{DiagEuclideanMetric{Float64, Vector{Float64}}, LogTargetDensity, AdvancedHMC.var"#∂ℓπ∂θ#66"{LogTargetDensity}}, θ::Vector{Float64}, r::Vector{Float64})