SMALLTALK LAB
Benford’s Law in Squeak: Count Leading Digits
Extract first significant digits, build a histogram, and check where Benford’s expected frequencies are a poor fit.
Before you begin
Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.
01 - Strip trailing digits
Repeated integer division by ten removes the rightmost digit. The absolute value ignores the sign. Use integers in this exercise.
| leading |
leading := [:n | | v |
v := n abs.
[v >= 10] whileTrue: [v := v // 10].
v].
{leading value: 742. leading value: -503. leading value: 9}Expected: #(7 5 9)

Try it: Zero has no nonzero leading digit. Skip it before putting observations in a histogram.
02 - Create the data without rounding
Each shift doubles 1. The growing values span multiple orders of magnitude, but the first six are far too few for a meaningful fit.
(0 to: 5) collect: [:p | 1 bitShift: p]Expected: #(1 2 4 8 16 32)

Try it: Leading digits here are 1, 2, 4, 8, 1, 3.
03 - Tally twenty observations
Index the nine buckets by the leading digit. Each observation enters exactly one bucket. The worked sample is all positive; real data should filter zeros first.
| leading sample tally |
leading := [:n | | v |
v := n abs.
[v >= 10] whileTrue: [v := v // 10].
v].
sample := (0 to: 19) collect: [:p | 1 bitShift: p].
tally := Array new: 9 withAll: 0.
sample do: [:n | | d |
d := leading value: n.
tally at: d put: (tally at: d) + 1].
tallyExpected: #(6 4 2 2 2 2 0 2 0)

Try it: Add the bucket counts. Expect twenty, the number of observations.
04 - Compute the expected share
Under the model digit d gets the fraction log base 10 of (d+1)/d. On a logarithmic decade, the interval from 1 to 2 is longer than the interval from 9 to 10.
| expected |
expected := (1 to: 9) collect: [:d | ((d + 1) / d) asFloat log].
{((expected first * 100) roundTo: 0.1) closeTo: 30.1.
((expected last * 100) roundTo: 0.1) closeTo: 4.6}Expected: #(true true)

Try it: The shares across 1 to 9 sum to one because the ratios multiply to 10.
05 - Compare counts and expected values
Digit 1 occurs six times in this example, a share of 30%. The Benford model expects about six of twenty. That is a comparison, not a statistical conclusion.
| leading sample tally |
leading := [:n | | v |
v := n abs.
[v >= 10] whileTrue: [v := v // 10].
v].
sample := (0 to: 19) collect: [:p | 1 bitShift: p].
tally := Array new: 9 withAll: 0.
sample do: [:n | | d |
d := leading value: n.
tally at: d put: (tally at: d) + 1].
{tally first / sample size. ((2.0 log) * sample size) between: 6 and: 7}Expected: {3/10. true}

Try it: Work out the observed proportion for digit 9 by dividing the last bucket by the sample count.
06 - Use a counterexample
The assigned numbers 701–720 all start with 7. Even a much larger dataset restricted to 700–799 would still fail the model. Assigned identifiers and narrow ranges are ill-suited to the comparison.
| leading sample tally |
leading := [:n | | v |
v := n abs.
[v >= 10] whileTrue: [v := v // 10].
v].
sample := 701 to: 720.
tally := Array new: 9 withAll: 0.
sample do: [:n | | d |
d := leading value: n.
tally at: d put: (tally at: d) + 1].
tallyExpected: #(0 0 0 0 0 0 20 0 0)

Try it: Try 101 to: 120. All twenty should move to the first bucket.
Replay the checkpoints

Reason about correctness and limits
Benford behavior depends on how values are generated; it is not a law for arbitrary lists. A deviation alone is not evidence of fraud.
For decimals one must define the first nonzero significant digit and avoid floating rounding surprises. Do not pass decimals into this integer implementation.
Three practice challenges
Skip zero
Why can zero not be passed into tally at: d?
Show solution
The nine slots are 1 to 9. Zero also has no nonzero leading digit.
Change the scale
What happens to the first significant digit when multiplying these positive integers by ten?
Show solution
It remains the same because only a trailing zero is appended.
Estimate a count
About how many digit-1 hits are expected in 1,000 observations under the model?
Show solution
About 301; 1000 times log10(2).
Further reading
Frank Benford’s original 1938 paper (PDF). The Workspace examples above are original.
Continue learning
Probability Lab | Online Variance | Csv Charts
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.