Yaps: Python Frontend to Stan

Guillaume Baudart, Martin Hirzel, Kiran Kate, Louis Mandel, Avraham ShinnararXiv 2018

Abstract

Stan is a premier domain-specific language for probabilistic programming, yet its integration into Python environments has historically relied on embedding code within multi-line strings. This "leaky abstraction" hinders developer productivity by bypassing standard IDE features and complicating debugging. We present Yaps (Yet Another Pythonic Stan), a frontend that reinterprets Python's Abstract Syntax Tree (AST) to allow native-feeling Stan model development. Yaps provides a watertight abstraction, enabling real-time error checking and syntax highlighting while maintaining 100% numerical parity with traditional Stan models.

Introduction: The Problem of Leaky Abstractions

While Stan is a powerful tool for statistical modeling, using it within Python has traditionally been clunky. Existing interfaces, such as PyStan or PyCmdStan, typically require users to define their models as long, multi-line Python strings.

This approach creates a "leaky abstraction" with several significant downsides. First, Python IDEs cannot provide syntax highlighting, auto-completion, or linting for code hidden inside a string. Second, developers must constantly switch contexts between Python and Stan syntax. Finally, error reporting is often poor; errors in the Stan code are frequently reported relative to generated C++ or temporary files, making debugging a significant challenge for researchers.

Traditional string-based model definition in PyCmdStan, illustrating the lack of native IDE support for the embedded Stan code.

Methods: Reinterpreting the Python AST

The core of Yaps is a reinterpretation approach. Unlike "lazy evaluation" frameworks (e.g., PyMC3) that build computation graphs at runtime, Yaps uses a Python decorator (@yaps.model) to intercept a function's source code.

The Yaps compilation pipeline follows four key steps:

  1. Parsing: The standard Python ast module parses the function into an Abstract Syntax Tree.
  2. Transformation: The Python AST is converted into a Yaps Intermediate Representation (IR).
  3. Inference: Inspired by SlicStan, Yaps uses program analysis to automatically determine which variables belong in Stan’s specific blocks (e.g., data, parameters, or model), allowing for a more concise, "block-less" syntax.
  4. Code Generation: The IR is compiled into standard Stan code executable by engines like PyCmdStan.

To maintain valid Python syntax while mimicking Stan, Yaps introduces clever mappings. For instance, since Python lacks a native sampling operator (~), Yaps uses <~ (a valid combination of "less than" and "bitwise NOT") to represent sampling from a distribution.

The Yaps workflow: defining a model with native-like syntax, sampling, and inspecting the generated Stan code.

Key Results and Evaluation

To validate the compiler's robustness, the authors performed extensive "round-trip" testing (Stan → Yaps → Stan) against over 1,200 real-world models from the official Stan manual and developer repositories.

The results demonstrate high compatibility:

  • 100% success rate on official manual examples.
  • 97% success rate on the Stan development repository (with failures primarily due to deprecated Stan syntax).
  • Numerical Parity: Runtime tests across 13 models confirmed that Yaps-generated code produces posterior distributions identical to hand-written Stan models.

Furthermore, Yaps significantly improves the developer experience through reverse source location mapping. If the Stan compiler encounters an error, Yaps translates the location back to the original line in the Python file. This allows modern IDEs like VSCode to provide "red squiggly lines" for syntax errors or undefined variables within the model definition.

Conclusion and Future Work

Yaps bridges the gap between the rigorous semantics of Stan and the developer-friendly ecosystem of Python. By providing a watertight abstraction, it enables modern IDE features for probabilistic modeling, reducing cognitive load and debugging overhead. Future work includes implementing local variable type inference to further simplify syntax and exploring native Python meta-programming to dynamically modify Stan models.

Summary updated

BibTeX
@misc{baudart2018yaps,
  author = {Guillaume Baudart and Martin Hirzel and Kiran Kate and Louis Mandel and Avraham Shinnar},
  title = {{Yaps: Python Frontend to Stan}},
  year = {2018},
  eprint = {1812.04125},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1812.04125},
}
Figure