-
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
Open
kaandocal
wants to merge
10
commits into
TuringLang:master
Choose a base branch
from
kaandocal:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
71f1072
Added Adaptive Metropolis algorithm
kaandocal 3a23129
More documentation
kaandocal c40d4db
Concrete types for AMProposal
kaandocal 5afb87e
Added reference for the AM algorithm
kaandocal 8cb275d
Merged AMSampler into MetropolisHastings
kaandocal 46f77ca
Added Welford's algorithm for adaptive covariance calculations
970b0f5
trackstep -> trackstep!
c800aaf
Fixed diagm issue in AdaptiveMH test
eb91b36
Apply suggestions from code review
kaandocal 77bd3e5
Merge tag 'v0.6.8'
kaandocal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# 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} | ||
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.δ', true, true) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?