PoissonCeiling.lean

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

/-
PoissonCeiling.lean. Equation 0.17, the Poisson ceiling.

Under the no-structure null, a row's neighbour count is Poisson with mean `μ = n_q k / n`,
and the ceiling is the largest count that at least one of `n` rows would reach by chance.
Two facts about the tail are checked. The tail probability never increases with the count,
so the ceiling is well defined as a threshold, and the tail is the complement of the head,
so it lies between zero and one.
-/
import Mathlib

namespace DMO.PoissonCeiling

open Finset

/-- The head of the Poisson distribution, the probability of a count below `c`. -/
noncomputable def head (μ : ℝ) (c : ℕ) : ℝ := ∑ i ∈ range c, Real.exp (-μ) * μ ^ i / (i.factorial : ℝ)

/-- **Equation 0.17.** The tail, the probability of a count of at least `c`. -/
noncomputable def tail (μ : ℝ) (c : ℕ) : ℝ := 1 - head μ c

/-- Each Poisson mass is nonnegative for a nonnegative mean. -/
theorem mass_nonneg (μ : ℝ) (hμ : 0 ≤ μ) (i : ℕ) :
    0 ≤ Real.exp (-μ) * μ ^ i / (i.factorial : ℝ) := by
  positivity

/-- **The tail never increases with the count**, so a ceiling defined as the largest count
whose tail is still large enough is a threshold. -/
theorem tail_antitone (μ : ℝ) (hμ : 0 ≤ μ) (c : ℕ) : tail μ (c + 1) ≤ tail μ c := by
  unfold tail head
  rw [Finset.sum_range_succ]
  linarith [mass_nonneg μ hμ c]

/-- The tail at zero is one. Every count is at least zero. -/
theorem tail_zero (μ : ℝ) : tail μ 0 = 1 := by
  simp [tail, head]

/-- The tail is at most one. -/
theorem tail_le_one (μ : ℝ) (hμ : 0 ≤ μ) (c : ℕ) : tail μ c ≤ 1 := by
  unfold tail head
  have : 0 ≤ ∑ i ∈ range c, Real.exp (-μ) * μ ^ i / (i.factorial : ℝ) :=
    Finset.sum_nonneg fun i _ => mass_nonneg μ hμ i
  linarith

/-- **The expected number of rows at or above a count** is `n` times the tail, and the ceiling
of equation 0.17 is the largest count at which that number is still at least one. -/
noncomputable def expectedAtLeast (n : ℕ) (μ : ℝ) (c : ℕ) : ℝ := n * tail μ c

theorem expectedAtLeast_antitone (n : ℕ) (μ : ℝ) (hμ : 0 ≤ μ) (c : ℕ) :
    expectedAtLeast n μ (c + 1) ≤ expectedAtLeast n μ c := by
  unfold expectedAtLeast
  exact mul_le_mul_of_nonneg_left (tail_antitone μ hμ c) (by positivity)

/-- The Poisson mean of equation 0.17, the slots handed out per row. -/
noncomputable def poissonMean (nq k n : ℕ) : ℝ := (nq * k : ℝ) / n

/-- The chapter's example. A thousand queries, ten neighbours each, a hundred thousand rows,
mean one tenth. -/
theorem example_mean : poissonMean 1000 10 100000 = 0.1 := by
  unfold poissonMean
  norm_num

end DMO.PoissonCeiling