From a4e6e4ea25e967fc0bf5f1c188a59490ba793b34 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Sat, 5 Mar 2022 12:44:47 +0100 Subject: [PATCH 1/3] Show timing and allocations for tests --- src/turing_model.jl | 26 +++++++++++++------------- test/Project.toml | 2 ++ test/data_constructors.jl | 2 +- test/priors.jl | 2 +- test/runtests.jl | 17 ++++++++++++++++- test/turing_model.jl | 14 +++++++------- test/utils.jl | 6 +++--- 7 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/turing_model.jl b/src/turing_model.jl index 9e87e252..36955f12 100644 --- a/src/turing_model.jl +++ b/src/turing_model.jl @@ -112,7 +112,8 @@ function turing_model( # Random-Effects Conditionals if has_ranef(formula) if priors isa DefaultPrior - custom_prior = CustomPrior(TDist(3), median(y) + mad(y) * TDist(3), nothing) + intercept = median(y) + mad(y; normalize=false) * TDist(3) + custom_prior = CustomPrior(TDist(3), intercept, nothing) else custom_prior = priors end @@ -138,7 +139,7 @@ function turing_model( σ ~ Exponential(residual) μ = α .+ X * β if !isempty(intercept_ranef) - τ ~ mad(y) * truncated(TDist(3), 0, Inf) + τ ~ mad(y; normalize=false) * truncated(TDist(3), 0, Inf) zⱼ ~ filldist(Normal(), n_gr) αⱼ = zⱼ .* τ μ .+= αⱼ[idxs] @@ -150,7 +151,8 @@ function turing_model( return normal_model_ranef(y, X) else if priors isa DefaultPrior - custom_prior = CustomPrior(TDist(3), median(y) + mad(y) * TDist(3), nothing) + intercept = median(y) + mad(y; normalize=false) * TDist(3) + custom_prior = CustomPrior(TDist(3), intercept, nothing) else custom_prior = priors end @@ -210,9 +212,8 @@ function turing_model( # Random-Effects Conditionals if has_ranef(formula) if priors isa DefaultPrior - custom_prior = CustomPrior( - TDist(3), median(y) + mad(y) * TDist(3), Gamma(2, 0.1) - ) + intercept = median(y) + mad(y; normalize=false) * TDist(3) + custom_prior = CustomPrior(TDist(3), intercept, Gamma(2, 0.1)) else custom_prior = priors end @@ -239,7 +240,7 @@ function turing_model( ν ~ prior.auxiliary μ = α .+ X * β if !isempty(intercept_ranef) - τ ~ 0 + mad(y) * truncated(TDist(3), 0, Inf) + τ ~ 0 + mad(y; normalize=false) * truncated(TDist(3), 0, Inf) zⱼ ~ filldist(Normal(), n_gr) αⱼ = zⱼ .* τ μ .+= αⱼ[idxs] @@ -251,9 +252,8 @@ function turing_model( return student_model_ranef(y, X) else if priors isa DefaultPrior - custom_prior = CustomPrior( - TDist(3), median(y) + mad(y) * TDist(3), Gamma(2, 0.1) - ) + intercept = median(y) + mad(y; normalize=false) * TDist(3) + custom_prior = CustomPrior(TDist(3), intercept, Gamma(2, 0.1)) else custom_prior = priors end @@ -331,7 +331,7 @@ function turing_model( β ~ filldist(prior.predictors, predictors) μ = α .+ X * β if !isempty(intercept_ranef) - τ ~ mad(y) * truncated(TDist(3), 0, Inf) + τ ~ mad(y; normalize=false) * truncated(TDist(3), 0, Inf) zⱼ ~ filldist(Normal(), n_gr) αⱼ = zⱼ .* τ μ .+= αⱼ[idxs] @@ -413,7 +413,7 @@ function turing_model( β ~ filldist(prior.predictors, predictors) μ = α .+ X * β if !isempty(intercept_ranef) - τ ~ mad(y) * truncated(TDist(3), 0, Inf) + τ ~ mad(y; normalize=false) * truncated(TDist(3), 0, Inf) zⱼ ~ filldist(Normal(), n_gr) αⱼ = zⱼ .* τ μ .+= αⱼ[idxs] @@ -497,7 +497,7 @@ function turing_model( ϕ = 1 / ϕ⁻ μ = α .+ X * β if !isempty(intercept_ranef) - τ ~ mad(y) * truncated(TDist(3), 0, Inf) + τ ~ mad(y; normalize=false) * truncated(TDist(3), 0, Inf) zⱼ ~ filldist(Normal(), n_gr) αⱼ = zⱼ .* τ μ .+= αⱼ[idxs] diff --git a/test/Project.toml b/test/Project.toml index c25ed297..55297e9c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -5,8 +5,10 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" [compat] CSV = "0.9, 1" CategoricalArrays = "0.10" DataFrames = "1" +TimerOutputs = "0.5" diff --git a/test/data_constructors.jl b/test/data_constructors.jl index 7e02ea72..eb86fd00 100644 --- a/test/data_constructors.jl +++ b/test/data_constructors.jl @@ -1,4 +1,4 @@ -@testset "data_constructors.jl" begin +@timed_testset "data_constructors" begin @testset "data_response" begin @testset "NamedTuples" begin expected = [2, 3, 4, 5] diff --git a/test/priors.jl b/test/priors.jl index 8fe74e4d..16672a41 100644 --- a/test/priors.jl +++ b/test/priors.jl @@ -1,4 +1,4 @@ -@testset "prior.jl" begin +@timed_testset "priors" begin @testset "types" begin @test DefaultPrior() isa T.Prior diff --git a/test/runtests.jl b/test/runtests.jl index fa3eb4f9..9c249ec1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,6 +5,7 @@ using DataFrames using CategoricalArrays: CategoricalValue using CategoricalArrays: categorical, levels using Statistics: mean, std +using TimerOutputs: TimerOutputs, @timeit using Random: seed! const T = TuringGLM @@ -29,9 +30,23 @@ df_str = DataFrame(nt_str) df_cat = DataFrame(nt_cat) -@testset "TuringGLM.jl" begin +# To capture time and allocation information. +const TIMEROUTPUT = TimerOutputs.TimerOutput() +macro timed_testset(str, block) + return quote + @timeit TIMEROUTPUT "$($(esc(str)))" begin + @testset "$($(esc(str)))" begin + $(esc(block)) + end + end + end +end + +@testset "TuringGLM" begin include("data_constructors.jl") include("utils.jl") include("priors.jl") include("turing_model.jl") end + +show(TIMEROUTPUT; compact=true, sortby=:firstexec) diff --git a/test/turing_model.jl b/test/turing_model.jl index 301334d8..f144e564 100644 --- a/test/turing_model.jl +++ b/test/turing_model.jl @@ -1,10 +1,10 @@ -@testset "turing_model.jl" begin +@timed_testset "turing_model" begin DATA_DIR = joinpath("..", "data") kidiq = CSV.read(joinpath(DATA_DIR, "kidiq.csv"), DataFrame) wells = CSV.read(joinpath(DATA_DIR, "wells.csv"), DataFrame) roaches = CSV.read(joinpath(DATA_DIR, "roaches.csv"), DataFrame) cheese = CSV.read(joinpath(DATA_DIR, "cheese.csv"), DataFrame) - @testset "Gaussian Model" begin + @timed_testset "Gaussian Model" begin f = @formula(kid_score ~ mom_iq * mom_hs) @testset "standardize=false" begin m = turing_model(f, kidiq) @@ -38,7 +38,7 @@ @test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] ≈ 0.593 atol = 0.2 end end - @testset "Student Model" begin + @timed_testset "Student Model" begin f = @formula(kid_score ~ mom_iq * mom_hs) @testset "standardize=false" begin m = turing_model(f, kidiq, Student()) @@ -59,7 +59,7 @@ @test quantile(chn)[:ν, Symbol("50.0%")] ≈ 1.178 atol = 0.5 end end - @testset "Logistic Model" begin + @timed_testset "Logistic Model" begin f = @formula(switch ~ arsenic + dist + assoc + educ) @testset "standardize=false" begin m = turing_model(f, wells, Logistic()) @@ -78,7 +78,7 @@ @test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] ≈ -0.009 atol = 0.2 end end - @testset "Pois Model" begin + @timed_testset "Pois Model" begin f = @formula(y ~ roach1 + treatment + senior + exposure2) @testset "standardize=false" begin m = turing_model(f, roaches, Pois()) @@ -97,7 +97,7 @@ @test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] ≈ -0.5145 atol = 0.2 end end - @testset "NegBin Model" begin + @timed_testset "NegBin Model" begin f = @formula(y ~ roach1 + treatment + senior + exposure2) @testset "standardize=false" begin m = turing_model(f, roaches, NegBin()) @@ -118,7 +118,7 @@ @test quantile(chn)[:ϕ⁻, Symbol("50.0%")] ≈ 3.56 atol = 0.2 end end - @testset "Hierarchical Model" begin + @timed_testset "Hierarchical Model" begin f = @formula(y ~ (1 | cheese) + background) m = turing_model(f, cheese) chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2) diff --git a/test/utils.jl b/test/utils.jl index ebfe5dad..bc4b9d8d 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,5 +1,5 @@ -@testset "utils" begin - @testset "center_predictors" begin +@timed_testset "utils" begin + @timed_testset "center_predictors" begin @testset "NamedTuples" begin f = @formula(y_int ~ x_float + x_cat) X = T.data_fixed_effects(f, nt_str) @@ -29,7 +29,7 @@ end end - @testset "standardize_predictors" begin + @timed_testset "standardize_predictors" begin @testset "NamedTuples" begin f = @formula(y_int ~ x_float + x_cat) y = T.data_response(f, nt_str) From 7c6a8e71974153aec3eb81dff8722ca4d7e44137 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Sat, 5 Mar 2022 12:49:00 +0100 Subject: [PATCH 2/3] Revert removal of newline --- src/turing_model.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/turing_model.jl b/src/turing_model.jl index fa378e03..cdf04390 100644 --- a/src/turing_model.jl +++ b/src/turing_model.jl @@ -320,6 +320,7 @@ function _model(μ_X, σ_X, prior, ::Type{Poisson}) return (; α, β, y) end end + # Models with NegativeBinomial likelihood function _model(μ_X, σ_X, prior, intercept_ranef, idx, ::Type{NegativeBinomial}) @model function negbin_model_ranef( From 187aa25f9b3ddcb2a6fe2230480083d39521a2a2 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Sat, 5 Mar 2022 20:45:43 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Jose Storopoli --- test/turing_model.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/turing_model.jl b/test/turing_model.jl index e8bdf592..38d349d9 100644 --- a/test/turing_model.jl +++ b/test/turing_model.jl @@ -38,7 +38,7 @@ @test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] ≈ 0.593 atol = 0.2 end end - @timed_testset "Student Model" begin + @timed_testset "TDist Model" begin f = @formula(kid_score ~ mom_iq * mom_hs) @testset "standardize=false" begin m = turing_model(f, kidiq; model=TDist) @@ -59,7 +59,7 @@ @test quantile(chn)[:ν, Symbol("50.0%")] ≈ 1.178 atol = 0.5 end end - @timed_testset "Logistic Model" begin + @timed_testset "Bernoulli Model" begin f = @formula(switch ~ arsenic + dist + assoc + educ) @testset "standardize=false" begin m = turing_model(f, wells; model=Bernoulli) @@ -78,7 +78,7 @@ @test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] ≈ -0.009 atol = 0.2 end end - @timed_testset "Pois Model" begin + @timed_testset "Poisson Model" begin f = @formula(y ~ roach1 + treatment + senior + exposure2) @testset "standardize=false" begin m = turing_model(f, roaches; model=Poisson) @@ -97,7 +97,7 @@ @test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] ≈ -0.5145 atol = 0.2 end end - @timed_testset "NegBin Model" begin + @timed_testset "NegativeBinomial Model" begin f = @formula(y ~ roach1 + treatment + senior + exposure2) @testset "standardize=false" begin m = turing_model(f, roaches; model=NegativeBinomial)