How a Simple Statistic Law Can Help Detecting Fraud With AWS and Python | by Alexandre Bruffa | May, 2022

Checking the validity of Benford’s legislation with Amazon Internet Providers and Python

Illustration by Gianca Chavest

Frank Benford was {an electrical} engineer identified for rediscovering a statistical curiosity concerning the prevalence of digits in lists of knowledge. This curiosity is named Benford’s law or the legislation of anomalous numbers, and has functions in accounting fraud detection, legal trials, election information, amongst others.

That is quite simple. Make an inventory of on a regular basis numbers yow will discover just like the variety of pages of your favourite e-book, the size of the river close to your house, the variety of inhabitants of your city, the land space of your nation, and so forth.

Then, calculate what number of of these numbers start with 1, with 2, and so forth. Widespread sense would dictate that the distribution of every main digit is analogous, however it’s not. The distribution is logarithmic, as proven under:

The system is the next:

P(d) = log(1 + 1 / d)
d ∈ [1, ..., 9]


  • d is an integer between 1 and 9 (0 just isn’t taken under consideration).
  • P(d) stands for the likelihood that d is the main digit.
  • log refers back to the logarithm base 10.

In different phrases, it’s extra possible that your metropolis had 100,000 or 1 million inhabitants than 900,000 or 9 million. It sounds unimaginable, proper? Let’s examine it!

First, we have to construct an infinite record of on a regular basis life numbers. These numbers can simply be discovered on the web, we simply have to repeat them from web sites after which analyze them. However oh boy, it sounds so boring! We wish to work with large information, it may take hours!

One other answer could be working with web sites or platforms which have an API like Reddit. Nonetheless, that is fairly restricted: not all web sites have an API, and we must understand one integration per web site, which is laborious and finally boring.

Hopefully, there’s a higher workaround. Do you bear in mind my earlier article concerning the PDF technology system? Nicely, we’ll reuse the primary parts to create an automatic information extraction system.

That is how we’ll do it: run a Chromium occasion on Lambda, go to an inventory of internet sites, and retrieve related information from them. Then we’ll course of the info and evaluate it with the anticipated outcome.

This text will give attention to the Lambda half and the info extraction. If you wish to make an ideal integration with Cognito, API Gateway + Authorizer, and an RDS database, I invite you to learn the next article:

Now, we have to discover web sites containing related information for our experiment. I extremely advocate the next:

  • Keep away from slow-loading web sites or web sites with some kind of Captcha or CloudFlare safety.
  • Additionally, ensure that the info offered is constant and well-referenced: we want high-quality information to examine the validity of Benford’s legislation.
  • Desire web sites with well-formatted information, if doable contained in HTML tables.

Let’s start with this nice Wikipedia article: List of mountains by elevation.

We will work out that the related information of the article (elevation of every mountain in meters and ft) is situated within the second and third columns of a number of HTML tables with a wikitable class:

We all know the place the info is, we will now extract it because of the querySelectorAll javascript technique. We use the CSS selectors .wikitable and td with the :nth-child CSS pseudo-class.

Not unhealthy! We retrieved all the weather we want. Now we’ll extract the worth of every ingredient because of the Spread syntax, the map method, and the textContent property:

Superior! Our selectors and Javascript expression are prepared, let’s go to the server-side.

We’ll reuse the identical Lambda Layer as in my previous article. It comprises the headless Chromium, the Pyppeteer library, and different dependencies.

The config file

In a config file, we arrange an inventory of internet sites with related information and their corresponding selector.

Be aware: this record might be saved within the DynamoDB desk, you possibly can examine the following article if you wish to make an ideal integration with different AWS providers.

The code

Now, let’s code the perform! 🚀


  • We create a dictionary with 9 keys, equivalent to the 9 allowed main digits (from 1 to 9), and their related values to 0.
  • We loop the web sites variable, to go to every URL and extract the related information.
  • To comprehend the extraction, we use the evaluate perform of Pyppeteer with the Javascript expression and selectors we outlined beforehand.
  • We loop the extracted array of values and we eliminated the irrelevant ones (detrimental numbers or starting with 0, empty values, and so forth. ).
  • We replace the dictionary by incrementing the corresponding worth.
  • We rely what number of values have been eliminated for informational functions.

The Take a look at

On this article, we’ll execute the perform on the server-side. So we create a brand new Take a look at with an empty physique:

With the config file we arrange, we run the take a look at, and we obtained the next outcome:

That is large! We received greater than 5000 values. Here’s a graphical illustration:

Excellent!! Benford’s legislation is actual, and we received a outcome fairly close to from the anticipated!

The anomaly we beforehand described will be discovered within the monetary statements of an organization. It sounds loopy, proper? If the anomaly cannot be discovered, that would imply that the statements are artificially created, and this may be a fraud.

Let’s examine it with the earnings assertion, the stability sheet, and the money stream of 4 ginormous corporations: Amazon, Apple, Google, and Microsoft. This data is offered on web sites like MarketWatch or Yahoo Finance.

We received the next outcome:

We received greater than 2000 values, it is a large experiment! Here’s a graphical illustration:

That is an superior outcome! Benford’s legislation works on monetary statements and apparently, not one of the 4 corporations is committing fraud (we hope so!).

This text confirmed you tips on how to extract information from a web site on AWS Lambda, and course of it. We additionally be taught concerning the unimaginable Benford’s legislation and we may examine it with a large experiment.

When you strive the info extraction to examine Benford’s legislation, please inform me which information you used and present me your outcome within the feedback, I might be happy to see them!

Benford’s legislation utilized in accounting fraud detection is brilliantly defined within the film The Accountant, I extremely advocate watching it for those who like my article.

A particular because of Gianca Chavest for designing the superior illustration.

More Posts