Benford's Law Detection Feature in financial_rigor.py: A Complete Guide
The benford_check function in tools/financial_rigor.py implements a fast statistical test that compares the leading digits of financial numbers against Benford's expected logarithmic distribution to flag potentially fabricated or manipulated data.
The xbtlin/ai-berkshire repository provides a lightweight, zero-dependency mechanism for detecting anomalous financial data through digit distribution analysis. This Benford's Law detection feature serves as a critical first-line defense against data manipulation errors or fraudulent entries that might otherwise propagate into valuation models.
How Benford's Law Detection Works in financial_rigor.py
The benford_check routine follows a rigorous eight-step pipeline to analyze the authenticity of numerical sequences. According to the source code in tools/financial_rigor.py, the implementation combines classical statistical methods with practical thresholds designed specifically for financial analysis workflows.
Leading Digit Extraction and Sample Validation
The process begins by converting every input value to a positive float and isolating its most significant digit (1-9). As implemented in lines 220-229, the function extracts these leading digits into a comprehensive list for distribution analysis.
Before proceeding to statistical testing, the function enforces a minimum sample size guard at lines 230-233. Benford's Law requires sufficient observation density to produce reliable results, so the analysis aborts early if the dataset contains fewer than 50 observations. This prevents false positives from undersampled distributions.
Statistical Conformance Metrics
Once the sample passes validation, the function calculates two core statistical measures at lines 241-245:
- MAD (Mean Absolute Deviation) – The average absolute difference between observed digit frequencies and the theoretical Benford probabilities
- Chi-square – A goodness-of-fit test quantifying how closely the empirical distribution matches the expected logarithmic pattern
The expected frequencies follow the classic Benford formula log10(1 + 1/d) for digits 1-9, pre-computed in the _BENFORD constant at lines 211-212.
Classification and Reporting Logic
The function classifies conformance using MAD thresholds defined at lines 247-255:
- < 0.006 – "Close" (高度符合)
- < 0.012 – "Acceptable"
- < 0.015 – "Marginally Acceptable"
- ≥ 0.015 – "Nonconforming"
The reporting module (lines 262-779) outputs a formatted table comparing observed versus expected frequencies, highlighting deviations exceeding 0.03 absolute difference. For programmatic integration, the function returns a dictionary containing mad, chi2, conformity, and is_conforming values.
Implementation Details from the Source Code
The benford_check function in tools/financial_rigor.py operates entirely in-memory without external I/O dependencies. At line 81, the return structure ensures downstream scripts can immediately react to conformity results through the boolean is_conforming flag.
The observed distribution calculation (lines 235-240) normalizes digit counts into probabilities, creating a frequency map that directly compares against the immutable _BENFORD baseline. This design ensures thread-safe operation and consistent results across multiple invocations.
Practical Usage Examples
The following examples demonstrate how to integrate the Benford's Law detection feature into financial data pipelines:
# Example 1: Validating synthetic financial figures
from tools.financial_rigor import benford_check
# Simulated revenue, EPS, or cash flow values
sample_values = [
1_200_000, 3_450_000, 2_780_000, 9_100_000,
4_560_000, 5_120_000, 6_030_000, 7_890_000,
8_210_000, 1_340_000, 2_210_000, 3_850_000,
# ... (ensure >50 items total)
]
result = benford_check(sample_values)
print(result)
# Output: {'mad': 0.004321, 'chi2': 2.13,
# 'conformity': 'Close (高度符合)',
# 'is_conforming': True}
# Example 2: Detecting artificially uniform data
suspicious = [100, 101, 102, 103] * 20 # Forces first digit "1"
out = benford_check(suspicious)
# Console warning: "数据首位数字分布异常, 可能存在人为调整"
# out['is_conforming'] == False
These implementations allow analysts to screen data from annual reports, market feeds, or analyst estimates before incorporating them into valuation models.
Summary
- The
benford_checkfunction intools/financial_rigor.pyprovides statistical fabrication detection through leading-digit frequency analysis. - Minimum sample requirement: 50 observations required for reliable analysis (enforced at lines 230-233).
- Dual metrics: Uses MAD and Chi-square to measure conformance against Benford's Law.
- Four-tier classification: Ranges from "Close" to "Nonconforming" based on MAD thresholds (<0.006, <0.012, <0.015).
- Return value: Dictionary with
mad,chi2,conformitystring, and booleanis_conformingflag. - Zero dependencies: Pure Python implementation requiring only a list of numerical inputs.
Frequently Asked Questions
What is Benford's Law and why is it used in financial analysis?
Benford's Law predicts that in naturally occurring datasets, the digit 1 appears as the leading digit approximately 30.1% of the time, while higher digits appear with decreasing frequency following a logarithmic distribution. In financial analysis, deviations from this pattern can indicate manual data adjustments, transcription errors, or fraudulent entries that violate the expected statistical distribution of authentic financial records.
What are the MAD thresholds used in financial_rigor.py?
The implementation uses four specific Mean Absolute Deviation thresholds at lines 247-255: MAD below 0.006 indicates "Close" conformity, below 0.012 indicates "Acceptable", below 0.015 indicates "Marginally Acceptable", and anything higher marks the data as "Nonconforming". These thresholds align with standard statistical practice for digit distribution analysis.
How many observations are required for the benford_check function?
The function requires a minimum of 50 observations to perform reliable analysis. As implemented in lines 230-233, the code checks the length of the extracted digit list and aborts early if the sample size falls below this threshold, preventing statistically invalid conclusions from undersampled data.
What does the function return when testing financial data?
The function returns a dictionary containing four keys: mad (float), chi2 (float), conformity (descriptive string), and is_conforming (boolean). This structure, defined at line 81, allows both human-readable diagnostics and programmatic decision-making, enabling downstream scripts to automatically reject or flag nonconforming datasets.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →