-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathabstractmcmc.jl
223 lines (203 loc) · 8.37 KB
/
abstractmcmc.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
214
215
216
217
218
219
220
221
222
223
module AbstractMCMCTests
import ..ADUtils
using AdvancedMH: AdvancedMH
using Distributions: sample
using Distributions.FillArrays: Zeros
using DynamicPPL: DynamicPPL
using ForwardDiff: ForwardDiff
using LinearAlgebra: I
using LogDensityProblems: LogDensityProblems
using LogDensityProblemsAD: LogDensityProblemsAD
using Random: Random
using ReverseDiff: ReverseDiff
using StableRNGs: StableRNG
import Mooncake
using Test: @test, @test_throws, @testset
using Turing
using Turing.Inference: AdvancedHMC
function initialize_nuts(model::Turing.Model)
# Create a log-density function with an implementation of the
# gradient so we ensure that we're using the same AD backend as in Turing.
f = LogDensityProblemsAD.ADgradient(DynamicPPL.LogDensityFunction(model))
# Link the varinfo.
f = Turing.Inference.setvarinfo(
f,
DynamicPPL.link!!(Turing.Inference.getvarinfo(f), model),
Turing.Inference.getADType(DynamicPPL.getcontext(LogDensityProblemsAD.parent(f))),
)
# Choose parameter dimensionality and initial parameter value
D = LogDensityProblems.dimension(f)
initial_θ = rand(D) .- 0.5
# Define a Hamiltonian system
metric = AdvancedHMC.DiagEuclideanMetric(D)
hamiltonian = AdvancedHMC.Hamiltonian(metric, f)
# Define a leapfrog solver, with initial step size chosen heuristically
initial_ϵ = AdvancedHMC.find_good_stepsize(hamiltonian, initial_θ)
integrator = AdvancedHMC.Leapfrog(initial_ϵ)
# Define an HMC sampler, with the following components
# - multinomial sampling scheme,
# - generalised No-U-Turn criteria, and
# - windowed adaption for step-size and diagonal mass matrix
proposal = AdvancedHMC.HMCKernel(
AdvancedHMC.Trajectory{AdvancedHMC.MultinomialTS}(
integrator, AdvancedHMC.GeneralisedNoUTurn()
),
)
adaptor = AdvancedHMC.StanHMCAdaptor(
AdvancedHMC.MassMatrixAdaptor(metric), AdvancedHMC.StepSizeAdaptor(0.65, integrator)
)
return AdvancedHMC.HMCSampler(proposal, metric, adaptor)
end
function initialize_mh_rw(model)
f = DynamicPPL.LogDensityFunction(model)
d = LogDensityProblems.dimension(f)
return AdvancedMH.RWMH(MvNormal(Zeros(d), 0.1 * I))
end
# TODO: Should this go somewhere else?
# Convert a model into a `Distribution` to allow usage as a proposal in AdvancedMH.jl.
struct ModelDistribution{M<:DynamicPPL.Model,V<:DynamicPPL.VarInfo} <:
ContinuousMultivariateDistribution
model::M
varinfo::V
end
function ModelDistribution(model::DynamicPPL.Model)
return ModelDistribution(model, DynamicPPL.VarInfo(model))
end
Base.length(d::ModelDistribution) = length(d.varinfo[:])
function Distributions._logpdf(d::ModelDistribution, x::AbstractVector)
return logprior(d.model, DynamicPPL.unflatten(d.varinfo, x))
end
function Distributions._rand!(
rng::Random.AbstractRNG, d::ModelDistribution, x::AbstractVector{<:Real}
)
model = d.model
varinfo = deepcopy(d.varinfo)
for vn in keys(varinfo)
DynamicPPL.set_flag!(varinfo, vn, "del")
end
DynamicPPL.evaluate!!(model, varinfo, DynamicPPL.SamplingContext(rng))
x .= varinfo[:]
return x
end
function initialize_mh_with_prior_proposal(model)
return AdvancedMH.MetropolisHastings(
AdvancedMH.StaticProposal(ModelDistribution(model))
)
end
function test_initial_params(
model, sampler, initial_params=DynamicPPL.VarInfo(model)[:]; kwargs...
)
# Execute the transition with two different RNGs and check that the resulting
# parameter values are the same.
rng1 = Random.MersenneTwister(42)
rng2 = Random.MersenneTwister(43)
transition1, _ = AbstractMCMC.step(rng1, model, sampler; initial_params, kwargs...)
transition2, _ = AbstractMCMC.step(rng2, model, sampler; initial_params, kwargs...)
vn_to_val1 = DynamicPPL.OrderedDict(transition1.θ)
vn_to_val2 = DynamicPPL.OrderedDict(transition2.θ)
for vn in union(keys(vn_to_val1), keys(vn_to_val2))
@test vn_to_val1[vn] ≈ vn_to_val2[vn]
end
end
@testset "External samplers" begin
@testset "AdvancedHMC.jl" begin
@testset "adtype=$adtype" for adtype in ADUtils.adbackends
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
# Need some functionality to initialize the sampler.
# TODO: Remove this once the constructors in the respective packages become "lazy".
sampler = initialize_nuts(model)
sampler_ext = DynamicPPL.Sampler(
externalsampler(sampler; adtype, unconstrained=true), model
)
# FIXME: Once https://github.com/TuringLang/AdvancedHMC.jl/pull/366 goes through, uncomment.
# @testset "initial_params" begin
# test_initial_params(model, sampler_ext; n_adapts=0)
# end
sample_kwargs = (
n_adapts=1_000,
discard_initial=1_000,
# FIXME: Remove this once we can run `test_initial_params` above.
initial_params=DynamicPPL.VarInfo(model)[:],
)
@testset "inference" begin
DynamicPPL.TestUtils.test_sampler(
[model],
sampler_ext,
2_000;
rtol=0.2,
sampler_name="AdvancedHMC",
sample_kwargs...,
)
end
end
end
@testset "don't drop `ADgradient` (PR: #2223)" begin
rng = Random.default_rng()
model = DynamicPPL.TestUtils.DEMO_MODELS[1]
sampler = initialize_nuts(model)
sampler_ext = externalsampler(
sampler; unconstrained=true, adtype=AutoForwardDiff()
)
# Initial step.
state = last(
AbstractMCMC.step(rng, model, DynamicPPL.Sampler(sampler_ext); n_adapts=0)
)
@test state.logdensity isa LogDensityProblemsAD.ADGradientWrapper
# Subsequent step.
state = last(
AbstractMCMC.step(
rng, model, DynamicPPL.Sampler(sampler_ext), state; n_adapts=0
),
)
@test state.logdensity isa LogDensityProblemsAD.ADGradientWrapper
end
end
@testset "AdvancedMH.jl" begin
@testset "RWMH" begin
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
# Need some functionality to initialize the sampler.
# TODO: Remove this once the constructors in the respective packages become "lazy".
sampler = initialize_mh_rw(model)
sampler_ext = DynamicPPL.Sampler(
externalsampler(sampler; unconstrained=true), model
)
@testset "initial_params" begin
test_initial_params(model, sampler_ext)
end
@testset "inference" begin
DynamicPPL.TestUtils.test_sampler(
[model],
sampler_ext,
2_000;
discard_initial=1_000,
thinning=10,
rtol=0.2,
sampler_name="AdvancedMH",
)
end
end
end
# NOTE: Broken because MH doesn't really follow the `logdensity` interface, but calls
# it with `NamedTuple` instead of `AbstractVector`.
# @testset "MH with prior proposal" begin
# @testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
# sampler = initialize_mh_with_prior_proposal(model);
# sampler_ext = DynamicPPL.Sampler(externalsampler(sampler; unconstrained=false), model)
# @testset "initial_params" begin
# test_initial_params(model, sampler_ext)
# end
# @testset "inference" begin
# DynamicPPL.TestUtils.test_sampler(
# [model],
# sampler_ext,
# 10_000;
# discard_initial=1_000,
# rtol=0.2,
# sampler_name="AdvancedMH"
# )
# end
# end
# end
end
end
end