Skip to content

Commit 9247281

Browse files
Tor FjeldeTor Fjelde
authored andcommitted
renamed RAM to RobostMetropolisHastings + removed the separate module for this
1 parent f2889a0 commit 9247281

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

src/AdvancedMH.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module AdvancedMH
33
# Import the relevant libraries.
44
using AbstractMCMC
55
using Distributions
6-
using LinearAlgebra: I
6+
using LinearAlgebra: LinearAlgebra, I
77
using FillArrays: Zeros
8+
using DocStringExtensions: FIELDS
89

910
using LogDensityProblems: LogDensityProblems
1011

src/RobustAdaptiveMetropolis.jl

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
module RobustAdaptiveMetropolis
2-
3-
using Random, LogDensityProblems, LinearAlgebra, AbstractMCMC
4-
using DocStringExtensions: FIELDS
5-
6-
using AdvancedMH: AdvancedMH
7-
8-
export RAM
9-
101
# TODO: Should we generalise this arbitrary symmetric proposals?
112
"""
12-
RAM
3+
RobustAdaptiveMetropolis
134
145
Robust Adaptive Metropolis-Hastings (RAM).
156
@@ -56,7 +47,7 @@ julia> # Set the seed so get some consistency.
5647
Random.seed!(1234);
5748
5849
julia> # Sample!
59-
chain = sample(model, RAM(), 10_000; chain_type=Chains, num_warmup, progress=false, initial_params=zeros(2));
50+
chain = sample(model, RobustAdaptiveMetropolis(), 10_000; chain_type=Chains, num_warmup, progress=false, initial_params=zeros(2));
6051
6152
julia> isapprox(cov(Array(chain)), model.A; rtol = 0.2)
6253
true
@@ -67,7 +58,7 @@ It's also possible to restrict the eigenvalues to avoid either too small or too
6758
```jldoctest ram-gaussian`
6859
julia> chain = sample(
6960
model,
70-
RAM(eigenvalue_lower_bound=0.1, eigenvalue_upper_bound=2.0),
61+
RobustAdaptiveMetropolis(eigenvalue_lower_bound=0.1, eigenvalue_upper_bound=2.0),
7162
10_000;
7263
chain_type=Chains, num_warmup, progress=false, initial_params=zeros(2)
7364
);
@@ -79,7 +70,7 @@ true
7970
# References
8071
[^VIH12]: Vihola (2012) Robust adaptive Metropolis algorithm with coerced acceptance rate, Statistics and computing.
8172
"""
82-
Base.@kwdef struct RAM{T,A<:Union{Nothing,AbstractMatrix{T}}} <: AdvancedMH.MHSampler
73+
Base.@kwdef struct RobustAdaptiveMetropolis{T,A<:Union{Nothing,AbstractMatrix{T}}} <: AdvancedMH.MHSampler
8374
"target acceptance rate. Default: 0.234."
8475
α::T=0.234
8576
"negative exponent of the adaptation decay rate. Default: `0.6`."
@@ -93,16 +84,16 @@ Base.@kwdef struct RAM{T,A<:Union{Nothing,AbstractMatrix{T}}} <: AdvancedMH.MHSa
9384
end
9485

9586
"""
96-
RAMState
87+
RobustAdaptiveMetropolisState
9788
9889
State of the Robust Adaptive Metropolis-Hastings (RAM) algorithm.
9990
100-
See also: [`RAM`](@ref).
91+
See also: [`RobustAdaptiveMetropolis`](@ref).
10192
10293
# Fields
10394
$(FIELDS)
10495
"""
105-
struct RAMState{T1,L,A,T2,T3}
96+
struct RobustAdaptiveMetropolisState{T1,L,A,T2,T3}
10697
"current realization of the chain."
10798
x::T1
10899
"log density of `x` under the target model."
@@ -119,14 +110,14 @@ struct RAMState{T1,L,A,T2,T3}
119110
isaccept::Bool
120111
end
121112

122-
AbstractMCMC.getparams(state::RAMState) = state.x
123-
AbstractMCMC.setparams!!(state::RAMState, x) = RAMState(x, state.logprob, state.S, state.logα, state.η, state.iteration, state.isaccept)
113+
AbstractMCMC.getparams(state::RobustAdaptiveMetropolisState) = state.x
114+
AbstractMCMC.setparams!!(state::RobustAdaptiveMetropolisState, x) = RobustAdaptiveMetropolisState(x, state.logprob, state.S, state.logα, state.η, state.iteration, state.isaccept)
124115

125-
function step_inner(
116+
function ram_step_inner(
126117
rng::Random.AbstractRNG,
127118
model::AbstractMCMC.LogDensityModel,
128-
sampler::RAM,
129-
state::RAMState
119+
sampler::RobustAdaptiveMetropolis,
120+
state::RobustAdaptiveMetropolisState
130121
)
131122
# This is the initial state.
132123
f = model.logdensity
@@ -146,8 +137,7 @@ function step_inner(
146137
return x_new, lp_new, U, logα, isaccept
147138
end
148139

149-
function adapt(sampler::RAM, state::RAMState, logα::Real, U::AbstractVector)
150-
# Update `
140+
function ram_adapt(sampler::RobustAdaptiveMetropolis, state::RobustAdaptiveMetropolisState, logα::Real, U::AbstractVector)
151141
Δα = exp(logα) - sampler.α
152142
S = state.S
153143
# TODO: Make this configurable by defining a more general path.
@@ -167,7 +157,7 @@ end
167157
function AbstractMCMC.step(
168158
rng::Random.AbstractRNG,
169159
model::AbstractMCMC.LogDensityModel,
170-
sampler::RAM;
160+
sampler::RobustAdaptiveMetropolis;
171161
initial_params=nothing,
172162
kwargs...
173163
)
@@ -183,22 +173,22 @@ function AbstractMCMC.step(
183173

184174
# Construct the initial state.
185175
lp = LogDensityProblems.logdensity(f, x)
186-
state = RAMState(x, lp, S, zero(T), 0, 1, true)
176+
state = RobustAdaptiveMetropolisState(x, lp, S, zero(T), 0, 1, true)
187177

188178
return AdvancedMH.Transition(x, lp, true), state
189179
end
190180

191181
function AbstractMCMC.step(
192182
rng::Random.AbstractRNG,
193183
model::AbstractMCMC.LogDensityModel,
194-
sampler::RAM,
195-
state::RAMState;
184+
sampler::RobustAdaptiveMetropolis,
185+
state::RobustAdaptiveMetropolisState;
196186
kwargs...
197187
)
198188
# Take the inner step.
199-
x_new, lp_new, U, logα, isaccept = step_inner(rng, model, sampler, state)
189+
x_new, lp_new, U, logα, isaccept = ram_step_inner(rng, model, sampler, state)
200190
# Accept / reject the proposal.
201-
state_new = RAMState(isaccept ? x_new : state.x, isaccept ? lp_new : state.logprob, state.S, logα, state.η, state.iteration + 1, isaccept)
191+
state_new = RobustAdaptiveMetropolisState(isaccept ? x_new : state.x, isaccept ? lp_new : state.logprob, state.S, logα, state.η, state.iteration + 1, isaccept)
202192
return AdvancedMH.Transition(state_new.x, state_new.logprob, state_new.isaccept), state_new
203193
end
204194

@@ -213,23 +203,22 @@ end
213203
function AbstractMCMC.step_warmup(
214204
rng::Random.AbstractRNG,
215205
model::AbstractMCMC.LogDensityModel,
216-
sampler::RAM,
217-
state::RAMState;
206+
sampler::RobustAdaptiveMetropolis,
207+
state::RobustAdaptiveMetropolisState;
218208
kwargs...
219209
)
220210
# Take the inner step.
221-
x_new, lp_new, U, logα, isaccept = step_inner(rng, model, sampler, state)
211+
x_new, lp_new, U, logα, isaccept = ram_step_inner(rng, model, sampler, state)
222212
# Adapt the proposal.
223-
S_new, η = adapt(sampler, state, logα, U)
213+
S_new, η = ram_adapt(sampler, state, logα, U)
224214
# Check that `S_new` has eigenvalues in the desired range.
225215
if !valid_eigenvalues(S_new, sampler.eigenvalue_lower_bound, sampler.eigenvalue_upper_bound)
226216
# In this case, we just keep the old `S` (p. 13 in Vihola, 2012).
227217
S_new = state.S
228218
end
229219

230220
# Update state.
231-
state_new = RAMState(isaccept ? x_new : state.x, isaccept ? lp_new : state.logprob, S_new, logα, η, state.iteration + 1, isaccept)
221+
state_new = RobustAdaptiveMetropolisState(isaccept ? x_new : state.x, isaccept ? lp_new : state.logprob, S_new, logα, η, state.iteration + 1, isaccept)
232222
return AdvancedMH.Transition(state_new.x, state_new.logprob, state_new.isaccept), state_new
233223
end
234224

235-
end

0 commit comments

Comments
 (0)