Documentation

CslibFrontier.Languages.Probabilistic.Rational

Exact rational probabilities and Bernoulli coins in ProbComp #

Every closed ProbComp program is a finite tree of uniform Fin (n + 1) choices, so the mass it assigns to any output is a rational number in [0, 1], computed structurally by ProbComp.evalRat without passing through ℝ≥0∞ (ProbComp.coe_evalRat_eq_toReal_eval, ProbComp.eval_eq_iff_evalRat_eq). Conversely, ProbComp.bernoulliRat p samples a single Fin q.den draw (q = min (max p 0) 1) to realize any clamped rational acceptance probability p ∈ ℚ without rejection loops (ProbComp.toReal_eval_bernoulliRat_true).

Main definitions #

Main results #

def Cslib.ProbComp.evalRat {α : Type} [DecidableEq α] :
ProbComp α → α → ℚ

The exact rational probability with which the closed program M outputs a, computed by recursion on its finite tree of unifFin calls.

@[simp]
theorem Cslib.ProbComp.evalRat_pure {α : Type} [DecidableEq α] (b a : α) :
(pure b).evalRat a = if a = b then 1 else 0
theorem Cslib.ProbComp.coe_evalRat_eq_toReal_eval {α : Type} [DecidableEq α] (M : ProbComp α) (a : α) :
↑(M.evalRat a) = (M.eval a).toReal

evalRat agrees with the real-valued PMF semantics (ProbComp.eval M a).toReal.

theorem Cslib.ProbComp.evalRat_eq_of_eval_eq {α : Type} [DecidableEq α] {M N : ProbComp α} (h : M.eval = N.eval) (a : α) :
M.evalRat a = N.evalRat a

Programs with equal ProbComp.eval distributions have equal rational probabilities at every outcome.

theorem Cslib.ProbComp.eval_eq_iff_evalRat_eq {α : Type} [DecidableEq α] {M N : ProbComp α} :
M.eval = N.eval ↔ ∀ (a : α), M.evalRat a = N.evalRat a

Two closed ProbComp programs have the same PMF law if and only if they assign the same rational probability to every outcome.

theorem Cslib.ProbComp.evalRat_nonneg {α : Type} [DecidableEq α] (M : ProbComp α) (a : α) :
0 ≤ M.evalRat a
theorem Cslib.ProbComp.evalRat_le_one {α : Type} [DecidableEq α] (M : ProbComp α) (a : α) :
M.evalRat a ≤ 1
@[simp]
theorem Cslib.ProbComp.evalRat_unifFin (n : ℕ) (k : Fin (n + 1)) :
(unifFin n).evalRat k = (↑n + 1)⁻¹
@[simp]
theorem Cslib.ProbComp.evalRat_uniform {α : Type} [Fintype α] [Nonempty α] [DecidableEq α] (a : α) :
theorem Cslib.ProbComp.evalRat_bind {α β : Type} [Fintype α] [DecidableEq α] [DecidableEq β] (M : ProbComp α) (f : α → ProbComp β) (b : β) :
(M >>= f).evalRat b = ∑ a : α, M.evalRat a * (f a).evalRat b
theorem Cslib.ProbComp.evalRat_map {α β : Type} [Fintype α] [DecidableEq α] [DecidableEq β] (f : α → β) (M : ProbComp α) (b : β) :
(f <$> M).evalRat b = ∑ a : α, if b = f a then M.evalRat a else 0

Flip a biased coin with clamped rational acceptance probability min (max p 0) 1 using one uniform draw from Fin q.den.

theorem Cslib.ProbComp.eval_bernoulliRat_true (p : ℚ) :
(bernoulliRat p).eval true = ↑(min (max p 0) 1).num.toNat / ↑(min (max p 0) 1).den

Exact ℝ≥0∞ acceptance probability of bernoulliRat p.

Exact real acceptance probability of bernoulliRat p: min (max (p : ℝ) 0) 1.

theorem Cslib.ProbComp.mul_toReal_eval_bernoulliRat_true_eq_min {a b : ℚ} (ha : 0 ≤ a) :
↑b * ((bernoulliRat (if b ≤ a then 1 else a / b)).eval true).toReal = min ↑a ↑b

Rejection-sampling identity: keeping mass b with the clamped ratio if b ≤ a then 1 else a / b leaves mass min a b.

Complementary false probability of bernoulliRat p.

When 1 ≤ p, bernoulliRat p outputs true with probability 1.