Abstract
Probabilistic programming frameworks are powerful tools for statistical modelling and inference, yet they are not immediately generalisable to phylogenetic problems due to the unique computational properties of phylogenetic trees. We present TreeFlow, a software library designed to bridge this gap by enabling probabilistic programming and automatic differentiation (AD) with phylogenetic trees. Built on TensorFlow, TreeFlow implements inference algorithms for phylogenetic tree times and model parameters given a fixed tree topology. Our results demonstrate that TreeFlow allows for rapid prototyping of complex evolutionary models and provides competitive performance for gradient-based inference compared to specialized phylogenetic libraries.
Introduction
In modern statistics, Probabilistic Programming Languages (PPLs) like Stan or PyMC3 allow researchers to specify complex models and perform inference without manually deriving gradients or implementing low-level algorithms. However, phylogenetics—the study of evolutionary relationships—has largely remained isolated from this revolution.
The primary obstacle is the "Tree Object." Phylogenetic trees are hybrid structures: they consist of a discrete branching topology and continuous branch lengths or node heights. Standard PPLs struggle with the recursive nature of trees and the complex constraints required to calculate the phylogenetic likelihood (the probability of observed molecular data given a tree). Consequently, biologists often rely on specialized, monolithic software. Developing new models in these environments frequently requires thousands of lines of high-performance C++, a significant barrier to innovation.
The TreeFlow Framework
TreeFlow addresses these challenges by leveraging TensorFlow and TensorFlow Probability (TFP) to treat phylogenetic trees as first-class citizens within a machine learning framework. To achieve this, the library introduces several key technical innovations:
- Tensor-Centric Tree Representation: Trees are represented as collections of Tensors (integer arrays for topology and float arrays for node heights). This allows the library to utilize TensorFlow’s accelerated linear algebra and GPU support.
- Differentiable Likelihoods via
TensorArray: The core of phylogenetic inference is Felsenstein’s pruning algorithm, a dynamic programming approach. TreeFlow implements this usingTensorArray, which enables automatic differentiation while maintaining $O(n)$ linear scaling relative to the number of sequences. - The Node Height Ratio Transform: Gradient-based inference algorithms, such as Automatic Differentiation Variational Inference (ADVI), require parameters to exist in an unconstrained real-valued space. TreeFlow employs a ratio transform to map strictly ordered node times (where parents must be older than children) into an unconstrained space that optimizers can navigate efficiently.
Experimental Validation
The authors evaluated TreeFlow across three distinct scenarios to test flexibility, scalability, and accuracy:
Rapid Prototyping: Carnivore Mitogenomes
To demonstrate flexibility, the authors analyzed a dataset of 62 carnivore mitochondrial genomes. They sought to test a model where the ratio of DNA transitions to transversions ($\kappa$) varied across different lineages. In traditional software, implementing such a lineage-specific model would be a major undertaking. In TreeFlow, this was achieved by modifying only a few lines of code to treat $\kappa$ as a vector across branches rather than a global scalar.
Scalability: Influenza A
Using a dataset of 980 Influenza A sequences, the authors compared TreeFlow’s ADVI implementation against the gold-standard MCMC tool, BEAST 2. TreeFlow’s ADVI converged in approximately 3 hours, whereas BEAST 2 required nearly 6 hours. While TreeFlow provided comparable parameter estimates for substitution rates and tree heights, it exhibited a known characteristic of mean-field variational inference: a slight underestimation of posterior uncertainty.
Computational Benchmarks
In benchmarks ranging from 32 to 2,048 taxa, TreeFlow demonstrated consistent linear scaling. While specialized C++ libraries like BEAGLE remain 2–3 times faster for standard models, TreeFlow proved to be an order of magnitude faster than naive AD-based implementations in other frameworks (such as JAX), confirming that its execution model is well-optimized for tree-based recursion.
Discussion and Limitations
TreeFlow represents a significant step toward making phylogenetics more accessible to the broader statistical community. Its primary advantage is the trade-off between raw execution speed and massive gains in modeling flexibility.
However, two main limitations remain. First, TreeFlow currently performs inference on a fixed tree topology. Since the branching order is often unknown in biological studies, extending AD-based methods to explore topology space remains a major computational challenge. Second, the current reliance on "Mean Field" ADVI assumes independence between model parameters, which can lead to overconfident results. Future work will focus on implementing structured posterior approximations to better capture correlations between evolutionary parameters.
By providing a bridge between evolutionary biology and modern computational frameworks, TreeFlow enables researchers to move from model conception to implementation in a fraction of the time previously required.
Summary updated