How to code up a bespoke probability density in Stan

How to code up a bespoke probability density in Stan

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

Keywords

Stanlog probabilitycustom distributionHamiltonian Monte CarloBayesian inference

Summary

This tutorial by Ben Lambert explains how to sample from a probability distribution not included in Stan’s built-in library. The key idea is that Stan’s sampling statements are equivalent to incrementing the log probability (target) by the log density of the distribution. The video demonstrates this with a custom distribution p(x|a) = (1-a) * x^{-a} for a in [0,1] and x in [0,1]. After generating data via inverse transform sampling, the author codes the model in Stan using a functions block to define the log density, then runs MCMC to infer the parameter a. The posterior mean is close to the true value, validating the approach. The video also shows alternative ways to code the custom distribution, including using the _lpdf suffix to treat it like a built-in distribution. Finally, it notes that constant terms in the log density can be omitted without affecting inference. The tutorial is practical and clear, suitable for users with some familiarity with Stan and Bayesian statistics.

162 words

Critical Evaluation

Value of the Information & Strength of the Argument

The video provides valuable practical knowledge for Stan users, demonstrating a technique that extends the software’s capabilities. The argumentation is solid: it starts with the conceptual equivalence between sampling statements and log probability increments, then applies it to a concrete example. The step-by-step coding is clear, and the validation via posterior inference reinforces the correctness. The explanation of alternative coding methods (e.g., using _lpdf) adds depth. The only minor weakness is that the video does not discuss potential pitfalls or limitations of the approach, but overall the value is high for its target audience.

Scientific Rigor, Source Quality, Title Accuracy

The scientific rigor is good: the method is standard in Bayesian computation, and the example is correctly implemented. The video does not cite external sources, but it references the instructor’s book and website in the description. The title accurately reflects the content. No comments were provided, so no analysis of public reception is possible.

163 words

Title / Content Match

The title accurately reflects the content, which focuses on coding a custom probability density in Stan.

Quality & Reliability

8/10

The video is a clear, well-structured tutorial by an academic (Ben Lambert) with a solid background in Bayesian statistics. It explains the concept of incrementing the log probability in Stan and demonstrates a custom distribution example. The methods are standard and correctly presented, with practical coding steps. Minor limitations: no formal citations, but the content aligns with established Stan documentation and Bayesian methodology.

Key Moments

Cited Sources

Concurring Sources

  • Stan User's Guide — Official documentation on custom functions and target increments.

Contribution & Novelties

The video offers a clear, practical tutorial on extending Stan to custom distributions, which is a common need but often not well documented. It bridges the gap between theory and implementation, showing exactly how to write the log density and use it in a model. The example is simple yet illustrative, and the alternative coding approaches provide flexibility. This is particularly useful for researchers and practitioners who need to implement non-standard models.

Pour aller plus loin :

  • Stan User’s Guide — Official documentation on custom functions and target increments.
  • Hamiltonian Monte Carlo — Background on the algorithm underlying Stan.
  • Bayesian Data Analysis by Gelman et al. — Comprehensive reference on Bayesian methods and computation.

114 words

Radar Profile

The radar profile shows high scores in quality and reliability, with moderate scores in quantity and technical level. This indicates a focused, well-executed tutorial that provides solid information but is not exhaustive. The technical level is appropriate for intermediate users, and the reliability is high due to the standard methods used.

Reliability 8/10