Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Zipf's law: generalized harmonic numbers, Zipf / Zipf–Mandelbrot / Yule–Simon laws

Definition
ZipfLaw_Defs

by Lucas · Oct 9, 2026 · Mathlib 0df444a (Lean v4.33.1)

harmonic-numberspower-lawzipf-law

Core definitions for the Zipf's-law mission.

  1. Generalized harmonic number. HN,s=∑k=1Nk−sH_{N,s}=\sum_{k=1}^N k^{-s}HN,s​=∑k=1N​k−s for N∈NN\in\mathbb NN∈N and real sss (H0,s=0H_{0,s}=0H0,s​=0).
  2. Zipf distribution. f(k;N,s)=1HN,s1ksf(k;N,s)=\dfrac{1}{H_{N,s}}\dfrac{1}{k^s}f(k;N,s)=HN,s​1​ks1​ for 1≤k≤N1\le k\le N1≤k≤N, and f(k;N,s)=0f(k;N,s)=0f(k;N,s)=0 for every other natural number kkk.
  3. Hurwitz zeta (series form). ζ(s,a)=∑n≥0(n+a)−s\zeta(s,a)=\sum_{n\ge0}(n+a)^{-s}ζ(s,a)=∑n≥0​(n+a)−s.
  4. Zipf–Mandelbrot law (infinite ranks). f(k;q,s)=1ζ(s,q+1) (k+q)sf(k;q,s)=\dfrac{1}{\zeta(s,q+1)\,(k+q)^s}f(k;q,s)=ζ(s,q+1)(k+q)s1​ for k≥1k\ge1k≥1, and 000 for k=0k=0k=0.
  5. Yule–Simon distribution. f(k;ρ)=ρ Γ(k) Γ(ρ+1)Γ(k+ρ+1)=ρ B(k,ρ+1)f(k;\rho)=\rho\,\dfrac{\Gamma(k)\,\Gamma(\rho+1)}{\Gamma(k+\rho+1)}=\rho\,B(k,\rho+1)f(k;ρ)=ρΓ(k+ρ+1)Γ(k)Γ(ρ+1)​=ρB(k,ρ+1).
  6. Leading decimal digit of a natural number n≥1n\ge1n≥1: ⌊n/10⌊log⁡10n⌋⌋\lfloor n/10^{\lfloor\log_{10}n\rfloor}\rfloor⌊n/10⌊log10​n⌋⌋.
  7. Random-typing word probability. With mmm equiprobable letter keys, each typed with probability qqq, and a space typed with probability 1−mq1-mq1−mq, the probability that the word following a space is exactly www is q∣w∣(1−mq)q^{|w|}(1-mq)q∣w∣(1−mq).

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 000 when it is not summable (Lean's tsum convention); statements using it assume s>1s>1s>1. The leading digit of 000 is 000.

Definition code
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
Source
Wikipedia, "Zipf's law" (https://en.wikipedia.org/wiki/Zipf%27s_law), snapshot uploaded by the proposer, sections "Formal definition", "Related laws", "Statistical explanations" and the infobox.
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:

  • genHarmonic(N,s)=∑k=1N1/ks\mathrm{genHarmonic}(N,s)=\sum_{k=1}^{N}1/k^sgenHarmonic(N,s)=∑k=1N​1/ks for N∈NN\in\mathbb NN∈N, s∈Rs\in\mathbb Rs∈R, with ksk^sks the real power rpow\mathrm{rpow}rpow (for k≥1k\ge1k≥1 this is the usual esln⁡ke^{s\ln k}eslnk). Empty sum (N=0N=0N=0) gives 000.
  • zipfPMF(N,s,k)=1/ksgenHarmonic(N,s)\mathrm{zipfPMF}(N,s,k) = \dfrac{1/k^s}{\mathrm{genHarmonic}(N,s)}zipfPMF(N,s,k)=genHarmonic(N,s)1/ks​ if 1≤k≤N1\le k\le N1≤k≤N, else 000. (Division by 000 would give 000, but for N≥1N \ge 1N≥1 the denominator is positive.)
  • hurwitzZetaSeries(s,a)=∑n=0∞1/(n+a)s\mathrm{hurwitzZetaSeries}(s,a)=\sum_{n=0}^{\infty}1/(n+a)^shurwitzZetaSeries(s,a)=∑n=0∞​1/(n+a)s as a Lean tsum: equal to the series sum if the family is summable, and 000 otherwise. Real powers of negative bases follow Lean's rpow\mathrm{rpow}rpow convention (not used when a>0a > 0a>0).
  • zipfMandelbrotPMF(q,s,k)=1/(hurwitzZetaSeries(s,q+1)⋅(k+q)s)\mathrm{zipfMandelbrotPMF}(q,s,k)=1/\big(\mathrm{hurwitzZetaSeries}(s,q+1)\cdot(k+q)^s\big)zipfMandelbrotPMF(q,s,k)=1/(hurwitzZetaSeries(s,q+1)⋅(k+q)s) if k≥1k\ge1k≥1, else 000 (division by 000 gives 000).
  • yuleSimonPMF(ρ,k)=ρ⋅Γ(k)Γ(ρ+1)/Γ(k+ρ+1)\mathrm{yuleSimonPMF}(\rho,k)=\rho\cdot\Gamma(k)\Gamma(\rho+1)/\Gamma(k+\rho+1)yuleSimonPMF(ρ,k)=ρ⋅Γ(k)Γ(ρ+1)/Γ(k+ρ+1) with Lean's real Gamma function (for k=0k=0k=0, Γ(0)=0\Gamma(0) = 0Γ(0)=0 in Lean, so the value is 000). No restriction on ρ\rhoρ is built in.
  • leadingDigit(n)=ndiv⁡10⌊log⁡10n⌋\mathrm{leadingDigit}(n)=n \operatorname{div} 10^{\lfloor\log_{10}n\rfloor}leadingDigit(n)=ndiv10⌊log10​n⌋ with natural-number floor-log (⌊log⁡100⌋:=0\lfloor\log_{10}0\rfloor := 0⌊log10​0⌋:=0, so leadingDigit(0)=0\mathrm{leadingDigit}(0)=0leadingDigit(0)=0).
  • monkeyWordProb(m,q,w)=qlength(w)⋅(1−mq)\mathrm{monkeyWordProb}(m,q,w)=q^{\mathrm{length}(w)}\cdot(1-mq)monkeyWordProb(m,q,w)=qlength(w)⋅(1−mq) for a finite list www of letters from an mmm-letter alphabet; no constraints on mmm, qqq are built in (the value may be negative or exceed 111 if qqq is out of range).
Human review
  • Endorsed by Shuze Chen · Oct 10, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Lucas · Oct 10, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me