Foundational knowledge for writing Stan 2.37 models including program structure, type system, distributions, and best practices. Use when creating or reviewing Stan models.
View on GitHubchoxos/BayesianAgent
bayesian-modeling
plugins/bayesian-modeling/skills/stan-fundamentals/SKILL.md
January 21, 2026
Select agents to install to:
npx add-skill https://github.com/choxos/BayesianAgent/blob/main/plugins/bayesian-modeling/skills/stan-fundamentals/SKILL.md -a claude-code --skill stan-fundamentalsInstallation paths:
.claude/skills/stan-fundamentals/# Stan Fundamentals
## When to Use This Skill
- Writing new Stan models from scratch
- Understanding Stan program structure
- Learning Stan syntax and conventions
- Translating models from other languages to Stan
- Optimizing existing Stan code
## Program Structure
Stan models have up to 7 blocks in this exact order:
```stan
functions { } // User-defined functions
data { } // Input data declarations
transformed data { } // Data preprocessing
parameters { } // Model parameters
transformed parameters { } // Derived parameters
model { } // Log probability
generated quantities { } // Posterior predictions
```
All blocks are optional. Empty string is valid (but useless) Stan program.
## Type System Quick Reference
### Scalars
```stan
int n; // Integer
real x; // Real number
complex z; // Complex number
```
### Vectors and Matrices
```stan
vector[N] v; // Column vector
row_vector[N] r; // Row vector
matrix[M, N] A; // Matrix
```
### Arrays (Modern Syntax)
```stan
array[N] real x; // 1D array of reals
array[M, N] int y; // 2D array of integers
array[J] vector[K] theta; // Array of vectors
```
### Constrained Types
```stan
real<lower=0> sigma; // Non-negative
real<lower=0, upper=1> p; // Probability
simplex[K] theta; // Sums to 1
ordered[K] c; // Ascending
corr_matrix[K] Omega; // Correlation
cov_matrix[K] Sigma; // Covariance
cholesky_factor_corr[K] L_Omega; // Cholesky correlation
```
## Key Distributions
### Continuous (SD parameterization!)
```stan
y ~ normal(mu, sigma); // sigma is SD
y ~ student_t(nu, mu, sigma);
y ~ cauchy(mu, sigma);
y ~ exponential(lambda);
y ~ gamma(alpha, beta);
y ~ beta(a, b);
y ~ lognormal(mu, sigma);
```
### Discrete
```stan
y ~ bernoulli(theta);
y ~ binomial(n, theta);
y ~ poisson(lambda);
y ~