BlockErrorRate.lean

Lean 4 source, lean/DataMiningAsObservation/BlockErrorRate.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
BlockErrorRate.lean. Chapter 13 section 13.4, the block error rate.

The block error rate is the fraction of transmitted blocks the receiver cannot decode. It
lies in the unit interval. A block of `n` symbols each lost independently with probability
`q` fails with probability `1 − (1 − q)^n`, which is at most `n q` by Bernoulli's inequality
and never falls as the block grows. The measured rates of the radio track, and the refuted
refresh law that was fit against them, are the ledger's numbers.
-/
import Mathlib
import DataMiningAsObservation.Bonferroni

namespace DMO.BlockErrorRate

/-- The block error rate from counts. -/
noncomputable def bler (errors blocks : ℕ) : ℝ := (errors : ℝ) / blocks

/-- **In the unit interval.** -/
theorem bler_mem_unit (errors blocks : ℕ) (hb : 0 < blocks) (hle : errors ≤ blocks) :
    0 ≤ bler errors blocks ∧ bler errors blocks ≤ 1 := by
  unfold bler
  have : (0 : ℝ) < blocks := by exact_mod_cast hb
  refine ⟨by positivity, ?_⟩
  rw [div_le_one this]
  exact_mod_cast hle

/-- The failure probability of a block of `n` independent symbols each lost with probability `q`. -/
noncomputable def blockFail (q : ℝ) (n : ℕ) : ℝ := 1 - (1 - q) ^ n

/-- **A block fails with probability at most `n q`.** -/
theorem blockFail_le (q : ℝ) (hq1 : q ≤ 1) (n : ℕ) : blockFail q n ≤ n * q :=
  DMO.Bonferroni.family_error_le q hq1 n

/-- **Longer blocks fail no less often.** -/
theorem blockFail_mono (q : ℝ) (hq0 : 0 ≤ q) (hq1 : q ≤ 1) (n : ℕ) :
    blockFail q n ≤ blockFail q (n + 1) := by
  unfold blockFail
  have h : 0 ≤ 1 - q := by linarith
  have h1 : 1 - q ≤ 1 := by linarith
  have := pow_le_pow_of_le_one h h1 (Nat.le_succ n)
  simp only [Nat.succ_eq_add_one] at this
  linarith

/-- **A block of one symbol fails exactly when the symbol is lost.** -/
theorem blockFail_one (q : ℝ) : blockFail q 1 = q := by
  unfold blockFail; ring

/-- **A lossless channel never fails a block**, and a block of no symbols never fails. -/
theorem blockFail_zero (n : ℕ) : blockFail 0 n = 0 := by
  unfold blockFail; simp

end DMO.BlockErrorRate