“God built the universe on numbers.”

If you are working with a random dataset of numerical values, you would expect the distribution of the leading digits to be uniform. However, it is often not exactly so.

Benford’s Law (also known as the Newcomb-Benford Law, the Law of Anomalous Numbers or the First-Digit Law) states that in the numerical datasets of decimal format, the leading digit of any number is much more likely to be a 1, than any other digit. More formally, a leading digit d is expected to appear with probability:

This can be generalized to other bases as well.

It was first discovered by an astronomer called Simon Newcomb in 1881:

That the ten digits do not occur with equal frequency must be evident to any one making much use of logarithmic tables, and noticing how much faster the first pages wear out than the last ones. The first significant figure is oftener 1 than any other digit, and the frequency diminishes up to 9.

Later, in 1938, a physicist called Frank Benford re-introduced this law in his paper:

It has been observed that the pages of a much used table of common logarithms show evidences of a selective use of the natural numbers. The pages containing the logarithms of the low numbers 1 and 2 are apt to be more stained and frayed by use than those of the higher numbers 8 and 9. Of course, no one could be expected to be greatly interested in the condition of a table of logarithms, but the matter may be considered more worthy of study when we recall that the table is used in the building up of our scientific, engineering, and general factual literature. There may be, in the relative cleanliness of the pages of a logarithm table, data on how we think and how we react when dealing with things that can be described by means of numbers.

Let’s look at two examples of the Benford’s law in practice.

**I. Powers of two**

The sequence of powers of 2, {2, 4, 8, 16, 32, …}, is a clear example of the Law of Anomalous Numbers.

Quick code explanation:

- After importing the required libraries, the sequence of powers of 2 is created and assigned to variable ‘dataset’.
- The leading digits are extracted, counted and their frequencies are stored in the variable ‘freq’.
- The predictions of the Benford’s Law are also calculated according to the formula mentioned above and the results are stored in the variable ‘predi’.
- finally the results from the second and the third steps are plotted.

The resulting plot looks like this:

The orange bars show the actual frequencies of the leading digits, while the blue line shows the predictions of the Benford’s Law. As it can be clearly seen from the graph, the predictions and the reuslts are almost identical.

**II. Populations of countries**

Another great example of this phenomenon can be seen in the populations of countries. To check for its validity, we follow the same steps as in the last example. You can find the dataset here.

Quick code explanation:

This code is very similar to the previous one, except that we have used an external dataset. After organizing the external dataset for our purposes, the results are plotted.

The graph shows that the actual results are indeed very similar to the predictions.

**References**

*Newcomb, Simon: “Note on the Frequency of Use of the Different Digits in Natural Numbers”
*

*Benford, Frank: “The Law of Anomalous Numbers”*