tapeaudit checks a sample of auto-loan tape rows against their loan files under explicit per-field tolerances and states what the result certifies about the loans nobody looked at: an exact hypergeometric upper bound on the number of exception loans left in the pool. It also plans sample sizes under both the finite-pool and binomial models, designs two-stage plans with exact operating characteristics, and simulates the coverage of its own bounds. All data is synthetic.
Before an auto-loan securitisation, an accounting firm compares a sample of loans on the issuer's data tape with the loan files (contracts, credit reports, titles), and investors read the exception rate as a statement about data quality. Sample sizes come from habit (100, 150, 200), tolerances live in spreadsheets, and the report gives a count of exceptions with no statement of what it certifies about the thousands of loans that were not examined.
flowchart LR
T[Loan tape CSV] --> S[Seeded stratified sample<br/>largest-remainder allocation]
F[Loan-file JSON documents] --> C
S --> C[Field-by-field comparison<br/>exact, abs_tol, recomputed, VIN]
C --> E[Exceptions<br/>loan, field, tape, file, rule, difference]
E --> B[Exact bounds<br/>Clopper-Pearson and hypergeometric<br/>Bonferroni across strata]
B --> R[Agreed-upon-procedures report]
P[Sample planning<br/>two-stage OC curves] -.-> S
M[Coverage simulation] -.-> B
fields.pyandtolerances.yamldefine one rule per field:exact(origination date, FICO, state, term),abs_tol(balances +/- $1.00, APR +/- 0.0005),recomputed(LTV = amount financed / vehicle value within 0.5 points) andidentifier(VIN).vin.pyvalidates North American VINs: 17 characters, no I, O or Q, position-9 check digit.compare.pyturns a tape row and a loan file into exceptions with the rule and the difference. A blank or non-numeric tape value, or a loan file that cannot support the LTV recomputation (zero or missing value), is reported as an exception with no difference; a missing field raisesMalformedInputErrornaming the loan and field, which the CLI prints as an error.sampling.pydraws seeded simple or stratified samples (proportional, largest remainder, at least one loan per stratum).bounds.pycomputes the one-sided Clopper-Pearson bound, the largest pool exception count D with P(X <= x | N, D, n) > alpha, minimum sample sizes under both models, and the stratified bound (sum of per-stratum hypergeometric bounds at alpha / strata, Bonferroni).plans.pyevaluates two-stage plans (n1, c1, n2, c2) exactly and searches a coarse grid (stage sizes in steps of 25 up to 400, c2 below 12) for the one with the smallest expected sample size that meets both risks; it raises when no plan on the grid qualifies.simulate.pyrepeats seeded audits and scores how often each bound covers the true count.report.pyrenders the Markdown table.
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
tapeaudit generate --seed 3 --out data/
tapeaudit plan --pool 12000 --target-rate 0.01 --confidence 0.95
tapeaudit run --tape data/tape.csv --files loanfiles/ --archive data/archive.jsonl \
--n 295 --stratify originator --out data/
tapeaudit report --results data/results.json
python scripts/demo.pyrun reads loan files from --files; a file missing there is copied out of --archive (the generated store of every loan's true values), so only sampled loans ever get a file written.
tapeaudit plan --pool 12000 --target-rate 0.01 --confidence 0.95:
Pool: 12,000 loans
Target: fewer than 120 exception loans (1.00%) at 95% confidence, up to 0 exceptions found
n = 295 (hypergeometric, finite pool)
n = 299 (binomial, Clopper-Pearson)
The finite-pool bound saves 4 loans.
python scripts/demo.py (seed-3 pool, stratified sample of 295):
Pool: 12,000 synthetic loans, 246 planted defects
Plan: n = 295 (hypergeometric), n = 299 (binomial)
Bound after 0 exceptions in 295: 119 loans (Clopper-Pearson x pool: 121.2)
# Tape audit: agreed-upon procedures
Pool: 12,000 loans. Sample: 295 loans, stratified by originator (4 strata), seed 3.
| Field | Loans sampled | Exceptions | Rate | 95% upper bound, exception loans in pool |
|---|---:|---:|---:|---:|
| origination_date | 295 | 0 | 0.00% | 680 |
| fico | 295 | 1 | 0.34% | 758 |
| state | 295 | 0 | 0.00% | 680 |
| term | 295 | 0 | 0.00% | 680 |
| original_balance | 295 | 0 | 0.00% | 680 |
| current_balance | 295 | 0 | 0.00% | 680 |
| amount_financed | 295 | 0 | 0.00% | 680 |
| apr | 295 | 3 | 1.02% | 899 |
| ltv | 295 | 4 | 1.36% | 961 |
| vin | 295 | 3 | 1.02% | 899 |
| **any field** | 295 | 10 | 3.39% | 1,360 |
Allocation:
| Stratum | Pool | Sampled |
|---|---:|---:|
| ORG-A | 4,212 | 104 |
| ORG-B | 3,595 | 88 |
| ORG-C | 2,406 | 59 |
| ORG-D | 1,787 | 44 |
Upper bound: with 95% confidence the pool holds no more exception loans than shown; each stratum gets an exact hypergeometric bound at alpha/4 = 0.0125 and the bounds are summed (Bonferroni: valid whatever the dependence between strata, but several times wider than a simple-random bound at the same n). Exceptions count loans, not field differences.
Same sample, 0 exceptions: stratified Bonferroni bound 680 loans (5.7x the simple-random 119)
Two-stage plan, cheapest on the search grid (pool 12,000; accept 0.25% exceptions, reject 1.5%):
n1=225 c1=0 n2=350 c2=3; P(accept | 30) = 0.9532, P(accept | 180) = 0.0497, mean loans examined at 30 = 376.0
Coverage over 2,000 seeded audits of n = 295 (96 true APR-exception loans):
hypergeometric 95% bound covers 100.0%, mean bound 264.6
Clopper-Pearson x pool covers 100.0%, mean bound 267.3
stratified Bonferroni bound covers 100.0%, mean bound 854.4
Exact coverage of the hypergeometric bound at n = 295: 100.00% at 100 true loans, 95.03% at 120 (the bound is tight there)
Zero exceptions in 295 certifies fewer than 120 exception loans (bound: 119) where the binomial model needs 299 loans. That headline is for a simple random sample. The stratified report above uses the Bonferroni bound, which after zero exceptions in the same 295 loans says 680 (5.7% of the pool), about 5.7 times wider: stratification buys allocation control, not a tighter pool bound, and the report states this under its table.
Coverage: at 96 true loans (far below every bound) all three bounds cover 100% of runs, which a bound twice too wide would also do. Tightness is shown where it matters, at the boundary: the exact coverage of the hypergeometric bound is 100% up to 119 true loans, 95.03% at 120 and 95.01% at 190 (just past the one-exception bound of 189), so it is exact rather than loose; the tests assert this and a simulation at 120 true loans (about 95.5%, against 100% for Clopper-Pearson x pool).
- Finite-pool bound. The sample is drawn without replacement from a pool of 12,000, so the hypergeometric model is the exact one. It certifies the same target with 295 loans instead of 299 and gives a tighter bound for any observed count; the binomial bound is kept as the cross-check and the conservative choice for very large pools.
- Exact rather than normal-approximate. At rates near 1% and n near 300 the normal approximation is inaccurate; the bound is found by bisection on the exact hypergeometric CDF, and tests check known answers (0 in 299 gives 0.009969, 0 in 295 bounds the pool at 119).
- Tolerances in data, not code.
tolerances.yamlstates every tolerance. Rounding differences of up to $0.50 are generated in 20% of loans and never become exceptions. - Bonferroni across strata. Stratified bounds use alpha / K per stratum and sum the results. A union bound needs no independence or other assumption about the strata, so the sum has confidence at least 1 - alpha whatever the dependence. The price is width: with 0 exceptions it is roughly K ln(K/alpha) / ln(1/alpha) times the simple-random bound, i.e. 680 against 119 for K = 4 and n = 295 (5.7x), and in simulation it covers 100% of runs with a mean bound of about 850 where the simple bound averages 265. Use simple random sampling when the headline bound matters more than per-stratum allocation; the stratified bound is the valid conservative cross-check, not a tight bound. An exact joint stratified bound is not implemented.
- Exact means exact text. The
exactrule compares trimmed text, so a CSV spelling such as60.0against a loan-file60is an exception (tolerances.yamlsays so; a test pins it). Useabs_tolfor a field whose number formatting may differ. - Plan search is on a grid.
search_planreturns the plan with the smallest expected sample size among stage sizes in steps of 25 up to 400 and c2 below 12; it is not a global optimum and raises outside that grid. - Loans, not differences. Exceptions are counted per loan, so a loan with two bad fields counts once in the "any field" row.
- Seeded and clock-free. Generation, sampling and simulation take explicit seeds; nothing reads the wall clock or locale.
91 tests, run with pytest -q. They cover the VIN check digit, the Clopper-Pearson and hypergeometric known answers, stratified allocation, tolerance edges, a census comparison that recovers exactly the planted APR, FICO, VIN and LTV defects (precision and recall 1.0, no rounding exceptions), blank and malformed tape values (an exception row, never a crash) and loan files missing a field (a clear error), coverage over 2,000 seeded audits plus exact coverage at the bound's boundary and for the stratified bound, the two-stage acceptance probability against brute-force enumeration to 1e-12, the CLI's plan command, a generate/run/report round trip and its two input-error paths (a malformed and a missing loan file), and the APR defect count for other seeds and pool sizes.
src/tapeaudit/
fields.py tolerances.yaml vin.py compare.py comparison rules and engine
sampling.py bounds.py plans.py simulate.py sampling and statistics
generate.py audit.py report.py cli.py data, orchestration, output
scripts/demo.py
tests/
MIT. See LICENSE.
