algo-risk-benford
用班佛定律分析首位數字,偵測資料造假跡象
中文翻譯
---
name: "algo-risk-benford"
description: "套用班佛定律(Benford's Law),分析首位數字的頻率分布,偵測數值資料集中的異常。當使用者需要稽核財務資料中的舞弊跡象、驗證資料完整性、或偵測捏造的數字時使用,即使他們說的是「資料造假偵測」、「首位數字檢定」或「會計舞弊篩查」也一樣。"
metadata:
category: "WP-40 風險演算法"
tags: ["risk", "benfords-law", "fraud-detection", "data-audit"]
---
# 班佛定律分析(Benford's Law Analysis)
## 概述
班佛定律預測,在自然產生的資料集中,首位數字 d 出現的機率是 P(d) = log₁₀(1 + 1/d)。數字 1 出現的比例約 30.1%,數字 9 只有約 4.6%。偏離這個分布,可能代表資料被捏造或操弄。分析複雜度 O(n)。
## 何時使用
**觸發條件:**
- 要稽核財務資料(費用、發票、報稅表)有沒有被操弄
- 要篩查大型資料集的資料完整性問題
- 要偵測捏造或被刻意湊整的數字
**何時不要用:**
- 指派或連續編號的數字(郵遞區號、電話號碼、ID)
- 範圍受限的資料集(例如人的年齡、百分比)
- 小型資料集(< 500 筆,統計檢定力不足)
## 演算法
```
鐵律:班佛定律適用於「自然產生」且橫跨多個數量級的資料
沒有橫跨多個數量級的資料(例如攝氏溫度、人的身高)
「不會」符合班佛定律。這類資料偏離班佛分布是「預期中的」,
不可疑。下結論說有舞弊之前,一定要先確認資料類型適用。
```
### 階段 1:輸入驗證
從資料集擷取首位數字。過濾:移除零、負數(取絕對值)、小於 10 的值。確認資料集橫跨多個數量級。
**關卡:** 500 筆以上、資料至少橫跨 2 個數量級。
### 階段 2:核心演算法
1. 擷取每個數字的首位數
2. 計算每個數字(1-9)的出現頻率
3. 把觀察到的頻率和班佛預期值比較:P(d) = log₁₀(1 + 1/d)
4. 統計檢定:卡方檢定、MAD(平均絕對偏差)、KS 檢定
### 階段 3:驗證
MAD 門檻:< 0.006(高度符合)、0.006-0.012(可接受)、0.012-0.015(邊緣)、> 0.015(不符合)。標記偏差大的特定數字。
**關卡:** MAD 已算出、不符合的數字已辨識。
### 階段 4:輸出
回傳符合度評估,附上各數字層級的分析。
## 輸出格式
```json
{
"conformity": "marginal",
"mad": 0.013,
"chi_squared": {"statistic": 18.5, "p_value": 0.018, "df": 8},
"digit_analysis": [{"digit": 1, "observed_pct": 25.1, "expected_pct": 30.1, "deviation": -5.0}],
"metadata": {"records": 5000, "dataset": "Q4 expense reports"}
}
```
## 範例
### 輸入輸出範例
**輸入:** 某公司應付帳款分類帳中的 1000 筆發票金額
**預期:** 首位數字應該接近 30.1%、17.6%、12.5%、9.7%、7.9%、6.7%、5.8%、5.1%、4.6%。合法資料的 MAD < 0.012。
### 邊界情況
| 輸入 | 預期 | 原因 |
|-------|----------|-----|
| 所有金額都在 $90-$99 | 數字 9 佔主導 | 範圍受限,班佛定律不適用 |
| 整數數字暴增(數字 1、5) | 標記待審查 | 可能代表以整數估算,或操弄門檻 |
| 政府預算資料 | 通常符合得很好 | 大型的自然產生財務資料集適用班佛定律 |
## 常見陷阱
- **不是舞弊的證據**:不符合是「紅旗」,不是證據。很多合法的流程也會產生非班佛的分布。一定要進一步調查。
- **第二位數檢定**:首位數檢定抓得到明顯的捏造。第二位數分析能抓到更細微的操弄(例如湊到核准門檻)。
- **合併資料集**:把不同流程的資料集混在一起,可能人為造成或抹除班佛的符合度。要分析同質的資料集。
- **核准門檻**:如果超過 $5,000 的費用需要副總核准,預期會有一堆金額剛好落在 $5,000 以下($4,9xx 區間的數字 4)。這是行為模式,要靠第二位數分析才標得出來。
- **樣本數很重要**:卡方檢定對樣本數敏感。有 10 萬筆以上時,連微不足道的偏差在統計上都會顯著。要以 MAD 為主要指標。
## 參考資料
- 第二位和第三位數的延伸,見 `references/higher-digit-tests.md`
- 舞弊偵測的案例研究,見 `references/fraud-case-studies.md`英文原文
---
name: "algo-risk-benford"
description: "Apply Benford's Law to detect anomalies in numerical datasets by analyzing first-digit frequency distributions. Use this skill when the user needs to audit financial data for fraud indicators, validate data integrity, or detect fabricated numbers — even if they say 'data manipulation detection', 'first digit test', or 'accounting fraud screening'."
metadata:
category: "WP-40 風險演算法"
tags: ["risk", "benfords-law", "fraud-detection", "data-audit"]
---
# Benford's Law Analysis
## Overview
Benford's Law predicts that in naturally occurring datasets, the leading digit d appears with probability P(d) = log₁₀(1 + 1/d). Digit 1 appears ~30.1% of the time, digit 9 only ~4.6%. Deviations from this distribution may indicate data fabrication or manipulation. Analysis runs in O(n).
## When to Use
**Trigger conditions:**
- Auditing financial data (expenses, invoices, tax returns) for manipulation
- Screening large datasets for data integrity issues
- Detecting fabricated or artificially rounded numbers
**When NOT to use:**
- For assigned/sequential numbers (zip codes, phone numbers, IDs)
- For datasets with constrained ranges (e.g., human ages, percentages)
- For small datasets (< 500 records — insufficient statistical power)
## Algorithm
```
IRON LAW: Benford's Law Applies to NATURALLY OCCURRING Data Spanning Orders of Magnitude
Data that doesn't span multiple orders of magnitude (e.g., temperatures
in Celsius, human heights) will NOT follow Benford's Law. Deviation from
Benford's in such data is EXPECTED, not suspicious. Always verify the
data type is appropriate before concluding fraud.
```
### Phase 1: Input Validation
Extract leading digits from dataset. Filter: remove zeros, negatives (take absolute value), values < 10. Verify dataset spans multiple orders of magnitude.
**Gate:** 500+ records, data spans at least 2 orders of magnitude.
### Phase 2: Core Algorithm
1. Extract first digit of each number
2. Count frequency of each digit (1-9)
3. Compare observed frequencies against Benford's expected: P(d) = log₁₀(1 + 1/d)
4. Statistical tests: chi-squared test, MAD (Mean Absolute Deviation), KS test
### Phase 3: Verification
MAD thresholds: < 0.006 (close conformity), 0.006-0.012 (acceptable), 0.012-0.015 (marginal), > 0.015 (non-conforming). Flag specific digits with large deviations.
**Gate:** MAD computed, non-conforming digits identified.
### Phase 4: Output
Return conformity assessment with digit-level analysis.
## Output Format
```json
{
"conformity": "marginal",
"mad": 0.013,
"chi_squared": {"statistic": 18.5, "p_value": 0.018, "df": 8},
"digit_analysis": [{"digit": 1, "observed_pct": 25.1, "expected_pct": 30.1, "deviation": -5.0}],
"metadata": {"records": 5000, "dataset": "Q4 expense reports"}
}
```
## Examples
### Sample I/O
**Input:** 1000 invoice amounts from a company's AP ledger
**Expected:** First digits should approximate 30.1%, 17.6%, 12.5%, 9.7%, 7.9%, 6.7%, 5.8%, 5.1%, 4.6%. MAD < 0.012 for legitimate data.
### Edge Cases
| Input | Expected | Why |
|-------|----------|-----|
| All amounts $90-$99 | Digit 9 dominates | Constrained range — Benford's doesn't apply |
| Round number spike (digit 1, 5) | Flag for review | May indicate round-number estimation or threshold manipulation |
| Government budget data | Typically conforms well | Large naturally-occurring financial datasets fit Benford's |
## Gotchas
- **Not proof of fraud**: Non-conformity is a RED FLAG, not evidence. Many legitimate processes produce non-Benford distributions. Always investigate further.
- **Second-digit test**: First digit test catches gross fabrication. Second-digit analysis catches more subtle manipulation (e.g., rounding to approval thresholds).
- **Combining datasets**: Mixing datasets from different processes may artificially create or destroy Benford conformity. Analyze homogeneous datasets.
- **Approval thresholds**: If expenses over $5,000 require VP approval, expect a spike of amounts just below $5,000 (digit 4 in the $4,9xx range). This is a behavioral pattern, flagged by second-digit analysis.
- **Sample size matters**: Chi-squared test is sensitive to sample size. With 100K+ records, even trivial deviations become statistically significant. Use MAD as primary metric.
## References
- For second and third digit extensions, see `references/higher-digit-tests.md`
- For case studies in fraud detection, see `references/fraud-case-studies.md`