-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Description
When trying to use the MALA sampler with a DensityModel, I encountered a MethodError indicating ambiguity in the propose function. This error occurs when running the example code provided in the README.md of the AdvancedMH.jl package.
Steps to Reproduce
-
Install the required packages:
using Pkg Pkg.add(["AdvancedMH", "Distributions", "MCMCChains", "ForwardDiff", "StructArrays", "LinearAlgebra"])
-
Run the following code:
using AdvancedMH using Distributions using MCMCChains using ForwardDiff using StructArrays using LinearAlgebra # Generate a set of data from the posterior we want to estimate. data = rand(Normal(0, 1), 30) # Define the components of a basic model. insupport(θ) = θ[2] >= 0 dist(θ) = Normal(θ[1], θ[2]) density(θ) = insupport(θ) ? sum(logpdf.(dist(θ), data)) : -Inf # Construct a DensityModel. model = DensityModel(density) # Set up the sampler with a multivariate Gaussian proposal. σ² = 0.01 spl = MALA(x -> MvNormal((σ² / 2) .* x, σ² * I)) # Sample from the posterior. chain = sample(model, spl, 100000; initial_params=ones(2), chain_type=StructArray, param_names=["μ", "σ"])
Error Message
ERROR: MethodError: propose(::Random.TaskLocalRNG, ::MALA{RandomWalkProposal{false, var"#5#6"}}, ::DensityModel{typeof(density)}) is ambiguous.
Candidates:
propose(rng::Random.AbstractRNG, ::MALA, model)
@ AdvancedMH ~/.julia/packages/AdvancedMH/7JckQ/src/MALA.jl:22
propose(rng::Random.AbstractRNG, sampler::AdvancedMH.MHSampler, model::Union{AbstractMCMC.LogDensityModel, DensityModel})
@ AdvancedMH ~/.julia/packages/AdvancedMH/7JckQ/src/mh-core.jl:51
Possible fix, define
propose(::Random.AbstractRNG, ::MALA, ::Union{AbstractMCMC.LogDensityModel, DensityModel})
Environment Information
- Julia version:
v"1.10.4" - AdvancedMH.jl version:
v0.7.4 - Distributions.jl version:
v0.25.109 - MCMCChains.jl version:
v6.0.6 - ForwardDiff.jl version:
v0.10.36 - StructArrays.jl version:
v0.6.18
Additional Context
This code is taken directly from the README.md of the AdvancedMH.jl package. It seems that there might be an incompatibility between the current implementations of propose for MALA and DensityModel.
I also tried initializing the sampler with spl = MALA(MvNormal(2, sqrt(σ²))) or spl = MALA(σ²) instead of spl = MALA(x -> MvNormal((σ² / 2) .* x, σ² * I)), but this resulted in the same error.
Possible Solution
As suggested in the error message, defining a method for propose(::Random.AbstractRNG, ::MALA, ::Union{AbstractMCMC.LogDensityModel, DensityModel}) might resolve this ambiguity.
I would greatly appreciate any assistance in resolving this issue or guidance on how to properly use the MALA sampler with a DensityModel. Thank you for your time and effort in maintaining this package.