@@ -8,6 +8,8 @@ using Test
8
8
using DiffResults
9
9
using ForwardDiff
10
10
11
+ include (" util.jl" )
12
+
11
13
@testset " AdvancedMH" begin
12
14
# Set a seed
13
15
Random. seed! (1234 )
@@ -106,21 +108,27 @@ using ForwardDiff
106
108
end
107
109
108
110
@testset " is_symmetric_proposal" begin
111
+ # True distributions
112
+ d1 = Normal (5 , .7 )
113
+
109
114
# Model definition.
110
- m1 = DensityModel (s -> logpdf (Normal (), s. x) + logpdf (Normal (5 ,.7 ), s. y))
115
+ m1 = DensityModel (x -> logpdf (d1, x))
116
+
117
+ # Set up the proposal (StandardNormal is a custom distribution in "util.jl").
118
+ p1 = RandomWalkProposal (StandardNormal ())
119
+
120
+ # Implement `is_symmetric_proposal` for StandardNormal random walk proposal.
121
+ AdvancedMH. is_symmetric_proposal (:: RandomWalkProposal{<:StandardNormal} ) = true
111
122
112
- # Set up the proposal.
113
- p1 = (x= RandomWalkProposal (Normal (0 ,.5 )), y= RandomWalkProposal (Normal (0 ,.5 )))
114
- AdvancedMH. is_symmetric_proposal (proposal:: typeof (p1)) = true
123
+ # Make sure `is_symmetric_proposal` behaves correctly.
115
124
@test AdvancedMH. is_symmetric_proposal (p1)
116
125
117
126
# Sample from the posterior with initial parameters.
118
- chain1 = sample (m1, MetropolisHastings (p1), 100000 ; chain_type= Vector{NamedTuple})
127
+ chain1 = sample (m1, MetropolisHastings (p1), 100000 ;
128
+ chain_type= StructArray, param_names= [" x" ])
119
129
120
- @test mean (getindex .(chain1, :x )) ≈ 0 atol= 0.05
121
- @test mean (getindex .(chain1, :y )) ≈ 5 atol= 0.05
122
- @test std (getindex .(chain1, :x )) ≈ 1 atol= 0.05
123
- @test std (getindex .(chain1, :y )) ≈ .7 atol= 0.05
130
+ @test mean (chain1. x) ≈ mean (d1) atol= 0.05
131
+ @test std (chain1. x) ≈ std (d1) atol= 0.05
124
132
end
125
133
126
134
@testset " MALA" begin
0 commit comments