How to code up a model with discrete parameters in Stan

How to code up a model with discrete parameters in Stan

🎙 Ben Lambert 👥 148K 📅 November 14, 2018 ⏱ 21 min 👁 8K 📄 tutorial 🧭 2026-08-17
Available in: English (current) Français

Keywords

Standiscrete parametersmarginalizationlog-sum-expBayesian

Summary

The video, presented by Ben Lambert, explains how to implement Bayesian models with discrete parameters in Stan, which typically only supports continuous parameters. The key technique is to marginalize out the discrete parameter by summing over its possible values in the joint posterior, then using the log-sum-exp function for numerical stability. The tutorial uses a coin-flipping example where the number of flips (n) is unknown and discrete, while the probability of heads (theta) is continuous. The model is coded in Stan with data, transformed data, parameters, transformed parameters, and model blocks. The likelihood is binomial, and priors are discrete uniform for n and continuous uniform for theta. After fitting, the posterior probabilities for each possible n are computed in the generated quantities block using Rao-Blackwellization. The video demonstrates the entire process in RStudio, showing code and output. The approach is efficient and allows inference on discrete parameters despite Stan’s limitations.

150 words

Critical Evaluation

Value of the Information & Strength of the Argument

The video provides a valuable tutorial on a practical problem in Bayesian computation: handling discrete parameters in Stan. The argumentation is clear and logical, starting with the problem, then deriving the marginalization approach mathematically, and finally implementing it in code. The example is well-chosen and effectively illustrates the concepts. The explanation of log-sum-exp and Rao-Blackwellization adds depth. The video is self-contained and provides a solid foundation for users needing to implement such models.

Scientific Rigor, Source Quality, Title Accuracy

The video is scientifically rigorous in its methodology, correctly applying Bayesian principles and Stan’s syntax. However, it does not cite specific sources or references within the video, relying on the instructor’s expertise. The description provides links to the instructor’s website and a playlist, but no direct references to academic papers. The title accurately describes the content, and the video fulfills its promise. The lack of formal citations is a minor weakness, but the content is standard and well-known in the field.

169 words

Title / Content Match

The title accurately reflects the content: the video explains how to code models with discrete parameters in Stan by marginalization.

Quality & Reliability

8/10

The video provides a clear, step-by-step tutorial on marginalizing discrete parameters in Stan, with correct mathematical derivations and practical code examples. The method is standard and well-established in Bayesian computation. Minor limitations: no formal references cited, and the video is from 2018, but the content remains relevant.

Key Moments

Cited Sources

Concurring Sources

  • Stan User's Guide — Official Stan documentation that describes the log-sum-exp function and model coding practices.

Contribution & Novelties

The video provides a clear, practical tutorial on a specific technique in Stan: marginalizing discrete parameters. It fills a gap for users who need to handle such models, as Stan does not natively support discrete parameters. The example is simple yet illustrative, and the code is provided step-by-step. The explanation of log-sum-exp and Rao-Blackwellization adds value for understanding numerical stability and efficiency.

Pour aller plus loin :

  • Stan User’s Guide — Official documentation for Stan, including details on log-sum-exp and model coding.
  • Bayesian Data Analysis — Gelman et al.’s book, which covers Bayesian computation and marginalization.
  • Rao-Blackwellization — Wikipedia article explaining the Rao-Blackwell theorem, relevant to the efficiency gain mentioned.

110 words

Radar Profile

The radar profile shows high scores in information quantity, quality, and reliability, with a slightly lower technical level, indicating a well-balanced tutorial that is accessible yet rigorous. The strong performance across all dimensions suggests a highly useful resource for practitioners.

Reliability 8/10

💬 No comments were provided for analysis.