{
 "round": "r1",
 "question": "When loan records from different source systems are converted into one target loan schema, which reconciliation catches the faults a conversion really makes: count-and-sum totals, field-level checks per record, or a sample review, and how far can each be trusted?",
 "data": {
  "sources_file": "data/sources.json",
  "as_of_date": "2026-09-30",
  "hmda_state_year": ["MT", 2017]
 },
 "layers": {
  "code": "the rule document is right; a SQL change is applied to the correctly converted table, touching exactly the rows a buggy step would touch",
  "rule": "the rule document is wrong; the SQL conversion, the independent re-implementation and the source side of the totals all read the faulted rule document"
 },
 "main_faults": [
  {"id": "M01", "layer": "code", "name": "504 undisbursed and closed loans dropped by a hard-coded status filter", "sql": "DELETE FROM loan WHERE source_system='504' AND status IN ('undisbursed','closed_other')"},
  {"id": "M02", "layer": "code", "name": "7(a) cancelled code misspelled in a CASE, so cancelled loans get no status", "sql": "UPDATE loan SET status=NULL WHERE source_system='7A' AND status='cancelled'"},
  {"id": "M03", "layer": "rule", "name": "charge-off mapped to closed for both SBA programs", "patch": {"status_map.7A.CHGOFF": "closed_other", "status_map.504.CHGOFF": "closed_other"}},
  {"id": "M04", "layer": "code", "name": "504 charge-offs mapped to paid in full by a CASE typo", "sql": "UPDATE loan SET status='paid_in_full' WHERE source_system='504' AND status='charged_off'"},
  {"id": "M05", "layer": "rule", "name": "HMDA loan amount documented as dollars (it is thousands)", "patch": {"units.HMDA.loan_amount_000s": 1}},
  {"id": "M06", "layer": "code", "name": "HMDA loan amount multiplier left out of the SQL", "sql": "UPDATE loan SET principal_usd = principal_usd/1000 WHERE source_system='HMDA'"},
  {"id": "M07", "layer": "rule", "name": "HMDA applicant income documented as dollars (it is thousands)", "patch": {"units.HMDA.applicant_income_000s": 1}},
  {"id": "M08", "layer": "code", "name": "7(a) rate left in percent for fiscal year 2021 (one batch through an older script)", "sql": "UPDATE loan SET rate_decimal = rate_decimal*100 WHERE source_system='7A' AND fiscal_year=2021"},
  {"id": "M09", "layer": "code", "name": "lender lookup joined on bank name, so a loan fans out to every lender location sharing that name", "sql": "INSERT INTO loan SELECT l.* FROM loan l JOIN (SELECT lender_name, count(DISTINCT lender_key) AS k FROM loan WHERE source_system='7A' GROUP BY 1 HAVING count(DISTINCT lender_key) > 1) d USING (lender_name), range(1, 100) r(i) WHERE l.source_system='7A' AND r.i < d.k"},
  {"id": "M10", "layer": "code", "name": "7(a) loans inner-joined to active FDIC institutions, so loans with no or an inactive certificate drop", "sql": "DELETE FROM loan WHERE source_system='7A' AND (lender_fdic_cert IS NULL OR lender_fdic_cert NOT IN (SELECT cert FROM fdic WHERE active=1))"},
  {"id": "M11", "layer": "code", "name": "SBA approval dates read with day and month swapped where the day is 12 or less", "sql": "UPDATE loan SET origination_date = make_date(year(origination_date), day(origination_date), month(origination_date)) WHERE source_system IN ('7A','504') AND day(origination_date) <= 12 AND day(origination_date) <> month(origination_date)"},
  {"id": "M12", "layer": "code", "name": "7(a) gross and guaranteed amounts swapped", "sql": "UPDATE loan SET principal_usd = guaranteed_usd, guaranteed_usd = principal_usd WHERE source_system='7A'"},
  {"id": "M13", "layer": "code", "name": "charge-off amount read from the approved-amount column", "sql": "UPDATE loan SET chargeoff_usd = principal_usd WHERE status='charged_off'"},
  {"id": "M14", "layer": "rule", "name": "HMDA purchased loans mapped as originations", "patch": {"status_map.HMDA.6": "originated"}},
  {"id": "M15", "layer": "code", "name": "rows identical in every field removed by a SELECT DISTINCT", "sql": "DELETE FROM loan WHERE exact_duplicate"},
  {"id": "M16", "layer": "code", "name": "504 amounts rounded to whole thousands", "sql": "UPDATE loan SET principal_usd = round(principal_usd/1000)*1000, chargeoff_usd = round(chargeoff_usd/1000)*1000, third_party_usd = round(third_party_usd/1000)*1000 WHERE source_system='504'"},
  {"id": "M17", "layer": "code", "name": "charge-off dates one day early (a timestamp read in the wrong time zone)", "sql": "UPDATE loan SET chargeoff_date = chargeoff_date - INTERVAL 1 DAY WHERE chargeoff_date IS NOT NULL"},
  {"id": "M18", "layer": "rule", "name": "revolver map written from the dictionary (0 and 1; the file writes N and Y)", "patch": {"revolver_map.7A": {"0": false, "1": true}}, "dictionary": true},
  {"id": "M19", "layer": "rule", "name": "status map written from the dictionary (PIF; the 7(a) file writes P I F)", "patch": {"status_map.7A.P I F": null, "status_map.7A.PIF": "paid_in_full"}, "dictionary": true}
 ],
 "checks": {
  "totals": {"by": "source_system", "measures": ["count", "sum principal_usd", "sum guaranteed_usd", "sum chargeoff_usd"], "tolerance_usd": 1.0, "source_side": "raw file with the rule document's units"},
  "totals_by_status": {"by": ["source_system", "status"], "measures": ["count", "sum principal_usd"], "tolerance_usd": 1.0, "source_side": "raw status grouped through the rule document's status map; an unmapped code groups as null"},
  "target_rules": [
   "R01 loan_id present and unique",
   "R02 status present",
   "R03 status in the segment's domain (commercial: undisbursed, active, paid_in_full, charged_off, cancelled, closed_other; residential: originated, approved_not_accepted, denied, withdrawn, incomplete, purchased)",
   "R04 record_kind is loan or application",
   "R05 principal_usd present and from 1,000 to 50,000,000",
   "R06 guaranteed_usd not above principal_usd",
   "R07 rate_decimal from 0 to 0.25 when present",
   "R08 term_months from 1 to 480 when present",
   "R09 chargeoff_usd above 0 only when status is charged_off",
   "R10 charged_off has a chargeoff_date",
   "R11 chargeoff_date only when status is charged_off",
   "R12 paid_in_full has a paid_in_full_date",
   "R13 paid_in_full_date and chargeoff_date not before origination_date",
   "R14 origination_date from 1990-10-01 to the as-of date",
   "R15 fiscal_year equals the federal fiscal year (October to September) of origination_date",
   "R16 applicant_income_usd from 1,000 to 100,000,000 when present",
   "R17 is_revolver present for 7(a)",
   "R18 lender_name present",
   "R19 chargeoff_usd not above principal_usd"
  ],
  "target_rules_fire": "a rule catches a fault when its failure count on the faulted conversion differs from its count on the correct conversion",
  "code_census": {"fields": "rules coded_fields", "fires": "a non-null source value that is not a key of its code map", "chosen_after_seeing_the_data": true},
  "record_compare": {"implementation": "tools/reference.py, Python, reads the raw files and the rule document, shares no code with sql/convert.sql", "fires": "any loan_id missing, extra or repeated, or any field different (amounts compared to the cent, rates to 1e-9)", "expected_by_construction": "every code fault, no rule fault"},
  "sample_review": {"per_source": [60, 200], "direction": "source record traced to its target record(s)", "reviewer": "knows the correct rules; any field different, no target record or more than one target record is a catch", "seed": 7, "report": "exact probability of drawing at least one affected record (hypergeometric, per source, combined), plus one seeded draw"},
  "cheap_set": ["totals", "target_rules", "code_census"]
 },
 "stakes": {
  "records_affected": "source rows whose target rows are missing, repeated or differ in any field from the correct conversion",
  "principal_affected_usd": "sum of correct principal over affected source rows",
  "chargeoff_reported_usd": "sum of chargeoff_usd over target rows with status charged_off, per SBA program",
  "chargeoff_rate": "charged-off loans over disbursed loans (active, paid_in_full, charged_off), by count and by principal, per SBA program"
 },
 "fresh_faults": {
  "tool": "tools/fresh_faults.py",
  "seed": 20261123,
  "n": 40,
  "drawn_after": "the checks and this protocol are committed",
  "families": {"drop": 0.15, "duplicate": 0.10, "remap": 0.15, "null": 0.10, "scale": 0.15, "swap": 0.10, "shift_date": 0.10, "round": 0.10, "truncate": 0.05},
  "predicates": {"whole_source": 0.3, "one_code_value": 0.4, "random_share": 0.3},
  "random_shares": [0.001, 0.005, 0.02, 0.1],
  "layer": "remap: rule or code with equal chance; scale over a whole source: rule or code with equal chance; every other draw: code",
  "redraw": "a drawn fault that changes no record is drawn again; the number of redraws is reported"
 },
 "lenders": {
  "population": "distinct 7(a) lender locations (LocationID) whose rows carry an FDIC certificate",
  "truth": "the certificate in the SBA file",
  "candidates": "every FDIC institution, active or not",
  "matchers": ["exact normalized name, unique nationwide", "normalized name and state, unique", "normalized name, city and state, unique"],
  "normalize": "upper case, '&' to AND, punctuation to space, drop the words THE, NATIONAL ASSOCIATION, N A, NA, INC, CORP, CORPORATION, CO, COMPANY, collapse spaces",
  "hmda_link": "HMDA panel Respondent RSSD ID equals FDIC FED_RSSD",
  "fields_compared": {"SBA-FDIC": ["name", "city", "state", "zip5", "active"], "HMDA-FDIC": ["name", "city", "state"]},
  "source_wins": {"legal name": "FDIC", "address (city, state, zip)": "FDIC", "active status": "FDIC", "lender the loan is assigned to and its SBA location id": "SBA", "HMDA respondent id": "HMDA"}
 },
 "decision": {
  "default": "sign off a conversion when record counts and balance totals tie out",
  "expected_call": "gate go-live on totals plus target rules plus a code census; keep the record compare for code changes; do not gate on a sample",
  "reversal": [
   "if totals alone catch at least 80% of the fresh faults, totals alone are enough",
   "if the cheap set misses more than 10% of the fresh faults that change a charge-off figure (charged-off amount or charge-off rate), add the record compare to the go-live gate"
  ]
 }
}
