How Benford's Law Detection in `financial_rigor.py` Identifies Financial Data Anomalies

The benford_check function in tools/financial_rigor.py detects financial data anomalies by comparing the empirical distribution of leading digits against Benford's expected logarithmic frequencies, flagging deviations via Mean Absolute Deviation (MAD) and chi-square statistics.

The ai-berkshire repository provides a specialized financial analysis toolkit that includes rigorous statistical validation methods. Within tools/financial_rigor.py, the Benford's Law detection implementation offers a lightweight, automated way to screen financial datasets for potential manipulation or data quality issues by analyzing the natural distribution of leading digits.

How the benford_check Function Works

The core logic resides in the benford_check function defined at line 14 of tools/financial_rigor.py. This implementation follows the classic Benford's Law methodology while optimizing for quick validation within automated workflows.

Extracting Leading Digits from Financial Values

The function accepts a Python list of numeric values and isolates the leading digit (1-9) for each positive number. Lines 20-28 calculate the significant digit using logarithmic transformation:

sig = 10 ** (log10(v) - floor(log10(v)))

This mathematical approach extracts the first digit without converting to strings, casting the result to an integer for classification.

Sample Size Validation

Benford's Law requires sufficient volume to produce statistically meaningful results. The code enforces a minimum threshold at lines 30-33, aborting the analysis if fewer than 50 leading digits are collected:

if n < 50:
    return None

This guard prevents false positives from undersampled datasets.

Statistical Conformance Testing

Once digits are extracted, the function computes the empirical distribution and compares it against expected Benford frequencies. The expected distribution is pre-computed at line 11 in the _BENFORD constant using the formula log10(1 + 1/d) for digits 1 through 9.

Lines 35-40 tally the observed frequencies, while lines 42-45 calculate two key conformance metrics:

  • Mean Absolute Deviation (MAD): The average absolute gap between observed and expected frequencies (line 42)
  • Chi-square statistic: The classic goodness-of-fit test (lines 44-45)

Anomaly Thresholds and Classification

The function classifies conformance based on MAD values at lines 48-55:

  • MAD < 0.006: "Close" conformity
  • MAD < 0.012: "Acceptable" conformity
  • MAD < 0.015: "Marginally Acceptable"
  • MAD ≥ 0.015: "Nonconforming" (signals potential anomalies)

A "Nonconforming" classification indicates significant deviation from Benford's Law, suggesting possible data manipulation, rounding artifacts, or other inconsistencies requiring investigation.

Interpreting Benford's Law Results

The function provides both human-readable console output and machine-parseable return values.

Console Output and Flagged Deviations

Lines 63-79 generate a diagnostic table comparing observed versus expected percentages for each digit. The output specifically flags digits whose deviation exceeds ±0.03, highlighting specific anomalies in the distribution pattern.

Programmatic Return Values

At line 81, the function returns a dictionary containing:

{
    "mad": float,           # Mean Absolute Deviation

    "chi2": float,          # Chi-square statistic

    "conformity": str,      # Text classification (e.g., "Acceptable")

    "is_conforming": bool   # True if MAD < 0.015

}

This structure enables automated decision-making in data pipelines, where is_conforming can trigger downstream alerts or validation failures.

Usage Examples

The benford_check function supports both command-line and programmatic invocation.

Run the analysis from the command line:

python3 tools/financial_rigor.py benford \
    --values '[12345, 67890, 23456, 34567, 45678, 56789, 67890]'

Import and use within Python scripts:

from tools.financial_rigor import benford_check

# Revenue figures in millions (ensure >50 entries)

revenues = [
    1234, 2345, 3456, 4567, 5678, 6789, 7890,
    8912, 9123, 10123, 11234, 12345, 13456,
    # ... additional values

]

report = benford_check(revenues)

print(report["conformity"])      # e.g., "Acceptable (可接受)"

print(report["is_conforming"])   # True or False

Summary

  • benford_check in tools/financial_rigor.py implements logarithmic extraction of leading digits (lines 20-28) to prepare financial data for analysis.
  • A 50-digit minimum sample size requirement (lines 30-33) ensures statistical validity before testing.
  • The function calculates Mean Absolute Deviation (MAD) and chi-square statistics (lines 42-45) to quantify conformance with Benford's expected distribution.
  • Four classification tiers based on MAD thresholds (lines 48-55) identify Nonconforming data that may indicate manipulation or errors.
  • The function returns structured data including a boolean is_conforming flag (line 81) for automated pipeline integration.

Frequently Asked Questions

What is the minimum dataset size required for Benford's Law detection in financial_rigor.py?

The implementation requires at least 50 leading digits to perform analysis. As shown in lines 30-33 of tools/financial_rigor.py, the function returns None if the sample size is insufficient, preventing unreliable results from small datasets.

How does the function determine if financial data conforms to Benford's Law?

The function calculates the Mean Absolute Deviation (MAD) between observed and expected digit frequencies. According to lines 48-55, data is considered conforming if MAD is less than 0.015, with stricter classifications for lower MAD values (Close: <0.006, Acceptable: <0.012).

What specific values does the benford_check function return?

At line 81, the function returns a dictionary containing the MAD value, chi-square statistic, a text conformity classification (e.g., "Acceptable"), and a boolean is_conforming flag that is True only when MAD falls below the 0.015 threshold.

Can benford_check detect specific types of financial fraud?

While the function cannot prove fraud definitively, it flags Nonconforming distributions (MAD ≥ 0.015) that deviate significantly from Benford's Law. Such deviations may indicate fabricated data, rounding biases, or artificial truncation, warranting further forensic investigation of the flagged financial series.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →