Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodError in MALA sampler with DensityModel #97

Closed
162348 opened this issue Jul 3, 2024 · 3 comments
Closed

MethodError in MALA sampler with DensityModel #97

162348 opened this issue Jul 3, 2024 · 3 comments

Comments

@162348
Copy link

162348 commented Jul 3, 2024

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

  1. Install the required packages:

    using Pkg
    Pkg.add(["AdvancedMH", "Distributions", "MCMCChains", "ForwardDiff", "StructArrays", "LinearAlgebra"])
  2. 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.

@cpfiffer
Copy link
Member

cpfiffer commented Jul 3, 2024

Hm, this seems to be fixed in AdvancedMH 0.8.2, you have AdvancedMH 0.7.4. Can you try updating? If that doesn't work, we may have some dependency issues to resolve.

@162348
Copy link
Author

162348 commented Jul 4, 2024

Thank you for your support and guidance. I've managed to resolve the issue, and I hope what I found helps:

  1. Preparing a new environment and installing AdvancedMH 0.8.2 resolved the MethodError.
  2. I identified that Soss package was suppressing Turing and AdvancedMH from being updated, and removing it enabled me to update them.
  3. Specifically, FillArrays was constrained to v0.13.11 (the latest is v1.11.0), and this constraint was (somehow) preventing Turing from being updated.

Thank you again for your assistance.

@162348 162348 closed this as completed Jul 4, 2024
@cpfiffer
Copy link
Member

cpfiffer commented Jul 4, 2024

Of course! Thanks for the detailed issue and solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants