Benford's Law Check

easy
Unit 6 · Hypothesis Testing I0/3 to milestone

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 P(d)=log⁡10(1+1/d)P(d) = \log_{10}(1 + 1/d). 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–9
  • digit_distribution(values): the observed proportions of leading digits 1–9, skipping zeros
  • benford_mad(values): the mean absolute deviation between observed and expected proportions. Nigrini's rule of thumb says above 0.015 means non-conformity.
#fraud-detection#statistics#distributions2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Benford's Law Check · TechGradify