stan-fundamentals
Foundational knowledge for writing Stan 2.37 models including program structure, type system, distributions, and best practices. Use when creating or reviewing Stan models.
What this skill does
# 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 ~ neg_binomial_2(mu, phi);
y ~ categorical(theta);
```
### Multivariate
```stan
y ~ multi_normal(mu, Sigma); // Sigma is COVARIANCE
y ~ multi_normal_cholesky(mu, L);
y ~ lkj_corr(eta);
```
## Essential Patterns
### Vectorization
```stan
// GOOD - Efficient
y ~ normal(mu, sigma);
// BAD - Slow
for (n in 1:N) y[n] ~ normal(mu[n], sigma);
```
### Non-Centered Parameterization
```stan
parameters {
vector[J] theta_raw;
}
transformed parameters {
vector[J] theta = mu + tau * theta_raw;
}
model {
theta_raw ~ std_normal();
}
```
### Target Syntax
```stan
// These are equivalent:
y ~ normal(mu, sigma);
target += normal_lpdf(y | mu, sigma);
```
## Common Priors
```stan
// Location parameters
mu ~ normal(0, 10);
// Scale parameters
sigma ~ exponential(1);
sigma ~ cauchy(0, 2.5); // half-Cauchy when sigma > 0
// Probabilities
theta ~ beta(1, 1); // Uniform on (0,1)
// Regression coefficients
beta ~ normal(0, 2.5);
// Correlation matrices
Omega ~ lkj_corr(2); // eta=2 favors identity
```
## R Integration (cmdstanr)
```r
library(cmdstanr)
mod <- cmdstan_model("model.stan")
fit <- mod$sample(data = stan_data, chains = 4)
fit$summary()
fit$cmdstan_diagnose()
```
## Bayesian Workflow (Statistical Rethinking)
### 1. Prior Predictive Check
```r
# Simulate from priors before fitting
n_sim <- 1000
prior_alpha <- rnorm(n_sim, 0, 10)
prior_sigma <- rexp(n_sim, 1)
# Plot: do these produce sensible y values?
```
### 2. Fit Model
```r
fit <- mod$sample(data = stan_data, chains = 4, adapt_delta = 0.95)
```
### 3. Diagnostics
```r
fit$summary() # Rhat, ESS
fit$cmdstan_diagnose() # Divergences, treedepth
library(bayesplot)
mcmc_rank_hist(fit$draws()) # Ranked traceplots (preferred)
```
### 4. Posterior Predictive Check
```r
y_rep <- fit$draws("y_rep", format = "matrix")
library(bayesplot)
ppc_dens_overlay(y, y_rep[1:100, ])
```
### 5. Model Comparison
```r
library(loo)
loo1 <- loo(fit1$draws("log_lik"))
loo2 <- loo(fit2$draws("log_lik"))
loo_compare(loo1, loo2)
```
## link vs sim Pattern
### link(): Uncertainty in mu (epistemic)
```r
# Posterior of expected value
post <- fit$draws(format = "df")
mu <- post$alpha + post$beta * x_new # Matrix of mu samples
mu_PI <- apply(mu, 2, quantile, c(0.055, 0.945))
```
### sim(): Prediction interval (epistemic + aleatoric)
```r
# Includes observation noise
y_sim <- rnorm(n_samples, mu, post$sigma)
y_PI <- apply(y_sim, 2, quantile, c(0.055, 0.945))
```
## Generated Quantities Template
Always include for diagnostics and model comparison:
```stan
generated quantities {
vector[N] log_lik; // For LOO/WAIC
array[N] real y_rep; // For posterior predictive checks
for (n in 1:N) {
log_lik[n] = normal_lpdf(y[n] | mu[n], sigma);
y_rep[n] = normal_rng(mu[n], sigma);
}
}
```
## Diagnostic Checklist
- [ ] Rhat < 1.01 for all parameters
- [ ] ESS_bulk > 400
- [ ] ESS_tail > 400
- [ ] Zero divergences
- [ ] Not hitting max_treedepth
- [ ] Prior predictive produces sensible values
- [ ] Posterior predictive matches data pattern
## Key Differences from BUGS
| Feature | Stan | BUGS/JAGS |
|---------|------|-----------|
| Normal | `normal(mu, sigma)` SD | `dnorm(mu, tau)` precision |
| MVN | `multi_normal(mu, Sigma)` cov | `dmnorm(mu, Omega)` precision |
| Execution | Sequential (order matters) | Declarative (order doesn't matter) |
| Sampling | HMC/NUTS | Gibbs/Metropolis |
Related in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.