The gambler's ruin chain, the coupon collector, and simple random walk on
Definitionmm_classicalThis file presents the classical chains of Chapter 2 of Levin-Peres-Wilmer directly through their driving randomness, so that all probabilities are elementary counting.
The gambler's ruin chain on moves from each interior state to its two neighbours with probability each and is absorbed at and at .
For the coupon collector with types, the probability that independent uniform draws fail to collect every type is the fraction of the draw sequences that are not surjective, and the expected collection time is the tail sum , an infinite series with the convention that a non-summable family sums to .
Simple random walk on started at is presented by sign strings: to a string of steps is associated the deterministic position after steps, and probabilities of walk events are counts of the equally likely sign strings, taken in the theorems that use these definitions.
import Definitions.Def_mm_path
import Mathlib.Analysis.SpecialFunctions.Log.Basic
import Mathlib.Analysis.SpecialFunctions.Sqrt
/-!
The classical chains of Levin–Peres–Wilmer, *Markov Chains and Mixing Times*,
Chapter 2: the gambler's ruin chain (§2.1), the coupon-collector process
(§2.2), and simple random walk on `ℤ` (§2.7).
The coupon collector and the walk on `ℤ` are presented directly by their
driving randomness — uniform draws `Fin t → Fin n`, respectively uniform sign
sequences `Fin r → Bool` — so that all probabilities are elementary counting.
-/
namespace MarkovMixing
noncomputable section
open scoped BigOperators
/-- The **gambler's ruin** chain on `{0, 1, …, n}`: fair ±1 steps at the
interior states, absorption at `0` and at `n` (LPW §2.1). -/
def gamblersChain (n : ℕ) : Matrix (Fin (n + 1)) (Fin (n + 1)) ℝ :=
fun k l =>
if k = 0 ∨ k = Fin.last n then (if l = k then 1 else 0)
else if l.val = k.val + 1 ∨ l.val + 1 = k.val then 1 / 2 else 0
/-- `P{τ > t}` for the **coupon collector** with `n` coupon types: the
probability that `t` independent uniform draws miss at least one type, i.e.
the fraction of functions `Fin t → Fin n` that are not surjective (LPW §2.2). -/
def couponMissProb (n t : ℕ) : ℝ :=
((Finset.univ.filter fun d : Fin t → Fin n => ¬Function.Surjective d).card : ℝ) / n ^ t
/-- `E(τ)` for the coupon collector with `n` types, via the tail-sum formula
`E τ = ∑_{t ≥ 0} P{τ > t}` (LPW §2.2). -/
def couponExpTime (n : ℕ) : ℝ :=
∑' t : ℕ, couponMissProb n t
/-- The position at time `t` of the **simple random walk on `ℤ`** started at
`k`, driven by the sign sequence `ω` (`true` = step `+1`, `false` = step `-1`);
each `ω : Fin r → Bool` is equally likely (LPW §2.7). -/
def srwPos {r : ℕ} (k : ℤ) (ω : Fin r → Bool) (t : ℕ) : ℤ :=
k + ∑ i : Fin r, if (i : ℕ) < t then (if ω i then 1 else -1) else 0
end
end MarkovMixing
Read-back
What the Lean code literally says, in plain math · claude-fable-5
Read-back: Def_mm_classical
gamblersChain. For every natural number , this defines a real-valued square matrix indexed by the states . Its entry is determined by cases on the row index : if or (the two endpoint states), the row is that of an absorbing state — when and otherwise; for every other row index (so ), the entry is when or (i.e. is the immediate right or left neighbor of , as natural numbers), and otherwise. Note the degenerate case : there is a single state , which is simultaneously both endpoints, and the matrix is the identity. When both states are endpoints and the matrix is the identity. No claim is made here that this matrix is stochastic; it is simply this explicit matrix of reals.
couponMissProb. For all natural numbers and , this defines the real number
that is: the number of functions from a -element index set to an -element set that fail to be surjective (fail to hit every one of the values), divided by . Both numerator and denominator are cast to real numbers and the division is real division. Edge cases forced by the totality conventions: when and , the unique empty function is not surjective, so the value is ; when and , the unique function between empty types is (vacuously) surjective and , so the value is ; when and there are no such functions at all and the denominator is , so the expression is , which by the convention for real division equals .
couponExpTime. For every natural number , this defines the real number
the sum over all natural numbers of the quantity defined above (the fraction of functions from a -element set to an -element set that are not surjective). The infinite sum is the topological sum operator: if the family of terms is not summable, the expression takes the junk value by convention rather than ; it equals the limit of the partial sums only when the family is summable. For the terms are for all and at , so the sum is .
srwPos. For an implicit natural number , an integer , a function (a string of Booleans), and a natural number , this defines the integer
In words: starting from , each of the first coordinates of contributes a step of (if that coordinate is true) or (if false), and coordinates with index contribute nothing. In particular, when all coordinates contribute, and increasing beyond changes nothing; when or the value is just . This is a plain arithmetic expression in the given data; no probability measure or randomness is defined here.
Confirmed by the mission captain (proposal self-audit).