Benford Digit Forensics
Naturally occurring numeric data spanning several orders of magnitude - account balances, transaction amounts, population figures - has leading digits distributed very unevenly. A "1" is the first significant digit roughly 30% of the time; a "9" only about 4.6% of the time, following P(d) = log10(1 + 1/d). This is Benford's Law, and it is not a curiosity: numbers people INVENT tend toward a much flatter, closer-to-uniform digit distribution than the real Benford curve, because people do not intuitively fabricate numbers with the right amount of "1-heaviness". This header computes the observed first-digit and second-digit frequencies of a numeric sample and compares them against the theoretical Benford probabilities with a proper chi-square goodness-of-fit statistic - summing (observed-expected)^2/expected across all nine first-digit categories (or ten second-digit categories) - and includes the standard published chi-square critical value table (degrees of freedom 1 to 20, at significance levels 0.05 and 0.01) needed to judge the result, since MQL4 has no built-in inverse chi-square function to compute one on the fly. Comparing a ledger's digit frequencies against Benford's expectation is a genuine, widely-used forensic accounting technique - not proof of fraud on its own, but a real statistical red flag worth investigating further when it fires. The demo runs both tests against a naturally Benford-conforming dataset (compound growth values) and a fabricated one (numbers drawn uniformly from a fixed range, the way an invented set of "plausible" figures tends to look), and the two datasets produce clearly different verdicts. Notes and limitations - Benford's Law applies to numbers that span several orders of magnitude and arise from an unconstrained, multiplicative-ish process (growth series, mixed transaction sizes). It does NOT apply to data with a built-in range restriction (e.g. percentages capped at 0-100, or a fixed-size ID field) - testing such data against Benford is a misuse of the technique, not a limitation of this header's arithmetic. - A failed chi-square test is a statistical red flag, not proof of fraud - legitimate datasets can occasionally fail by chance at the chosen significance level (roughly 1-in-20 at p=0.05), and some legitimate processes genuinely do not follow Benford at all. Treat a failure as "investigate further", never as a standalone verdict. - BdfFirstDigitChiSquare/BdfSecondDigitChiSquare both require at least 5 usable (nonzero) values and return false otherwise - the chi-square approximation is not meaningful on smaller samples. - BdfChiSquareCritical only tabulates degrees of freedom 1 to 20 at alpha 0.05 and 0.01 (covering the first-digit test at df=8 and the second-digit test at df=9 with headroom); it returns -1.0 for any other combination rather than guessing.
Script · MT5 · v1.0 · Free
About Benford Digit Forensics
Benford Digit Forensics is a script for MetaTrader 5. It has 2 settings, such as Compound periods and Fabricated count. MQL source code included.
Files in the download: BenfordDigitForensics.mq5.mqh, BenfordDigitForensics.txt, BenfordDigitForensicsDemo.ex5, BenfordDigitForensicsDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Compound periods | 200 | length of the natural (compound growth) series |
| Fabricated count | 200 | length of the fabricated (uniform) series |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code