Skip to content
SlateGitOrgPublic

About

Expense-claim analytics that estimates bunching under receipt and approval limits against a counterfactual density, then flags employees with cost-tier-aware tests under false-discovery control.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

justunder

CI

justunder

justunder is a command-line toolkit for auditing expense claims. It estimates how many claims are parked just under a receipt limit ($75) or an approval limit ($500) by comparing the observed amount distribution with a counterfactual density fitted away from the limit (the bunching estimator used in tax and policy research). It then tests each employee against the counterfactual of their own cost tier, with Benjamini-Hochberg false-discovery control, finds purchases split across expense reports, tests the cents endings of receipt-less claims by simulation, and merges the three kinds of evidence into one ranked case file. All data is synthetic and produced by a seeded generator in the repository.

The problem

Internal audit at an engineering consultancy of about 800 staff reviews two years of travel and expense claims. Policy requires an itemised receipt for any claim of $75.00 or more and a second approver for claims of $500.00 or more, so abuse shows up as claims parked just under those limits, single purchases split across claims, and invented amounts on receipt-less claims.

The usual tests are a list of every claim between $70 and $75 and a share-of-round-amounts rule. Both flag honest staff: in expensive cities an ordinary dinner costs $70, and some people simply round. Neither can see a purchase split into two claims in two reports. justunder replaces them with tests whose reference distribution is estimated from the data and whose false-positive rate is controlled.

How it works

flowchart LR
    A["Claim lines<br/>employees.csv, claims.csv"] --> B["Bin around each limit<br/>$1 bins at $75, $5 bins at $500"]
    B --> C["Counterfactual density<br/>degree-5 polynomial + round-amount dummies<br/>fitted outside the excluded window"]
    C --> D["Excess and missing mass<br/>bootstrap 95% CI"]
    C --> E["Tier-aware employee test<br/>binomial tail + Benjamini-Hochberg"]
    A --> F["Split episodes<br/>one employee, one merchant,<br/>2 days, different reports"]
    A --> G["Digit test on receipt-less claims<br/>chi-square, Monte Carlo p at own n"]
    E --> H["Ranked case file"]
    F --> H
    G --> H
    D --> I["report.md + charts"]
    H --> I
Loading
  • Bunching (justunder.bunching): claims are binned in $1 bins over [$40, $120) for the receipt limit and $5 bins over [$350, $650) for the approval limit. A degree-5 polynomial (Chebyshev basis) plus dummies for bins whose lower edge is a multiple of $5 or $10 is fitted by least squares to the bins outside an excluded window ([$70, $80) and [$470, $530)). Excess mass is observed minus counterfactual below the limit, missing mass is counterfactual minus observed above it. The 95% interval comes from a 500-draw residual bootstrap. Results are given overall, by cost tier and by department, with a sensitivity table over window widths.
  • Employees (justunder.employees): for each employee with at least 15 claims in [$50, $100), the expected share of claims in [$70, $75) comes from the counterfactual of the employee's own cost tier. The p-value is a binomial upper tail and the family is controlled at q = 0.05 with Benjamini-Hochberg. --pooled uses one counterfactual for everybody to show what a tier-blind test flags.
  • Splits (justunder.splits): claims of one employee at one merchant are chained while dates are at most 2 days apart; a chain is an episode when it spans at least two expense reports, every piece is under $500 and the pieces sum to $500 or more. Multi-night hotel stays are billed line by line inside one report, so they never qualify.
  • Digits (justunder.digits): receipt-less claims are classified by cents ending (.00, .50, .99, other multiple of 5 cents, other) against the population distribution of receipt-less claims in the same category. Each employee with at least 5 such claims gets a chi-square statistic and a Monte Carlo p-value from 20,000 multinomial draws at that employee's own n, then Benjamini-Hochberg. The naive rule (more than 30% round-dollar amounts) is computed alongside.
  • Cases (justunder.cases): one row per flagged employee, sorted by number of evidence types (descending), dollar exposure (descending) and employee id (ascending). Exposure is a rough dollar size, not a measured loss, and adds one estimate per evidence type: for bunching, the claims above the tier's expected count in [$70, $75) times the employee's mean amount there; for digits, the whole-dollar receipt-less claims above the expected count times the employee's mean whole-dollar amount; for splits, the full total of every split episode (both pieces, not only the part above $500). The three estimates are of different kinds, so compare exposure within one evidence type rather than across types.

Quick start

Requires Python 3.12 or newer.

python -m venv .venv
.venv/bin/pip install -e ".[dev]"

.venv/bin/justunder generate --seed 23 --out data/      # add --no-inject for honest claims only
.venv/bin/justunder bunching --data data/
.venv/bin/justunder employees --data data/              # add --pooled for the tier-blind test
.venv/bin/justunder splits --data data/
.venv/bin/justunder digits --data data/
.venv/bin/justunder cases --data data/ --top 8
.venv/bin/justunder report --data data/ --out out/      # report.md, bunching_75.png, bunching_500.png

Example output

The data set is synthetic: 800 employees (60% standard, 30% mid, 10% high cost tier), 24 months to 2026-09-30 and 186,601 claim lines. The generator plants 15 threshold huggers (309 claims moved into $70-$75), 3 employees with 2 split episodes each and 2 fabricators of receipt-less amounts, next to look-alikes: high-tier meals that cluster at $60-$75, team dinners and multi-night hotel stays over $500. Output below comes from justunder generate --seed 23.

justunder bunching (tables abridged with ...):

Limit $75 (bins $1, excluded window $70-$80)
group           claims  excess [95% CI]  of cf  missing [95% CI]  of cf
--------------  ------  ---------------  -----  ----------------  -----
all claims       59029   354 [203, 494]  10.2%    19 [-110, 144]   0.7%
tier: standard   30169    195 [97, 287]  12.9%     35 [-52, 117]   2.8%
tier: mid        18516   200 [113, 280]  18.3%       5 [-67, 75]   0.6%
tier: high       10344   -40 [-103, 28]  -4.6%     -21 [-88, 48]  -2.9%

window sensitivity
window  excess [95% CI]  missing [95% CI]
------  ---------------  ----------------
+/- 3     182 [89, 272]     62 [-20, 149]
+/- 5    354 [203, 494]    19 [-110, 144]
...

Limit $500 (bins $5, excluded window $470-$530)
group           claims  excess [95% CI]   of cf  missing [95% CI]  of cf
--------------  ------  ---------------  ------  ----------------  -----
all claims        5927    -24 [-83, 37]   -3.8%     -11 [-70, 51]  -2.2%
tier: standard    3255    -16 [-62, 30]   -4.3%      -7 [-51, 33]  -2.2%
tier: mid         1670     14 [-20, 52]    8.8%      -9 [-49, 26]  -6.3%
tier: high        1002     -22 [-48, 4]  -23.4%       4 [-20, 28]   5.3%
...

The injected excess is 309 claims; the population estimate at $75 is 354 with an interval that excludes zero. Nothing is hidden at $500 in this data set, and the estimator says so: every excess and missing interval at $500 includes zero.

justunder employees (tier-specific counterfactuals, first rows of 15):

counterfactual: tier-specific
employee      tier  claims  in $70-75  expected         p       q
--------  --------  ------  ---------  --------  --------  ------
E0471          mid     738         64      14.3  1.09e-22  0.0000
E0081          mid     450         39       8.7  1.86e-14  0.0000
E0123          mid     437         38       8.5  3.58e-14  0.0000
...
E0396     standard     450         23       6.1  9.00e-08  0.0000
E0117          mid     418         25       8.1  1.08e-06  0.0001
E0788     standard     470         19       6.3  3.10e-05  0.0017
E0252     standard     422         17       5.7  8.21e-05  0.0041

750 employees tested; 15 flagged with tier-specific counterfactuals. A tier-blind (pooled) test flags 47, including 30 high-tier employees that the tier-aware test does not flag.

All 15 planted huggers are flagged and nobody else is. The contrast line is the point of the tier-specific counterfactual: the pooled test flags 30 honest high-tier employees, whose ordinary meals sit at $60-$75.

justunder splits:

employee  merchant  first date  reports   total            claims
--------  --------  ----------  -------  ------  ----------------
E0176     SUP-9011  2024-10-11        2  543.14  C002568, C002821
E0176     ENT-9012  2025-11-15        2  699.52  C104396, C104661
E0220     SUP-9020  2024-12-30        2  520.80  C023000, C023208
E0220     ENT-9000  2025-07-18        2  741.09  C073779, C074061
E0371     SUP-9024  2025-07-18        2  605.08  C073826, C074107
E0371     ENT-9017  2025-10-06        2  749.01  C094222, C094471

6 episodes

justunder digits:

employee  receipt-less  .00 share  chi2        p       q  naive rule
--------  ------------  ---------  ----  -------  ------  ----------
E0490               36        78%  77.4  0.00005  0.0155         yes
E0493               31        61%  33.5  0.00005  0.0155         yes
E0524                7         0%  32.6  0.00010  0.0207          no

622 employees with at least 5 receipt-less claims; 3 flagged by the simulation-calibrated test, 97 by the naive rule (more than 30% round-dollar).

The two fabricators (E0490 and E0493) are flagged. The third row, E0524, is an honest employee and a false positive: five of its seven receipt-less claims end in .50 (taxis at $7.50 and $27.50, meals at $26.50 and $58.50, supplies at $66.50), a run the reference distribution makes very unlikely. It is flagged on a surplus of .50 endings, not on round dollars (its .00 share is 0%, which is why the naive rule misses it). With 622 employees tested at q = 0.05, the procedure accepts that a small fraction of flags (about one in twenty) can be chance; this is one of them. The naive rule flags 97 employees, almost all of them honest people with a few receipt-less claims.

justunder cases --top 8:

employee      tier  evidence  types  bunch q  digit q  splits  exposure $
--------  --------  --------  -----  -------  -------  ------  ----------
E0471          mid  bunching      1   0.0000        -       0    3,594.24
E0081          mid  bunching      1   0.0000        -       0    2,185.90
E0123          mid  bunching      1   0.0000        -       0    2,135.36
E0448     standard  bunching      1   0.0000        -       0    1,548.61
E0213     standard  bunching      1   0.0000        -       0    1,490.39
E0316     standard  bunching      1   0.0000        -       0    1,386.21
E0573          mid  bunching      1   0.0000        -       0    1,370.09
E0371         high    splits      1        -        -       2    1,354.09

21 cases, top 8 shown

Design decisions

  • Round amounts are modelled, not ignored. Claims pile up at multiples of $5 and $10 for reasons that have nothing to do with limits. Dummies for those bins, estimated outside the window, stop a spike at $70 from being read as bunching. The cost is two extra parameters and a wider interval; test_round_amount_spikes_are_modelled_not_mistaken_for_bunching shows the bias they remove.
  • The excluded window is a parameter, and the report shows its effect. Too narrow a window lets the bunching leak into the fit; too wide a window extrapolates the polynomial over a long gap. The sensitivity table re-estimates with several widths so a reader can see how stable the result is (at $75 the point estimate moves between about 180 and 355).
  • Each employee is tested against their own cost tier. A tier-blind counterfactual is dominated by standard-tier claims and treats a high-tier employee's normal $70 dinner as suspicious. The price is that a tier's counterfactual is fitted on fewer claims; the high tier's mass near $75 is estimated from about 10,000 claims in the binned range.
  • The digit test is calibrated by simulation, not by the chi-square distribution. With 5 to 15 receipt-less claims and rare cells such as .99, the asymptotic approximation is wrong. Each employee's p-value comes from 20,000 multinomial draws at their own n and category mix. The cost is run time; each employee gets an independent seeded stream so results do not depend on iteration order.
  • Splits are keyed, not just windowed. An episode needs one employee, one merchant, a gap of at most 2 days and at least two reports. Requiring one employee excludes team dinners, and requiring at least two reports excludes a hotel stay billed line by line. A split spread over more than 2-day gaps is missed by design.
  • The counterfactual is plain least squares. Counts are fitted unweighted, with no weighting for their different variances and no clamp at zero; the fitted counterfactual could go negative in a sparse bin. At these volumes (tens to hundreds of claims per bin) it does not, and the bootstrap interval reflects the fit's actual noise; with much thinner data, use wider bins or fewer parameters.
  • Everything is deterministic. The generator, the bootstrap and the Monte Carlo draws take explicit seeds; every sort ends in a unique key, so the case file is identical across runs and machines.

Testing

.venv/bin/python -m pytest -q

75 tests pass in about 45 seconds with GITHUB_ACTIONS=true CI=true COLUMNS=80. They cover a hand-computed bunching fixture (excess and missing mass of 120 with a linear fit, and within 1.0 with degree 5 and dummies), the binomial agreement of the Monte Carlo p-value, Benjamini-Hochberg, the split detector's look-alikes, the generator's documented properties, and recovery of every injected pattern on seed 23: the population excess is within 35% of the injected count with an interval that excludes zero (and includes zero with --no-inject), all 15 huggers are flagged with at most one other employee, the pooled test flags at least 5 honest high-tier employees, exactly the 6 planted episodes are found, both fabricators are flagged, and the calibrated digit test flags at most 2% of honest employees with 5 to 15 receipt-less claims where the naive rule flags at least 10%.

Project layout

src/justunder/
  config.py      limits, windows, tiers and shared constants
  generate.py    seeded synthetic data generator and injected patterns
  io.py          loading employees.csv, claims.csv, truth.json
  bunching.py    counterfactual fit, excess and missing mass, bootstrap
  employees.py   tier-aware binomial test per employee
  fdr.py         Benjamini-Hochberg
  splits.py      split-purchase episodes
  digits.py      cents-ending test with Monte Carlo p-values
  cases.py       merged, ranked case file
  analysis.py    grouping and formatting shared by the CLI and the report
  plots.py       histogram with counterfactual and excluded window
  report.py      report.md and charts
  tables.py      plain-text and Markdown tables
  cli.py         typer commands
tests/           unit, acceptance and CLI tests

License

MIT. See LICENSE.

About

Expense-claim analytics that estimates bunching under receipt and approval limits against a counterfactual density, then flags employees with cost-tier-aware tests under false-discovery control.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages