Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

ci: test turing models #53

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/models.yml
Original file line number Diff line number Diff line change
@@ -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 }}
6 changes: 5 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
5 changes: 3 additions & 2 deletions turing/04-ordinal_regression-esoph.jl
Original file line number Diff line number Diff line change
@@ -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!
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion turing/07-robust_beta_binomial_regression-wells.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion turing/08-robust_robit_regression-wells.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions turing/13-model_comparison-roaches.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions turing/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down