diff --git a/.github/workflows/models.yml b/.github/workflows/models.yml new file mode 100644 index 0000000..887ba54 --- /dev/null +++ b/.github/workflows/models.yml @@ -0,0 +1,47 @@ +name: Test Models +on: + push: + branches: + - master + - main + pull_request: + branches: + - master + - main + +jobs: + test-turing: + name: Test Turing Models + runs-on: ubuntu-latest + env: + JULIA_NUM_THREADS: "auto" + strategy: + matrix: + jl-file: [ + "01-predictive_checks.jl", + "02-linear_regression-kidiq.jl", + "03-logistic_regression-wells.jl", + "04-ordinal_regression-esoph.jl", + "05-poisson_regression-roaches.jl", + "06-robust_linear_regression-duncan.jl", + "07-robust_beta_binomial_regression-wells.jl", + #"08-robust_robit_regression-wells.jl", # FIXME: broken + "09-robust_negative_binomial_regression-roaches.jl", + "10-robust_zero_inflated_regression-negative_binomial-roaches.jl", + "10-robust_zero_inflated_regression-poisson-roaches.jl", + "11-hierarchical_varying_intercept-cheese.jl", + "11-hierarchical_varying_intercept-non_centered-cheese.jl", + "12-hierarchical_varying_intercept_slope-cheese.jl", + "13-model_comparison-roaches.jl", + ] + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v1 + with: + version: "1" + - uses: julia-actions/cache@v1 + - name: Test ${{ matrix.jl-file }} + run: | + julia --project=turing -e 'using Pkg; Pkg.instantiate()' + julia --project=turing turing/${{ matrix.jl-file }} diff --git a/flake.nix b/flake.nix index 457f94b..7ac8963 100644 --- a/flake.nix +++ b/flake.nix @@ -39,7 +39,11 @@ julia ]; - inherit (self.checks.${system}.pre-commit-check) shellHook; + shellHook = '' + export JULIA_NUM_THREADS="auto" + export JULIA_PROJECT="turing" + ${self.checks.${system}.pre-commit-check.shellHook} + ''; }; packages.default = pkgs.stdenvNoCC.mkDerivation rec { name = "slides"; diff --git a/turing/04-ordinal_regression-esoph.jl b/turing/04-ordinal_regression-esoph.jl index bcf7c42..179abcf 100644 --- a/turing/04-ordinal_regression-esoph.jl +++ b/turing/04-ordinal_regression-esoph.jl @@ -1,9 +1,10 @@ using Turing -using Bijectors using CSV using DataFrames using LinearAlgebra using CategoricalArrays +using Bijectors: transformed, OrderedBijector +using DataFrames: transform! # reproducibility using Random: seed! @@ -36,7 +37,7 @@ y = esoph[:, :tobgp] # define the model @model function ordered_regression(X, y; predictors=size(X, 2), ncateg=maximum(y)) # priors - cutpoints ~ Bijectors.ordered(filldist(TDist(3) * 5, ncateg - 1)) + cutpoints ~ transformed(filldist(TDist(3) * 5, ncateg - 1), OrderedBijector()) β ~ filldist(TDist(3) * 2.5, predictors) # likelihood diff --git a/turing/07-robust_beta_binomial_regression-wells.jl b/turing/07-robust_beta_binomial_regression-wells.jl index dae50b7..16e3279 100644 --- a/turing/07-robust_beta_binomial_regression-wells.jl +++ b/turing/07-robust_beta_binomial_regression-wells.jl @@ -24,7 +24,14 @@ X = standardize(ZScoreTransform, X; dims=1) y = wells[:, :switch] # define alternate parameterizations -BetaBinomial2(n, μ, ϕ) = BetaBinomial(n, μ * ϕ, (1 - μ) * ϕ) +function BetaBinomial2(n, μ, ϕ) + α = μ * ϕ + β = (1 - μ) * ϕ + α = α > 0 ? α : 1e-4 # numerical stability + β = β > 0 ? β : 1e-4 # numerical stability + + return BetaBinomial(n, α, β) +end # define the model @model function beta_binomial_regression(X, y; predictors=size(X, 2)) diff --git a/turing/08-robust_robit_regression-wells.jl b/turing/08-robust_robit_regression-wells.jl index c97dce8..35210aa 100644 --- a/turing/08-robust_robit_regression-wells.jl +++ b/turing/08-robust_robit_regression-wells.jl @@ -22,7 +22,7 @@ y = wells[:, :switch] # define the model # setting ν to 4 since we have a lot of parameters -# and Turing v0.21.9 samples slower than Stan +# and Turing v0.29 samples slower than Stan @model function robit_regression(X, y; predictors=size(X, 2), N=size(X, 1), ν=4) # priors α ~ TDist(3) * 2.5 diff --git a/turing/13-model_comparison-roaches.jl b/turing/13-model_comparison-roaches.jl index d67ccf5..579f51d 100644 --- a/turing/13-model_comparison-roaches.jl +++ b/turing/13-model_comparison-roaches.jl @@ -1,3 +1,10 @@ +# FIXME: ParetoSmooth is not compatible with Turing 0.29 +using Pkg +Pkg.compat("Turing", "0.22") +Pkg.compat("Bijectors", "0.10") +Pkg.compat("StatsBase", "0.33") +Pkg.add("ParetoSmooth") + using Turing using CSV using DataFrames diff --git a/turing/Project.toml b/turing/Project.toml index ecf48ae..915626f 100644 --- a/turing/Project.toml +++ b/turing/Project.toml @@ -5,7 +5,6 @@ CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02" PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150" -ParetoSmooth = "a68b5a21-f429-434e-8bfa-46b447300aac" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" TransformVariables = "84d833dd-6860-57f9-a1a7-6da5db126cff" @@ -18,7 +17,6 @@ CategoricalArrays = "0.10" DataFrames = "1" LazyArrays = "1" PDMats = "0.11" -ParetoSmooth = "0.7" StatsBase = "0.34" StatsFuns = "1" TransformVariables = "0.8"