Skip to content

Commit 9772f1c

Browse files
committed
Use StableRNGs for tests
1 parent 69f58f3 commit 9772f1c

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
66
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
88
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
9+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
910
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1011
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1112
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ using CategoricalArrays: CategoricalValue
66
using CategoricalArrays: categorical, levels
77
using Statistics: mean, std
88
using TimerOutputs: TimerOutputs, @timeit
9-
using Random: seed!
109

1110
const T = TuringGLM
1211

test/turing_model.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using StableRNGs: StableRNG
2+
13
@timed_testset "turing_model" begin
24
DATA_DIR = joinpath("..", "data")
35
kidiq = CSV.read(joinpath(DATA_DIR, "kidiq.csv"), DataFrame)
@@ -8,15 +10,15 @@
810
f = @formula(kid_score ~ mom_iq * mom_hs)
911
@testset "standardize=false" begin
1012
m = turing_model(f, kidiq)
11-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
13+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
1214
@test summarystats(chn)[, :mean] 31.80 atol = 2.0
1315
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.507 atol = 0.2
1416
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.22 atol = 0.2
1517
end
1618

1719
@testset "standardize=true" begin
1820
m = turing_model(f, kidiq; standardize=true)
19-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
21+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
2022
@test summarystats(chn)[, :mean] 0.000 atol = 0.2
2123
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.648 atol = 0.2
2224
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.849 atol = 0.2
@@ -25,14 +27,14 @@
2527
@testset "custom_priors" begin
2628
priors = CustomPrior(Normal(), Normal(28, 5), nothing)
2729
m = turing_model(f, kidiq; priors)
28-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
30+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
2931
@test summarystats(chn)[, :mean] 28.758 atol = 2.0
3032
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.539 atol = 0.2
3133
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.3863 atol = 0.2
3234
end
3335
@testset "explicit calling Normal" begin
3436
m = turing_model(f, kidiq; model=Normal)
35-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
37+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
3638
@test summarystats(chn)[, :mean] 31.80 atol = 2.0
3739
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.507 atol = 0.2
3840
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.22 atol = 0.2
@@ -42,7 +44,7 @@
4244
f = @formula(kid_score ~ mom_iq * mom_hs)
4345
@testset "standardize=false" begin
4446
m = turing_model(f, kidiq; model=TDist)
45-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
47+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
4648
@test summarystats(chn)[, :mean] 33.31 atol = 2.0
4749
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.519 atol = 0.2
4850
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.340 atol = 0.2
@@ -52,7 +54,7 @@
5254
@testset "custom_priors" begin
5355
priors = CustomPrior(Normal(), Normal(28, 5), Exponential(2))
5456
m = turing_model(f, kidiq; model=TDist, priors)
55-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
57+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
5658
@test summarystats(chn)[, :mean] 28.565 atol = 2.0
5759
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.551 atol = 0.2
5860
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.255 atol = 0.2
@@ -63,7 +65,7 @@
6365
f = @formula(switch ~ arsenic + dist + assoc + educ)
6466
@testset "standardize=false" begin
6567
m = turing_model(f, wells; model=Bernoulli)
66-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
68+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
6769
@test summarystats(chn)[, :mean] -0.153 atol = 0.2
6870
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.467 atol = 0.2
6971
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.009 atol = 0.2
@@ -72,7 +74,7 @@
7274
@testset "custom_priors" begin
7375
priors = CustomPrior(Normal(), Normal(), nothing)
7476
m = turing_model(f, wells; model=Bernoulli, priors)
75-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
77+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
7678
@test summarystats(chn)[, :mean] -0.155 atol = 0.2
7779
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.468 atol = 0.2
7880
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.009 atol = 0.2
@@ -82,7 +84,7 @@
8284
f = @formula(y ~ roach1 + treatment + senior + exposure2)
8385
@testset "standardize=false" begin
8486
m = turing_model(f, roaches; model=Poisson)
85-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
87+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
8688
@test summarystats(chn)[, :mean] 2.969 atol = 0.5
8789
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.006 atol = 0.2
8890
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.5145 atol = 0.2
@@ -91,7 +93,7 @@
9193
@testset "custom_priors" begin
9294
priors = CustomPrior(Normal(2, 5), Normal(), nothing)
9395
m = turing_model(f, roaches; model=Poisson, priors)
94-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
96+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
9597
@test summarystats(chn)[, :mean] 2.963 atol = 0.5
9698
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.006 atol = 0.2
9799
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.5145 atol = 0.2
@@ -101,7 +103,7 @@
101103
f = @formula(y ~ roach1 + treatment + senior + exposure2)
102104
@testset "standardize=false" begin
103105
m = turing_model(f, roaches; model=NegativeBinomial)
104-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
106+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
105107
@test summarystats(chn)[, :mean] 2.448 atol = 0.5
106108
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.013 atol = 0.2
107109
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.734 atol = 0.2
@@ -111,7 +113,7 @@
111113
@testset "custom_priors" begin
112114
priors = CustomPrior(Normal(), Normal(2, 5), Exponential(0.5))
113115
m = turing_model(f, roaches; model=NegativeBinomial, priors)
114-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
116+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
115117
@test summarystats(chn)[, :mean] 2.401 atol = 0.5
116118
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.013 atol = 0.2
117119
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.723 atol = 0.2
@@ -121,7 +123,7 @@
121123
@timed_testset "Hierarchical Model" begin
122124
f = @formula(y ~ (1 | cheese) + background)
123125
m = turing_model(f, cheese)
124-
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
126+
chn = sample(StableRNG(123), m, NUTS(), MCMCThreads(), 2_000, 2)
125127
@test summarystats(chn)[, :mean] 68.07 atol = 2.0
126128
@test summarystats(chn)[Symbol("β[1]"), :mean] 6.60 atol = 0.2
127129
@test summarystats(chn)[Symbol("zⱼ[1]"), :mean] 0.348 atol = 0.2

0 commit comments

Comments
 (0)