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

Commit

Permalink
ci: test stan models (#54)
Browse files Browse the repository at this point in the history
* ci: test stan models

* feat(stan): kidiq.data.json

* feat(stan): wells.data.json

* feat(stan): esoph.data.json

* feat(stan): roaches.data.json

* feat(stan): cheese.data.json

* ci: fix stan data command

* fix(stan): 02-linear_regression_QR

* fix(stan): 08-robust_robit_regression

* fix(stan): 10-robust_zero_inflated_regression poisson opt

* fix(stan): invalid operator in 11 ncp

* fix(stan): 01 prior/posterior checks data file

* fix(readme): note on CI
  • Loading branch information
storopoli committed Dec 3, 2023
1 parent 92b6fbc commit cf4aa81
Show file tree
Hide file tree
Showing 14 changed files with 7,329 additions and 19 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,111 @@ jobs:
run: |
julia --project=turing -e 'using Pkg; Pkg.instantiate()'
julia --project=turing turing/${{ matrix.jl-file }}
test-stan:
name: Test Stan Models
runs-on: ubuntu-latest
strategy:
matrix:
stan:
[
{
model: "01-predictive_checks-posterior",
data: "coin_flip.data.json",
},
{
model: "01-predictive_checks-prior",
data: "coin_flip.data.json",
},
{ model: "02-linear_regression-QR", data: "kidiq.data.json" },
{
model: "02-linear_regression-optimized",
data: "kidiq.data.json",
},
{ model: "02-linear_regression", data: "kidiq.data.json" },
{
model: "03-logistic_regression-optimized",
data: "wells.data.json",
},
{ model: "03-logistic_regression", data: "wells.data.json" },
{
model: "04-ordinal_regression-optimized",
data: "esoph.data.json",
},
{ model: "04-ordinal_regression", data: "esoph.data.json" },
{
model: "05-poisson_regression-optimized",
data: "roaches.data.json",
},
{ model: "05-poisson_regression", data: "roaches.data.json" },
{ model: "06-robust_linear_regression", data: "kidiq.data.json" },
{
model: "07-robust_beta_binomial_regression",
data: "wells.data.json",
},
{ model: "08-robust_robit_regression", data: "wells.data.json" },
{
model: "09-robust_negative_binomial_regression-optimized",
data: "roaches.data.json",
},
{
model: "09-robust_negative_binomial_regression",
data: "roaches.data.json",
},
{
model: "10-robust_zero_inflated_regression-negative_binomial-optimized",
data: "roaches.data.json",
},
{
model: "10-robust_zero_inflated_regression-negative_binomial",
data: "roaches.data.json",
},
{
model: "10-robust_zero_inflated_regression-poisson-optimized",
data: "roaches.data.json",
},
{
model: "10-robust_zero_inflated_regression-poisson",
data: "roaches.data.json",
},
{
model: "11-hierarchical_varying_intercept-non_centered",
data: "cheese.data.json",
},
{
model: "11-hierarchical_varying_intercept",
data: "cheese.data.json",
},
{
model: "12-hierarchical_varying_intercept_slope",
data: "cheese.data.json",
},
{
model: "13-model_comparison-negative_binomial",
data: "roaches.data.json",
},
{ model: "13-model_comparison-poisson", data: "roaches.data.json" },
{
model: "13-model_comparison-zero_inflated-negative_binomial",
data: "roaches.data.json",
},
{
model: "13-model_comparison-zero_inflated-poisson",
data: "roaches.data.json",
},
]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v8

- name: Cache Nix artifacts
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Test ${{ matrix.stan.model }}
run: |
echo "Compiling: ${{ matrix.stan.model }}"
nix develop -L . --command bash -c "stan stan/${{ matrix.stan.model }}"
nix develop -L . --command bash -c "stan/${{ matrix.stan.model }} sample data file=stan/${{ matrix.stan.data }}"
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ The whole content is a set of several slides found at [`the latest release`](htt
Here is a brief table of contents:

1. **What is Bayesian Statistics?**
2. **Common Probability Distributions**
3. **Priors**
4. **Predictive Checks**
5. **Bayesian Linear Regression**
6. **Bayesian Logistic Regression**
7. **Bayesian Ordinal Regression**
8. **Bayesian Regression with Count Data: Poisson Regression**
9. **Robust Bayesian Regression**
10. **Hierarchical Models**
11. **Markov Chain Monte Carlo (MCMC) and Model Metrics**
12. **Model Comparison: Cross-Validation and Other Metrics**
1. **Common Probability Distributions**
1. **Priors**
1. **Predictive Checks**
1. **Bayesian Linear Regression**
1. **Bayesian Logistic Regression**
1. **Bayesian Ordinal Regression**
1. **Bayesian Regression with Count Data: Poisson Regression**
1. **Robust Bayesian Regression**
1. **Hierarchical Models**
1. **Markov Chain Monte Carlo (MCMC) and Model Metrics**
1. **Model Comparison: Cross-Validation and Other Metrics**

## Probabilistic Programming Languages (PPLs)

Along with slides for the content, this repository also holds `Stan` code and also `Turing.jl` code for all models.
`Stan` and `Turing.jl` represents, respectively, the present and future of [probabilistic programming](https://en.wikipedia.org/wiki/Probabilistic_programming) languages.

All model files are tested in [GitHub Actions](https://github.com/storopoli/Bayesian-Statistics/actions/workflows/models.yml)
against the latest `Stan` and Julia/`Turing.jl` versions.

### `Stan`

[**`Stan`**](https://mc-stan.org) (Carpenter et al., 2017) `Stan` is a state-of-the-art platform for statistical modeling and high-performance statistical computation.
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
shellHook = ''
export JULIA_NUM_THREADS="auto"
export JULIA_PROJECT="turing"
export CMDSTAN_HOME="${pkgs.cmdstan}/opt/cmdstan"
${self.checks.${system}.pre-commit-check.shellHook}
'';
};
Expand Down
4 changes: 1 addition & 3 deletions stan/02-linear_regression-QR.stan
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ generated quantities {
vector[K] beta;
beta = R_ast_inverse * theta; // coefficients on X scale
}
// results:All 4 chains finished successfully.
Mean chain execution time: 0.2 seconds.
Total execution time: 0.2 seconds.
// results:
//All 4 chains finished successfully.
//Mean chain execution time: 0.2 seconds.
//Total execution time: 0.2 seconds.
Expand Down
2 changes: 1 addition & 1 deletion stan/03-logistic_regression-optimized.stan
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ model {

// likelihood
y ~ bernoulli_logit_glm(X, alpha, beta);
// no binomial_logit_glm available in Stan <=2.29
// no binomial_logit_glm available in Stan 2.33
}
// results:
//All 4 chains finished successfully.
Expand Down
2 changes: 1 addition & 1 deletion stan/08-robust_robit_regression.stan
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ model {

// likelihood
vector[N] p_hat;
for (n in 1:N) p_hat[n] = student_t_cdf(alpha + X[n] * beta, nu, 0, sqrt((nu - 2)/nu));
for (n in 1:N) p_hat[n] = student_t_cdf(alpha + X[n] * beta | nu, 0, sqrt((nu - 2)/nu));
y ~ bernoulli(p_hat);
}
// results:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ model {
if (y[n] == 0) {
target += log_sum_exp(bernoulli_lpmf(1 | gamma),
bernoulli_lpmf(0 | gamma) +
poisson_log_glm_lpmf(y[n] | X[n], alpha, beta));
poisson_log_glm_lpmf(y[n] | to_matrix(X[n]), alpha, beta));
} else {
target += bernoulli_lpmf(0 | gamma) +
poisson_log_glm_lpmf(y[n] | X[n], alpha, beta);
poisson_log_glm_lpmf(y[n] | to_matrix(X[n]), alpha, beta);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stan/11-hierarchical_varying_intercept-non_centered.stan
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parameters {
vector[J] z; // group-level non-centered intercepts
}
transformed parameters {
vector[J] alpha_j = z * tau .+ alpha; // group-level intercepts
vector[J] alpha_j = z * tau + alpha; // group-level intercepts
}
model {
// priors
Expand Down
Loading

0 comments on commit cf4aa81

Please sign in to comment.