TreePPL: A Universal Probabilistic Programming Language for Phylogenetics

Viktor Senderov, Jan Kudlicka, Daniel Lundén, Viktor Palmkvist, Mariana P. Braga, Emma Granqvist, Gizem Çaylak, Thimothée Virgoulay, David Broman, Fredrik RonquistbioRxiv 2023

Abstract

We present TreePPL, a universal probabilistic programming language (PPL) designed for probabilistic modeling and inference in phylogenetics. In TreePPL, the model is expressed as a computer program, which can generate simulations from the model conditioned on some input data. Specialized inference machinery then uses this program to estimate the posterior probability distribution. The aim is to allow the user to focus on describing the model, and provide the inference machinery for free. The TreePPL modeling language is meant to be familiar to users of R or Python, and utilizes a functional programming style that facilitates the application of generic inference algorithms. The model program can be conveniently compiled and run from a Python or R environment, which can be used for pre-processing, feeding the model with the observed data, controlling and running the inference, and receiving and post-processing the output data. The inference machinery is generated by a compiler framework developed specifically for supporting domain-specific modeling and inference, the Miking CorePPL framework. It currently supports a range of inference strategies—including sequential Monte Carlo, Markov chain Monte Carlo, and combinations thereof—and is based on several recent innovations that are important for efficient PPL inference on phylogenetic models.

Introduction

In statistical phylogenetics—the study of evolutionary relationships—researchers often find themselves trapped between two worlds. On one hand, they have powerful but rigid software (like MrBayes or BEAST) that supports a fixed set of models. On the other, they have flexible frameworks (like RevBayes) that require a deep understanding of probabilistic graphical models.

The core challenge is universality. Many evolutionary processes, such as diversification (where lineages branch and go extinct), involve "stochastic recursion"—processes where the number of random variables isn't fixed and depends on the simulation itself. Most existing tools struggle to describe these cleanly. Furthermore, writing the math for the inference algorithms (like MCMC or SMC) for every new model is error-prone and time-consuming. TreePPL aims to solve this by letting biologists focus on the "what" (the model) while the system handles the "how" (the inference).

System Architecture and Methods

TreePPL is a domain-specific, functional programming language inspired by OCaml, Python, and R. Its architecture relies on several key technical pillars that separate model specification from the "heavy lifting" of statistical inference.

The TreePPL compilation and execution pipeline, showing the transition from front-end compilation to the back-end and the Python/R environment.
  • The Miking CorePPL Framework: TreePPL acts as a "front-end" that compiles down to CorePPL, a specialized intermediate language designed for efficient probabilistic inference.
  • Probabilistic Primitives: The language uses three main constructs: assume (to define latent random variables), observe (to condition the model on real-world data), and weight (to manually adjust the likelihood).
  • Selective CPS Transformation: To manage the "checkpoints" where a program might need to pause or branch during inference, the compiler uses a partial Continuation-Passing Style (CPS) transformation. This provides a significant performance boost (up to 2x) over standard PPL implementations.
  • Advanced Inference Engines: It supports Sequential Monte Carlo (SMC) and MCMC. Notably, it includes the Alive Particle Filter (APF), which is essential for models where many simulations "fail" (e.g., a lineage goes extinct before the present day).

Applications

The paper demonstrates the language's utility through three primary scientific examples:

  1. Diversification Models: Tested using a phylogenetic tree of the Alcedinidae (kingfishers) family to estimate speciation and extinction rates.
  2. Host Repertoire Models: A complex model describing how parasites (like butterflies) evolve to interact with different host plants.
  3. Tree Inference: A conceptual implementation of the coalescent process to infer the tree structure itself from DNA sequences.

Key Results and Insights

By decoupling the model from the inference algorithm, TreePPL reduces the risk of implementation errors—a major issue in the field. One of the hardest parts of using SMC in a PPL is ensuring that different "particles" (simulations) are compared at the same logical point; TreePPL’s compiler automatically identifies these "checkpoints," sparing the user from manual, error-prone synchronization.

Posterior distributions for speciation (λ) and extinction (μ) rates comparing the Bootstrap Particle Filter (BPF) and the Alive Particle Filter (APF).

The use of the Alive Particle Filter (APF) proved significantly more efficient than standard filters for diversification models, as it doesn't waste computation on extinct lineages. Furthermore, the selective CPS transformation allows TreePPL to compete with state-of-the-art PPLs, offering a path toward "automatic" inference that doesn't sacrifice too much speed compared to hand-coded solutions.

Limitations and Future Work

While TreePPL is expressive, current automated inference doesn't yet scale to massive genomic datasets as well as highly optimized, hand-coded C++ tools. The authors are working on automatically implementing "Felsenstein’s pruning algorithm" to bridge this gap. Additionally, the current MCMC implementation for tree structures is relatively basic; future versions will need more sophisticated "moves" (like NNI or SPR) to explore the space of possible trees efficiently. Finally, the team plans to use machine learning to automatically "tune" the parameters of the inference algorithms, further reducing the burden on the end-user.

Summary updated

BibTeX
@misc{senderov2023treeppl,
  author = {Viktor Senderov and Jan Kudlicka and Daniel Lundén and Viktor Palmkvist and Mariana P. Braga and Emma Granqvist and Gizem Çaylak and Thimothée Virgoulay and David Broman and Fredrik Ronquist and Fredrik Ronquist and Fredrik Ronquist},
  title = {{TreePPL: A Universal Probabilistic Programming Language for Phylogenetics}},
  year = {2023},
  doi = {10.1101/2023.10.10.561673},
  howpublished = {bioRxiv},
}
Figure