-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathruntests.jl
133 lines (106 loc) · 4.99 KB
/
runtests.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
using AdvancedMH
using Distributions
using StructArrays
using MCMCChains
using Random
using Test
@testset "AdvancedMH" begin
# Set a seed
Random.seed!(1234)
# Generate a set of data from the posterior we want to estimate.
data = rand(Normal(0, 1), 300)
# Define the components of a basic model.
insupport(θ) = θ[2] >= 0
dist(θ) = Normal(θ[1], θ[2])
density(θ) = insupport(θ) ? sum(logpdf.(dist(θ), data)) : -Inf
# Construct a DensityModel.
model = DensityModel(density)
@testset "StaticMH" begin
# Set up our sampler with initial parameters.
spl1 = StaticMH([Normal(0,1), Normal(0, 1)])
spl2 = StaticMH(MvNormal([0.0, 0.0], 1))
# Sample from the posterior.
chain1 = sample(model, spl1, 100000; chain_type=StructArray, param_names=["μ", "σ"])
chain2 = sample(model, spl2, 100000; chain_type=StructArray, param_names=["μ", "σ"])
# chn_mean ≈ dist_mean atol=atol_v
@test mean(chain1.μ) ≈ 0.0 atol=0.1
@test mean(chain1.σ) ≈ 1.0 atol=0.1
@test mean(chain2.μ) ≈ 0.0 atol=0.1
@test mean(chain2.σ) ≈ 1.0 atol=0.1
end
@testset "RandomWalk" begin
# Set up our sampler with initial parameters.
spl1 = RWMH([Normal(0,1), Normal(0, 1)])
spl2 = RWMH(MvNormal([0.0, 0.0], 1))
# Sample from the posterior.
chain1 = sample(model, spl1, 100000; chain_type=StructArray, param_names=["μ", "σ"])
chain2 = sample(model, spl2, 100000; chain_type=StructArray, param_names=["μ", "σ"])
# chn_mean ≈ dist_mean atol=atol_v
@test mean(chain1.μ) ≈ 0.0 atol=0.1
@test mean(chain1.σ) ≈ 1.0 atol=0.1
@test mean(chain2.μ) ≈ 0.0 atol=0.1
@test mean(chain2.σ) ≈ 1.0 atol=0.1
end
@testset "Adaptive random walk" begin
# Set up our sampler with initial parameters.
spl1 = MetropolisHastings([AdaptiveProposal(Normal(0,.4)), AdaptiveProposal(Normal(0,1.2))])
spl2 = MetropolisHastings((μ=AdaptiveProposal(Normal(0,1.4)), σ=AdaptiveProposal(Normal(0,0.2))))
# Sample from the posterior.
chain1 = sample(model, spl1, 100000; chain_type=StructArray, param_names=["μ", "σ"])
chain2 = sample(model, spl2, 100000; chain_type=StructArray, param_names=["μ", "σ"])
# chn_mean ≈ dist_mean atol=atol_v
@test mean(chain1.μ) ≈ 0.0 atol=0.1
@test mean(chain1.σ) ≈ 1.0 atol=0.1
@test mean(chain2.μ) ≈ 0.0 atol=0.1
@test mean(chain2.σ) ≈ 1.0 atol=0.1
end
@testset "Compare adaptive to simple random walk" begin
data = rand(Normal(2., 1.), 500)
m1 = DensityModel(x -> loglikelihood(Normal(x,1), data))
p1 = RandomWalkProposal(Normal())
p2 = AdaptiveProposal(Normal())
c1 = sample(m1, MetropolisHastings(p1), 10000; chain_type=Chains)
c2 = sample(m1, MetropolisHastings(p2), 10000; chain_type=Chains)
@test ess(c2).nt.ess > ess(c1).nt.ess
end
@testset "parallel sampling" begin
spl1 = StaticMH([Normal(0,1), Normal(0, 1)])
chain1 = sample(model, spl1, MCMCDistributed(), 10000, 4;
param_names=["μ", "σ"], chain_type=Chains)
@test mean(chain1["μ"]) ≈ 0.0 atol=0.1
@test mean(chain1["σ"]) ≈ 1.0 atol=0.1
if VERSION >= v"1.3"
chain2 = sample(model, spl1, MCMCThreads(), 10000, 4;
param_names=["μ", "σ"], chain_type=Chains)
@test mean(chain2["μ"]) ≈ 0.0 atol=0.1
@test mean(chain2["σ"]) ≈ 1.0 atol=0.1
end
end
@testset "Proposal styles" begin
m1 = DensityModel(x -> logpdf(Normal(x,1), 1.0))
m2 = DensityModel(x -> logpdf(Normal(x[1], x[2]), 1.0))
m3 = DensityModel(x -> logpdf(Normal(x.a, x.b), 1.0))
m4 = DensityModel(x -> logpdf(Normal(x,1), 1.0))
p1 = StaticProposal(Normal(0,1))
p2 = StaticProposal([Normal(0,1), InverseGamma(2,3)])
p3 = (a=StaticProposal(Normal(0,1)), b=StaticProposal(InverseGamma(2,3)))
p4 = StaticProposal((x=1.0) -> Normal(x, 1))
c1 = sample(m1, MetropolisHastings(p1), 100; chain_type=Vector{NamedTuple})
c2 = sample(m2, MetropolisHastings(p2), 100; chain_type=Vector{NamedTuple})
c3 = sample(m3, MetropolisHastings(p3), 100; chain_type=Vector{NamedTuple})
c4 = sample(m4, MetropolisHastings(p4), 100; chain_type=Vector{NamedTuple})
@test keys(c1[1]) == (:param_1, :lp)
@test keys(c2[1]) == (:param_1, :param_2, :lp)
@test keys(c3[1]) == (:a, :b, :lp)
@test keys(c4[1]) == (:param_1, :lp)
end
@testset "Initial parameters" begin
# Set up our sampler with initial parameters.
spl1 = StaticMH([Normal(0,1), Normal(0, 1)])
val = [0.4, 1.2]
# Sample from the posterior.
chain1 = sample(model, spl1, 10, init_params = val)
@test chain1[1].params == val
end
@testset "EMCEE" begin include("emcee.jl") end
end