Zipf's law: generalized harmonic numbers, Zipf / Zipf–Mandelbrot / Yule–Simon laws
DefinitionZipfLaw_DefsCore definitions for the Zipf's-law mission.
- Generalized harmonic number. for and real ().
- Zipf distribution. for , and for every other natural number .
- Hurwitz zeta (series form). .
- Zipf–Mandelbrot law (infinite ranks). for , and for .
- Yule–Simon distribution. .
- Leading decimal digit of a natural number : .
- Random-typing word probability. With equiprobable letter keys, each typed with probability , and a space typed with probability , the probability that the word following a space is exactly is .
These are the objects in which every statement of the mission is phrased.
Formalization Note. Powers are real powers (Real.rpow). The Hurwitz series takes the value when it is not summable (Lean's tsum convention); statements using it assume . The leading digit of is .
import Mathlib
/-!
# Zipf's law — core definitions
Shared definitions for the Zipf's-law mission (source: Wikipedia, "Zipf's law").
-/
namespace ZipfLaw
/-- The `N`-th generalized harmonic number `H_{N,s} = ∑_{k=1}^{N} 1 / k^s`
(real exponent `s`; `H_{0,s} = 0`). -/
noncomputable def genHarmonic (N : ℕ) (s : ℝ) : ℝ :=
∑ k ∈ Finset.Icc 1 N, 1 / (k : ℝ) ^ s
/-- The (generalized) Zipf distribution on `N` elements with exponent `s`: the element of
rank `k ∈ {1, …, N}` has probability `(1 / k^s) / H_{N,s}`; every other `k` gets `0`. -/
noncomputable def zipfPMF (N : ℕ) (s : ℝ) (k : ℕ) : ℝ :=
if 1 ≤ k ∧ k ≤ N then (1 / (k : ℝ) ^ s) / genHarmonic N s else 0
/-- The Hurwitz zeta function, as its defining series `ζ(s, a) = ∑_{n ≥ 0} 1 / (n + a)^s`
(the value is `0` when the series is not summable). -/
noncomputable def hurwitzZetaSeries (s a : ℝ) : ℝ :=
∑' n : ℕ, 1 / ((n : ℝ) + a) ^ s
/-- The Zipf–Mandelbrot law on the ranks `k = 1, 2, 3, …` with shift `q` and exponent `s`:
`f(k; q, s) = (1 / C) · 1 / (k + q)^s` with normalizing constant
`C = ∑_{k ≥ 1} 1 / (k + q)^s = ζ(s, q + 1)` (Hurwitz zeta). Rank `0` gets `0`. -/
noncomputable def zipfMandelbrotPMF (q s : ℝ) (k : ℕ) : ℝ :=
if 1 ≤ k then 1 / (hurwitzZetaSeries s (q + 1) * ((k : ℝ) + q) ^ s) else 0
/-- The Yule–Simon distribution with parameter `ρ`:
`f(k; ρ) = ρ · B(k, ρ + 1) = ρ · Γ(k) Γ(ρ + 1) / Γ(k + ρ + 1)` for `k = 1, 2, …`. -/
noncomputable def yuleSimonPMF (ρ : ℝ) (k : ℕ) : ℝ :=
ρ * (Real.Gamma k * Real.Gamma (ρ + 1) / Real.Gamma (k + ρ + 1))
/-- The leading decimal digit of a natural number (`leadingDigit 0 = 0`). -/
def leadingDigit (n : ℕ) : ℕ :=
n / 10 ^ Nat.log 10 n
/-- Random typing with `m` equiprobable letter keys, each hit with probability `q`, and a
space bar hit with probability `1 - m q`: the probability that the word (maximal block of
letters) following a space is exactly `w`, namely `q^{|w|} (1 - m q)`. -/
noncomputable def monkeyWordProb (m : ℕ) (q : ℝ) (w : List (Fin m)) : ℝ :=
q ^ w.length * (1 - m * q)
end ZipfLaw
Read-back
What the Lean code literally says, in plain math · Aristotle (Harmonic) - non-blind, same agent as the drafter
Non-blind read-back — not independent testimony. This read-back was written by the same agent (Aristotle, by Harmonic) that drafted the Lean statement, with full knowledge of the source article and of the intended meaning. It is not a blind audit by an independent reader, and no reviewer should treat it as one. Please compare it against the Lean code yourself.
The file defines, in namespace ZipfLaw:
- for , , with the real power (for this is the usual ). Empty sum () gives .
- if , else . (Division by would give , but for the denominator is positive.)
- as a Lean
tsum: equal to the series sum if the family is summable, and otherwise. Real powers of negative bases follow Lean's convention (not used when ). - if , else (division by gives ).
- with Lean's real Gamma function (for , in Lean, so the value is ). No restriction on is built in.
- with natural-number floor-log (, so ).
- for a finite list of letters from an -letter alphabet; no constraints on , are built in (the value may be negative or exceed if is out of range).
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.