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

Proposal for MH only works correctly with subtypes of MvNormal #2180

Closed
torfjelde opened this issue Mar 11, 2024 · 0 comments
Closed

Proposal for MH only works correctly with subtypes of MvNormal #2180

torfjelde opened this issue Mar 11, 2024 · 0 comments

Comments

@torfjelde
Copy link
Member

MH is implemented in a way where even if it receives a AdvancedMH.RandomWalkProposal it will still hit the "general" propose!! that assumes the proposal is a NamedTuple, i.e.

Turing.jl/src/mcmc/mh.jl

Lines 377 to 383 in 3a315ce

function propose!!(
rng::AbstractRNG,
vi::AbstractVarInfo,
model::Model,
spl::Sampler{<:MH},
proposal
)

instead of the one that is hit if we're a subtype of MvNormal

Turing.jl/src/mcmc/mh.jl

Lines 410 to 417 in 3a315ce

# Make a proposal if we DO have a covariance proposal matrix.
function propose!!(
rng::AbstractRNG,
vi::AbstractVarInfo,
model::Model,
spl::Sampler{<:MH},
proposal::AdvancedMH.RandomWalkProposal{issymmetric,<:MvNormal}
) where {issymmetric}

For example, if I want to use a filldist(..., n) as a proposal instead of product_distribution(Fill(...), n), stuff breaks:

julia> using Turing, AdvancedMH, LinearAlgebra

julia> @model demo() = x ~ MvNormal(zeros(3), I)
demo (generic function with 2 methods)

julia> sample(
           demo(),
           MH(AdvancedMH.RandomWalkProposal(filldist(Normal(), 3))),
           10
       )
Sampling 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| Time: 0:00:00
ERROR: MethodError: no method matching _dist_tuple(::RandomWalkProposal{…}, ::DynamicPPL.TypedVarInfo{…}, ::@NamedTuple{})

Closest candidates are:
  _dist_tuple(::NamedTuple{propnames}, ::DynamicPPL.VarInfo, ::NamedTuple{names}) where {names, propnames}
   @ Turing ~/.julia/packages/Turing/I5L1s/src/mcmc/mh.jl:335

Stacktrace:
  [1] dist_val_tuple(spl::DynamicPPL.Sampler{MH{(), RandomWalkProposal{…}}}, vi::DynamicPPL.TypedVarInfo{@NamedTuple{x::DynamicPPL.Metadata{…}}, Float64})
    @ Turing.Inference ~/.julia/packages/Turing/I5L1s/src/mcmc/mh.jl:317
  [2] propose!!(rng::Random.TaskLocalRNG, vi::DynamicPPL.TypedVarInfo{…}, model::DynamicPPL.Model{…}, spl::DynamicPPL.Sampler{…}, proposal::RandomWalkProposal{…})
    @ Turing.Inference ~/.julia/packages/Turing/I5L1s/src/mcmc/mh.jl:385
  [3] step(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}, spl::DynamicPPL.Sampler{…}, vi::DynamicPPL.TypedVarInfo{…}; kwargs::@Kwargs{})
    @ Turing.Inference ~/.julia/packages/Turing/I5L1s/src/mcmc/mh.jl:466
  [4] step(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}, spl::DynamicPPL.Sampler{…}, vi::DynamicPPL.TypedVarInfo{…})
    @ Turing.Inference ~/.julia/packages/Turing/I5L1s/src/mcmc/mh.jl:456

IIUC we should just relax

Turing.jl/src/mcmc/mh.jl

Lines 410 to 417 in 3a315ce

# Make a proposal if we DO have a covariance proposal matrix.
function propose!!(
rng::AbstractRNG,
vi::AbstractVarInfo,
model::Model,
spl::Sampler{<:MH},
proposal::AdvancedMH.RandomWalkProposal{issymmetric,<:MvNormal}
) where {issymmetric}

to be any AdvancedMH.RandomWalkProposal 🤷

torfjelde added a commit that referenced this issue Mar 13, 2024
* fixed dispatch issue of `propose!!` (ref #2180)

* added test for `filldist` proposal for MH

* bump patch version

* added `demo_dot_assume_observe_index` to models that are allowed to
fail the MLE test, as it should be

* fixed test

* try comparing median instead of mean to avoid outliers completely
ruining the estimate

* try with initial params

* upped the number of samples used for `filldist` proposal test
@yebai yebai closed this as completed Mar 14, 2024
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