Hubbry Logo
Stan (software)Stan (software)Main
Open search
Stan (software)
Community hub
Stan (software)
logo
7 pages, 0 posts
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Stan (software)
Stan (software)
from Wikipedia
Stan
Original authorStan Development Team
Initial releaseAugust 30, 2012 (2012-08-30)
Stable release
2.37.0[1] Edit this on Wikidata / 2 September 2025; 5 months ago (2 September 2025)
Written inC++
Operating systemUnix-like, Microsoft Windows, Mac OS X
PlatformIntel x86 - 32-bit, x64
TypeStatistical package
LicenseNew BSD License
Websitemc-stan.org
Repository

Stan is a probabilistic programming language for statistical inference written in C++.[2] The Stan language is used to specify a (Bayesian) statistical model with an imperative program calculating the log probability density function.[2]

Stan is licensed under the New BSD License. Stan is named in honour of Stanislaw Ulam, pioneer of the Monte Carlo method.[2]

Stan was created by a development team consisting of 52 members[3] that includes Andrew Gelman, Bob Carpenter, Daniel Lee, Ben Goodrich, and others.

Example

[edit]

A simple linear regression model can be described as , where . This can also be expressed as . The latter form can be written in Stan as the following:

data {
  int<lower=0> N;
  vector[N] x;
  vector[N] y;
}
parameters {
  real alpha;
  real beta;
  real<lower=0> sigma;
}
model {
  y ~ normal(alpha + beta * x, sigma);
}

Interfaces

[edit]

The Stan language itself can be accessed through several interfaces:

In addition, higher-level interfaces are provided with packages using Stan as backend, primarily in the R language:[4]

  • rstanarm provides a drop-in replacement for frequentist models provided by base R and lme4 using the R formula syntax;
  • brms[5] provides a wide array of linear and nonlinear models using the R formula syntax;
  • prophet provides automated procedures for time series forecasting.

Algorithms

[edit]

Stan implements gradient-based Markov chain Monte Carlo (MCMC) algorithms for Bayesian inference, stochastic, gradient-based variational Bayesian methods for approximate Bayesian inference, and gradient-based optimization for penalized maximum likelihood estimation.

Automatic differentiation

[edit]

Stan implements reverse-mode automatic differentiation to calculate gradients of the model, which is required by HMC, NUTS, L-BFGS, BFGS, and variational inference.[2] The automatic differentiation within Stan can be used outside of the probabilistic programming language.

Usage

[edit]

Stan is used in fields including social science,[9] pharmaceutical statistics,[10] market research,[11] and medical imaging.[12]

See also

[edit]
  • PyMC is a probabilistic programming language in Python
  • ArviZ a Python library for Exploratory Analysis of Bayesian Models

References

[edit]

Further reading

[edit]
[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
Stan is a probabilistic programming language designed for specifying and fitting Bayesian statistical models, enabling users to define a joint probability distribution over parameters and data through a declarative syntax that compiles to efficient C++ code for inference. It supports full Bayesian inference via Markov chain Monte Carlo methods, such as the No-U-Turn Sampler (NUTS), as well as optimization for maximum likelihood estimation and variational inference for approximate Bayesian computation. Written primarily in C++, Stan leverages automatic differentiation to compute gradients and Hessians, facilitating high-performance computation for complex models ranging from simple regressions to hierarchical and spatiotemporal analyses. Developed by a core team led by statistician of , along with computational linguist Bob Carpenter and others including Matthew D. Hoffman, Daniel Lee, Ben Goodrich, and Jiqiang Guo, Stan emerged from collaborative efforts in statistical computing and was first released as version 1.0 in 2012. The project is open-source, hosted on under the stan-dev organization, and licensed under the new BSD license for its C++ core, with interfaces available under compatible licenses like GPLv3. By 2015, the development team had expanded to include over 40 contributors, reflecting its growing adoption in academia and industry for reproducible statistical analysis. Stan interfaces with multiple programming environments, including (via rstan), Python (via pystan), Julia, , and a standalone command-line tool (CmdStan), allowing seamless integration into diverse workflows for data scientists and researchers. Its math library provides robust support for probability distributions, linear algebra, and optimization, making it suitable for applications in fields such as , , social sciences, and where is essential. As of 2025, Stan continues to evolve with regular releases, such as version 2.37.0 in September 2025, incorporating enhancements for scalability and usability in modern computing environments.

Introduction and History

Development and Team

Stan was founded in 2012 by and a team of colleagues at , driven by the need to overcome limitations in existing (MCMC) tools like BUGS and JAGS, which relied on less efficient sampling methods and restrictive declarative languages. The initial development focused on creating an imperative language that could support more scalable and flexible , particularly through advanced techniques like . As of 2025, the project has approximately 84 contributors, including statisticians, computer scientists, and experts from various domains, with ongoing contributions coordinated through the Stan Development Team. Since its inception at Columbia, the team has expanded to include collaborators from multiple institutions worldwide, fostering a collaborative environment for maintenance and enhancements. In 2016, Stan became a fiscally sponsored project of NumFocus, a supporting open-source scientific computing, which provides infrastructural and financial backing to sustain the project's growth. Stan is released under the New BSD 3-clause License, a permissive that encourages broad collaboration by allowing modifications and redistribution with minimal restrictions, while requiring attribution to original contributors. This licensing choice has facilitated its evolution from early prototypes—initially focused on basic model specification and inference—to a comprehensive system with interfaces for multiple languages and robust capabilities.

Key Milestones and Versions

Stan was first released on August 30, 2012, as version 1.0, providing core (MCMC) capabilities for through its language. The project advanced significantly with , released on February 25, 2014, which introduced enhanced modeling features and improved performance for larger datasets, marking a shift toward more robust statistical applications. Subsequent releases built on this foundation, with version 2.7 in July 2015 adding automatic differentiation variational inference (ADVI) for faster approximate posterior estimation. Key community events began with the inaugural StanCon conference in 2016, held in , fostering collaboration among developers and users; annual conferences have since continued, including StanCon 2024 in and the planned StanCon 2026 in . In the 2020s, Stan saw increased compatibility with other probabilistic programming frameworks like PyMC through shared tools such as ArviZ for posterior analysis. Post-2020 versions emphasized , with within-chain parallelization introduced in Stan 2.23 in 2020 via the reduce_sum function for multi-threading support, and GPU acceleration enhanced through integration starting from version 2.21 in 2019 but refined in later releases for heterogeneous devices. By , developments included support for HistFactory models tailored for high-energy physics experiments, enabling more flexible histogram-based analyses. The latest stable release, version 2.37.0 on September 2, 2025, included memory efficiency improvements in the Pathfinder tool for variational .

Core Components

Probabilistic Modeling Language

Stan's probabilistic modeling language is a declarative designed for specifying complex statistical models, particularly those amenable to . It allows users to define the of data and parameters in a structured, block-based format, emphasizing model declaration over procedural simulation or . This approach facilitates clear separation of model components, enabling efficient compilation into for algorithms. The language organizes model specifications into six core blocks, each serving a distinct purpose in the modeling . The data block declares input variables, such as observed data, with constraints like non-negativity (e.g., int<lower=0> N; array[N] real y;), ensuring and dimensionality checks before model execution. These declarations are read once per chain and cannot include statements or distributions. Following the data block, the transformed data block computes deterministic transformations or constants from the input data (e.g., real<lower=0> alpha = 0.1;), executed once per chain to prepare derived quantities like sums or scales, without allowing probabilistic statements. The parameters block then defines the unknown variables to be estimated, such as means or variances (e.g., real mu_y; real<lower=0> tau_y;), which are automatically transformed to unconstrained for sampling efficiency, with constraints specified at declaration. The transformed parameters block derives additional parameters from the base parameters and data (e.g., real<lower=0> sigma_y = pow(tau_y, -0.5);), evaluated repeatedly during and included in output summaries. These blocks together support constrained parameter spaces, where bounds like <lower=0> or <upper=1> are enforced through adjustments during transformation, preventing invalid values without runtime . Central to model specification is the model block, where the log posterior density is defined declaratively using sampling statements (e.g., y[n] ~ normal(mu_y, sigma_y);) or explicit increments (e.g., target += normal_lpdf(y | mu_y, sigma_y);). The ~ notation is that adds the log density to the target function while dropping constant terms for computational efficiency, whereas target += retains all terms, useful for model comparison via log-likelihoods. This block supports loops and local variables for expressing dependencies, such as in hierarchical models where group-level parameters depend on population-level ones (e.g., for (j in 1:J) mu[j] ~ normal(mu_global, sigma_global);). Custom distributions can be defined in a separate functions block and invoked similarly, extending beyond built-in families like normal or beta. Finally, the generated quantities block computes posterior predictive quantities or transformations post-sampling (e.g., real variance_y = sigma_y * sigma_y;), executed for each draw to generate or summaries without affecting the inference process. Unlike imperative languages like Python or C++, Stan's declarative syntax avoids specifying execution paths or simulation steps, focusing solely on the of the probability model to ensure portability and optimization during compilation. This design promotes and for hierarchical and multivariate models.

Automatic Differentiation Engine

Stan's automatic differentiation engine is implemented in the Stan Math Library, a C++ template library that provides reverse-mode (AD) for efficient computation of gradients and Hessians. This implementation leverages and to track dependencies during forward and propagate derivatives backward, enabling the automatic generation of first- and second-order derivatives without manual coding. The library's design ensures that AD computations are fused with the original function evaluation, minimizing overhead and supporting complex expressions involving built-in types like doubles, vectors, and matrices. The core of the engine is the stan::math , which defines custom operators and functions tailored for probabilistic modeling. These include support for dense and sparse vectors, matrices, and a comprehensive set of univariate and multivariate probability distributions, allowing seamless differentiation of statistical functions such as log densities and cumulative distribution functions. For instance, operations like matrix multiplications or element-wise transformations automatically produce the required partial through templated variadic functions, which handle arbitrary argument counts at for optimal performance. This extensibility permits users to define new differentiable functions by implementing forward and reverse passes, ensuring the library remains adaptable to domain-specific needs. Beyond its integration within Stan's probabilistic programming framework, the Stan Math Library is designed for standalone use in external C++ applications, such as general-purpose optimization or . Developers can link against the library to compute gradients for user-defined objectives, exporting AD utilities without relying on Stan's higher-level components; for example, it includes interfaces for and solvers that also support differentiated outputs. This modularity has made it a reusable tool in fields requiring high-precision derivative computations. A key strength of the is its performance characteristics, particularly its linear O(n scaling in the number of parameters n for evaluations, which contrasts with the quadratic costs of finite differences or forward-mode in high dimensions. This efficiency arises from the reverse-mode approach, where a single backward pass computes all partial relative to an output scalar, making it suitable for models with thousands of parameters. Benchmarks demonstrate that computations remain feasible even for large-scale problems, with usage proportional to the expression graph size. In Stan's algorithms, this engine underpins gradient-based samplers like by providing accurate for trajectory simulations.

Inference Algorithms

Hamiltonian Monte Carlo Methods

(HMC) serves as the foundational (MCMC) algorithm in Stan for exploring Bayesian posterior distributions, enabling efficient sampling by incorporating information from the log posterior density. Unlike random-walk methods, HMC simulates Hamiltonian dynamics to propose correlated moves that traverse the parameter space more effectively, reducing autocorrelation and improving exploration in high dimensions. This approach relies on the automatic differentiation engine to compute the necessary gradients of the target density. In HMC, the position variables qq (corresponding to model parameters) are augmented with auxiliary momentum variables pp, drawn from a Gaussian distribution to ensure ergodicity. The joint system evolves according to the Hamiltonian H(q,p)=U(q)+K(p)H(q, p) = U(q) + K(p), where U(q)=logπ(q)U(q) = -\log \pi(q) represents the (negative log posterior) and K(p)=12pM1pK(p) = \frac{1}{2} p^\top M^{-1} p the , with MM as the . The continuous-time dynamics are governed by Hamilton's equations: dqdt=Hp=M1p,dpdt=Hq=U(q).\frac{dq}{dt} = \frac{\partial H}{\partial p} = M^{-1} p, \quad \frac{dp}{dt} = -\frac{\partial H}{\partial q} = -\nabla U(q). These equations are discretized using the leapfrog integrator for proposed moves, which applies symplectic updates with step size ϵ\epsilon: a half-step momentum update ppϵ2U(q)p \leftarrow p - \frac{\epsilon}{2} \nabla U(q), a full-step position update qq+ϵM1pq \leftarrow q + \epsilon M^{-1} p, and another half-step momentum update ppϵ2U(q)p \leftarrow p - \frac{\epsilon}{2} \nabla U(q). The proposal is then accepted or rejected via the Metropolis-Hastings rule to maintain detailed balance, with the leapfrog scheme providing second-order accuracy and volume preservation. Multiple leapfrog steps (typically L=10L = 10 to 100100) form a trajectory of total integration time ϵL\epsilon L. The No-U-Turn Sampler (NUTS), Stan's default implementation, extends HMC by adaptively determining the number of steps during warmup, eliminating the need for manual tuning of LL. NUTS constructs a of possible trajectories from the current position, exploring both directions until a is detected—defined as the trajectory looping back such that the at the endpoints points away from each other—or a maximum tree depth is reached, preventing inefficient periodic or divergent paths. This recursive exploration efficiently samples diverse trajectory lengths while controlling computational cost, often achieving higher effective sample sizes than fixed-step HMC. Stan supports static HMC with user-specified fixed LL, but emphasizes dynamic adaptations during an initial warmup phase to optimize performance. The mass matrix MM is tuned to approximate the posterior covariance, using either a diagonal estimate (default for scalability) or a dense Cholesky factorization for better conditioning in correlated posteriors, which rescales the geometry to improve step efficiency. Step size ϵ\epsilon is adapted via dual averaging, targeting an average Metropolis acceptance rate of 0.8 to balance autocorrelation and rejection rates; this employs a shrinkage parameter γ=0.05\gamma = 0.05, adaptation rate κ=0.75\kappa = 0.75, and initial scale t0=10t_0 = 10, ensuring robust initialization for sampling. These adaptations are disabled after warmup to preserve the Markov property. To assess sampling quality, Stan provides built-in convergence diagnostics, including the split-R^\hat{R} (R-hat) statistic, which compares within-chain and between-chain variances across multiple parallel chains to detect non-convergence (values above 1.01 indicate potential issues). Additionally, effective sample size (ESS) quantifies the number of independent draws equivalent to the autocorrelated MCMC output, computed via the MCMC central limit theorem as ESS=n/(1+2k=1ρk)\text{ESS} = n / (1 + 2 \sum_{k=1}^\infty \rho_k), where nn is the number of samples and ρk\rho_k the autocorrelation at lag kk; Stan uses a robust estimator truncating at half the chain length to avoid bias in finite samples. These metrics, reported per parameter, guide users in evaluating chain mixing and precision.

Variational and Optimization Techniques

Stan implements variational through Variational (ADVI), a black-box variational method that approximates the posterior distribution with a Gaussian in the unconstrained parameter space. ADVI optimizes the (ELBO) using stochastic gradient ascent, enabling scalable for complex models. It supports two families: mean-field ADVI, which assumes a fully factorized Gaussian (diagonal ) for faster computation and simpler structure, and full-rank ADVI, which uses a full to capture posterior correlations at the cost of increased computational expense. Stan also supports Pathfinder, a variational inference algorithm that uses parallel quasi-Newton optimization to locate multiple modes of the posterior before generating approximate samples via Laplace approximation around those modes. Pathfinder can provide better approximations than ADVI, especially for multimodal posteriors, and is useful for initialization or as a faster alternative to full MCMC. It includes single-path and multi-path variants, with the latter running multiple independent optimizations. As of Stan 2.37 (September 2025), Pathfinder has seen memory usage improvements. For , Stan provides optimization routines to compute maximum (MAP) estimates by maximizing the log-posterior density. The objective is to solve argmaxθlogp(θy)\arg\max_{\theta} \log p(\theta \mid y) using gradients obtained via . The default algorithm is L-BFGS, a limited-memory that approximates the Hessian with a history of recent gradients for efficient convergence on large problems, while offers exact Hessian-based steps but is less efficient overall. These techniques are particularly suited for large datasets or real-time applications where methods are computationally prohibitive due to their slower convergence. However, variational approximations like ADVI systematically underestimate posterior variances because they minimize the KL divergence from the approximating distribution to the true posterior, leading to overly narrow credible intervals compared to MCMC results. Similar considerations apply to Pathfinder approximations.

Interfaces and Integrations

Language-Specific Interfaces

Stan provides several language-specific interfaces that enable users to leverage its probabilistic modeling and inference capabilities directly within popular programming environments. These interfaces compile Stan models into executable code and facilitate interaction with Stan's core engine, allowing seamless integration into workflows without requiring users to write C++ code. The primary interfaces include for , PyStan and CmdStanPy for Python, and CmdStan for command-line and other language bindings. RStan serves as the primary interface for the R programming language, built using Rcpp to bridge Stan's C++ library with R's statistical ecosystem. It allows users to specify, compile, and fit Stan models entirely within R scripts or interactive sessions, with the key function stan() handling model compilation, sampling via Hamiltonian Monte Carlo (such as the No-U-Turn Sampler), optimization, and variational inference. The resulting stanfit objects provide methods for posterior extraction, diagnostics like trace plots and Gelman-Rubin statistics, and integration with R packages for visualization and analysis. RStan supports reproducible workflows by caching compiled models and is distributed via CRAN, ensuring compatibility with R's package manager. For Python users, PyStan offers a direct binding to Stan's C++ API, enabling the definition and fitting of Bayesian models in Python code, including support for interactive environments like Jupyter notebooks. It provides classes like StanModel for compilation and sampling, with methods to interface data and extract posterior samples, making it suitable for exploratory data analysis and scripting. Complementing PyStan, CmdStanPy is a lightweight Python wrapper around the CmdStan command-line tool, emphasizing modularity by separating model compilation from inference execution; it supports advanced features like parallel chains and is optimized for production environments with large datasets. Both interfaces facilitate Jupyter-based workflows, where users can iteratively refine models and visualize results using libraries like Matplotlib or ArviZ. CmdStan provides a standalone to Stan, primarily for C++ but extensible to other languages through its executable binaries. Users compile Stan programs into platform-independent executables that perform tasks such as MCMC sampling, optimization, or posterior predictive checks via simple command invocations, outputting results in CSV format for easy parsing. This interface underpins bindings for languages like Julia through the Stan.jl package, which invokes CmdStan executables from Julia code to fit models while leveraging Julia's features. CmdStan's minimal dependencies and low memory footprint make it ideal for deployment in constrained environments, such as high-performance computing clusters. A key advantage of these interfaces is their cross-language compatibility: a Stan model is compiled once into an via CmdStan, then callable from multiple environments without recompilation, promoting efficiency and portability across , Python, Julia, and C++ workflows. This design allows teams to share models while using preferred languages for data preparation and post-processing. Specialized packages built on these core interfaces extend functionality for specific use cases.

Ecosystem Packages and Tools

rstanarm is an R package that facilitates Bayesian applied regression modeling by emulating familiar R functions like lm() and glm(), while utilizing Stan via the rstan interface for backend estimation of common regression models such as linear, generalized linear, and mixed-effects models. It automates the specification of weakly informative default priors to promote and efficient MCMC sampling, and incorporates streamlined post-processing features for generating posterior summaries, intervals, and diagnostics. brms provides an intuitive R interface for fitting Bayesian generalized (non-)linear multilevel models using Stan, employing formula syntax akin to that in the lme4 package to specify hierarchical structures, response distributions (e.g., Gaussian, binomial, Poisson), and link functions without requiring direct Stan code authoring. This package supports advanced features including autocorrelation, censored data, and flexible prior distributions, enabling users to perform full Bayesian inference on complex multilevel data while leveraging Stan's sampling capabilities. Prophet, an open-source tool developed by Meta (formerly ), specializes in time-series forecasting by fitting additive models that capture nonlinear trends, seasonalities, and holiday effects, with Stan serving as the core backend for model optimization and to produce rapid, interpretable predictions. Complementing these domain-specific packages are diagnostic and evaluation tools such as ShinyStan, an built on the Shiny framework that offers an interactive web interface for examining MCMC outputs from Stan models, including trace plots, posterior density visualizations, Gelman-Rubin diagnostics, and posterior predictive simulations to assess convergence and model fit. The loo package computes efficient approximations of leave-one-out cross-validation (via Pareto-smoothed , PSIS-LOO) and the widely applicable information criterion (WAIC) directly from posterior draws of Stan-fitted Bayesian models, providing model weights, standard errors, and diagnostics to support reliable model comparison and selection. A notable 2025 addition to the ecosystem is stanhf, a command-line tool that translates HistFactory specifications— a standard schema for defining statistical models in high-energy physics experiments involving event counts and systematic uncertainties—into executable Stan programs, enabling probabilistic inference for collider with Stan's and sampling methods.

Applications and Usage

Primary Domains

Stan has found extensive application in the social sciences, particularly for hierarchical modeling of survey data and multilevel structures that account for nested data hierarchies, such as individuals within groups, regions, or time periods. This approach enables robust inference on complex social phenomena by pooling information across levels, reducing variance in estimates for sparse subgroups while preserving heterogeneity. For instance, Bayesian hierarchical weighting adjustments have been employed to improve survey inference by combining poststratification with multilevel regression, enhancing accuracy in national polls and demographic analyses. In the pharmaceutical domain, Stan facilitates Bayesian modeling of dose-response relationships and pharmacokinetics, supporting sequential updates to prior knowledge with new clinical data for more precise predictions of drug behavior. Tools like Torsten extend Stan to handle ordinary differential equations for population pharmacokinetic models, enabling efficient fitting of compartmental models to longitudinal concentration data. This has been demonstrated in analyses of drugs like somatrogon, where hierarchical Bayesian methods quantify inter-individual variability and support dose optimization in pediatric populations. Stan is prominently used in ecology and epidemiology for time-series and spatial models that capture dynamic processes in populations and disease spread. In ecology, state-space models implemented in Stan analyze ecological time series, such as population trajectories or predator-prey interactions, by separating observation error from process noise through hierarchical structures. For epidemiology, these capabilities extend to spatiotemporal forecasting, including Bayesian hierarchical models for under-reporting and transmission dynamics during the , where time-varying coefficients model evolving infection rates across regions. Within , Stan supports Gaussian processes for and in functional data, leveraging its for scalable inference on covariance kernels like squared exponential or Matérn. This is particularly valuable for tasks requiring probabilistic predictions, such as spatial interpolation or time-series forecasting. Additionally, extensions enable probabilistic neural networks by integrating components into Bayesian frameworks, allowing for variational inference on network parameters to model uncertainty in high-dimensional inputs. Recent expansions of Stan include applications in high-energy physics, notably through the stanhf package that implements HistFactory models for statistical of collider data, facilitating blinded inference and testing in particle searches. In market research, Stan underpins hierarchical Bayesian models for and optimization, enabling attribution of consumer preferences and media impacts across heterogeneous segments.

Practical Examples and Limitations

A foundational application of Stan is fitting a model, which demonstrates the language's syntax for specifying data, parameters, priors, and likelihoods. In this model, the response variable yy is assumed to follow a centered at the linear predictor α+βx\alpha + \beta x, with unknown variance σ2\sigma^2. Normal priors are placed on the intercept α\alpha and slope β\beta, while σ\sigma receives a half-Cauchy prior for positivity. Sampling is performed using the No-U-Turn Sampler (NUTS), Stan's default algorithm, which efficiently explores the posterior distribution. The complete Stan program for this example is as follows:

data { int<lower=0> N; vector[N] x; vector[N] y; } parameters { real alpha; real beta; real<lower=0> sigma; } model { alpha ~ normal(0, 10); beta ~ normal(0, 10); sigma ~ cauchy(0, 5); y ~ normal(alpha + beta * x, sigma); } generated quantities { real pred_y = normal_rng(alpha + beta * 1.5, sigma); // Example prediction }

data { int<lower=0> N; vector[N] x; vector[N] y; } parameters { real alpha; real beta; real<lower=0> sigma; } model { alpha ~ normal(0, 10); beta ~ normal(0, 10); sigma ~ cauchy(0, 5); y ~ normal(alpha + beta * x, sigma); } generated quantities { real pred_y = normal_rng(alpha + beta * 1.5, sigma); // Example prediction }

This code block defines the data inputs, declares parameters with constraints, specifies weakly informative priors in the model block, and includes a generated quantities block for posterior predictions. When compiled and run via an interface like RStan or CmdStan, NUTS draws samples from the joint posterior, enabling inference on parameters such as the credible interval for β\beta. For more complex scenarios, Stan supports hierarchical modeling, such as in where conversion rates differ between variants but share common hyperpriors to account for uncertainty across experiments. A hierarchical beta-binomial model is suitable for aggregated binary outcomes (e.g., successes out of trials per variant), treating success probabilities p1p_1 and p2p_2 as drawn from a shared parameterized by hyperparameters aa and bb. This setup pools information, improving estimates when sample sizes are limited, and uses NUTS for posterior sampling. An example Stan program for this hierarchical A/B testing model, assuming binomial likelihoods, is:

data { int<lower=0> N1; // Trials for variant 1 int<lower=0> C1; // Successes for variant 1 int<lower=0> N2; // Trials for variant 2 int<lower=0> C2; // Successes for variant 2 } parameters { real<lower=0> a; real<lower=0> b; real<lower=0, upper=1> p1; real<lower=0, upper=1> p2; } model { // Weakly informative prior on hyperparameters (e.g., horseshoe-like) target += -2.5 * log(a + b); p1 ~ beta(a, b); p2 ~ beta(a, b); C1 ~ binomial(N1, p1); C2 ~ binomial(N2, p2); } generated quantities { real diff = p1 - p2; // Posterior difference in conversion rates }

data { int<lower=0> N1; // Trials for variant 1 int<lower=0> C1; // Successes for variant 1 int<lower=0> N2; // Trials for variant 2 int<lower=0> C2; // Successes for variant 2 } parameters { real<lower=0> a; real<lower=0> b; real<lower=0, upper=1> p1; real<lower=0, upper=1> p2; } model { // Weakly informative prior on hyperparameters (e.g., horseshoe-like) target += -2.5 * log(a + b); p1 ~ beta(a, b); p2 ~ beta(a, b); C1 ~ binomial(N1, p1); C2 ~ binomial(N2, p2); } generated quantities { real diff = p1 - p2; // Posterior difference in conversion rates }

This model infers not only variant-specific probabilities but also the hyperprior strength, allowing quantification of whether variant 2 improves conversions (e.g., via the posterior of p2p1>0p_2 - p_1 > 0). Note that for individual-level binary data, the binomial can be replaced with Bernoulli, yielding a hierarchical logistic regression equivalent. Despite its strengths, Stan has notable limitations, particularly in computational demands and algorithmic constraints. Fitting large models with many parameters or data points incurs high computational costs due to the iterative nature of MCMC sampling, often requiring hours or days on standard hardware even for moderately sized datasets. Additionally, NUTS sampling can be sensitive to initial parameter values; poor initialization may lead to slow convergence or divergent transitions, necessitating careful tuning of the adaptation phase. A key restriction is the lack of built-in support for discrete parameters within HMC, as the method relies on continuous differentiable densities; discrete variables must be marginalized out or approximated, limiting direct modeling of mixture components or latent classes without workarounds. To mitigate these issues and ensure reliable inference, several best practices are recommended. Running multiple independent chains (typically 4 or more) allows assessment of convergence via metrics like the Gelman-Rubin statistic (R-hat < 1.1), while the warm-up period—Stan’s adaptive tuning phase for step size and —should comprise at least half of total iterations to stabilize sampling. Prior predictive checks, implemented by simulating data from the prior distribution, help verify that priors generate plausible outcomes before fitting, avoiding implausible posterior geometries. These techniques, combined with diagnostic tools like trace plots, enhance model robustness.

References

Add your contribution
Related Hubs
User Avatar
No comments yet.