This repository has been archived by the owner on May 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathRAM.jl
213 lines (182 loc) · 6.08 KB
/
RAM.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
### Reference:
### Matti Vihola
### Robust Adaptive Metropolis Algorithm with Coerced Acceptance Rate
### Statistics and Computing, 2012, 22 (5), pp 997-1008
### Abstract RAM state
abstract type RAMState{F<:VariateForm} <: MHSamplerState{F} end
### RAM state subtypes
## UnvRAMState holds the internal state ("local variables") of the RAM sampler for univariate parameters
mutable struct UnvRAMState <: RAMState{Univariate}
pstate::ParameterState{Continuous, Univariate} # Parameter state used internally by RAM
tune::MCTunerState
ratio::Real # Acceptance ratio
S::Real
SST::Real
randnsample::Real
η::Real
count::Integer
diagnosticindices::Dict{Symbol, Integer}
function UnvRAMState(
pstate::ParameterState{Continuous, Univariate},
tune::MCTunerState,
ratio::Real,
S::Real,
SST::Real,
randnsample::Real,
η::Real,
count::Integer,
diagnosticindices::Dict{Symbol, Integer}
)
if !isnan(ratio)
@assert ratio > 0 "Acceptance ratio should be positive"
end
@assert count >= 0 "Number of iterations (count) should be non-negative"
new(pstate, tune, ratio, S, SST, randnsample, η, count, diagnosticindices)
end
end
UnvRAMState(
pstate::ParameterState{Continuous, Univariate},
tune::MCTunerState=BasicMCTune(),
S::Real=NaN,
SST::Real=abs2(S)
) =
UnvRAMState(pstate, tune, NaN, S, SST, NaN, NaN, 0, Dict{Symbol, Integer}())
## MuvRAMState holds the internal state ("local variables") of the RAM sampler for multivariate parameters
mutable struct MuvRAMState <: RAMState{Multivariate}
pstate::ParameterState{Continuous, Multivariate} # Parameter state used internally by RAM
tune::MCTunerState
ratio::Real # Acceptance ratio
S::RealLowerTriangular
SST::RealMatrix
randnsample::RealVector
η::Real
count::Integer
diagnosticindices::Dict{Symbol, Integer}
function MuvRAMState(
pstate::ParameterState{Continuous, Multivariate},
tune::MCTunerState,
ratio::Real,
S::RealLowerTriangular,
SST::RealMatrix,
randnsample::RealVector,
η::Real,
count::Integer,
diagnosticindices::Dict{Symbol, Integer}
)
if !isnan(ratio)
@assert ratio > 0 "Acceptance ratio should be positive"
end
@assert count >= 0 "Number of iterations (count) should be non-negative"
new(pstate, tune, ratio, S, SST, randnsample, η, count, diagnosticindices)
end
end
MuvRAMState(
pstate::ParameterState{Continuous, Multivariate},
tune::MCTunerState=BasicMCTune(),
S::RealLowerTriangular=RealLowerTriangular(Array{eltype(pstate)}(pstate.size, pstate.size)),
SST::RealMatrix=S*S'
) =
MuvRAMState(pstate, tune, NaN, S, SST, Array{eltype(pstate)}(pstate.size), NaN, 0, Dict{Symbol, Integer}())
### Robust adaptive Metropolis (RAM) sampler
struct RAM <: MHSampler
S0::RealLowerTriangular # Initial adaptation matrix
targetrate::Real # Target acceptance rate
γ::Real # Exponent for scaling stepsize η
function RAM(S0::RealLowerTriangular, targetrate::Real, γ::Real)
@assert all(i -> i > 0, diag(S0)) "All diagonal elements of initial adaptation matrix must be positive"
@assert 0 < targetrate < 1 "Target acceptance rate should be between 0 and 1"
@assert 0.5 < γ <= 1 "Exponent of stepsize must be greater than 0.5 and less or equal to 1"
new(S0, targetrate, γ)
end
end
RAM(S0::RealMatrix; targetrate::Real=0.234, γ::Real=0.7) = RAM(RealLowerTriangular(S0), targetrate, γ)
RAM(S0::RealVector; targetrate::Real=0.234, γ::Real=0.7) = RAM(RealLowerTriangular(diagm(S0)), targetrate, γ)
RAM(S0::Real=1., n::Integer=1; targetrate::Real=0.234, γ::Real=0.7) = RAM(fill(S0, n), targetrate=targetrate, γ=γ)
### Initialize RAM sampler
## Initialize parameter state
function initialize!(
pstate::ParameterState{Continuous, F},
parameter::Parameter{Continuous, F},
sampler::RAM,
outopts::Dict
) where F<:VariateForm
parameter.logtarget!(pstate)
@assert isfinite(pstate.logtarget) "Log-target not finite: initial value out of support"
if !isempty(outopts[:diagnostics])
pstate.diagnostickeys = copy(outopts[:diagnostics])
pstate.diagnosticvalues = Array{Any}(length(pstate.diagnostickeys))
end
end
## Initialize RAM state
function sampler_state(
parameter::Parameter{Continuous, Univariate},
sampler::RAM,
tuner::MCTuner,
pstate::ParameterState{Continuous, Univariate},
vstate::VariableStateVector,
diagnostickeys::Vector{Symbol}
)
sstate = UnvRAMState( generate_empty(pstate), tuner_state(parameter, sampler, tuner), sampler.S0[1, 1], NaN)
set_diagnosticindices!(sstate, [:accept], diagnostickeys)
sstate
end
function sampler_state(
parameter::Parameter{Continuous, Multivariate},
sampler::RAM,
tuner::MCTuner,
pstate::ParameterState{Continuous, Multivariate},
vstate::VariableStateVector,
diagnostickeys::Vector{Symbol}
)
sstate = MuvRAMState(
generate_empty(pstate),
tuner_state(parameter, sampler, tuner),
copy(sampler.S0),
Array{eltype(pstate)}(pstate.size, pstate.size)
)
set_diagnosticindices!(sstate, [:accept], diagnostickeys)
sstate
end
## Reset parameter state
function reset!(
pstate::ParameterState{Continuous, Univariate},
x::Real,
parameter::Parameter{Continuous, Univariate},
sampler::RAM
)
pstate.value = x
parameter.logtarget!(pstate)
end
function reset!(
pstate::ParameterState{Continuous, Multivariate},
x::RealVector,
parameter::Parameter{Continuous, Multivariate},
sampler::RAM
)
pstate.value = copy(x)
parameter.logtarget!(pstate)
end
## Reset sampler state
function reset!(
sstate::RAMState{Univariate},
pstate::ParameterState{Continuous, Univariate},
parameter::Parameter{Continuous, Univariate},
sampler::RAM,
tuner::MCTuner
)
reset!(sstate.tune, sampler, tuner)
sstate.S = sampler.S0[1, 1]
sstate.count = 0
end
function reset!(
sstate::RAMState{Multivariate},
pstate::ParameterState{Continuous, Multivariate},
parameter::Parameter{Continuous, Multivariate},
sampler::RAM,
tuner::MCTuner
)
reset!(sstate.tune, sampler, tuner)
sstate.S = copy(sampler.S0)
sstate.count = 0
end
show(io::IO, sampler::RAM) = print(io, "RAM sampler: target rate = $(sampler.targetrate), γ = $(sampler.γ)")