s LearnSqueak.com

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.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Strip trailing digits
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Zero has no nonzero leading digit. Skip it before putting observations in a histogram.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
(0 to: 5) collect: [:p | 1 bitShift: p]

Expected: #(1 2 4 8 16 32)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Create the data without rounding
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Leading digits here are 1, 2, 4, 8, 1, 3.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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

Expected: #(6 4 2 2 2 2 0 2 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Tally twenty observations
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Add the bucket counts. Expect twenty, the number of observations.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Compute the expected share
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The shares across 1 to 9 sum to one because the ratios multiply to 10.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Compare counts and expected values
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Work out the observed proportion for digit 9 by dividing the last bucket by the sample count.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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].
tally

Expected: #(0 0 0 0 0 0 20 0 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Use a counterexample
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

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

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Benford’s Law in Squeak: Count Leading Digits native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The complete static checkpoints appear above.

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.