In Mathlib, proofs by calculation whose steps are equalities of kernels are rare. Each of their structural steps is a chain of rewritings, which buries the meaningful steps. Such steps are, e.g., reassociating a composition, marginalizing an output or moving a kernel along a copy. With Kernel-Hom, the structural steps are proved by kernel_disch, in the same way as cat_disch or monoidal proves the easy steps of categorical proofs. Thus, a proof can be written as a calc whose lines are the meaningful rewrites. With measure_kernel, this also holds for calculations on measures built from kernels. On paper, the structural steps are invisible. Indeed, they relate string diagrams that only differ by a deformation of the wires or by the axioms of copy-discard categories.
We illustrate this with Douc et al. (2018)Marginal noteRandal Douc, Eric Moulines, Pierre Priouret, and Philippe Soulier, 2018. “Markov Chains”. In Springer Series in Operations Research and Financial Engineering., about data augmentation algorithms.
Statement
Let \mathsf{X} and \mathsf{Y} be Polish spaces, equipped with their Borel σ-fields \mathcal{X} and \mathcal{Y}. Let \pi be a probability measure on \mathsf{X} and R a Markov kernel from \mathsf{X} to \mathsf{Y}. By disintegration, there exist a kernel S from \mathsf{Y} to \mathsf{X} and a probability measure \tilde\pi on \mathsf{Y} such that
A data augmentation algorithm draws y from R(x, \cdot), then x from S(y, \cdot). Thus, the sequence of the x is a Markov chain with kernel RS(x, A) = \int R(x, \mathrm{d}y)\, S(y, A).
Lemma 2.3.5. The distribution \pi is reversible with respect to the kernel RS, i.e. the measure \pi \otimes RS on \mathsf{X} \times \mathsf{X} is symmetric (Definition 1.5.1):
\forall A, B \in \mathcal{X}, \quad \pi \otimes RS(A \times B) = \pi \otimes RS(B \times A).
Proof. Let A, B \in \mathcal{X}. Using (2.3.11), we have that
In Mathlib, S is the posterior R†π (posterior) and \tilde\pi is R ∘ₘ π. The kernel RS is S ∘ₖ R, and the measure \pi \otimes RS is π ⊗ₘ (S ∘ₖ R). The reversibility IsReversible is defined by the equality of the integrals of RS(\cdot, B) over A and of RS(\cdot, A) over B. The lemma isReversible_of_map_swap derives it from the symmetry of the measure, stated as its invariance by the swap of the coordinates.
/-- Definition 1.5.1 of Douc et al.: if the measure `π ⊗ κ` on `X × X` is symmetric, then `π` is
reversible with respect to `κ`. -/lemmaisReversible_of_map_swap{π:MeasureX}[SFiniteπ]{κ:KernelXX}[IsSFiniteKernelκ](h:(π⊗ₘκ).mapProd.swap=π⊗ₘκ):IsReversibleκπ:=X:Type u_1inst✝²:MeasurableSpaceXπ:MeasureXinst✝¹:SFiniteπκ:KernelXXinst✝:IsSFiniteKernelκh:Measure.mapProd.swap(π⊗ₘκ)=π⊗ₘκ⊢ κ.IsReversibleπX:Type u_1inst✝²:MeasurableSpaceXπ:MeasureXinst✝¹:SFiniteπκ:KernelXXinst✝:IsSFiniteKernelκh:Measure.mapProd.swap(π⊗ₘκ)=π⊗ₘκA:SetXB:SetXhA:MeasurableSetAhB:MeasurableSetB⊢ ∫⁻(x:X)inA,(κx)B∂π=∫⁻(x:X)inB,(κx)A∂πAll goals completed! 🐙/-- Lemma 2.3.5 of Douc et al.: the distribution `π` is reversible with respect to the kernel `RS`,
where `S = R†π`. -/theoremisReversible_comp[StandardBorelSpaceX][NonemptyX](π:MeasureX)[IsProbabilityMeasureπ](R:KernelXY)[IsMarkovKernelR]:IsReversible((R†π)∘ₖR)π:=byX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ (R†π∘ₖR).IsReversibleπ-- By Definition 1.5.1, the measure `π ⊗ RS` on `X × X` must be symmetric.applyisReversible_of_map_swapX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ Measure.mapProd.swap(π⊗ₘ(R†π∘ₖR))=π⊗ₘ(R†π∘ₖR)-- Equation (2.3.11) of the book: `π(dx) R(x, dy) = S(y, dx) π̃(dy)`.haveh:π⊗ₘR=swapYX∘ₘ((R∘ₘπ)⊗ₘR†π):=byX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ (R†π∘ₖR).IsReversibleπrw[compProd_posterior_eq_swap_compX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ π⊗ₘR=⇑(swapYX)∘ₘ⇑(swapXY)∘ₘπ⊗ₘR]X:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ π⊗ₘR=⇑(swapYX)∘ₘ⇑(swapXY)∘ₘπ⊗ₘRmeasure_kernelX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ Kernel.id×ₖR∘ₖconstUnitπ=swapYX∘ₖ(swapXY∘ₖ(Kernel.id×ₖR∘ₖconstUnitπ))kernel_dischX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ Measure.mapProd.swap(π⊗ₘ(R†π∘ₖR))=π⊗ₘ(R†π∘ₖR)-- The computation of the book: `π ⊗ RS = ∫ S(y, ·) ⊗ S(y, ·) π̃(dy)`.havekey:π⊗ₘ((R†π)∘ₖR)=((R†π)×ₖ(R†π))∘ₘ(R∘ₘπ):=byX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelR⊢ (R†π∘ₖR).IsReversibleπcalcπ⊗ₘ((R†π)∘ₖR)_=(Kernel.id∥ₖR†π)∘ₘ(π⊗ₘR):=byX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ π⊗ₘ(R†π∘ₖR)=⇑(Kernel.id∥ₖR†π)∘ₘπ⊗ₘRmeasure_kernelX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ Kernel.id×ₖ(R†π∘ₖR)∘ₖconstUnitπ=Kernel.id∥ₖR†π∘ₖ(Kernel.id×ₖR∘ₖconstUnitπ);kernel_dischAll goals completed! 🐙_=(Kernel.id∥ₖR†π)∘ₘ(swapYX∘ₘ((R∘ₘπ)⊗ₘR†π)):=byX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ ⇑(Kernel.id∥ₖR†π)∘ₘπ⊗ₘR=⇑(Kernel.id∥ₖR†π)∘ₘ⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†πrw[hX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ ⇑(Kernel.id∥ₖR†π)∘ₘ⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π=⇑(Kernel.id∥ₖR†π)∘ₘ⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π]All goals completed! 🐙_=((R†π)×ₖ(R†π))∘ₘ(R∘ₘπ):=byX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ ⇑(Kernel.id∥ₖR†π)∘ₘ⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπmeasure_kernelX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†π⊢ Kernel.id∥ₖR†π∘ₖ(swapYX∘ₖ(Kernel.id×ₖR†π∘ₖ(R∘ₖconstUnitπ)))=R†π×ₖR†π∘ₖ(R∘ₖconstUnitπ);kernel_dischX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†πkey:π⊗ₘ(R†π∘ₖR)=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ⊢ Measure.mapProd.swap(π⊗ₘ(R†π∘ₖR))=π⊗ₘ(R†π∘ₖR)-- This measure is symmetric.rw[←Measure.swap_comp,X:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†πkey:π⊗ₘ(R†π∘ₖR)=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ⊢ ⇑(swapXX)∘ₘπ⊗ₘ(R†π∘ₖR)=π⊗ₘ(R†π∘ₖR)keyX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†πkey:π⊗ₘ(R†π∘ₖR)=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ⊢ ⇑(swapXX)∘ₘ⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ]X:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†πkey:π⊗ₘ(R†π∘ₖR)=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ⊢ ⇑(swapXX)∘ₘ⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπmeasure_kernelX:Type u_1Y:Type u_2inst✝⁵:MeasurableSpaceXinst✝⁴:MeasurableSpaceYinst✝³:StandardBorelSpaceXinst✝²:NonemptyXπ:MeasureXinst✝¹:IsProbabilityMeasureπR:KernelXYinst✝:IsMarkovKernelRh:π⊗ₘR=⇑(swapYX)∘ₘ(⇑R∘ₘπ)⊗ₘR†πkey:π⊗ₘ(R†π∘ₖR)=⇑(R†π×ₖR†π)∘ₘ⇑R∘ₘπ⊢ swapXX∘ₖ(R†π×ₖR†π∘ₖ(R∘ₖconstUnitπ))=R†π×ₖR†π∘ₖ(R∘ₖconstUnitπ)kernel_dischAll goals completed! 🐙
The formal proof has the structure of the proof of the book. The hypothesis h is equation (2.3.11), which follows from compProd_posterior_eq_swap_comp. The calculation key is the computation of the book, and its only meaningful step is the rewrite with h. The symmetry of the right-hand side of key is the cocommutativity of the copy. All the other steps are structural, and are proved by measure_kernel followed by kernel_disch. Without the tactics, the last step of key can be proved with the lemmas of Mathlib by