Documentation

CslibFrontier.Probability.DDGTree.Rejection

Exact fair-bit block-rejection sampling and capped trials #

A block-rejection sampler (Cslib.Probability.BlockRejectionSpec) reads fair bits in independent blocks of length width and applies a single-block decoder attempt : List Bool → Option α. On some a the sampler accepts and halts with a; on none it rejects the block and repeats on the remaining bits.

Running at most trials blocks gives the capped law BlockRejectionSpec.runTrials (realized by decodeTrials on trials * width fair bits), whose failure probability is rejectProb ^ trials and whose fallback completion runCapped lies within statistical distance (rejectProb ^ trials).toReal of the target distribution. Reading arbitrarily many blocks defines a computable discrete distribution generating tree BlockRejectionSpec.toDDGTree satisfying toDDGTree.run n = runTrials (n / width) for every bit budget n, generating the target law whenever 0 < width and acceptProb ≠ 0, and reading width / acceptProb fair bits in expectation (expectedFlips_toDDGTree).

Instantiating BlockRejectionSpec with binary decoding yields:

Main definitions #

Main results #

References #

Specification of a block-rejection fair-bit sampler: each trial reads width independent fair bits and applies attempt; some a accepts a, and none rejects the block.

  • width : ℕ

    Number of fair bits read per rejection trial.

  • attempt : List Bool → Option α

    Single-block decoder returning some a on acceptance and none on rejection.

Law of a single width-bit rejection trial.

Rejection probability of a single width-bit trial.

Acceptance probability 1 - rejectProb of a single width-bit trial.

The rejection probability is finite, as a finiteness rule.

Acceptance probability of a single block equals ∑' a, spec.singleLaw (some a).

Decode at most trials consecutive width-bit blocks from bits, returning the first accepted value or none.

theorem Cslib.Probability.BlockRejectionSpec.decodeTrials_succ {α : Type u_1} (spec : BlockRejectionSpec α) (t : ℕ) (bits : List Bool) :
spec.decodeTrials (t + 1) bits = match spec.attempt (List.take spec.width bits) with | some a => some a | none => spec.decodeTrials t (List.drop spec.width bits)
theorem Cslib.Probability.BlockRejectionSpec.decodeTrials_append_of_some {α : Type u_1} {spec : BlockRejectionSpec α} {t : ℕ} {w : List Bool} {a : α} (hw : t * spec.width ≤ w.length) (h : spec.decodeTrials t w = some a) (v : List Bool) :
spec.decodeTrials t (w ++ v) = some a
theorem Cslib.Probability.BlockRejectionSpec.decodeTrials_mono_of_some {α : Type u_1} {spec : BlockRejectionSpec α} {t t' : ℕ} {w : List Bool} {a : α} (htt : t ≤ t') (h : spec.decodeTrials t w = some a) :
spec.decodeTrials t' w = some a
theorem Cslib.Probability.BlockRejectionSpec.decodeTrials_map {α : Type u_1} {β : Type u_2} (spec : BlockRejectionSpec α) (f : α → β) (spec' : BlockRejectionSpec β) (hw : spec'.width = spec.width) (ha : ∀ (bits : List Bool), spec'.attempt bits = Option.map f (spec.attempt bits)) (t : ℕ) (bits : List Bool) :
spec'.decodeTrials t bits = Option.map f (spec.decodeTrials t bits)

Post-composing a single-block decoder with Option.map f commutes with decodeTrials.

noncomputable def Cslib.Probability.BlockRejectionSpec.runTrials {α : Type u_1} (spec : BlockRejectionSpec α) (trials : ℕ) :
PMF (Option α)

Distribution of running at most trials independent width-bit blocks.

theorem Cslib.Probability.BlockRejectionSpec.runTrials_succ {α : Type u_1} (spec : BlockRejectionSpec α) (t : ℕ) :
spec.runTrials (t + 1) = spec.singleLaw.bind fun (o : Option α) => match o with | some a => PMF.pure (some a) | none => spec.runTrials t

Failure probability after t trials is rejectProb ^ t.

theorem Cslib.Probability.BlockRejectionSpec.runTrials_some {α : Type u_1} (spec : BlockRejectionSpec α) (t : ℕ) (a : α) :
(spec.runTrials t) (some a) = spec.singleLaw (some a) * ∑ j ∈ Finset.range t, spec.rejectProb ^ j

Accepted outcome mass after t trials.

theorem Cslib.Probability.BlockRejectionSpec.runTrials_some_of_singleLaw {α : Type u_1} {spec : BlockRejectionSpec α} {target : PMF α} (h : ∀ (a : α), spec.singleLaw (some a) = spec.acceptProb * target a) (t : ℕ) (a : α) :
(spec.runTrials t) (some a) = (1 - spec.rejectProb ^ t) * target a

When a single trial outputs some a with probability acceptProb * target a, t trials output some a with probability (1 - rejectProb ^ t) * target a.

noncomputable def Cslib.Probability.BlockRejectionSpec.runCapped {α : Type u_1} (spec : BlockRejectionSpec α) (fallback : α) (t : ℕ) :
PMF α

Capped t-trial sampler substituting fallback if all t blocks reject.

theorem Cslib.Probability.BlockRejectionSpec.runCapped_apply {α : Type u_1} [DecidableEq α] {spec : BlockRejectionSpec α} {target : PMF α} (h : ∀ (a : α), spec.singleLaw (some a) = spec.acceptProb * target a) (fallback : α) (t : ℕ) (a : α) :
(spec.runCapped fallback t) a = (1 - spec.rejectProb ^ t) * target a + if a = fallback then spec.rejectProb ^ t else 0
theorem Cslib.Probability.BlockRejectionSpec.dist_runCapped_eq {α : Type u_1} [Fintype α] {spec : BlockRejectionSpec α} {target : PMF α} (h : ∀ (a : α), spec.singleLaw (some a) = spec.acceptProb * target a) (fallback : α) (t : ℕ) :
dist (spec.runCapped fallback t) target = (spec.rejectProb ^ t).toReal * (1 - (target fallback).toReal)

Statistical distance between the capped fallback sampler and the target distribution is (rejectProb ^ t).toReal * (1 - (target fallback).toReal), hence at most (rejectProb ^ t).toReal.

theorem Cslib.Probability.BlockRejectionSpec.dist_runCapped_le {α : Type u_1} [Fintype α] {spec : BlockRejectionSpec α} {target : PMF α} (h : ∀ (a : α), spec.singleLaw (some a) = spec.acceptProb * target a) (fallback : α) (t : ℕ) :
dist (spec.runCapped fallback t) target ≤ (spec.rejectProb ^ t).toReal
theorem Cslib.Probability.BlockRejectionSpec.closeTo_runCapped {α : Type} {spec : BlockRejectionSpec α} {target : PMF α} (h : ∀ (a : α), spec.singleLaw (some a) = spec.acceptProb * target a) (fallback : α) (t : ℕ) :
CloseTo (spec.runCapped fallback t) target (spec.rejectProb ^ t)

Without any Fintype assumption, spec.runCapped fallback t lies within CloseTo statistical distance spec.rejectProb ^ t of target.

Unbounded DDGTree realization #

Exact discrete distribution generating tree executing spec on consecutive width-bit blocks.

Running toDDGTree on n fair bits has the exact law of n / width rejection trials.

theorem Cslib.Probability.BlockRejectionSpec.toDDGTree_generates {α : Type u_1} {spec : BlockRejectionSpec α} {target : PMF α} (hw : 0 < spec.width) (hacc : spec.acceptProb ≠ 0) (h : ∀ (a : α), spec.singleLaw (some a) = spec.acceptProb * target a) :
spec.toDDGTree.Generates target

If 0 < width, acceptProb ≠ 0, and accepted trials follow target, toDDGTree generates target exactly.

Closed-form expected fair-bit cost of toDDGTree: (width : ℝ≥0∞) / acceptProb whenever 0 < width (including ⊤ when acceptProb = 0).

When 0 < width and 1 / 2 ≤ acceptProb, toDDGTree reads at most 2 * width fair bits in expectation.

Bounded integers (Fin n) #

Block-rejection specification for Fin n reading k bits per trial and keeping binary values strictly below n.

Exact block-rejection specification for Fin n reading Nat.clog 2 n bits per trial.

Dyadic bounded-integer rejection specification reading Nat.log 2 n + 1 bits per trial (2 ^ width = dyadicSize n), matching UniformNat.dyadicSize.

After t trials of uniformFinRejectionSpec n (n ≥ 1), rejection probability is at most (1 / 2) ^ t.

Root-leaf DDGTree outputting 0 : Fin n without flipping any fair bits.

Exact DDGTree generating the uniform distribution on Fin n: 0 flips at n = 1 and Nat.clog 2 n-bit block rejection for n ≥ 2.

At any power of two 2 ^ k, uniformFinTree (2 ^ k) reads k fair bits in expectation, attaining the Knuth–Yao entropy lower bound.

For every n ≥ 1, uniformFinTree n reads at most 2 * Nat.clog 2 n fair bits in expectation.

Subset rejection sampling #

def Cslib.Probability.subsetDyadicSpec {α : Type u_1} [DecidableEq α] (m : ℕ) (enum : Fin m → α) (S : Finset α) :

Exact dyadic block-rejection sampler for a subset S : Finset α inside an indexed range enum : Fin m → α.

theorem Cslib.Probability.acceptProb_subsetDyadicSpec {α : Type u_1} [DecidableEq α] {m : ℕ} {enum : Fin m → α} (hinj : Function.Injective enum) {S : Finset α} (hcov : ∀ a ∈ S, ∃ (i : Fin m), enum i = a) :
(subsetDyadicSpec m enum S).acceptProb = ↑S.card / ↑(dyadicSize m)
theorem Cslib.Probability.subsetDyadicSpec_generates {α : Type u_1} [DecidableEq α] {m : ℕ} {enum : Fin m → α} (hinj : Function.Injective enum) {S : Finset α} (hS : S.Nonempty) (hcov : ∀ a ∈ S, ∃ (i : Fin m), enum i = a) :
theorem Cslib.Probability.expectedFlips_subsetDyadicSpec {α : Type u_1} [DecidableEq α] {m : ℕ} {enum : Fin m → α} (hinj : Function.Injective enum) {S : Finset α} (hS : S.Nonempty) (hcov : ∀ a ∈ S, ∃ (i : Fin m), enum i = a) :
(subsetDyadicSpec m enum S).toDDGTree.expectedFlips = ↑(Nat.log 2 m + 1) * ↑(dyadicSize m) / ↑S.card