Stan introduction

This notebook is a minimal introduction to Stan using linear regression as an example.

data {
    int n, p;
    matrix [n, p] X;
    vector[n] y;
}

parameters {
    vector[p] theta;
    real<lower=0> sigma;
}

model {
    y ~ normal(X * theta, sigma);
    theta ~ normal(0, 1);
    sigma ~ gamma(2, 2);
}
import numpy as np

np.random.seed(0)
n = 100
p = 3
X = np.random.normal(0, 1, (n, p))
theta = np.random.normal(0, 1, p)
sigma = np.random.gamma(2, 2)
y = np.random.normal(X @ theta, sigma)

print(f"coefficients: {theta.round(3)}")
print(f"observation noise scale: {sigma:.3f}")
coefficients: [-1.307  1.658 -0.118]
observation noise scale: 1.867
import cmdstanpy

model = cmdstanpy.CmdStanModel(stan_file="stan_intro.stan")
fit = model.sample(data={"n": n, "p": p, "X": X, "y": y}, seed=0)
print(fit.diagnose())
print(fit.summary()[["5%", "50%", "95%"]].round(3))
20:11:47 - cmdstanpy - INFO - compiling stan file /home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan to exe file /home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro
20:12:08 - cmdstanpy - INFO - compiled model executable: /home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro
20:12:08 - cmdstanpy - INFO - CmdStan start processing
                                                                                
                                                                                
                                                                                
                                                                                
20:12:09 - cmdstanpy - INFO - CmdStan done processing.
20:12:09 - cmdstanpy - WARNING - Non-fatal error during sampling:
Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
	Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
	Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
	Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
	Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
	Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
	Exception: gamma_lpdf: Random variable is inf, but must be positive finite! (in '/home/docs/checkouts/readthedocs.org/user_builds/gptools-stan/checkouts/latest/stan/docs/stan_intro/stan_intro.stan', line 15, column 4 to column 24)
Consider re-running with show_console=True if the above output is unclear!
Processing csv files: /tmp/tmpsulwqsdo/stan_introetmt8uoa/stan_intro-20230914201208_1.csv, /tmp/tmpsulwqsdo/stan_introetmt8uoa/stan_intro-20230914201208_2.csv, /tmp/tmpsulwqsdo/stan_introetmt8uoa/stan_intro-20230914201208_3.csv, /tmp/tmpsulwqsdo/stan_introetmt8uoa/stan_intro-20230914201208_4.csv

Checking sampler transitions treedepth.
Treedepth satisfactory for all transitions.

Checking sampler transitions for divergences.
No divergent transitions found.

Checking E-BFMI - sampler transitions HMC potential energy.
E-BFMI satisfactory.

Effective sample size satisfactory.

Split R-hat values satisfactory all parameters.

Processing complete, no problems detected.

               5%      50%      95%
lp__     -124.329 -121.071 -119.731
theta[1]   -1.532   -1.229   -0.932
theta[2]    1.429    1.748    2.074
theta[3]   -0.387   -0.064    0.254
sigma       1.725    1.923    2.176