# Markov kernels in Mathlib's probability library

Rémy Degenne

► To cite this version:

Rémy Degenne. Markov kernels in Mathlib's probability library. 2025. hal-05302581

HAL Id: hal-05302581

https://inria.hal.science/hal-05302581v1

Preprint submitted on 7 Oct 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# Markov kernels in Mathlib's probability library

Rémy Degenne

**Abstract.** The probability folder of Mathlib, Lean's mathematical library, makes a heavy use of Markov kernels. We present their definition and properties and describe the formalization of the disintegration theorem for Markov kernels. That theorem is used to define conditional probability distributions of random variables as well as posterior distributions. We then explain how Markov kernels are used in a more unusual way to get a common definition of independence and conditional independence and, following the same principles, to define sub-Gaussian random variables. Finally, we also discuss the role of kernels in our formalization of entropy and Kullback-Leibler divergence.

<footnote>Keywords: Formalization, Mathlib, Lean, Probability, Markov Kernel, Independence, Sub-Gaussian</footnote>

<footnote>Contact information:</footnote>

<footnote>R. Degenne: Univ. Lille, Inria, CNRS, Centrale Lille, UMR 9189-CRISTAL, F-59000 Lille, France.
[remy.degenne@inria.fr](mailto:remy.degenne@inria.fr)</footnote>
# 1 Introduction

In probability theory, we study objects defined on a probability space, which is a measurable space $\Omega$ (a set equipped with a $\sigma$-algebra of measurable sets), on which a probability measure $P$ is defined. The probability measure assigns a probability to events (measurable subsets of $\Omega$). The main objects of study are random variables, which are measurable functions from $\Omega$ to other measurable spaces. We can ask for the probability of a random variable $X : \Omega \to \mathcal{X}$ to take a value in a measurable set of $\mathcal{X}$. We will also want for example to determine its distribution, or to prove that it is independent from another random variable, or to find its conditional distribution given a third random variable. All those random variables are usually defined on the same measurable space $\Omega$, which might not be explicitly specified, and it is understood that $\Omega$ can be extended if needed to introduce more random variables.

A Markov kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ is a function from a measurable space $\mathcal{X}$ to the set of probability measures on a measurable space $\mathcal{Y}$ satisfying a measurability condition. It can be seen as describing the effect of a random transformation. In particular, a Markov kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ from a probability space maps the probability measure $P$ on $\mathcal{X}$ to a probability measure $\kappa \circ_P P$ on $\mathcal{Y}$. One can consider a category with measurable spaces as objects and Markov kernels as morphisms. In that kernel point of view, there isn't a single measurable space on which every random variable is defined, and we focus instead on the transformations between them.

The object of this paper is to present the implementation, properties and use of Markov kernels in the probability folder of MATHLIB [mat20], the mathematical library of the LEAN programming language and theorem prover [Ebn+17; MU21]. Markov kernels play a more important role in that library than in most mathematical texts.

**Structure of the paper** We first introduce Markov kernels, detail particular kernels and describe the different ways in which they can be composed in Section 2. We then present the formalization of the disintegration theorem for kernels and its use to define conditional distributions (Section 3). Having explained the main properties of kernels, we then present two examples of their use in MATHLIB, first to define conditional independence and independence (Section 4), then to define sub-Gaussian random variables (Section 5). We finally discuss an application of kernels for entropy and Kullback-Leibler divergence (Section 6).

**Codebase** The definitions and theorems presented in this paper were contributed by the author to the probability folder of MATHLIB, except for the results of Section 6 on entropy and
divergences. The development of kernels, their properties and the disintegration theorem consist of around 10000 lines of code in the Probability/Kernel folder (excluding notably the Ionescu-Tulcea subfolder that was not added by the author of this paper). Results about independence can be found in the Probability/Independence folder (around 4000 lines of code), and the definition of sub-Gaussian random variables is in Probability/Moments (around 2500 lines together with properties of moment generating functions).

This paper contains links to definitions and theorems from the MATHLIB github repository, like for example ProbabilityTheory.Kernel. They all point to a recent commit at the time of writing.

The results discussed in Section 6 about entropy were contributed by the author to the PFR project [And+23]. This is a collaborative formalization of the article [Gow+25], in which the authors prove the polynomial Freiman-Ruzsa conjecture of Katalin Marton. A few remarks in Section 6 on Kullback-Leibler and Rényi divergences come from the work of the author and Lorenzo Luccioli on a formalization of information-theoretic divergences in LEAN [LD24]. We are in the process of integrating results about divergences into MATHLIB: at the time of writing, the definition of Kullback-Leibler divergence is in MATHLIB but only a few properties have been ported. Entropy and other divergences have not yet been the object of pull requests.

**Markov kernels in other proof assistants** The formalization of Markov kernels in other proof assistants is mostly motivated by the semantics of probabilistic programming languages [Sta20], while our goal is to provide a general-purpose theory of kernels for probability theory. In Isabelle/HOL, [HMS23] formalized quasi-Borel spaces and they defined Markov kernels, finite and s-finite kernels. In Rocq/Coq, [ACS25] also formalized Markov, finite and s-finite kernels, and additionally $\sigma$-finite kernels. They discuss in details the problem of proving the measurability needed to define the composition of kernels, which we will omit in this paper. Both of those formalizations build classes of kernels and some of their compositions (corresponding to what we present in Section 2) but don't discuss disintegration of kernels or conditional distributions.

**Notation** $\mathbb{R}_{+,\infty}$ is the set of nonnegative extended real numbers. $\Omega, \mathcal{T}, \mathcal{U}, \mathcal{X}, \mathcal{Y}, \mathcal{Z}$ denote measurable spaces. The set of sets of $\mathcal{X}$ is denoted by $\mathcal{P}(\mathcal{X})$. The set of nonnegative measures on a measurable space $\mathcal{X}$ is written $\mathcal{M}(\mathcal{X})$. A kernel from $\mathcal{X}$ to $\mathcal{Y}$ is denoted by $\mathcal{K} \rightsquigarrow \mathcal{Y}$. The expectation of a random variable $X$ with respect to a measure $\mu$ is written $\mu[X] = \int_{\omega} X(\omega) d\mu$. This is a Lebesgue integral if $X$ takes values in $\mathbb{R}_{+,\infty}$, and a Bochner
integral in other cases (see [Gou22] for a discussion of integrals in MATHLIB). Other notation,
notably about constructions of kernels, will be introduced with the corresponding definitions.

2 Probability spaces and Markov kernels

In MATHLIB, a type can be equipped with a measurable space structure by writing the
code {$\Omega$: Type*} [MeasurableSpace $\Omega$]. The code snippets in this paper will most often omit
the type arguments for brevity: the reader should assume that every type is equipped with a
measurable space structure. A probability measure can be defined on a measurable space
with {P: Measure $\Omega$} [IsProbabilityMeasure P].

The central object of this paper is the transition kernel (simply kernel in the following)
between measurable spaces, and in particular Markov kernels. A kernel $\kappa: \mathcal{X} \rightsquigarrow \mathcal{Y}$ is a
function from $\mathcal{X}$ to the set of measures on $\mathcal{Y}$, such that for every measurable set $B$ of $\mathcal{Y}$,
the function $\mathcal{X} \to \mathbb{R}_{+\infty}$ defined by $x \mapsto \kappa(x)(B)$ is measurable. It is said to be a Markov kernel
if $\kappa(x)$ is a probability measure for every $x \in \mathcal{X}$.

It is useful to think of a Markov kernel as a conditional distribution, in the sense that
$\kappa(x)$ is the distribution of some random element of $\mathcal{Y}$ given that a random element of
$\mathcal{X}$ takes the value $x$. Kernels arise naturally when describing the outcomes of sequential
experiments. For a toy example, suppose that if the weather is good one day, we know that
it will be good the next day with probability 0.8, and if it is bad weather instead, it will be
good the next day with probability 0.4. We can describe that situation with a Markov kernel
$\kappa: \{\text{good, bad}\} \rightsquigarrow \{\text{good, bad}\}$ such that $\kappa(\text{good})$ is the probability measure on $\{\text{good, bad}\}$
giving probability 0.8 to good weather and 0.2 to bad weather, and $\kappa(\text{bad})$ is the probability
measure giving probability 0.4 to good weather and 0.6 to bad weather. On such a simple
example there is no need for the measurability condition of kernels: every function from
a finite set with discrete $\sigma$-algebra to a measurable space is measurable. However, the
measurability is important in non-discrete spaces.

We can define a measurable space on the set of measures on $\mathcal{Y}$ as the coarsest $\sigma$-algebra
such that the function $M(\mathcal{Y}) \to \mathbb{R}_{+\infty}$ defined by $\mu \mapsto \mu(B)$ is measurable for every measurable set $B$ of $\mathcal{Y}$. That measurable space was already implemented in MATHLIB by Johannes Hölzl in 2019 in the context of the implementation of the Giry monad (see [Höl17] for his work on the same topic in Isabelle/HOL). A kernel is then simply a measurable function from $\mathcal{X}$ to the set of measures on $\mathcal{Y}$. In that sense, kernels were already defined by Hötzl in 2019, and the composition of a measure and a kernel (see Section 2.2) was implemented as the
monadic bind of the Giry monad. But apart from a short file containing the definition and that composition, kernels were not used prior to our work.

We introduced a definition `ProbabilityTheory.Kernel` in `MATHLIB` as the following structure:

```c
structure Kernel (X Y : Type*) [MeasurableSpace X] [MeasurableSpace Y] where
toFun : X → Measure Y
measurable' : Measurable toFun
```

So Kernel $X \otimes Y$ is the code for what we write $X \rightsquigarrow Y$ in this text. It is then equipped with a coercion to a function, which means that we can write $\kappa \times X$ for the measure image of $x$ by $\kappa$ instead of $\kappa \cdot \text{toFun} \times$. The measurability condition is called measurable' in the structure to reserve the name measurable for the more useful measurability of the coercion to a function.

Kernels are fully specified by their action on measurable functions. That is, if two kernels $\kappa, \eta : X \rightsquigarrow Y$ are such that for every measurable function $f : Y \to \mathbb{R}_{+\infty}$ and every $x \in X$, $\int_Y f(y)d\kappa(x) = \int_Y f(y)d\eta(x)$, then $\kappa = \eta$. We will use that fact repeatedly in the following in order to define kernels by giving their integrals.

## 2.1 Classes of kernels

A kernel $\kappa : X \rightsquigarrow Y$ is a Markov kernel if $\kappa(x)$ is a probability measure for every $x \in X$. If the supremum of $\kappa(x)(Y)$ over $x \in X$ is finite, then $\kappa$ is said to be a finite kernel. Finally, we also defined the class of s-finite kernels, which are kernels that can be expressed as a countable sum of finite kernels. Those three properties are denoted by typeclasses `[IsMarkovKernel $\kappa$]`, `[IsFiniteKernel $\kappa$]` and `[IsSFiniteKernel $\kappa$]` respectively. `[VO18]` is a good reference on classes of kernels, in particular the less often used s-finite class.

There is a fourth class of kernels that is mentioned in the literature but that we did not implement in `MATHLIB`: $\sigma$-finite kernels. Those are sums of countably many finite kernels that are pairwise singular. They don't enjoy the nice composition properties of the other classes (see the next subsection), and we decided to wait for a concrete use case before implementing them. Although, as we will discuss further down, the Radon-Nikodym theorem for kernels stated below could be a potential use case.

A measure can be seen as a kernel from the one-point space $1$ to the space on which it is defined. A random variable $X : X \to Y$ can be seen as a kernel $D_X : X \rightsquigarrow Y$ that maps $x$ to the Dirac measure at $X(x)$. Such a kernel is called a deterministic kernel and is a Markov kernel. Among the deterministic kernels, a few play a special role. The identity kernel $\text{id}_X : X \rightsquigarrow X$ is the deterministic kernel associated with the identity function: it
maps $x$ to the Dirac measure at $x$. The copy kernel $\Delta_x : \mathcal{X} \rightsquigarrow \mathcal{X} \times \mathcal{X}$ maps $x$ to the Dirac measure at $(x, x)$. The discard kernel $!_x : \mathcal{X} \rightsquigarrow 1$ maps $x$ to the unique probability measure on the one-point space $1$. Another useful (not deterministic) kernel is the constant kernel $\kappa_\mu : \Omega \rightsquigarrow \mathcal{X}$ which maps every point of $\Omega$ to the same probability measure $\mu$ on $\mathcal{X}$.

## 2.2 Composition of kernels

Kernels can be assembled in various ways. The first of those is the composition, which from $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and $\eta : \mathcal{Y} \rightsquigarrow \mathcal{Z}$ produces a kernel $\eta \circ_k \kappa : \mathcal{X} \rightsquigarrow \mathcal{Z}$ (Kernel.comp). It is such that for all measurable functions $f : \mathcal{Z} \to \mathbb{R}_{+\infty}$ and all $x \in \mathcal{X}$,

$$ \int_{z} f(z)d(\eta \circ_k \kappa)(x) = \int_{y} \int_{z} f(z)d\eta(y)d\kappa(x). $$

We use the notation $\circ_k$ to reflect the notation in MATHLIB, in which we can't use simply $\circ$ since this is notation for function composition: a kernel $\mathcal{X} \rightsquigarrow \mathcal{Y}$ is a function, but from $\mathcal{X}$ to $\mathcal{M}(\mathcal{Y})$, and composition of kernels is not the same as their composition as functions. We omit the proof that $\eta \circ_k \kappa$ is indeed a well defined kernel and refer the reader to [ACS25]. We will also omit similar proofs for other constructions of kernels. A composition of Markov kernels is a Markov kernel, and finite and s-finite kernels are also stable by composition. $\sigma$-finite kernels (which we did not implement) are not stable by composition. For finite kernels, the existence of a uniform bound $C < \infty$ such that $\kappa(x)(\mathcal{Y}) < C$ for all $x$ is important to have stability: if we require only that $\kappa(x)(\mathcal{Y}) < \infty$ for all $x$, then the composition of two such kernels would be $\sigma$-finite but not necessarily finite. The stability of Markov, finite and s-finite kernels will also be true for the other constructions presented below. For $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{Z}$, we write $\kappa_{\text{fst}} : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and $\kappa_{\text{snd}} : \mathcal{X} \rightsquigarrow \mathcal{Z}$ for the compositions of $\kappa$ with the deterministic kernels associated with the first and second projections respectively.

We can also form the parallel composition of two s-finite kernels $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and $\eta : \mathcal{T} \rightsquigarrow \mathcal{U}$, which is a kernel $\kappa \|_k \eta : \mathcal{X} \times \mathcal{T} \rightsquigarrow \mathcal{Y} \times \mathcal{U}$ (Kernel.parallelComp) defined by

$$ (\kappa \|_k \eta)(x, t) = \kappa(x) \otimes \eta(t), $$

where $\kappa(x) \otimes \eta(t)$ is the product measure of $\kappa(x)$ and $\eta(t)$. Note that here we need the kernels to be s-finite to ensure that the product measure is a measurable function. In MATHLIB, the parallel composition is defined for all kernels, but it is set to the default value 0 if either kernel is not s-finite. This is a standard trick in formalization to avoid having to carry hypotheses in every expression involving that parallel composition (they will instead be present as
hypotheses of the lemmas) [Buz20]. All kernel constructions that require s-finiteness use the same trick.

From those two operations, we can form other useful kernels. The product of two s-finite kernels $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and $\eta : \mathcal{X} \rightsquigarrow \mathcal{Z}$ is the kernel $\kappa \times_k \eta : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{Z}$, defined to be equal to $(\kappa \|_k \eta) \circ_k \Delta_{\mathcal{X}}$ (Kernel.prod). It is such that for all $x$, $(\kappa \times_k \eta)(x) = \kappa(x) \otimes \eta(x)$.

A more complex construction gives a kernel $\kappa \otimes_k \eta : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{Z}$ from two s-finite kernels $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and $\eta : \mathcal{X} \times \mathcal{Y} \rightsquigarrow \mathcal{Z}$. This is called composition-product of kernels in MATHLIB (Kernel.compProd). In the literature it is often called simply either composition or product depending on the source, but in LEAN code we have to use distinct names. Intuitively, that operation corresponds to generating data sequentially: first a random element $X$ is observed, then $\kappa$ describes the conditional distribution of $Y$ given $X$, and finally $\eta$ describes the distribution of $Z$ given $(X, Y)$. We can combine $\kappa$ and $\eta$ into a single kernel that gives the joint distribution of $(Y, Z)$ given $X$. This is $\kappa \otimes_k \eta$. It is defined as

$$ \kappa \otimes_k \eta = D_{s_{\mathcal{X},\mathcal{Y}}} \circ_k (\eta \|_k \mathrm{id}) \circ_k D_{\mathrm{assoc}_{\mathcal{X},\mathcal{Y},\mathcal{Y}}} \circ_k (\mathrm{id} \|_k \Delta_{\mathcal{Y}}) \circ_k (\mathrm{id} \|_k \kappa) \circ_k \Delta_{\mathcal{X}} $$

in which $s_{\mathcal{X},\mathcal{Y}} : \mathcal{X} \times \mathcal{Y} \to \mathcal{Y} \times \mathcal{Z}$ is the function that swaps the two coordinates, and $\mathrm{assoc}_{\mathcal{X},\mathcal{Y},\mathcal{Y}}$ is the associator $\mathcal{X} \times (\mathcal{Y} \times \mathcal{Y}) \to (\mathcal{X} \times \mathcal{Y}) \times \mathcal{Y}$. More importantly than the formula used in the definition, the composition-product satisfies the property that for all measurable functions $f : \mathcal{Y} \times \mathcal{Z} \to \mathbb{R}_{+\infty}$ and all $x \in \mathcal{X}$,

$$ \int_{(y,z)} f(y,z)d(\kappa \otimes_k \eta)(x) = \int_y \int_z f(y,z)d\eta(x,y)d\kappa(x). $$

Composition of kernels is associative, $\xi \circ_k (\eta \circ_k \kappa) = (\xi \circ_k \eta) \circ_k \kappa$, and the name “identity kernel” is justified by the fact that it is a neutral element for that composition, $\mathrm{id} \circ_k \kappa = \kappa$ and $\kappa \circ_k \mathrm{id} = \kappa$. The composition-product is, mathematically speaking, associative as well, but here the type theory used in LEAN makes it more complex to express that property. Indeed, suppose that we have three s-finite kernels $\kappa : \mathcal{T} \rightsquigarrow \mathcal{X}$, $\eta : \mathcal{T} \times \mathcal{X} \rightsquigarrow \mathcal{Y}$ and $\xi : \mathcal{T} \times (\mathcal{X} \times \mathcal{Y}) \rightsquigarrow \mathcal{Z}$. Then we can form a kernel $(\kappa \otimes_k \eta) \otimes_k \xi$ without issue. Associativity would be the statement that this is the same kernel as $\kappa \otimes_k (\eta \otimes_k \xi)$. However, there are problems with that second kernel: we can't build $\eta \otimes_k \xi$ since the type of $\xi$ is $\mathcal{T} \times (\mathcal{X} \times \mathcal{Y}) \rightsquigarrow \mathcal{Z}$ and not $(\mathcal{T} \times \mathcal{X}) \times \mathcal{Y} \rightsquigarrow \mathcal{Z}$. The product of types is not associative. Similarly, even if we could obtain $\kappa \otimes_k (\eta \otimes_k \xi)$, its type would be $\mathcal{T} \rightsquigarrow \mathcal{X} \times (\mathcal{Y} \times \mathcal{Z})$ while the type of $(\kappa \otimes_k \eta) \otimes_k \xi$ is $\mathcal{T} \rightsquigarrow (\mathcal{X} \times \mathcal{Y}) \times \mathcal{Z}$. We
can insert explicitly compositions with associators to fix the type issues, but that makes the statement of associativity more cumbersome.

Since measures can be seen as kernels from the one-point space, we can use these operations to combine measures and kernels. This leads to a composition definition $\kappa \circ_m \mu$ for a kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and a measure $\mu$ on $\mathcal{X}$, which is a measure on $\mathcal{Y}$. Similarly, we can define a composition-product $\kappa \otimes_m \mu$ for a kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ and a measure $\mu$ on $\mathcal{X}$, which is a measure on $\mathcal{X} \times \mathcal{Y}$ (Measure.compProd). For a deterministic kernel $D_X$ associated with a random variable $X : \mathcal{X} \to \mathcal{Y}$, $D_X \circ_m \mu$ is the pushforward measure of $\mu$ by $X$, which we denote by $X_*\mu$ and is such that $X_*\mu(B) = \mu(X^{-1}(B))$ for all measurable sets $B \subseteq \mathcal{Y}$.

The composition is related to the composition-product by the formula $\eta \circ_k \kappa = (\kappa \otimes_k \eta)_{\text{snd}}$. In a first attempt at implementing kernels in MATHLIB, we defined the composition-product as the primitive operation on kernels, and derived the composition and the product from it. This had the advantage that once the work is done to prove that the composition-product is indeed a kernel, the other operations are easy to define. There is however a drawback: the composition-product requires s-finite kernels, while the composition should be defined for all kernels. Defining the composition as $(\kappa \otimes_k \eta)_{\text{snd}}$ is a loss in generality. The change to use composition and parallel composition as basic operations also aligns better with the categorical viewpoint on kernels.

## 2.3 Copy-discard and Markov categories

We can form several categories with measurable spaces as objects and kernels as morphisms. Those are not defined as categories in MATHLIB, but every result needed to show the properties of those categories is present in the library and ready to be used if someone later needs to consider kernels as a copy-discard or Markov category. We briefly mention them here to explain how they relate to the objects presented so far. For an explanation of the different types of categories named below and the theory of Markov categories, see [CJ19; Fri20].

Measurable spaces and s-finite kernels are a copy-discard category (and in particular a symmetric monoidal category) with the following data:

* Objects: measurable spaces

* Morphisms: s-finite kernels, with identity kernels and composition of kernels $\eta \circ_k \kappa$

* Tensor product of objects: product of measurable spaces $\mathcal{X} \times \mathcal{Y}$

* Tensor product of morphisms: parallel composition $\kappa \|_k \eta$

* Unit: measurable space with one element (denoted by 1)

* The associator, left and right unitors and braiding are given by the deterministic kernels of the corresponding equivalences between measurable spaces
* Copy morphism: $\Delta_{\mathcal{X}} : \mathcal{X} \rightsquigarrow \mathcal{X} \times \mathcal{X}$

* Delete morphism: $!_{\mathcal{X}} : \mathcal{X} \rightsquigarrow 1$

If we restrict the morphisms to Markov kernels, we get a Markov category, which is a copy-discard category in which for every morphism $\kappa$, $d_{\mathcal{X}} \circ \kappa = d_{\mathcal{X}}$.

## 2.4 The Ionescu-Tulcea theorem

We highlight a recent application of the kernel framework: the Ionescu-Tulcea theorem, which was implemented in LEAN and added to MATHLIB by Etienne Marion [Mar25].

Markov kernels arise naturally in the description of the sequential evolution of a system. For example, in stochastic optimization, an algorithm sequentially queries a function $f : \mathbb{R}^d \to \mathbb{R}$ at points $X_1, X_2, \dots$ that are chosen based on the results of previous queries, with the goal of finding a minimizer of the function. A query returns a noisy observation $Y_i$ of the function value $f(X_i)$ (or possibly additional information like its gradient).

The conditional distribution of $Y_i$ given $X_i$ is naturally described by a Markov kernel $\kappa_i : \mathbb{R}^d \rightsquigarrow \mathbb{R}$. The choice of $X_{i+1}$ based on the previous queries is also described by a Markov kernel $\eta_i : (\mathbb{R}^d \times \mathbb{R})^i \rightsquigarrow \mathbb{R}^d$, and we thus naturally end up with a chain of kernels to describe the interaction. We will want to make global probabilistic statements about the optimization procedure. For example, we may want to prove that with probability 1, the sequence of queries converges to a minimizer of $f$. To do that, we need to define a probability space on which all the random variables $X_i$ and $Y_i$ are defined. The Ionescu-Tulcea theorem gives such a probability space. We can see it as a tool to go from the kernel point of view in which we describe transitions between multiple measurable spaces to the single probability space point of view, in which every random variable is defined on the same space.

**Theorem 2.1 (Ionescu-Tulcea).** Let $(X_n)_{n \in \mathbb{N}}$ be a family of measurable spaces. Let $(\kappa_n)_{n \in \mathbb{N}}$ be a family of Markov kernels such that for any $n$, $\kappa_n$ is a kernel from $\prod_{i=0}^n X_i$ to $X_{n+1}$. Then there exists a unique Markov kernel $\xi : X_0 \rightsquigarrow \prod_{i=1}^\infty X_i$ such that for any $n \ge 1$, $\pi_{[1,n]} * \xi = \kappa_0 \otimes_k \dots \otimes_k \kappa_{n-1}$. Here $\pi_{[1,n]} : \prod_{i=1}^\infty X_i \to \prod_{i=1}^n X_i$ is the projection on the first $n$ coordinates.

This theorem applies to the stochastic optimization setting described above with kernels $\eta_i \otimes_k \kappa_i : (\mathbb{R}^d \times \mathbb{R})^i \rightsquigarrow \mathbb{R}^d \times \mathbb{R}$ (where we abuse notation and see $\kappa_i$ as a kernel from $(\mathbb{R}^d \times \mathbb{R})^i \times \mathbb{R}^d$ to $\mathbb{R}$ that ignores the first argument).

In the formalization of that Theorem, an important difficulty is the lack of associativity of products of types, as explained in the previous subsection about the composition-product. The article [Mar25] discusses this issue in detail and presents a formalization strategy to avoid inserting associators everywhere.
# 3 Disintegration and conditional distributions

The previous section showed what kernels are and how they can be composed in different ways. We now turn to the main theorem about kernels that we formalized in MATHLIB: the disintegration theorem.

We say that a kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{Z}$ is disintegrated by a kernel $\eta : \mathcal{X} \times \mathcal{Y} \rightsquigarrow \mathcal{Z}$ if $\kappa = \kappa_{\text{fst}} \otimes \eta$. We also say that $\eta$ is a conditional kernel for $\kappa$. In the particular case of a product measure $\rho$ on $\mathcal{Y} \times \mathcal{Z}$ (which corresponds to the special case of $\mathcal{X}$ the unit space), disintegration means that $\rho$ can be expressed with the measure $\rho_{\text{fst}}$ on $\mathcal{Y}$ and a kernel $\eta : \mathcal{Y} \rightsquigarrow \mathcal{Z}$ as $\rho = \rho_{\text{fst}} \otimes \eta$: a joint measure can be decomposed into a marginal measure and a conditional distribution.

In general measurable spaces, disintegration is not always possible (refer to [Fad85] for exact hypotheses). However, it is possible for every finite kernel if we add assumptions on the spaces.

**Definition 3.1 (Assumptions on measurable spaces).** A measurable space $\mathcal{X}$ has a countably generated $\sigma$-algebra if there exists a countable collection of measurable sets such that the $\sigma$-algebra of $\mathcal{X}$ is the smallest $\sigma$-algebra containing all those sets.

A standard Borel space is a measurable space such that there exists a Polish topology on the underlying set such that the $\sigma$-algebra is the Borel $\sigma$-algebra of that topology.

We note that a standard Borel space has a countably generated $\sigma$-algebra. Furthermore, any standard Borel space is isomorphic as a measurable space to a subset of $\mathbb{R}$. Most spaces used in applied mathematics are standard Borel.

We first introduced the disintegration in MATHLIB as a definition: under assumptions on the spaces, we built a kernel $\eta$ disintegrating a given kernel $\kappa$. Since then, a predicate has been introduced to express that a kernel disintegrates another.

class IsCondKernel (κ : Kernel 𝒳 (Y × 𝒰)) (κCond : Kernel (𝒳 × 𝒴) 𝒰) : Prop where
protected disintegrate : κ.fst ⊗ₖ κCond = κ

The advantage of that predicate is that it allows us to work with disintegrations in more general spaces, if we happen to have another mechanism to know that a kernel disintegrates another. A conditional kernel is almost everywhere equal to a Markov kernel, and almost everywhere unique. Furthermore, if $\eta$ disintegrates $\kappa$ and $\eta$ is $\kappa_{\text{fst}}$-almost everywhere equal to $\eta'$, then $\eta'$ also disintegrates $\kappa$. That means that if $\kappa$ admits a disintegration, then there is a true Markov kernel that disintegrates $\kappa$.
The main result of this section is the following theorem, which states that there exists a conditional kernel under assumptions on the spaces. It is formalized in MATHLIB as a definition Kernel.condKernel that gives a conditional kernel, which we ensure is Markov, together with an instance of the class IsCondKernel.

**Theorem 3.2 (Kernel disintegration).** If $\mathcal{X}$ is a standard Borel space and either $\mathcal{X}$ is countable or $\mathcal{Y}$ has a countably generated $\sigma$-algebra, then every finite kernel $\mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{X}$ admits a disintegration.

As a corollary, if $\mathcal{X}$ is a standard Borel space then every finite measure on $\mathcal{Y} \times \mathcal{X}$ admits a disintegration. The proof goes through the construction of densities for finite kernels, and in particular it also gives a formalization of a Radon-Nikodym theorem. In order to state that theorem, we need to introduce the definition of the kernel with a density with respect to another kernel. For $f: \mathcal{X} \times \mathcal{Y} \to \mathbb{R}_{+\infty}$ a jointly measurable function and $\eta: \mathcal{X} \rightsquigarrow \mathcal{Y}$ an s-finite kernel, we define the kernel $f \cdot \eta: \mathcal{X} \rightsquigarrow \mathcal{Y}$ (Kernel.withDensity) by

$$ (f \cdot \eta)(x)(B) = \int_{y \in B} f(x, y)d\eta(x). $$

**Theorem 3.3 (Radon-Nikodym).** Suppose that either $\mathcal{X}$ is countable or $\mathcal{Y}$ has a countably generated $\sigma$-algebra. Let $\kappa, \eta: \mathcal{X} \rightsquigarrow \mathcal{Y}$ be finite kernels. Then there exists a jointly measurable function $\frac{d\kappa}{d\eta}: \mathcal{X} \times \mathcal{Y} \to \mathbb{R}_{+\infty}$ and a finite kernel $\kappa_{\perp\eta}: \mathcal{X} \rightsquigarrow \mathcal{Y}$ such that for every $x \in \mathcal{X}$, $\kappa_{\perp\eta}(x)$ is singular with respect to $\eta(x)$ and $\kappa = \frac{d\kappa}{d\eta} \cdot \eta + \kappa_{\perp\eta}$.

The remarkable feature of Theorem 3.3 is the joint measurability of the density $\frac{d\kappa}{d\eta}$ (Kernel.rnDeriv). If we only wanted measurability of $\frac{d\kappa}{d\eta}(x)$ for every $x \in \mathcal{X}$, the proof would be much simpler and would not require the assumption that $\mathcal{Y}$ has a countably generated $\sigma$-algebra: we would simply use the Radon-Nikodym theorem for measures on $\mathcal{Y}$ for each $x \in \mathcal{X}$ separately. The kernel Radon-Nikodym derivative is almost everywhere equal to the measure Radon-Nikodym derivative $\frac{d\kappa(x)}{d\eta(x)}$ and the main point of the theorem is that we can choose those derivatives (which are defined only almost everywhere) in a jointly measurable way.

Theorem 3.3 is stated for finite kernels, but it could be extended to $\kappa$ an s-finite kernel and $\eta$ a $\sigma$-finite kernel (which would require an implementation of $\sigma$-finite kernels).
**3.1 Proof of the disintegration theorem**

The main result from which both the disintegration theorem and the Radon-Nikodym theorem are derived is the existence of densities for finite kernels. For two kernels $\kappa, \eta : \mathcal{X} \rightsquigarrow \mathcal{Y}$, we write $\kappa \le \eta$ if for all $x \in \mathcal{X}$ and measurable set $B$ of $\mathcal{Y}$, $\kappa(x)(B) \le \eta(x)(B)$.

**Theorem 3.4 (Existence of densities).** Let $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{Z}$ and $\nu : \mathcal{X} \rightsquigarrow \mathcal{Y}$ be two finite kernels with $\kappa_{\text{fst}} \le \nu$, where $\mathcal{Y}$ has a countably generated $\sigma$-algebra. Then there exists a function $d_{\kappa, \nu} : \mathcal{X} \times \mathcal{Y} \to \mathcal{P}(\mathcal{Z}) \to \mathbb{R}$ jointly measurable on $\mathcal{X} \times \mathcal{Y}$ such that for all $x \in \mathcal{X}$ and all measurable sets $B$ of $\mathcal{Y}$ and $C$ of $\mathcal{Z}$, $\int_{y \in B} d_{\kappa, \nu}(x, y)(C) d\nu(x) = \kappa(x)(B \times C)$.

We added a definition for the density, *Kernel.density*, and its properties can be found
in the same file.

**From densities to disintegration** Using the fact that a standard Borel space embeds measurably into $\mathbb{R}$ (embeddingReal), we can reduce the proof of Theorem 3.2 to the case where $\mathcal{Z} = \mathbb{R}$: for a general standard Borel space, we map the kernel to one with values in $\mathbb{R}$, disintegrate it, and then pull back the conditional kernel to the original space. Thus from a finite kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathbb{R}$, we want to build a kernel $\eta : \mathcal{X} \times \mathcal{Y} \rightsquigarrow \mathbb{R}$ such that $\kappa = \kappa_{\text{fst}} \otimes \eta$. Since $\eta$ takes values in the measures on $\mathbb{R}$, we can build them from their cumulative distribution functions. A cumulative distribution function (CDF) is a nondecreasing right-continuous function $F : \mathbb{R} \to \mathbb{R}$ with limit 0 at $-\infty$ and limit 1 at $+\infty$, and characterizes a probability measure on $\mathbb{R}$. Nondecreasing right-continuous functions $\mathbb{R} \to \mathbb{R}$ are bundled in MATHLIB in a type *StieltjesFunction* and the corresponding measure can be obtained with *StieltjesFunction.density*.

More precisely, here we want to define a conditional kernel CDF for $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathbb{R}$ with
respect to another kernel $\nu : \mathcal{X} \rightsquigarrow \mathcal{Y}$ (and we will apply it to $\nu = \kappa_{\text{fst}}$). We call a function
$f : \mathcal{X} \times \mathcal{Y} \to \mathbb{R} \to \mathbb{R}$ which is nondecreasing right-continuous for all $p \in \mathcal{X} \times \mathcal{Y}$ a conditional
kernel CDF of $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathbb{R}$ with respect to $\nu : \mathcal{X} \rightsquigarrow \mathcal{Y}$ if for all $z \in \mathbb{R}$, $p \mapsto f(p)(z)$ is
measurable on the product $\mathcal{X} \times \mathcal{Y}$, if $f(p)$ tends to 0 at $-\infty$ and to 1 at $+\infty$ for all $p \in \mathcal{X} \times \mathcal{Y}$,
if $y \mapsto f(x, y)(z)$ is $\nu(x)$-integrable for all $x \in \mathcal{X}$ and $z \in \mathbb{R}$ and for all measurable sets $B$ of
$\mathcal{Y}$, $\int_{y \in B} f(x, y)(z) d\nu(x) = \kappa(x)(B \times (-\infty, z])$. For each $(x, y) \in \mathcal{X} \times \mathcal{Y}$, $f(x, y)$ is a CDF
and thus defines a probability measure on $\mathbb{R}$. The joint measurability of $f$ ensures that the
collection of measures defines a kernel.

Thanks to the continuity properties of CDFs, we can build first a function $f : \mathcal{X} \times \mathcal{Y} \to \mathbb{Q} \to \mathbb{R}$ (note the rational numbers $\mathbb{Q}$ instead of the reals $\mathbb{R}$) satisfying all those properties,
and then extend it to a function $\mathcal{X} \times \mathcal{Y} \to \mathbb{R} \to \mathbb{R}$. The countability of $\mathbb{Q}$ will be helpful when it comes to proving the right-continuity of $f$.

If $\mathcal{X}$ is countable, we can provide for each $x \in \mathcal{X}$ a measurable function $f : \mathcal{Y} \to \mathbb{Q} \to \mathbb{R}$ and proceed as above to obtain a kernel $\mathcal{Y} \rightsquigarrow \mathbb{R}$. Since $\mathcal{X}$ is countable, measurability is not an issue and we can put those kernel together into a kernel $\mathcal{X} \times \mathcal{Y} \rightsquigarrow \mathbb{R}$. If on the other hand $\mathcal{X}$ is not countable, we can't proceed separately for each $x \in \mathcal{X}$ and have to build a function which is measurable on the product. We are able to do so if $\mathcal{Y}$ has a countably generated $\sigma$-algebra thanks to the density provided by Theorem 3.4. The density $d_{\kappa,\nu} : \mathcal{X} \times \mathcal{Y} \to \mathcal{P}(\mathbb{R}) \to \mathbb{R}$ is jointly measurable on $\mathcal{X} \times \mathcal{Y}$ and such that for all $x \in \mathcal{X}$ and all measurable sets $B$ of $\mathcal{Y}$ and all $q \in \mathbb{Q}$, $\int_{y \in B} d_{\kappa,\nu}(x,y)((-\infty,q]) d\nu(x) = \kappa(x)(B \times (-\infty,q])$. It thus satisfies the integral condition, and we are able to check that the other conditions are also fulfilled.

The countability of $\mathbb{Q}$ is important when proving the right-continuity. We want to prove that for almost all $p \in \mathcal{X} \times \mathcal{Y}$, $d_{\kappa,\nu}(p)$ is right-continuous on $\mathbb{Q}$. Since $\mathbb{Q}$ is countable, it is enough to prove that for all $q \in \mathbb{Q}$, for almost all $p$, $d_{\kappa,\nu}(p)$ is right-continuous at $q$. To prove the almost everywhere equality of the limit on the right and the value of the function at a point $q \in \mathbb{Q}$, it suffices to prove that the integrals of those functions are equal on any measurable set $A$ of $\mathcal{X} \times \mathcal{Y}$. And since we know what the integrals of $d_{\kappa,\nu}$ are, we can prove that equality.

**From densities to Radon-Nikodym**

From $\kappa, \eta : \mathcal{X} \rightsquigarrow \mathcal{Y}$ two finite kernels we want to build a jointly measurable function $\frac{d\kappa}{d\eta} : \mathcal{X} \times \mathcal{Y} \to \mathbb{R}_{+ , \infty}$ and a finite kernel $\kappa_{\perp \eta} : \mathcal{X} \rightsquigarrow \mathcal{Y}$ such that for every $x \in \mathcal{X}$, $\kappa_{\perp \eta}(x)$ is singular with respect to $\eta(x)$ and $\kappa = \frac{d\kappa}{d\eta} \cdot \eta + \kappa_{\perp \eta}$.

We use Theorem 3.4 for $\mathcal{Z} = 1$, the one-point space, to obtain the following corollary.

**Corollary 3.5.** Let $\kappa, \eta : \mathcal{X} \rightsquigarrow \mathcal{Y}$ be two finite kernels with $\kappa \le \eta$, in which $\mathcal{Y}$ has a countably generated $\sigma$-algebra. Then there exists a function $d_{\kappa,\eta} : \mathcal{X} \times \mathcal{Y} \to \mathbb{R}$ jointly measurable on $\mathcal{X} \times \mathcal{Y}$ such that for all $x \in \mathcal{X}$ and all measurable sets $B$ of $\mathcal{Y}$, $\int_{y \in B} d_{\kappa,\eta}(x,y) d\eta(x) = \kappa(x)(B)$.

In the Radon-Nikodym Theorem 3.3, we don't have the condition $\kappa \le \eta$. We apply that corollary to $\kappa$ and $\kappa + \eta$, which satisfy that condition. The Radon-Nikodym derivative $\frac{d\kappa}{d\eta}$ is defined by the ratio $\frac{d_{\kappa,\kappa+\eta}}{1-d_{\kappa,\kappa+\eta}}$. It is then not hard to check that it satisfies the required properties, with $\kappa_{\perp\eta} = \kappa - \frac{d\kappa}{d\eta} \cdot \eta$. Thanks to the Radon-Nikodym Theorem 3.3 which is now proved, the condition $\kappa \le \eta$ in Corollary 3.5 can be relaxed to absolute continuity of $\kappa$ with respect to $\eta$ (that is, absolute continuity of $\kappa(x)$ with respect to $\eta(x)$ for all $x$).
**Proof of the existence of densities** We now give a sketch of the proof of Theorem 3.4. Our proof follows [Kal21, Theorem 9.27]. If we were interested in a single $x \in \mathcal{X}$, we could use the Radon-Nikodym theorem for measures to obtain a density. For $x \in \mathcal{X}$, $y \in \mathcal{Y}$ and $C$ a measurable set of $\mathcal{Z}$, let $\kappa(x)|_{\mathcal{Y} \times C}$ be the restriction of the measure $\kappa(x)$ to the set $\mathcal{Y} \times C$. Let then $(\kappa(x)|_{\mathcal{Y} \times C})_{\text{fst}}$ be the marginal of that measure on $\mathcal{Y}$. Then we could define the density as $d'_{\kappa, y}(x, y)(C) = \frac{d(\kappa(x)|_{\mathcal{Y} \times C})_{\text{fst}}}{d\nu(x)}(y)$ and it would satisfy the integral condition and be measurable in $y$. The problem is to obtain a density that is jointly measurable on $\mathcal{X} \times \mathcal{Y}$.

If the $\sigma$-algebra on $\mathcal{Y}$ is finite, then we can proceed as follows. Let $\{B_1, \dots, B_k\}$ be a partition of $\mathcal{Y}$ that generates its $\sigma$-algebra and for $y \in \mathcal{Y}$, let $B(y)$ be the set in the partition containing $y$ (this is a measurable function). We can then define $d_{\kappa, y}(x, y)(C) = \frac{\kappa(x)(B(y) \times C)}{\nu(x)(B(y))}$, which is jointly measurable on $\mathcal{X} \times \mathcal{Y}$ and satisfies the integral condition.

Our hypothesis on $\mathcal{Y}$ is not that its $\sigma$-algebra is finite, but that it is countably generated. But that assumption will allow us to reduce to the finite case. For a countably generated $\sigma$-algebra, there exists a sequence of finer and finer $\sigma$-algebras $\mathcal{F}_n$, each generated by a partition $\{B_{n,1}, \dots, B_{n,k_n}\}$, whose union generates the $\sigma$-algebra of $\mathcal{Y}$.

For each $n$, we can define a density $d_{\kappa, y}^n$ as above for the finite $\sigma$-algebra $\mathcal{F}_n$, which is jointly measurable on $\mathcal{X} \times \mathcal{Y}$ and satisfies the integral condition for all sets in $\mathcal{F}_n$. The key to turn that sequence into a density is to use a martingale convergence theorem, which was formalized and added to MATHLIB in [YD23]. $(\mathcal{F}_n)_{n \in \mathbb{N}}$ is a filtration, and for each $x \in \mathcal{X}$ and each measurable set $C$ of $\mathcal{Z}$, the sequence of functions $(d_{\kappa, y}^n(x, \cdot)(C))_{n \in \mathbb{N}} : \mathbb{N} \to \mathcal{Y} \to \mathbb{R}$ can be shown to be a martingale which is bounded in $L^1$.

Therefore, by the martingale convergence theorem, it converges almost everywhere to a limit. That limit is almost everywhere equal to the limit superior of the sequence, which is a jointly measurable function. We define $d_{\kappa, y}(x, y)(C)$ to be that limit superior. It is jointly measurable on $\mathcal{X} \times \mathcal{Y}$ and since it is almost everywhere the limit of the density process in $L^1$, it satisfies the integral condition for all measurable sets $C$ of $\mathcal{Z}$.

## 3.2 Conditional distributions

The application of disintegration to random variables gives the notion of conditional distribution. Let $(\Omega, P)$ be a probability space, and let $X : \Omega \to \mathcal{X}$ and $Y : \Omega \to \mathcal{Y}$ be two random variables. We can consider the joint distribution of $(X, Y)$, which is the probability measure $(X, Y)_*P$ on $\mathcal{X} \times \mathcal{Y}$ that we denote by $P_{XY}$. The distribution of $X$ is likewise denoted by $P_X$. If $\mathcal{Y}$ is a standard Borel space, then by the disintegration theorem there exists a kernel $P_{Y|X} : \mathcal{X} \rightsquigarrow \mathcal{Y}$ such that $P_{XY} = P_X \otimes_m P_{Y|X}$. That kernel is called a (regular) conditional
probability distribution of $Y$ given $X$. This disintegration is exactly how we define conditional distributions in Mathlib.

def condDistrib (Y : Ω → 𝒴) (X : Ω → 𝒳) (μ : Measure Ω) [IsFiniteMeasure μ] :
Kernel 𝒳 𝒴 :=
(μ.map fun a ↦→ (X a, Y a)).condKernel

As an important special case of a conditional distribution, in a standard Borel space we can define a Markov kernel associated to a conditional expectation. Let $\mathcal{G}$ be a sub-$\sigma$-algebra of the $\sigma$-algebra $\mathcal{F}$ of $\Omega$. Let $E$ be a Banach space. The conditional expectation of an integrable function $f : \Omega \to E$ with respect to $\mathcal{G}$ is a $\mathcal{G}$-measurable function $P[f|\mathcal{G}] : \Omega \to E$ such that for every $\mathcal{G}$-measurable set $A$, $\int_A P[f|\mathcal{G}]dP = \int_A f dP$. It exists and is almost everywhere unique. For a measurable set $B$, we write $P(B|\mathcal{G})$ for $P[1_B|\mathcal{G}]$.

In a standard Borel space, we can obtain the conditional distributions for the functions $Y = \text{id}_\Omega$ and $X$ the identity function on $\Omega$ but seen as a function from $(\Omega, \mathcal{G})$ to $(\Omega, \mathcal{F})$. This is a kernel from $(\Omega, \mathcal{G})$ to $(\Omega, \mathcal{F})$, which we denote by $P^\mathcal{G}$ and is called **condExpKernel** in MATHLIB. Its main property is that for every integrable function $f : \Omega \to \mathbb{R}$, the conditional expectation $P[f|\mathcal{G}]$ is almost everywhere equal to the function $\omega \mapsto P^\mathcal{G}(\omega)[f]$.

theorem condExp_ae_eq_integral_condExpKernel [NormedAddCommGroup E] {f : Ω → E}
[NormedSpace ℝ E] [CompleteSpace E] (hm : m ≤ mΩ) (hf_int : Integrable f μ) :
μ[f|m] =^m [μ] fun ω => ∫ y, f y ∂condExpKernel μ m ω

That code uses the notation $f =^m [\mu] g$, which means that $f$ and $g$ are equal $\mu$-almost
everywhere.

In general the conditional expectations $P(A|\mathcal{G})$ and $P(B|\mathcal{G})$ for different sets $A$ and $B$
are two functions defined only almost everywhere, and for example we have only almost
everywhere the equality $P(A \cup B|\mathcal{G}) = P(A|\mathcal{G}) + P(B|\mathcal{G})$ for $A,B$ disjoint. We can go around
that issue as long as only countably many sets are involved, but having a unique version of
the conditional expectation that consolidates into a kernel (condExpKernel) is much more
convenient since it allows the use of true equality.

3.3 Posterior kernel

A measure $\mu$ on $\mathcal{X}$ and a kernel $\kappa : \mathcal{X} \rightsquigarrow \mathcal{Y}$ define a joint measure $\mu \otimes_m \kappa$ on $\mathcal{X} \times \mathcal{Y}$.
We already know a disintegration of that measure by construction: it is disintegrated by $\kappa$.
However we can map the measure by the function that swaps the two coordinates, and we
obtain a joint measure on $\mathcal{Y} \times \mathcal{X}$. The marginal of that measure on $\mathcal{Y}$ is $\kappa \circ_m \mu$. If $\mathcal{X}$ is a
standard Borel space, then by the disintegration theorem there exists a kernel $\kappa_{\mu}^{\dagger} : \mathcal{Y} \rightsquigarrow \mathcal{X}$
such that the swapped joint measure $s_{\mathcal{X} \times \mathcal{Y}}(\mu \otimes_{m} \kappa)$ is equal to $(\kappa \circ_{m} \mu) \otimes_{m} \kappa_{\mu}^{\dagger}$.

That kernel is called the posterior kernel or Bayesian inverse of $\kappa$ with respect to $\mu$. It is a central object in Bayesian statistics, where $\mu$ is interpreted as the prior distribution on a parameter, $\kappa$ gives the distribution of the observed data given the parameter, and $\kappa_{\mu}^{\dagger}$ maps data to a distribution on the parameter called posterior distribution, that is interpreted as the updated belief about the parameter after observing the data.

A remark about notation: the notation $\kappa_{\mu}^{\dagger}$ makes the measure explicit, which departs from the notation $\kappa^{\dagger}$ used in parts of the literature on posterior kernels [Cle+17], in which the measure is understood from the context. In the code, we have to keep the measure explicit because it cannot be inferred otherwise, and we chose to keep the notation of this paper close to the code.

def posterior (κ : Kernel Ω 𝒳) (μ : Measure Ω) [IsFiniteMeasure μ] [IsFiniteKernel κ] :
Kernel 𝒳 Ω :=
((μ ⊗_m κ).map Prod swap).condKernel

scoped[ProbabilityTheory] infix:arg "†" => ProbabilityTheory.posterior

The last line defines a notation (available only in the ProbabilityTheory namespace) that
makes it possible to write $\kappa_{\mu}^{\dagger}$ in code as $\kappa^{\dagger}\mu$. As a check that the definition is correct, we can
prove that the posterior kernel satisfies Bayes' theorem.

lemma posterior_eq_withDensity (h_ac : ∀m ω ∂μ, κ ω ≪ κ ○_m μ) :
∀m y ∂(κ ○_m μ), (κ†μ) y
= μ.withDensity (fun x ↦ κ.rnDeriv (Kerneligation (κ ○_m μ)) x y)

That lemma states that for $\kappa \circ_m \mu$-almost all $y \in \mathcal{Y}$ (written $\forall^m y \partial(\kappa \circ_m \mu)$ in code), $\kappa_\mu^\dagger(y)$ is the measure on $\mathcal{X}$ with density $\omega \mapsto \frac{d\kappa}{d(\kappa \circ_m \mu)}(x, y)$ with respect to $\mu$, in which $\frac{d\kappa}{d(\kappa \circ_m \mu)}$ is the kernel Radon-Nikodym derivative with respect to the constant kernel with value $\kappa \circ_m \mu$. It requires the assumption that there exists a dominating measure: for $\mu$-almost all $x \in \mathcal{X}$, $\kappa(x)$ is absolutely continuous with respect to $\kappa \circ_m \mu$. This is in particular always true in the case of a countable space $\mathcal{X}$, for which the result simplifies to

lemma posterior_eq_withDensity_of_countable :
∀m y ∂(κ ○_m μ), (κ†μ) y = μ.withDensity (fun x ↦ (κ x).rnDeriv (κ ○_m μ) y)

That is, for $\kappa \circ_m \mu$-almost all $y \in \mathcal{Y}$, $\kappa_\mu^\dagger(y)$ is the measure on $\mathcal{X}$ defined by $\kappa_\mu^\dagger(y)(\{x\}) = \mu(\{x\}) \cdot \frac{d\kappa(x)}{d(\kappa \circ_m \mu)}(y)$ for all $x \in \mathcal{X}$.
# 4 Independence

Sections 2 and 3 described kernels and how they can be disintegrated and used to define conditional distributions. Those tools are used more widely in MATHLIB than in most mathematical texts, and in particular they are used to define independence and conditional independence.

**Definition 4.1** (Independence and conditional independence). Two random variables $X: \Omega \to \mathcal{X}$ and $Y: \Omega \to \mathcal{Y}$ are independent for a measure $P \in \mathcal{M}(\Omega)$ if for all measurable sets $A$ of $\mathcal{X}$ and $B$ of $\mathcal{Y}$,

$$P(X^{-1}(A) \cap Y^{-1}(B)) = P(X^{-1}(A)) \cdot P(Y^{-1}(B)).$$

Those two random variables are conditionally independent given a sub-$\sigma$-algebra $\mathcal{G}$ of the $\sigma$-algebra of $\Omega$ if for all measurable sets $A$ of $\mathcal{X}$ and $B$ of $\mathcal{Y}$, for $\mu$-almost every $\omega \in \Omega$,

$$P(X^{-1}(A) \cap Y^{-1}(B)|\mathcal{G})(\omega) = P(X^{-1}(A)|\mathcal{G})(\omega) \cdot P(Y^{-1}(B)|\mathcal{G})(\omega).$$

There is also a definition of both types of independence for more than two random variables (which is also in MATHLIB), but we will keep the discussion to two random variables for simplicity. In the library independence is defined first for sets of sets (intended to be used for $\pi$-systems), then for $\sigma$-algebras by seeing them as sets of sets, and finally for random variables by taking the $\sigma$-algebras they generate. We will only focus on the case of random variables here, but the same discussion applies to the other two cases.

We introduced a new definition using kernels (Kernel.IndepFun) and then define both types of independence as particular cases.

**Definition 4.2** (Kernel independence). Two random variables $X: \Omega \to \mathcal{X}$ and $Y: \Omega \to \mathcal{Y}$ are conditionally independent given a kernel $\kappa: \mathcal{T} \rightsquigarrow \Omega$ and a measure $\mu \in \mathcal{M}(\mathcal{T})$ if for all measurable sets $A$ of $\mathcal{X}$ and $B$ of $\mathcal{Y}$, we have that for $\mu$-almost every $t \in \mathcal{T}$,

$$\kappa(t)(X^{-1}(A) \cap Y^{-1}(B)) = \kappa(t)(X^{-1}(A)) \cdot \kappa(t)(Y^{-1}(B)).$$

This definition can recover unconditional independence by taking $\kappa$ to be a constant kernel with value $P$ from a space with one point. It recovers conditional independence for $\Omega$ a standard Borel space by taking $\mu$ to be the restriction of the measure $P$ to a sub-$\sigma$-algebra
and $\kappa$ the conditional expectation kernel $P^\mathcal{G}$. We obtain, almost everywhere,

$$P^\mathcal{G}(\omega)(X^{-1}(A) \cap Y^{-1}(B)) = P^\mathcal{G}(\omega)(X^{-1}(A)) \cdot P^\mathcal{G}(\omega)(Y^{-1}(B)),$$

and since $P^\mathcal{G}(\omega)(X^{-1}(A))$ is almost everywhere equal to $P(X^{-1}(A)|\mathcal{G})(\omega)$ and similarly for the other sets, we recover the definition of conditional independence.

We thus have three definitions of independence in MATHLIB: independence, conditional independence and independence given a kernel and a measure. However, the lemmas are all proved only once, for the kernel definition, and only restated with one-line proofs for the two particular cases.

**Strengths and weaknesses of the kernel definition** A drawback to that approach is a slight loss in generality for the conditional independence definition. Indeed, the existence of the conditional expectation kernel $P^\mathcal{G}$ is true only under assumptions on $\Omega$ (we defined it for $\Omega$ a standard Borel space), and thus in MATHLIB conditional independence is only defined in that case. However, Definition 4.1 does not need any assumption on $\Omega$ and an implementation of conditional independence which does not go through kernels would be more general, and its basic properties would hold in that generality. Unconditional independence properties could be obtained from the conditional case by taking $\mathcal{G}$ to be the trivial $\sigma$-algebra.

Nonetheless, conditional independence is overwhelmingly used in cases where the conditional expectation kernel exists, often to obtain properties of conditional distributions: for example if $X$ is independent of $Y$ given $Z$, the conditional distribution of $X$ given $(Y, Z)$ is almost surely equal to the conditional distribution of $X$ given $Z$ (see further down). In [For21], Patrick Forré introduces a generalization of conditional independence. It is defined for any measurable spaces but he makes the choice of requiring the existence of a conditional distribution to declare that two random variables are conditionally independent given another. Otherwise the property is not deemed useful enough. It is also notable that manipulating conditional independence is much easier when conditional kernels exists, as explained above in the discussion about the conditional expectation kernel. We thus made the choice of accepting the loss of generality for convenience: that choice could be revisited if the need for more general spaces arises. Another more minor refactor would be to follow the example of [For21] and (somewhat artificially) extend our definition to any space by requiring that there exists a conditional kernel such that the definition applies.

The proofs of the properties of independence with respect to a kernel and a measure are thus slightly simplified versions of those of conditional independence. The abstraction of the
conditional expectation into a kernel is also surprisingly helpful in practice. First, conditional expectations lead to long expressions which can be hard to read in the infoview of LEAN, and the kernel expression is often more readable. But besides that minor convenience, the main advantage is that the rigidity of kernels (due to the different types) prevents us from making mistakes when composing them. Proofs involving conditional independence would involve several $\sigma$-algebras on the same space, hence a function on a space could be measurable with respect to one $\sigma$-algebra but not another, and it is easy to inadvertently mistake one for the other when attempting to code a proof. When using kernels, the different types ensure that we cannot compose two kernels unless the types match precisely, and there is only one $\sigma$-algebra involved on each type. It is often the case in a proof that if two expressions involving kernels have the same type, then they are equal, which makes writing the proofs easier. That is of course not a mathematical truth but only a practical observation: only deterministic kernels satisfy $(\kappa \|_k \kappa) \circ_k \Delta_\mathcal{X} = \Delta_\mathcal{Y} \circ_k \kappa$, although they both have type $\mathcal{X} \rightsquigarrow \mathcal{Y} \times \mathcal{Y}$.

Thus we find that the formalization of some aspects of probability theory is made easier by considering many probability spaces linked by kernels, rather than the standard practice of considering a single probability space on which every random variable is defined. The relative rigidity of the type theory of LEAN can be an obstacle, as the discussion about associativity of composition-products showed, but it can also guide towards the correct statements as it detects badly typed expressions.

Our definition of independence with respect to a kernel and a measure is not an attempt to write the most general possible notion of independence, but merely a minimal abstraction of independence and conditional independence (in standard Borel spaces) that allows us to prove the properties we want only once, with a convenient framework. Many generalizations exist and we mention only two here, which also focus on kernels. [For21] defines transitional conditional independence, which is independence of two kernels given another kernel, in a transitional probability space (which is defined by a fourth kernel). We remark that our definition takes inspiration from this and features four objects as well: two random variables, a kernel and a measure. Both random variables and measures are special cases of kernels, and Forré extended independence to the general case of four kernels. [CJ19; Fri20] consider categorical definitions of independence of morphisms in Markov categories (and Markov kernels are such a category).

**Conditional independence and conditional distributions** Another characterization of conditional independence in standard Borel spaces is that $X$ and $Y$ are conditionally
independent given a third random variable Z (that is, given its generated σ-algebra) if and
only if the conditional distribution of Y given (X, Z) is almost surely equal to the conditional
distribution of Y given Z.

lemma condIndepFun_iff_condDistrib ProD_ae_eq_prodMkRight
  (hX : Measurable X) (hY : Measurable Y) (hZ : Measurable Z) :
  CondIndepFun (my.comap k) hk.comap_le Y X μ $\leftrightarrow$
  condDistrib X (fun ω $\mapsto$ (Z ω, Y ω)) μ =^m [μ.map (fun ω $\mapsto$ (Z ω, Y ω))]
  (condDistrib X Z μ).prodMkRight _ := by

The definition CondIndepFun m hm Y X μ means that Y and X are conditionally independent given the sub-σ-algebra m of the σ-algebra of Ω, for the measure μ. hm is a proof that m is indeed a sub-σ-algebra of the σ-algebra of Ω. Here the σ-algebra used is σ(Z), the σ-algebra generated by Z, which is obtained by MeasurableSpace.comap Z inferInstance. It is a sub-σ-algebra because Z is measurable, which is proved by hZ.comap_le. Writing about conditional independence given a random variable Z instead of a sub-σ-algebra G is currently rather verbose, and we could consider adding a more convenient notation for that common case in the future. The definition Kernel.prodMkRight turns a kernel L $\rightsquigarrow$ X into a kernel L $\times$ Y $\rightsquigarrow$ X by ignoring the second argument.

That result was not immediate given the definitions: CondIndepFun is defined in terms of
the conditional expectation kernel $P^{\sigma(Z)}$, which is a condDistrib of identity functions and
that definition needs to be related to conditional distributions of the variables involved.

5 Sub-Gaussian random variables

Independence is far from the only property in probability theory that is defined in two
versions, conditional and unconditional. Another example from MATHLIB is the notion of
sub-Gaussian random variables, and we applied the same idea to define them in a unified
way.

A sub-Gaussian random variable is a real-valued random variable whose tail decreases
at least as fast as a Gaussian. We refer the reader to [Ver18] for a thorough introduction to
sub-Gaussian random variables and their properties. Formally, $X : \Omega \to \mathbb{R}$ is sub-Gaussian
with constant $\sigma^2$ on a probability space $(\Omega, P)$ if $e^{uX}$ is integrable for all $u \in \mathbb{R}$ and the function
$u \mapsto P[e^{uX}]$ (its moment generating function, MGF) satisfies the inequality $P[e^{uX}] \le e^{u^2\sigma^2/2}$
for all $u \in \mathbb{R}$. The function $u \mapsto e^{u^2\sigma^2/2}$ is the MGF of a real Gaussian random variable with
mean 0 and variance $\sigma^2$, hence the name sub-Gaussian. Sub-Gaussianity is a useful property
to prove concentration inequalities such as Hoeffding's inequality [Hoe63], which states in the independent case that if $X_1, \dots, X_n$ are independent and sub-Gaussian with constant $\sigma^2$, then for all $t > 0$,

$$P\left(\sum_{i=1}^{n} X_i \ge t\right) \le \exp\left(-\frac{t^2}{2n\sigma^2}\right).$$

In MATHLIB, this is `HasSubgaussianMGF`. `measure_sum_range_ge_le_of_iIndepFun`. Concentration inequalities are useful to bound the probability that random variables deviates from their mean, and have many applications in statistics and machine learning. The Lean definition of sub-Gaussian that we added to MATHLIB is as follows.

```hropc
structure HasSubgaussianMGF (X : Ω → ℝ) (c : ℝ≥0) (μ : Measure Ω := by volume_tac) : Prop
    where
    integrable_expmul : ∀ t : ℝ, Integrable (fun ω ↦ exp (t * X ω)) μ
    mgf_le : ∀ t : ℝ, mgf X μ t ≤ exp (c * t ∧ 2/2)
```

The `:= by volume_tac` code defines a default value in case no measure is provided. That default value uses a tactic (`volume_tac`) that infers the measure if $\Omega$ has been endowed with a `MeasureSpace` instance. For example if $\Omega = ℝ$, we don't need to specify that we mean sub-Gaussian with respect to the Lebesgue measure.

The conditional version of the sub-Gaussian property is defined similarly, by replacing the expectation $P[\cdot]$ by the conditional expectation $P[\cdot|\mathcal{G}]$ given a sub-$\sigma$-algebra $\mathcal{G}$ of the $\sigma$-algebra of $\Omega$. A random variable $X : \Omega \to \mathbb{R}$ is conditionally sub-Gaussian with constant $\sigma^2$ given $\mathcal{G}$ if $e^{uX}$ is integrable for all $u \in \mathbb{R}$ and $P$-almost surely, $P[e^{uX} | \mathcal{G}] \le e^{u^2\sigma^2/2}$ for all $u \in \mathbb{R}$. We note that this is almost never mentioned as a separate definition in textbooks, which implicitly assume that conditional versions of properties are defined by replacing expectations by conditional expectations. That definition is not however how we defined it in MATHLIB: we instead introduce a new notion of sub-Gaussian with respect to a kernel and a measure, and define conditionally sub-Gaussian as a particular case of that definition for the conditional expectation kernel.

**Definition 5.1.** A random variable $X : \Omega \to \mathbb{R}$ is sub-Gaussian with respect to a Markov kernel $\kappa : \mathcal{T} \rightsquigarrow \Omega$ and a measure $\mu \in \mathcal{M}(\mathcal{T})$ if

* $\exp(uX)$ is integrable with respect to $\kappa \circ_m \mu$ for all $u \in \mathbb{R}$,
* for $\mu$-almost all $t$, the moment generating function of $X$ under $\kappa(t)$ satisfies, for all $u \in \mathbb{R}$,

$$ \kappa(t)[e^{uX}] \le e^{u^2/2}. $$

Again, the definition can also recover the unconditional case by taking $\kappa$ to be a constant kernel from a space with one point. When we specialize that definition to the conditional expectation kernel $P^\mathcal{G}$ and a measure $P$ restricted to $\mathcal{G}$ (denoted by $P|_{\mathcal{G}}$), the two conditions become:

* $\exp(uX)$ is integrable with respect to $P^\mathcal{G} \circ_m P|_{\mathcal{G}} = P$ for all $u \in \mathbb{R}$,

* for $P|_{\mathcal{G}}$-almost all $\omega \in \Omega$, for all $u \in \mathbb{R}$, $P^\mathcal{G}[e^{uX(\omega)}] \le e^{u^2/2}$.

Since $P^\mathcal{G}[e^{uX(\omega)}]$ is almost everywhere equal to $P[e^{uX}|\mathcal{G}](\omega)$ and is a $\mathcal{G}$-measurable function, we recover the definition of conditional sub-Gaussian: for $P$-almost all $\omega \in \Omega$, for all $u \in \mathbb{R}$, $P[e^{uX}|\mathcal{G}](\omega) \le e^{u^2/2}$.

An advantage of going through a kernel definition is that this definition is useful in itself, particularly in a context like the one of the Ionescu-Tulcea theorem where the probability space we study comes from a sequence of kernels.

The integrability condition in Definition 5.1 merits discussion. In the unconditional case, we require that $\exp(uX)$ is integrable with respect to $P$, which could be interpreted as a technical condition to ensure that the MGF that appears in the inequality is well-defined. The analogue of that for the kernel case would be to require that for $\mu$-almost all $t \in \mathcal{T}$, for all $u$, $\exp(uX)$ is integrable with respect to $\kappa(t)$ (and this is how we implemented it originally). This is implied by the integrability with respect to $\kappa \circ_m \mu$, but is a weaker condition. Let's call it weak integrability. Integrability of a measurable function $f$ with respect to $\kappa \circ_m \mu$ is equivalent to $\mu$-almost everywhere integrability of $f$ with respect to $\kappa(t)$ together with integrability of $t \mapsto \int_y \|f(y)\|d\kappa(t)$. The weak integrability condition that ensures only that the MGF inequality makes sense has a flaw: it does not compose well.

The issue became apparent when we wanted to prove the following result about sums of sub-Gaussian random variables. Suppose that we have a measure $\nu$ on $\mathcal{T}$ and two kernels $\kappa: \mathcal{T} \rightsquigarrow \mathcal{X}$ and $\eta: \mathcal{T} \times \mathcal{X} \rightsquigarrow \mathcal{Y}$. Those measure and kernels may describe the evolution of the state of a system (from a space $\mathcal{T}$ to $\mathcal{X}$ and then $\mathcal{Y}$), which we observe through $\mathbb{R}$-valued random variables $X: \mathcal{X} \to \mathbb{R}$ and $Y: \mathcal{Y} \to \mathbb{R}$. Then if $X$ is sub-Gaussian with respect to $\kappa$ and $\nu$ and $Y$ is sub-Gaussian with respect to $\eta$ and $\nu \otimes_m \kappa \in \mathcal{M}(\mathcal{U} \times \mathcal{X})$, then $X+Y$ (random variable on the measurable space $\mathcal{X} \times \mathcal{Y}$) is sub-Gaussian with respect to $\kappa \otimes_k \eta: \mathcal{U} \rightsquigarrow \mathcal{X} \times \mathcal{Y}$ and $\nu$.
**lemma** Kernel.HasSubgaussianMGF.add_compProd [IsZeroOrMarkovKernel $\eta$]
(hX : HasSubgaussianMGF X cX $\kappa$ $\nu$) (hY : HasSubgaussianMGF Y cY $\eta$ ($\nu \otimes_m \kappa$)) :
HasSubgaussianMGF (fun $p \mapsto X$ p.1 + Y p.2) (cX + cY) ($\kappa \otimes_k \eta$) $\nu$

That lemma is the right tool to prove concentration inequalities for sums of sub-Gaussian random variables in the Ionescu-Tulcea setting of a sequence of kernels. In order to prove the MGF inequality in that result, we have to obtain at least that for $\nu$-almost all $t \in \mathcal{T}$, for all $u \in \mathbb{R}$, $\exp(u(X + Y))$ is integrable with respect to $(\kappa \otimes_k \eta)(t) = \kappa(t) \otimes_m \eta$, in order for the MGF to be defined. With the weak integrability condition, we only know that for $\nu$-almost all $t \in \mathcal{T}$, for all $u$, $\exp(uX)$ is integrable with respect to $\kappa(t)$ and that for $(\nu \otimes_m \kappa)$-almost all $(t, x) \in \mathcal{T} \times \mathcal{X}$, for all $u$, $\exp(uY)$ is integrable with respect to $\eta(t, x)$. That is not enough to conclude, mainly because the result of integrability with respect to $\kappa(t) \otimes_m \eta$ is stronger than $\kappa(t)$-almost everywhere integrability with respect to $\eta(t, x)$. The stronger integrability condition in Definition 5.1 solves that issue.

**General principle** The general principle we applied both in the independence and sub-Gaussian cases is simple: where the non-conditional case features an expectation $\mu[\cdot]$ and the conditional case an almost sure statement about a conditional expectation $\mu[\cdot|\mathcal{G}]$, we introduce a kernel $\kappa : \mathcal{T} \rightsquigarrow \Omega$ and a measure $\nu \in \mathcal{M}(\mathcal{T})$ and replace the expectation by $\kappa(t)[\cdot]$ for $\nu$-almost all $t \in \mathcal{T}$. The unconditional case is recovered by taking $\mathcal{T} = \mathbf{1}$ (space with one point) and $\kappa$ the constant kernel from that space to $\Omega$. The conditional case is recovered in Borel spaces by taking $\mathcal{T} = \Omega$, $\nu$ the restriction of $\mu$ to $\mathcal{G}$ and $\kappa = P^\mathcal{G}$ the conditional expectation kernel.

A property of the type “for $\mu$-almost all $x$, $P(x)$” would be generalized to “for $\nu$-almost all $t \in \mathcal{T}$, for $\kappa(t)$-almost all $x \in \mathcal{X}$, $P(x)$”, which is equivalent to “for $\nu \otimes_m \kappa$-almost all $(t, x) \in \mathcal{T} \times \mathcal{X}$, $P(x)$”. The latter formulation is more convenient to use because it features a single “almost all” instead of two nested ones.

# 6 Information theory and divergences

In this last section on applications of kernels to the formalization other probabilistic notions, we will discuss information-theoretic quantities: entropy and the Kullback-Leibler divergence (KL). Those examples are not from MATHLIB, but from other projects (although the definition of KL is now in MATHLIB, klDiv). They should be seen as experiments on how the kernel approach can be applied to other notions, and not as a definitive design choices.
In the PFR project [And+23], we formalized the entropy of a probability measure $\mu$ on a finite set $\mathcal{X}$ as $H(\mu) = -\sum_{x \in \mathcal{X}} \mu(\{x\}) \log \mu(\{x\})$, and the conditional entropy of a kernel $\kappa: \mathcal{X} \to \mathcal{Y}$ with respect to a probability measure $\mu$ on $\mathcal{X}$ as

$$H_k(\kappa | \mu) = \sum_x \mu(x) H(\kappa(x)).$$

Conditional entropy of random variables in a probability space $(\Omega, P)$ was given a direct definition

$$H(X|Y) = -\sum_y \sum_x P(Y=y)P(X=x|Y=y)\log(P(X=x|Y=y)),$$

but we also proved that the conditional entropy of $X$ given $Y$ is equal to $H_k(P_{X|Y} | P_Y)$ and derived many properties for the kernel version of entropy, before then specializing to the conditional entropy. For example, the chain rule for kernel entropy can be stated as

$$H_k(\kappa \otimes_k \eta | \mu) = H_k(\kappa | \mu) + H_k(\eta | \mu \otimes_m \kappa).$$

The advantage of using the kernel approach there is mostly in the clarity of the proofs: abstracting away the conditional distribution into a generic kernel streamlines the reasoning.

The Kullback-Leibler divergence definition now in MATHLIB was developed in the Testin-gLowerBounds project [LD24]. A highlight of the project is a proof of the data-processing inequality for f-divergences, a class of divergences between probability distributions that includes KL, as well as the chain rule for KL. The Kullback-Leibler divergence between two probability measures $\mu, \nu$ on a measurable space $\mathcal{X}$ is defined as

$$KL(\mu, \nu) = \int_{\mathcal{X}} \log \left( \frac{d\mu}{d\nu}(x) \right) d\mu,$$

if $\mu$ is absolutely continuous with respect to $\nu$, and $+\infty$ otherwise. The usual formulation of the chain rule for KL states that if $\mu, \nu$ are two probability measures on $\mathcal{X}$ and $\kappa, \eta: \mathcal{X} \to \mathcal{Y}$ are two Markov kernels, then

$$KL(\mu \otimes_m \kappa, \nu \otimes_m \eta) = KL(\mu, \nu) + KL(\kappa, \eta | \mu),$$

in which $KL(\kappa, \eta | \mu) = \int_{\mathcal{X}} KL(\kappa(x), \eta(x)) d\mu$ is the conditional KL divergence. The chain rule proof then required the function $x \mapsto KL(\kappa(x), \eta(x))$ to be measurable, which is true in
spaces in which a kernel Radon-Nikodym derivative of $\kappa$ with respect to $\eta$ exists. A version of that same result that avoids the measurability issue and does not require any assumption on the measurable spaces is

$$KL(\mu \otimes_m \kappa, \nu \otimes_m \eta) = KL(\mu, \nu) + KL(\mu \otimes_m \kappa, \mu \otimes_m \eta).$$

Expressing the conditional divergence with composition-products of kernels instead of an integral avoids assumptions and makes manipulations easier. Similarly, the fact that “conditioning increases divergence” usually refers to the inequality $KL(\kappa \circ_m \mu, \eta \circ_m \mu) \le KL(\kappa, \eta | \mu)$, but the version $KL(\kappa \circ_m \mu, \eta \circ_m \mu) \le KL(\mu \otimes_m \kappa, \mu \otimes_m \eta)$ is more general. In fact, any divergence (a function $D$ that maps two probability measures $\mu, \nu$ to a value $D(\mu, \nu) \in \mathbb{R}_{+,\infty}$ with $D(\mu, \mu) = 0$) that satisfies the important data-processing inequality $D(\kappa \circ_m \mu, \kappa \circ_m \nu) \le D(\mu, \nu)$ also satisfies the “conditioning increases divergence” inequality for the composition-product of kernels version. In that generality, the composition-product version may not be equal to the integral version, although equality holds for the large class of f-divergences.

For the Rényi divergence $R_\alpha(\mu, \nu) = \frac{1}{\alpha-1} \log \left( \int_x \left( \frac{d\mu}{d(\mu+\nu)}(x) \right)^\alpha \left( \frac{d\nu}{d(\mu+\nu)}(x) \right)^{1-\alpha} d(\mu+\nu) \right)$ for $\alpha \in (0, 1)$, the composition-product and integral definitions do not coincide. It is remarkable that the conditional definition that enjoys a form of the chain rule is not the integral definition, but the composition-product of kernels version $R_\alpha(\mu \otimes_m \kappa, \nu \otimes_m \eta)$ [PW25, Chapter 7]. We thus expect Markov kernels to play an important role in the formalization of probability divergences in MATHLIB.

# 7 Conclusion

We presented several applications of Markov kernels to the formalization of probability theory in MATHLIB and in other projects. We first described the definition of kernels and their operations, and how they can be used to define conditional distributions through disintegration. We then showed how we introduced a kernel-centric definition for independence and conditional independence, and how a similar approach was used to work with sub-Gaussian random variables. We expect that approach to see more use in MATHLIB in the future.

An important area that needs to see more development is the link between the probability space defined by the Ionescu-Tulcea theorem and the structure of the sequence of kernels that generated it. For example, we can describe a Markov chain by a sequence of kernels that depend only on the last state (and not on the whole history), and we can feed that sequence to the Ionescu-Tulcea theorem to obtain a probability space. In that space, the Markov
chain property will be described by a list of conditional independence statements between random variables. But the lemmas required to turn a structure on the sequence of kernels into conditional independence properties in the probability space are not yet implemented. This needs to be done in order to model interactions like the stochastic optimization example we described in Section 2, or to formalize sequential algorithms like the ones used in machine learning.

## Acknowledgements

The author would like to thank Sébastien Gouëzel, Etienne Marion, and Kexing Ying for their valuable code reviews and suggestions, which prompted several improvements over the initial implementation of kernels and their applications. We would also like to acknowledge the collaborative nature of the construction of MATHLIB: this work is only possible because it builds on the prior efforts of many contributors over several years to build a comprehensive library for mathematics in LEAN, notably in measure theory.

## References

[ACS25] R. Affeldt, C. Cohen, and A. Saito. "Semantics of Probabilistic Programs using s-Finite Kernels in Dependent Type Theory". In: *ACM Transactions on Probabilistic Machine Learning* (2025).

[And+23] A. Anderson, M. Bakšys, J. Bayer, M. Collares, R. Degenne, Y. Dillies, B. Eltschig, S. Gouëzel, K. Kytölä, R. Lewis, P. Lez, L. Lorenzo, H. Macbeth, P. Massot, A. Mellendijk, K. Miller, P. Monticone, K. Morrison, O. Nash, U. Song, T. Tao, F. van Doorn, S. Wilshaw, and L. Wu. *Formalization of the Polynomial Freiman-Ruzsa Conjecture of Marton*. Version 0.1.0. Nov. 2023. URL: https://github.com/teorth/pfr.

[Buz20] K. Buzzard. *Division by zero in type theory: A FAQ*. Accessed: 2025-09-22. 2020. URL: https://xenaproject.wordpress.com/2020/07/05/division-by-zero-in-type-theory-a-faq/.

[CJ19] K. Cho and B. Jacobs. "Disintegration and Bayesian inversion via string diagrams". In: *Mathematical Structures in Computer Science* 29.7 (2019), pages 938–971.

[Cle+17] F. Clerc, V. Danos, F. Dahlqvist, and I. Garnier. "Pointless learning". In: *International conference on foundations of software science and computation structures*. Springer. 2017, pages 355–369.
[Ebn+17] G. Ebner, S. Ullrich, J. Roesch, J. Avigad, and L. de Moura. "A Metaprogramming Framework for Formal Verification". In: *Proc. ACM Program. Lang.* 1.ICFP (2017). DOI: 10.1145/3110278. URL: https://doi.org/10.1145/3110278.

[Fad85] A. M. Faden. "The existence of regular conditional probabilities: necessary and sufficient conditions". In: *The Annals of Probability* (1985), pages 288–298.

[For21] P. Forré. "Transitional conditional independence". In: *arXiv preprint arXiv:2104.11547* (2021).

[Fri20] T. Fritz. "A synthetic approach to Markov kernels, conditional independence and theorems on sufficient statistics". In: *Advances in Mathematics* 370 (2020), page 107239.

[Gou22] S. Gouëzel. "A formalization of the change of variables formula for integrals in mathlib". In: *International Conference on Intelligent Computer Mathematics*. Springer. 2022, pages 3–18.

[Gow+25] W. T. Gowers, B. Green, F. Manners, and T. Tao. "On a conjecture of Marton". In: *Annals of Mathematics* 201.2 (2025), pages 515–549.

[HMS23] M. Hirata, Y. Minamide, and T. Sato. "Semantic foundations of higher-order probabilistic programs in Isabelle/HOL". In: *14th International Conference on Interactive Theorem Proving (ITP 2023)*. Schloss Dagstuhl–Leibniz-Zentrum für Informatik. 2023, pages 18–1.

[Hoe63] W. Hoeffding. "Probability inequalities for sums of bounded random variables". In: *Journal of the American statistical association* 58.301 (1963), pages 13–30.

[Höl17] J. Hölzl. "Markov chains and Markov decision processes in Isabelle/HOL". In: *Journal of Automated Reasoning* 59.3 (2017), pages 345–387.

[Kal21] O. Kallenberg. *Foundations of modern probability*. Third Edition. Volume 99. Probability Theory and Stochastic Modelling. Springer Nature Switzerland, 2021, page 193. ISBN: 978-3-030-61870-4; 978-3-030-61871-1. DOI: 10.1007/978-3-030-61871-1. URL: https://doi.org/10.1007/978-3-030-61871-1.

[LD24] L. Luccioli and R. Degenne. *TestingLowerBounds*. 2024. URL: https://github.com/RemyDegenne/testing-lower-bounds.

[Mar25] E. Marion. "A Formalization of the Ionescu-Tulcea Theorem in Mathlib". In: *arXiv preprint arXiv:2506.18616* (2025).
[mat20] The mathlib Community. "The Lean Mathematical Library". In: *Proceedings of the 9th ACM SIGPLAN International Conference on Certified Programs and Proofs*. CPP 2020. New Orleans, LA, USA: Association for Computing Machinery, Jan. 2020, 367-381. DOI: 10.1145/3372885.3373824. URL: https://doi.org/10.1145/3372885.3373824.

[MU21] L. d. Moura and S. Ullrich. "The lean 4 theorem prover and programming language". In: *International Conference on Automated Deduction*. Springer. 2021, pages 625-635.

[PW25] Y. Polyanskiy and Y. Wu. *Information theory: From coding to learning*. Cambridge university press, 2025.

[Sta20] S. Staton. "Probabilistic programs as measures". In: *Foundations of Probabilistic Programming* (2020), page 43.

[VO18] M. Vákár and L. Ong. "On s-finite measures and kernels". In: *arXiv preprint arXiv:1810.01837* (2018).

[Ver18] R. Vershynin. *High-dimensional probability: An introduction with applications in data science*. Volume 47. Cambridge university press, 2018.

[YD23] K. Ying and R. Degenne. "A Formalization of Doob's Martingale Convergence Theorems in mathlib". In: *Proceedings of the 12th ACM SIGPLAN International Conference on Certified Programs and Proofs*. 2023, pages 334-347.