-
Notifications
You must be signed in to change notification settings - Fork 5
/
simple.jl
176 lines (147 loc) · 5.92 KB
/
simple.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
@testset "simple.jl" begin
# Load all required packages and define likelihood functions
ℓ(x) = logpdf(Normal(x, 0.5), 1.0)
ℓvec(x) = logpdf(MvNormal(x, 0.25 * I), [1.0])
@everywhere begin
ℓ(x) = logpdf(Normal(x, 0.5), 1.0)
ℓvec(x) = logpdf(MvNormal(x, 0.25 * I), [1.0])
end
@testset "Scalar model" begin
Random.seed!(1)
# model
prior = Normal(0, 1)
# true posterior
μ = 0.8
σ² = 0.2
# regular sampling
samples = sample(ESSModel(prior, ℓ), ESS(), 2_000; progress=false)
@test samples isa Vector{Float64}
@test length(samples) == 2_000
@test mean(samples) ≈ μ atol = 0.05
@test var(samples) ≈ σ² atol = 0.05
# parallel sampling
for alg in (MCMCThreads(), MCMCDistributed(), MCMCSerial())
samples = sample(ESSModel(prior, ℓ), ESS(), alg, 2_000, 5; progress=false)
@test samples isa Vector{Vector{Float64}}
@test length(samples) == 5
@test all(length(x) == 2_000 for x in samples)
@test mean(mean, samples) ≈ μ atol = 0.05
@test mean(var, samples) ≈ σ² atol = 0.05
# initial parameter
init_x = randn(5)
samples = sample(
ESSModel(prior, ℓ), ESS(), alg, 10, 5; progress=false, init_params=init_x
)
@test map(first, samples) == init_x
end
# initial parameter
init_x = randn()
samples = sample(ESSModel(prior, ℓ), ESS(), 10; progress=false, init_params=init_x)
@test first(samples) == init_x
end
@testset "Scalar model with nonzero mean" begin
Random.seed!(1)
# model
prior = Normal(0.5, 1)
# true posterior
μ = 0.9
σ² = 0.2
# regular sampling
samples = sample(ESSModel(prior, ℓ), ESS(), 2_000; progress=false)
@test samples isa Vector{Float64}
@test length(samples) == 2_000
@test mean(samples) ≈ μ atol = 0.05
@test var(samples) ≈ σ² atol = 0.05
# parallel sampling
for alg in (MCMCThreads(), MCMCDistributed(), MCMCSerial())
samples = sample(ESSModel(prior, ℓ), ESS(), alg, 2_000, 5; progress=false)
@test samples isa Vector{Vector{Float64}}
@test length(samples) == 5
@test all(length(x) == 2_000 for x in samples)
@test mean(mean, samples) ≈ μ atol = 0.05
@test mean(var, samples) ≈ σ² atol = 0.05
# initial parameter
init_x = randn(5)
samples = sample(
ESSModel(prior, ℓ), ESS(), alg, 10, 5; progress=false, init_params=init_x
)
@test map(first, samples) == init_x
end
# initial parameter
init_x = randn()
samples = sample(ESSModel(prior, ℓ), ESS(), 10; progress=false, init_params=init_x)
@test first(samples) == init_x
end
@testset "Scalar model (vectorized)" begin
Random.seed!(1)
# model
prior = MvNormal([0.0], I)
# true posterior
μ = [0.8]
σ² = [0.2]
# regular sampling
samples = sample(ESSModel(prior, ℓvec), ESS(), 2_000; progress=false)
@test samples isa Vector{Vector{Float64}}
@test length(samples) == 2_000
@test all(length(x) == 1 for x in samples)
@test mean(samples) ≈ μ atol = 0.05
@test var(samples) ≈ σ² atol = 0.05
# parallel sampling
for alg in (MCMCThreads(), MCMCDistributed(), MCMCSerial())
samples = sample(ESSModel(prior, ℓvec), ESS(), alg, 2_000, 5; progress=false)
@test samples isa Vector{Vector{Vector{Float64}}}
@test length(samples) == 5
@test all(length(x) == 2_000 for x in samples)
@test mean(mean, samples) ≈ μ atol = 0.05
@test mean(var, samples) ≈ σ² atol = 0.05
# initial parameter
init_x = [randn(1) for _ in 1:5]
samples = sample(
ESSModel(prior, ℓvec), ESS(), alg, 10, 5; progress=false, init_params=init_x
)
@test map(first, samples) == init_x
end
# initial parameter
init_x = randn(1)
samples = sample(
ESSModel(prior, ℓvec), ESS(), 10; progress=false, init_params=init_x
)
@test first(samples) == init_x
end
@testset "Scalar model with nonzero mean (vectorized)" begin
Random.seed!(1)
# model
prior = MvNormal([0.5], I)
# true posterior
μ = [0.9]
σ² = [0.2]
# regular sampling
samples = sample(ESSModel(prior, ℓvec), ESS(), 2_000; progress=false)
@test samples isa Vector{Vector{Float64}}
@test length(samples) == 2_000
@test all(length(x) == 1 for x in samples)
@test mean(samples) ≈ μ atol = 0.05
@test var(samples) ≈ σ² atol = 0.05
# parallel sampling
for alg in (MCMCThreads(), MCMCDistributed(), MCMCSerial())
samples = sample(ESSModel(prior, ℓvec), ESS(), alg, 2_000, 5; progress=false)
@test samples isa Vector{Vector{Vector{Float64}}}
@test length(samples) == 5
@test all(length(x) == 2_000 for x in samples)
@test mean(mean, samples) ≈ μ atol = 0.05
@test mean(var, samples) ≈ σ² atol = 0.05
# initial parameter
init_x = [randn(1) for _ in 1:5]
samples = sample(
ESSModel(prior, ℓvec), ESS(), alg, 10, 5; progress=false, init_params=init_x
)
@test map(first, samples) == init_x
end
# initial parameter
init_x = randn(1)
samples = sample(
ESSModel(prior, ℓvec), ESS(), 10; progress=false, init_params=init_x
)
@test first(samples) == init_x
end
end