A Formalization of Doob's Martingale Convergence Theorems in mathlib

Kexing Ying, Rémy Degenne · DOI

Abstract

This paper details the integration of advanced probability theory into Lean’s central mathematical library, mathlib. The authors formalize Doob’s Martingale Convergence Theorems, a cornerstone of stochastic process theory. By generalizing definitions to Banach space-valued random variables and implementing conditional expectation as a continuous linear operator, the work provides a versatile and rigorous foundation for future research in analysis and statistics. The contribution includes approximately 9,200 lines of code, covering fundamental results such as the almost everywhere and $L^1$ convergence theorems, as well as Lévy’s generalized Borel-Cantelli lemma.

Introduction

Formal verification employs computer-assisted proof systems to ensure mathematical results are logically sound. While fields like algebra and topology have seen significant growth in formal libraries, probability theory—specifically the study of stochastic processes—has historically lagged. Martingales, which model "fair games" where the future expected value equals the current value, are foundational to finance, statistics, and stochastic calculus. Proving that these processes converge requires a complex integration of measure theory, topology, and analysis. Previous formalizations in systems like Coq or Isabelle/HOL were often restricted to real-valued variables or lacked the integration necessary for a general-purpose library. This work addresses these gaps by building a comprehensive martingale theory in Lean.

Methods

The authors contributed approximately 9,200 lines of code to mathlib, following a "monolithic" design philosophy to ensure seamless integration with Lean’s existing analysis and measure theory components. Key technical choices included:

  • Banach Space Generalization: Martingales are defined for Banach spaces (complete normed vector spaces) rather than being limited to $\mathbb{R}$, allowing the library to support abstract functional analysis.
  • Conditional Expectation as an Operator: Conditional expectation was constructed as a continuous linear map from $L^1$ to itself. This involved defining it for $L^2$ functions via orthogonal projections and subsequently extending the definition.
  • Handling Equivalence Classes: A core challenge in formalizing $L^p$ spaces is the distinction between raw measurable functions and their equivalence classes under almost-everywhere equality.
Commutative diagram showing the lifting of properties from the space of measurable functions $\mathcal{L}^p$ to the quotient space $L^p$.
  • Upcrossing Estimate: To prove almost everywhere (a.e.) convergence, the authors formalized Doob’s "upcrossing estimate," a combinatorial argument counting how many times a sequence crosses a specific interval.
  • Total Functions: To manage "undefined" values in formal logic, the authors used an if...then...else pattern to ensure functions like condexp return a default value (zero) when hypotheses are not met, simplifying the API for complex proofs.

Key Insights and Ideas

A critical aspect of the project was API design. The authors introduced a limit_process definition that extracts the limit of a sequence, allowing it to be manipulated as a first-class object. The formalization also highlighted the rigor required for "trivial" steps; for example, nearly 100 lines of code were needed to prove that upcrossings are invariant under translation and reflection. Additionally, the paper provides a unified API for uniform integrability that bridges the differing definitions used in analysis and probability.

Key Results

The library now includes formal proofs for the Almost Everywhere Martingale Convergence Theorem and the $L^1$ Martingale Convergence Theorem. As a demonstration of the framework's utility, the authors formalized Lévy’s Generalized Borel-Cantelli Lemma. All code has been integrated into mathlib, offering a higher level of generality—supporting Banach space-valued martingales—than most introductory textbooks and previous formalization attempts.

Limitations and Future Work

One current limitation involves stopping times, which in Lean must share the same type as the time index, whereas they are often allowed to be infinite in theoretical contexts. This discrepancy led to some code duplication, which the authors intend to resolve by refactoring with Lean’s with_top type. Future work will also extend the library to include Markov kernels and regular conditional probability, which are essential for modeling complex stochastic interactions in fields like machine learning.

Summary updated

BibTeX
@inproceedings{ying2023formalization,
  author = {Kexing Ying and Rémy Degenne},
  title = {{A Formalization of Doob's Martingale Convergence Theorems in mathlib}},
  booktitle = {CPP},
  year = {2023},
  url = {https://openreview.net/forum?id=mWJDEfmBPvA},
}
Figure