value / scripts /validate_ltv.py
bonita2026's picture
Claude Fable 5
Deploy v20: engine-driven UI + SES email delivery (replaces v8 prompt wrapper)
7fd88a5 verified
Raw History Blame Contribute Delete
15.5 kB
#!/usr/bin/env python3
"""
validate_ltv.py — Calculates LTV and validates against jurisdiction-specific thresholds.
Usage:
python scripts/validate_ltv.py --loan 8500000 --value 12000000 --country uk --type commercial
python scripts/validate_ltv.py --loan 5000000 --value 8000000 --country spain --type residential
Output:
JSON with LTV %, maximum permitted LTV, safety margin, and GREEN/YELLOW/RED status.
Note: Thresholds reflect typical institutional lender requirements, not regulatory caps
(unless noted). Actual lender requirements vary — verify with specific lender/deal terms.
"""
import argparse
import json
import sys
# ─── LTV Thresholds by Country and Asset Type ─────────────────────────────────
# max_normal: Maximum LTV for GREEN status
# max_stress: Maximum LTV for YELLOW status (above this = RED)
# warning_buffer: how close to max_normal triggers a YELLOW-within-GREEN warning
LTV_THRESHOLDS = {
"spain": {
"residential": {
"max_normal": 0.80, "max_stress": 0.85,
"note": "Spain residential: typical bank max 80% (first-home exceptions to 90%)",
"regulatory_note": "Banco de España macroprudential guidance",
},
"commercial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "Spain CRE: conservative banca española standard",
"regulatory_note": "Typical Spanish CRE lending practice",
},
"industrial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "Spain industrial / logistics",
"regulatory_note": "Typical Spanish industrial lending",
},
"infrastructure": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "Spain infrastructure / PPP project finance",
"regulatory_note": "Typical project finance senior debt",
},
"mixed": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "Spain mixed-use: apply most conservative component",
"regulatory_note": "Use lower of component thresholds",
},
},
"uk": {
"residential": {
"max_normal": 0.85, "max_stress": 0.90,
"note": "UK residential mortgage (standard); BTL typically max 75-80%",
"regulatory_note": "PRA / FCA mortgage regulation",
},
"commercial": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "UK CRE: clearing bank / debt fund standard",
"regulatory_note": "Typical UK commercial real estate lending",
},
"industrial": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "UK industrial / logistics",
"regulatory_note": "Typical UK industrial lending",
},
"infrastructure": {
"max_normal": 0.80, "max_stress": 0.85,
"note": "UK PFI / infrastructure project finance",
"regulatory_note": "Typical UK infrastructure project finance",
},
"mixed": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "UK mixed-use",
"regulatory_note": "Apply lower of component thresholds",
},
},
"usa": {
"residential": {
"max_normal": 0.80, "max_stress": 0.85,
"note": "US residential conventional mortgage; FHA up to 96.5%",
"regulatory_note": "Fannie Mae / Freddie Mac conventional guidelines",
},
"commercial": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "US CRE: CMBS / life company standard",
"regulatory_note": "Typical CMBS underwriting guideline",
},
"industrial": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "US industrial / warehouse",
"regulatory_note": "Typical US industrial lending",
},
"infrastructure": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "US project finance / infrastructure",
"regulatory_note": "Typical US infrastructure project finance",
},
"mixed": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "US mixed-use",
"regulatory_note": "Apply lower of component thresholds",
},
},
"germany": {
"residential": {
"max_normal": 0.80, "max_stress": 0.85,
"note": "Germany residential (Wohnimmobilien): Pfandbrief-eligible up to 60%; total to 80%",
"regulatory_note": "Pfandbriefgesetz (PfandBG) mortgage lending value requirements",
},
"commercial": {
"max_normal": 0.60, "max_stress": 0.65,
"note": "Germany CRE: conservative Pfandbrief bank standard",
"regulatory_note": "BaFin / Pfandbrief mortgage lending value",
},
"industrial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "Germany industrial / logistics",
"regulatory_note": "Typical German industrial lending",
},
"infrastructure": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "Germany infrastructure project finance",
"regulatory_note": "Typical German project finance",
},
"mixed": {
"max_normal": 0.60, "max_stress": 0.65,
"note": "Germany mixed-use: apply conservative commercial threshold",
"regulatory_note": "Apply lower of component thresholds",
},
},
"france": {
"residential": {
"max_normal": 0.80, "max_stress": 0.85,
"note": "France residential mortgage",
"regulatory_note": "HCSF (Haut Conseil de Stabilité Financière) guidelines",
},
"commercial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "France CRE",
"regulatory_note": "Typical French CRE lending",
},
"industrial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "France logistique / industrial",
"regulatory_note": "Typical French industrial lending",
},
"infrastructure": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "France PPP / infrastructure",
"regulatory_note": "Typical French infrastructure project finance",
},
"mixed": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "France mixed-use",
"regulatory_note": "Apply lower of component thresholds",
},
},
"india": {
"residential": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "India housing finance: RBI guideline max 75-90% by loan size",
"regulatory_note": "RBI Master Direction on Housing Finance",
},
"commercial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "India CRE: RBI CRE category lending norms",
"regulatory_note": "RBI CRE category guidelines (higher risk weight)",
},
"industrial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "India industrial",
"regulatory_note": "Typical Indian industrial lending",
},
"infrastructure": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "India project finance / infrastructure",
"regulatory_note": "RBI infrastructure project finance guidelines",
},
"mixed": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "India mixed-use",
"regulatory_note": "Apply lower of component thresholds",
},
},
"uae": {
"residential": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "UAE residential mortgage: UAE Central Bank max 75% (nationals 80%)",
"regulatory_note": "UAE Central Bank Mortgage Regulation 2013",
},
"commercial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "UAE CRE",
"regulatory_note": "Typical UAE commercial real estate lending",
},
"industrial": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "UAE industrial",
"regulatory_note": "Typical UAE industrial lending",
},
"infrastructure": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "UAE project finance",
"regulatory_note": "Typical UAE/GCC project finance",
},
"mixed": {
"max_normal": 0.65, "max_stress": 0.70,
"note": "UAE mixed-use",
"regulatory_note": "Apply lower of component thresholds",
},
},
"default": {
"residential": {
"max_normal": 0.80, "max_stress": 0.85,
"note": "Industry default — jurisdiction not specified",
"regulatory_note": "General market reference",
},
"commercial": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "Industry default — jurisdiction not specified",
"regulatory_note": "General market reference",
},
"industrial": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "Industry default — jurisdiction not specified",
"regulatory_note": "General market reference",
},
"infrastructure": {
"max_normal": 0.75, "max_stress": 0.80,
"note": "Industry default — jurisdiction not specified",
"regulatory_note": "General market reference",
},
"mixed": {
"max_normal": 0.70, "max_stress": 0.75,
"note": "Industry default — jurisdiction not specified",
"regulatory_note": "General market reference",
},
},
}
# ─── Core Calculation ──────────────────────────────────────────────────────────
# See validate_dscr.py: same coverage contract, enforced by tests/test_v16.py
# against data/jurisdiction_matrix.json.
CALIBRATED_COUNTRIES = ["spain", "uk", "usa", "germany", "france", "india", "uae"]
REFERENCE_ONLY_COUNTRIES = ["canada", "netherlands"]
COUNTRY_CHOICES = CALIBRATED_COUNTRIES + REFERENCE_ONLY_COUNTRIES + ["default"]
def evaluate_ltv(
loan: float,
value: float,
country: str = "default",
asset_type: str = "commercial",
) -> dict:
if value <= 0:
return {"error": "Property value must be greater than 0"}
if loan < 0:
return {"error": "Loan amount cannot be negative"}
ltv = loan / value
calibrated = country in LTV_THRESHOLDS and country != "default"
country_thresholds = LTV_THRESHOLDS.get(country, LTV_THRESHOLDS["default"])
thr = country_thresholds.get(asset_type, country_thresholds.get("commercial", LTV_THRESHOLDS["default"]["commercial"]))
headroom = thr["max_normal"] - ltv
headroom_stress = thr["max_stress"] - ltv
# Status
if ltv <= thr["max_normal"]:
if headroom >= 0.05:
status = "GREEN"
status_detail = (
f"LTV {ltv*100:.1f}% is within the {thr['max_normal']*100:.0f}% normal threshold "
f"with {headroom*100:.1f}% headroom. Adequate collateral coverage."
)
else:
status = "YELLOW"
status_detail = (
f"LTV {ltv*100:.1f}% is within the {thr['max_normal']*100:.0f}% normal threshold "
f"but headroom is only {headroom*100:.1f}% — below the 5% comfort buffer. "
f"Monitor closely; any value decline pushes into breach territory."
)
elif ltv <= thr["max_stress"]:
status = "YELLOW"
status_detail = (
f"LTV {ltv*100:.1f}% exceeds normal maximum ({thr['max_normal']*100:.0f}%) "
f"but is within stress maximum ({thr['max_stress']*100:.0f}%). "
f"Review underwriting assumptions; consider additional equity or collateral."
)
else:
status = "RED"
status_detail = (
f"LTV {ltv*100:.1f}% EXCEEDS stress maximum of {thr['max_stress']*100:.0f}%. "
f"ESCALATE — this loan structure is outside standard market parameters. "
f"Additional equity injection required: "
f"{(loan - value * thr['max_stress']):,.0f} units of currency."
)
# Required loan reduction for GREEN
max_loan_normal = value * thr["max_normal"]
max_loan_stress = value * thr["max_stress"]
required_reduction = max(0, loan - max_loan_normal)
return {
"inputs": {
"loan": loan,
"property_value": value,
"country": country,
"asset_type": asset_type,
},
"result": {
"ltv": round(ltv, 4),
"ltv_pct": f"{ltv * 100:.2f}%",
"equity_pct": f"{(1 - ltv) * 100:.2f}%",
"equity_amount": round(value - loan, 0),
},
"threshold_applied": {
"max_normal_pct": f"{thr['max_normal'] * 100:.0f}%",
"max_stress_pct": f"{thr['max_stress'] * 100:.0f}%",
"max_loan_at_normal": round(max_loan_normal, 0),
"max_loan_at_stress": round(max_loan_stress, 0),
"note": thr["note"],
"regulatory_note": thr["regulatory_note"],
"threshold_calibrated": calibrated,
"calibration_note": (
"Country-specific threshold." if calibrated
else f"NO calibrated LTV threshold exists for '{country}'; industry default applied. "
"State this limitation in the output and verify with the lender."
),
},
"headroom": {
"vs_normal_threshold_pct": f"{headroom * 100:+.2f}%",
"vs_stress_threshold_pct": f"{headroom_stress * 100:+.2f}%",
"loan_reduction_needed_for_green": round(required_reduction, 0),
},
"status": status,
"status_detail": status_detail,
"escalation_required": status == "RED",
}
# ─── CLI ──────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(
description="Calculate LTV with jurisdiction-specific thresholds."
)
parser.add_argument("--loan", type=float, required=True, help="Loan amount")
parser.add_argument("--value", type=float, required=True, help="Property market value")
parser.add_argument(
"--country",
default="default",
choices=COUNTRY_CHOICES,
help=("Country (default: default). canada/netherlands are accepted but use "
"industry-default thresholds (threshold_calibrated=false in the output)."),
)
parser.add_argument(
"--type",
dest="asset_type",
default="commercial",
choices=["residential", "commercial", "industrial", "infrastructure", "mixed"],
help="Asset type (default: commercial)",
)
args = parser.parse_args()
result = evaluate_ltv(args.loan, args.value, args.country, args.asset_type)
print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == "__main__":
main()