Skip to content

Commit 3869546

Browse files
Fix residual calculation for default priors (#78)
* Update turing_model.jl * fix residual calculation * updating tests after change in residual definition Co-authored-by: Jose Storopoli <[email protected]>
1 parent cbcb0d0 commit 3869546

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/turing_model.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function _model(μ_X, σ_X, prior, intercept_ranef, idx, ::Type{Normal})
177177
μ_X=μ_X,
178178
σ_X=σ_X,
179179
prior=prior,
180-
residual=1 / std(y),
180+
residual=std(y),
181181
mad_y=mad(y; normalize=true),
182182
)
183183
α ~ prior.intercept
@@ -197,7 +197,7 @@ function _model(μ_X, σ_X, prior, intercept_ranef, idx, ::Type{Normal})
197197
end
198198
function _model(μ_X, σ_X, prior, ::Type{Normal})
199199
@model function normal_model(
200-
y, X; predictors=size(X, 2), μ_X=μ_X, σ_X=σ_X, prior=prior, residual=1 / std(y)
200+
y, X; predictors=size(X, 2), μ_X=μ_X, σ_X=σ_X, prior=prior, residual=std(y)
201201
)
202202
α ~ prior.intercept
203203
β ~ filldist(prior.predictors, predictors)
@@ -219,7 +219,7 @@ function _model(μ_X, σ_X, prior, intercept_ranef, idx, ::Type{TDist})
219219
μ_X=μ_X,
220220
σ_X=σ_X,
221221
prior=prior,
222-
residual=1 / std(y),
222+
residual=std(y),
223223
mad_y=mad(y; normalize=true),
224224
)
225225
α ~ prior.intercept
@@ -240,7 +240,7 @@ function _model(μ_X, σ_X, prior, intercept_ranef, idx, ::Type{TDist})
240240
end
241241
function _model(μ_X, σ_X, prior, ::Type{TDist})
242242
@model function student_model(
243-
y, X; predictors=size(X, 2), μ_X=μ_X, σ_X=σ_X, prior=prior, residual=1 / std(y)
243+
y, X; predictors=size(X, 2), μ_X=μ_X, σ_X=σ_X, prior=prior, residual=std(y)
244244
)
245245
α ~ prior.intercept
246246
β ~ filldist(prior.predictors, predictors)

test/turing_model.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
@testset "standardize=false" begin
1010
m = turing_model(f, kidiq)
1111
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
12-
@test summarystats(chn)[, :mean] 29.30 atol = 2.0
13-
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.533 atol = 0.2
14-
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.593 atol = 0.2
12+
@test summarystats(chn)[, :mean] 31.80 atol = 2.0
13+
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.507 atol = 0.2
14+
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.22 atol = 0.2
1515
end
1616

1717
@testset "standardize=true" begin
@@ -33,30 +33,30 @@
3333
@testset "explicit calling Normal" begin
3434
m = turing_model(f, kidiq; model=Normal)
3535
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
36-
@test summarystats(chn)[, :mean] 29.30 atol = 2.0
37-
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.533 atol = 0.2
38-
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.593 atol = 0.2
36+
@test summarystats(chn)[, :mean] 31.80 atol = 2.0
37+
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.507 atol = 0.2
38+
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.22 atol = 0.2
3939
end
4040
end
4141
@timed_testset "TDist Model" begin
4242
f = @formula(kid_score ~ mom_iq * mom_hs)
4343
@testset "standardize=false" begin
4444
m = turing_model(f, kidiq; model=TDist)
4545
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
46-
@test summarystats(chn)[, :mean] 40.380 atol = 2.0
47-
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.478 atol = 0.2
48-
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.736 atol = 0.2
49-
@test quantile(chn)[, Symbol("50.0%")] 1.039 atol = 0.5
46+
@test summarystats(chn)[, :mean] 33.31 atol = 2.0
47+
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.519 atol = 0.2
48+
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.340 atol = 0.2
49+
@test quantile(chn)[, Symbol("50.0%")] 2.787 atol = 0.5
5050
end
5151

5252
@testset "custom_priors" begin
5353
priors = CustomPrior(Normal(), Normal(28, 5), Exponential(2))
5454
m = turing_model(f, kidiq; model=TDist, priors)
5555
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
56-
@test summarystats(chn)[, :mean] 35.506 atol = 2.0
57-
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.522 atol = 0.2
58-
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.628 atol = 0.2
59-
@test quantile(chn)[, Symbol("50.0%")] 1.178 atol = 0.5
56+
@test summarystats(chn)[, :mean] 28.565 atol = 2.0
57+
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.551 atol = 0.2
58+
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.255 atol = 0.2
59+
@test quantile(chn)[, Symbol("50.0%")] 10.339 atol = 0.5
6060
end
6161
end
6262
@timed_testset "Bernoulli Model" begin
@@ -112,20 +112,20 @@
112112
priors = CustomPrior(Normal(), Normal(2, 5), Exponential(0.5))
113113
m = turing_model(f, roaches; model=NegativeBinomial, priors)
114114
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
115-
@test summarystats(chn)[, :mean] 2.422 atol = 0.5
115+
@test summarystats(chn)[, :mean] 2.401 atol = 0.5
116116
@test summarystats(chn)[Symbol("β[1]"), :mean] 0.013 atol = 0.2
117-
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.732 atol = 0.2
117+
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.723 atol = 0.2
118118
@test quantile(chn)[:ϕ⁻, Symbol("50.0%")] 3.56 atol = 0.2
119119
end
120120
end
121121
@timed_testset "Hierarchical Model" begin
122122
f = @formula(y ~ (1 | cheese) + background)
123123
m = turing_model(f, cheese)
124124
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)
125-
@test summarystats(chn)[, :mean] 68.33 atol = 2.0
126-
@test summarystats(chn)[Symbol("β[1]"), :mean] 6.928 atol = 0.2
127-
@test summarystats(chn)[Symbol("zⱼ[1]"), :mean] 0.306 atol = 0.2
128-
@test quantile(chn)[Symbol("zⱼ[2]"), Symbol("50.0%")] -1.422 atol = 0.5
125+
@test summarystats(chn)[, :mean] 68.07 atol = 2.0
126+
@test summarystats(chn)[Symbol("β[1]"), :mean] 6.60 atol = 0.2
127+
@test summarystats(chn)[Symbol("zⱼ[1]"), :mean] 0.348 atol = 0.2
128+
@test quantile(chn)[Symbol("zⱼ[2]"), Symbol("50.0%")] -1.376 atol = 0.5
129129
end
130130
@testset "Unsupported Model Likelihoods" begin
131131
@test_throws ArgumentError turing_model(@formula(y ~ x), nt_str; model=Binomial)

0 commit comments

Comments
 (0)