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

RW prior on vectors #94

Closed
patwa67 opened this issue Jan 25, 2024 · 1 comment
Closed

RW prior on vectors #94

patwa67 opened this issue Jan 25, 2024 · 1 comment

Comments

@patwa67
Copy link

patwa67 commented Jan 25, 2024

How can I apply random walk priors to a vector (beta[1:10]) using AdvancedMH in Turing? Not working sampling code is as follows:

chain = sample(
    model,
    MH(
        :sigmae => AdvancedMH.RandomWalkProposal(Normal(0, 0.1)),
        :alpha => AdvancedMH.RandomWalkProposal(Normal(0, 0.1)),
        :beta => AdvancedMH.RandomWalkProposal(Normal(0, 0.1)),
    ),
    1_000
)
@penelopeysm
Copy link
Member

penelopeysm commented Oct 22, 2024

Hi @patwa67! Sorry nobody replied to you in good time.

You need to give a multivariate distribution inside the proposal. See e.g.

function propose(
rng::Random.AbstractRNG,
proposal::RandomWalkProposal{issymmetric,<:Union{Distribution,AbstractArray}},
::DensityModelOrLogDensityModel
) where {issymmetric}
return rand(rng, proposal)
end
function propose(
rng::Random.AbstractRNG,
proposal::RandomWalkProposal{issymmetric,<:Union{Distribution,AbstractArray}},
model::DensityModelOrLogDensityModel,
t
) where {issymmetric}
return t + rand(rng, proposal)
end

so if your beta is a 10-element vector then rand(rng, proposal) should give a 10-element vector.

For example:

using Turing
using AdvancedMH
using LinearAlgebra: I

@model function f()
    a ~ Normal(0, 1)
    x ~ MvNormal(fill(a, 10), I)
end

chain = sample(
    f(),
    MH(
        :a => AdvancedMH.RandomWalkProposal(Normal(0, 1)),
        :x => AdvancedMH.RandomWalkProposal(MvNormal(zeros(10), I)),
    ),
    1000
)

Tested on:

julia> versioninfo()
Julia Version 1.10.5
Commit 6f3fdf7b362 (2024-08-27 14:19 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

(ppl) pkg> st
Status `~/ppl/Project.toml`
  [5b7e9947] AdvancedMH v0.8.3
  [fce5fe82] Turing v0.34.1

I'm going to close this, but please feel free to reopen if you have other questions or bugs. Do specify the model as well as that will make it possible to reproduce (the call to sample needs to match the way the model is defined).

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