Benford's law: leading digits, Benford probabilities, Benford sequences
DefinitionBenfordLaw_DefsCore definitions for Benford's law in an integer base (in the theorems , usually ). Write for the fractional part.
- Leading digit. . For , writing with , , this is .
- First digits. , the integer formed by the first significant digits (leading zeros discarded).
- -th digit. .
- Benford probability. .
- Digit frequency. For a real sequence , .
- Benford sequence. satisfies Benford's law in base if as for every .
- Strong form (uniform log-significand). A random variable on has a uniform log-significand in base if is measurable, almost surely, and the law of is the uniform (Lebesgue) distribution on .
These are the objects in which every statement of the mission is phrased.
Formalization Note. Real.logb uses , so , and (junk value). For the frequency is . The strong-form predicate is a Prop-valued definition HasUniformLogMantissa b P X; it forces .
import Mathlib
/-!
# Benford's law — core definitions
Shared definitions for the Benford's-law mission (source: Wikipedia, "Benford's law").
-/
namespace BenfordLaw
/-- The leading (most significant) digit of `x` in base `b`: writing `|x| = m · b^e` with
`1 ≤ m < b` and `e ∈ ℤ`, this is `⌊m⌋`. It is computed as `⌊b ^ frac(log_b |x|)⌋`.
(Junk value: for `x = 0` this returns `1`.) -/
noncomputable def leadingDigit (b : ℕ) (x : ℝ) : ℕ :=
⌊(b : ℝ) ^ Int.fract (Real.logb b x)⌋₊
/-- The integer formed by the first `k` significant digits of `x` in base `b`
(leading zeros discarded): `⌊m · b^(k-1)⌋` where `|x| = m · b^e`, `1 ≤ m < b`. -/
noncomputable def leadingDigits (b k : ℕ) (x : ℝ) : ℕ :=
⌊(b : ℝ) ^ (Int.fract (Real.logb b x) + ((k : ℝ) - 1))⌋₊
/-- The `n`-th significant digit of `x` in base `b` (`n = 1` is the leading digit). -/
noncomputable def digitAt (b n : ℕ) (x : ℝ) : ℕ :=
leadingDigits b n x % b
/-- The Benford probability of leading digit `d` in base `b`:
`log_b (d + 1) - log_b d = log_b (1 + 1/d)`. -/
noncomputable def benfordProb (b d : ℕ) : ℝ :=
Real.logb b (1 + 1 / (d : ℝ))
/-- Relative frequency of leading digit `d` (base `b`) among the first `N` terms
`a 0, …, a (N-1)` of a sequence. -/
noncomputable def digitFreq (b d : ℕ) (a : ℕ → ℝ) (N : ℕ) : ℝ :=
(((Finset.range N).filter (fun n => leadingDigit b (a n) = d)).card : ℝ) / N
/-- A real sequence satisfies Benford's law (exactly, in the asymptotic limit) in base `b`
if for every digit `1 ≤ d < b` the relative frequency of leading digit `d` among the first
`N` terms tends to `log_b (1 + 1/d)` as `N → ∞`. -/
def IsBenfordSeq (b : ℕ) (a : ℕ → ℝ) : Prop :=
∀ d : ℕ, 1 ≤ d → d < b →
Filter.Tendsto (digitFreq b d a) Filter.atTop (nhds (benfordProb b d))
/-- Strong form of Benford's law for a random variable: `X` is measurable, almost surely
positive, and the fractional part of `log_b X` is uniformly distributed on `[0, 1)`. -/
def HasUniformLogMantissa {Ω : Type*} [MeasurableSpace Ω] (b : ℕ)
(P : MeasureTheory.Measure Ω) (X : Ω → ℝ) : Prop :=
Measurable X ∧ (∀ᵐ ω ∂P, 0 < X ω) ∧
P.map (fun ω => Int.fract (Real.logb b (X ω))) =
MeasureTheory.volume.restrict (Set.Ico (0 : ℝ) 1)
end BenfordLaw
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 BenfordLaw, the following objects (all real logarithms are , Lean's convention, and ).
- (floor into the naturals, which is for negative arguments). For , , so the value is . For or , divides by (with , so ) and the value is ; no base restriction is built into the definition.
- , where is computed in (so gives exponent , a value in , floor ).
- (natural-number remainder; is the identity).
- with real division; for this is .
- in ; for this is .
- holds iff for every natural with , as (along the naturals). For the condition is vacuous, so every sequence qualifies.
- For a measurable space , a measure (not assumed to be a probability measure) and : holds iff (i) is measurable, (ii) for -almost every , and (iii) the pushforward measure of under equals Lebesgue measure restricted to . (If the map were not a.e.-measurable the pushforward would be the zero measure, so (iii) cannot hold then; (iii) also forces .)
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.