Documentation

CslibFrontier.Computability.Probabilistic.Sampling.FisherYates

Exact Fisher–Yates Uniform Permutation Batch Sampler #

This module implements a strict probabilistic polynomial-time (Cslib.Probability.IsPPTOn) 4 * n + 4-step Fisher–Yates rejection batch fyBatch n i perm and proves:

  1. Exact uniformity target (fisherYatesTarget_zero): running Fisher–Yates from (0, 1) yields PMF.uniformOfFintype (Equiv.Perm (Fin n)).
  2. Support preservation (fyBatch_support): every supported output of fyBatch n i (permOfEquiv π) is a valid permutation state (i', permOfEquiv π') with i' ≤ n.
  3. Exact harmonicity (fyBatch_harmonic): fisherYatesTarget n is harmonic under fyBatch n, so repeating fyBatch in a Las Vegas loop preserves exact uniformity without bias.
  4. Constant-probability completion (fyBatch_retry_le_half): from any partial state i ≤ n, a single 4 * n + 4-step batch fails to complete (i' < n) with probability at most 1 / 2 (proved via the potential function $\Phi(i) = 2^{n - i} \cdot \mathbf{1}_{i < n}$).
  5. Strict PPT certificate (isPPTOn_fyBatch): fyBatch is realized in strict probabilistic polynomial time on native multi-tape fair-coin Turing machines.

Main definitions #

Main results #

1. List representation of permutations and computable swaps #

List representation of π : Equiv.Perm (Fin n) as [π(0), π(1), ..., π(n - 1)].

@[simp]

permOfEquiv has length n.

Swap positions i and j of a list l : List ℕ.

@[simp]

listSwap preserves list length.

theorem Cslib.Probability.listSwap_permOfEquiv {n : ℕ} (π : Equiv.Perm (Fin n)) (i j : Fin n) :
listSwap (permOfEquiv π) ↑i ↑j = permOfEquiv (π * Equiv.swap i j)

listSwap on permOfEquiv π corresponds to right multiplication by Equiv.swap i j.

2. Exact Fisher–Yates continuation target and uniformity #

@[irreducible]
noncomputable def Cslib.Probability.fisherYatesTarget (n a✝ : ℕ) :

Exact Fisher–Yates continuation distribution from position i and current permutation π.

theorem Cslib.Probability.fisherYatesTarget_of_lt {n i : ℕ} (hi : i < n) (π : Equiv.Perm (Fin n)) [Nonempty (Fin (n - i))] :
fisherYatesTarget n i π = (PMF.uniformOfFintype (Fin (n - i))).bind fun (j : Fin (n - i)) => fisherYatesTarget n (i + 1) (π * Equiv.swap ⟨i, hi⟩ ⟨i + ↑j, ⋯⟩)

Evaluation of fisherYatesTarget at i < n.

Evaluation of fisherYatesTarget at n ≤ i.

Exact uniformity of Fisher–Yates: fisherYatesTarget n 0 1 is the uniform distribution on Equiv.Perm (Fin n).

3. The 4 * n + 4-step strict-PPT Fisher–Yates batch #

@[reducible, inline]

List of unary-encoded natural numbers (representing a permutation of Fin n).

noncomputable def Cslib.Probability.fyBatch (n i : ℕ) (perm : List ℕ) :

The 4 * n + 4-step probabilistic Fisher–Yates batch sampler.

4. Support invariant, harmonicity, and retry-probability bound ≤ 1 / 2 #

theorem Cslib.Probability.fyBatch_support {n i : ℕ} (hi : i ≤ n) (π : Equiv.Perm (Fin n)) (st' : ℕ × List ℕ) :
st' ∈ (fyBatch n i (permOfEquiv π)).eval.support → ∃ (i' : ℕ) (π' : Equiv.Perm (Fin n)), i' ≤ n ∧ st' = (i', permOfEquiv π')

fyBatch support invariant: applying fyBatch n to a valid state (i, permOfEquiv π) with i ≤ n only produces valid states (i', permOfEquiv π') with i' ≤ n.

theorem Cslib.Probability.fyBatch_harmonic {n i : ℕ} (hi : i ≤ n) (π : Equiv.Perm (Fin n)) (target : ℕ × List ℕ → PMF (Equiv.Perm (Fin n))) (htarget : ∀ (j : ℕ) (σ : Equiv.Perm (Fin n)), j ≤ n → target (j, permOfEquiv σ) = fisherYatesTarget n j σ) :
(fyBatch n i (permOfEquiv π)).eval.bind target = fisherYatesTarget n i π

Harmonicity of fisherYatesTarget under fyBatch: applying fyBatch n to a valid state (i, permOfEquiv π) preserves the exact continuation law fisherYatesTarget n i π.

Retry probability bound: after one 4 * n + 4-step batch fyBatch n i perm, the probability that step' < n is at most 2⁻¹ for every n, i : ℕ and perm.

5. Native multi-tape strict PPT certificate for fyBatch #

Strict PPT certificate for fyBatch: sampling the 4 * n + 4-step Fisher–Yates batch on inputs (n, i, perm) is IsPPTOn.