-
Notifications
You must be signed in to change notification settings - Fork 17
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
Adaptive Metropolis algorithm #57
base: master
Are you sure you want to change the base?
Changes from 8 commits
71f1072
3a23129
c40d4db
5afb87e
8cb275d
46f77ca
970b0f5
c800aaf
eb91b36
77bd3e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Simple Adaptive Metropolis Proposal | ||
# The proposal at each step is equal to a scalar multiple | ||
# of the empirical posterior covariance plus a fixed, small covariance | ||
# matrix epsilon which is also used for initial exploration. | ||
# | ||
# Reference: | ||
# H. Haario, E. Saksman, and J. Tamminen, "An adaptive Metropolis algorithm", | ||
# Bernoulli 7(2): 223-242 (2001) | ||
mutable struct AMProposal{FT <: Real, CT <: AbstractMatrix{FT}, | ||
MNT <: AbstractMvNormal} <: Proposal{MNT} | ||
epsilon::CT | ||
scalefactor::FT | ||
proposal::MNT | ||
μ::Vector{FT} | ||
M::Matrix{FT} | ||
δ::Vector{FT} | ||
N::Int | ||
end | ||
|
||
function AMProposal(epsilon::AbstractMatrix{FT}, | ||
scalefactor=FT(2.38^2 / size(epsilon, 1))) where {FT <: Real} | ||
kaandocal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sym = PDMat(Symmetric(epsilon)) | ||
proposal = MvNormal(zeros(size(sym, 1)), sym) | ||
AMProposal(sym, scalefactor, proposal, zeros(size(sym,1)), zeros(size(sym)...), | ||
zeros(size(sym,1)), 0) | ||
end | ||
|
||
logratio_proposal_density(::AMProposal, params_prev, params) = 0 | ||
|
||
# When the proposal is initialised the empirical posterior covariance is zero | ||
function trackstep!(proposal::AMProposal, params) | ||
proposal.μ .= params | ||
proposal.M .= 0 | ||
proposal.proposal = MvNormal(zeros(size(proposal.μ)), proposal.epsilon) | ||
proposal.N = 1 | ||
end | ||
|
||
# Recompute the empirical posterior covariance matrix | ||
function trackstep!(proposal::AMProposal, params, ::Union{Val{true}, Val{false}}) | ||
proposal.N += 1 | ||
proposal.δ .= params .- proposal.μ | ||
proposal.μ .+= proposal.δ ./ proposal.N | ||
|
||
mul!(proposal.M, params .- proposal.μ, proposal.δ', 1.0, 1.0) | ||
kaandocal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
prop_cov = proposal.M .* proposal.scalefactor ./ proposal.N .+ proposal.epsilon | ||
proposal.proposal = MvNormal(mean(proposal.proposal), Symmetric(prop_cov)) | ||
end | ||
|
||
function propose(rng::Random.AbstractRNG, p::AMProposal, ::DensityModel) | ||
return rand(rng, p.proposal) | ||
end | ||
|
||
function propose(rng::Random.AbstractRNG, p::AMProposal, ::DensityModel, t) | ||
return t + rand(rng, p.proposal) | ||
end |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -141,6 +141,22 @@ end | |||||||||||||||||||||||||||||||
transition(sampler, model, params) = transition(model, params) | ||||||||||||||||||||||||||||||||
transition(model, params) = Transition(model, params) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
# Called to update proposal when the first sample is drawn | ||||||||||||||||||||||||||||||||
trackstep!(proposal::Proposal, params) = nothing | ||||||||||||||||||||||||||||||||
trackstep!(proposal::AbstractArray, params) = foreach(trackstep!, proposal, params) | ||||||||||||||||||||||||||||||||
trackstep!(proposal::NamedTuple, params) = foreach(trackstep!, proposal, params) | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameters in the NamedTuple |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
# Called to update proposal when a new step is performed | ||||||||||||||||||||||||||||||||
# The last argument determines if the step is an acceptance step | ||||||||||||||||||||||||||||||||
trackstep!(proposal::Proposal, params, | ||||||||||||||||||||||||||||||||
::Union{Val{true}, Val{false}}) = nothing | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
trackstep!(proposal::AbstractArray, params, accept::Union{Val{true},Val{false}}) = | ||||||||||||||||||||||||||||||||
foreach((prop, par) -> trackstep!(prop, par, accept), proposal, params) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
trackstep!(proposal::NamedTuple, params, accept::Union{Val{true},Val{false}}) = | ||||||||||||||||||||||||||||||||
foreach((prop, par) -> trackstep!(prop, par, accept), proposal, params) | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the type annotations on
Suggested change
Additionally, again the |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
# Define the first sampling step. | ||||||||||||||||||||||||||||||||
# Return a 2-tuple consisting of the initial sample and the initial state. | ||||||||||||||||||||||||||||||||
# In this case they are identical. | ||||||||||||||||||||||||||||||||
|
@@ -157,6 +173,7 @@ function AbstractMCMC.step( | |||||||||||||||||||||||||||||||
transition = AdvancedMH.transition(spl, model, init_params) | ||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
trackstep!(spl.proposal, transition.params) | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||||||||||||||||||||||||||||
return transition, transition | ||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
@@ -180,8 +197,10 @@ function AbstractMCMC.step( | |||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
# Decide whether to return the previous params or the new one. | ||||||||||||||||||||||||||||||||
if -Random.randexp(rng) < logα | ||||||||||||||||||||||||||||||||
trackstep!(spl.proposal, params.params, Val(true)) | ||||||||||||||||||||||||||||||||
return params, params | ||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||
trackstep!(spl.proposal, params_prev.params, Val(false)) | ||||||||||||||||||||||||||||||||
return params_prev, params_prev | ||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a
[compat]
entry for PDMats?