Best practices for writing efficient, clean Stan programs
Writes efficient, clean Stan programs following canonical structure and vectorization patterns. Triggers when you create or modify Stan code, especially for parameterization, distribution statements, or ArviZ integration.
/plugin marketplace add sunxd3/bayesian-statistician-plugin/plugin install sunxd3-bayesian-statistician@sunxd3/bayesian-statistician-pluginThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Use this skill when writing or modifying Stan programs to ensure clean, efficient code.
Use canonical block order: functions, data, transformed data, parameters, transformed parameters, model, generated quantities.
Follow Stan style:
for (n in 1:N) {sigma_y, mu_group)N, K, J)Use appropriate types:
matrix, vector, row_vector with matrix operations (x * beta)array[N] real y (not legacy real y[N])array[M] row_vector[N] x over matrix[M, N]tuple(...) for multiple valuessum_to_zero_vector, sum_to_zero_matrix instead of manual constraintsMemory layout: matrices are column-major, arrays are row-major.
Always use log form:
y ~ normal(mu, sigma) or target += normal_lpdf(y | mu, sigma)y ~ normal(mu, sigma) for arrays, not loopsbernoulli_logit_glm, poisson_log_glm, normal_id_glmmu = X * beta once, reuselog_sum_exp on log scaleUse constrained types over manual checks:
<lower=0>, <upper=...>, ordered, positive_ordered, simplex, unit_vectorcholesky_factor_corr[K] L_Omega with multi_normal_choleskyFor custom transforms, use built-in *_constrain, *_unconstrain, *_jacobian functions.
For large-N models with independent terms, use reduce_sum:
Modularize complex logic in functions block:
tuple returns for multiple heterogeneous outputsBoth compilation and sampling can crash or OOM.
Defensive Stan patterns:
int<lower=1, upper=K> id[N]log, sqrt, divisionreal<lower=0.01> phi (never exactly 0)Execution:
CmdStanModel() and model.sample() in try-exceptOn crash/OOM:
parallel_chains (4 → 2 → 1)max_treedepth (10 → 8)Design Stan programs for downstream ArviZ workflow:
Generated quantities:
vector[N] log_lik - required for model comparison and downstream workflowvector[N] y_rep - required for all predictive checkslog_lik_y1, log_lik_y2Transformed parameters:
vector[N] mu = alpha + X * beta)Extending without refitting:
generate_quantities mode with original posterior drawsmodel.generate_quantities(data=data, mcmc_sample=fit) - orders of magnitude faster than refittingSave and cache:
az.from_cmdstanpy(fit, log_likelihood="log_lik", posterior_predictive=["y_rep"])idata.to_netcdf("posterior.nc") - makes all downstream analysis instantIf stuck on Stan patterns or ArviZ usage, search these resources:
Use WebSearch or WebFetch to find specific examples.
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code, generative art, algorithmic art, flow fields, or particle systems. Create original algorithmic art rather than copying existing artists' work to avoid copyright violations.
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.
Create beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations.