In many natural datasets (invoice amounts, populations, transaction sizes) the leading digit isn't uniform: 1 leads about 30% of the time and 9 under 5%. Benford's law says . Fabricated numbers often break it, which is why auditors check.
Implement:
first_digit(x): the leading non-zero digit of abs(x) (for example 0.0042 → 4). Raise ValueError for 0.benford_expected(): a length-9 array for digits 1–9digit_distribution(values): the observed proportions of leading digits 1–9, skipping zerosbenford_mad(values): the mean absolute deviation between observed and expected proportions. Nigrini's rule of thumb says above 0.015 means non-conformity.