"""Recalculate the published document cases with Python's standard library. Usage: python verify-document-cases.py document-cases.json python scripts/verify-document-cases.py (from the repository) Only uses the supplied public sample/constructed inputs; no Rust calculator code. This arithmetic check does not establish professional review or consumer outcomes. """ import json import sys from decimal import Decimal, ROUND_HALF_UP from pathlib import Path def money(value): return format(Decimal(value).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP), ".2f") def calculate(record): cases = {case["id"]: case for case in record["cases"]} cash = cases["cfpb-closing-cash"]["inputs"] def cash_due(values): v = {k: Decimal(x) for k, x in values.items()} return (v["closing_costs"] - v["paid_before"] - v["financed_costs"] + v["down_payment"] - v["deposit"] - v["funds_for_borrower"] - v["seller_credits"] - v["other_credits"]) estimate, final = cash_due(cash["estimate"]), cash_due(cash["final"]) components = cash["final_other_credits_components"] cash_result = { "estimate_cash_to_close": money(estimate), "final_cash_to_close": money(final), "closing_costs_change": money(Decimal(cash["final"]["closing_costs"]) - Decimal(cash["estimate"]["closing_costs"])), "cash_to_close_change": money(final - estimate), "final_other_credits": money(Decimal(components["title_rebate"]) + Decimal(components["seller_unpaid_tax_adjustment"]) - Decimal(components["buyer_hoa_adjustment_due"])), } assert money(estimate) == cash["estimate"]["document_cash_to_close"] assert money(final) == cash["final"]["document_cash_to_close"] budget = {k: Decimal(v) for k, v in cases["cfpb-housing-budget"]["inputs"].items()} payment = budget["monthly_principal_interest"] + budget["monthly_mortgage_insurance"] + budget["monthly_escrow"] hoa = budget["annual_non_escrow_hoa"] / 12 selected = payment + hoa wrong = payment + budget["monthly_taxes_insurance_assessments"] assert payment == budget["document_monthly_payment"] assert budget["monthly_escrow"] + hoa == budget["monthly_taxes_insurance_assessments"] budget_result = {"lender_payment": money(payment), "monthly_direct_hoa": money(hoa), "selected_housing_budget": money(selected), "double_counted_budget": money(wrong), "double_counted_amount": money(wrong - selected)} renewal = cases["tdi-renewal-exercise"]["inputs"] old, new = renewal["current"], renewal["renewal"] annual_old = Decimal(old["term_premium"]) * 12 / Decimal(old["term_months"]) annual_new = Decimal(new["term_premium"]) * 12 / Decimal(new["term_months"]) for key in ("deductible_basis", "event", "application", "backup_application"): assert old[key] == new[key], key deductible_old = Decimal(old["dwelling"]) * Decimal(old["deductible_percent"]) / 100 deductible_new = Decimal(new["dwelling"]) * Decimal(new["deductible_percent"]) / 100 renewal_result = {"current_annual_premium": money(annual_old), "renewal_annual_premium": money(annual_new), "annual_premium_change": money(annual_new - annual_old), "current_deductible": money(deductible_old), "renewal_deductible": money(deductible_new), "deductible_change": money(deductible_new - deductible_old), "backup_limit_change": money(Decimal(new["backup_limit"]) - Decimal(old["backup_limit"]))} return {"cfpb-closing-cash": cash_result, "cfpb-housing-budget": budget_result, "tdi-renewal-exercise": renewal_result} def verify(record): calculated = calculate(record) checks = 0 for case in record["cases"]: actual = calculated[case["id"]] assert actual == case["expected"], (case["id"], actual, case["expected"]) checks += len(actual) return {"cases": len(calculated), "expected_values_verified": checks, "status": "passed", "version": record["version"]} if __name__ == "__main__": default = Path(__file__).resolve().parents[1] / "src" / "document-cases.json" path = Path(sys.argv[1]) if len(sys.argv) > 1 else default print(json.dumps(verify(json.loads(path.read_text(encoding="utf-8"))), indent=2))