#!/usr/bin/env python3 """ validate_dscr.py — Calculates DSCR and returns GREEN/YELLOW/RED with jurisdiction-calibrated thresholds. Usage: python scripts/validate_dscr.py --noi 850000 --debt_service 620000 --jurisdiction uk python scripts/validate_dscr.py --noi 1200000 --debt_service 850000 --jurisdiction usa --asset_type commercial Output: JSON with DSCR value, applicable threshold, margin, and RED/YELLOW/GREEN status. Sources for thresholds: typical institutional lender requirements by market. These are reference ranges — actual lender requirements vary. Verify with specific lender. """ import argparse import json import sys # ─── DSCR Thresholds by Jurisdiction and Asset Type ────────────────────────── # Format: min_normal = minimum for GREEN; min_stress = minimum for YELLOW # Below min_stress = RED (escalate immediately) THRESHOLDS = { # ── USA ────────────────────────────────────────────────────────────────── "usa_commercial": { "min_normal": 1.25, "min_stress": 1.10, "note": "US CRE institutional lender standard (CMBS/life company)", "source": "Typical US commercial real estate lending", }, "usa_residential": { "min_normal": 1.20, "min_stress": 1.05, "note": "US residential rental / multifamily", "source": "Fannie Mae / Freddie Mac multifamily guidelines", }, "usa_industrial": { "min_normal": 1.25, "min_stress": 1.10, "note": "US industrial / logistics", "source": "Typical US industrial lending", }, # ── UK ─────────────────────────────────────────────────────────────────── "uk_commercial": { "min_normal": 1.20, "min_stress": 1.05, "note": "UK CRE standard (clearing banks / debt funds)", "source": "Typical UK commercial real estate lending", }, "uk_residential": { "min_normal": 1.25, "min_stress": 1.10, "note": "UK buy-to-let / PRS residential", "source": "UK PRA / typical BTL lender requirement", }, "uk_industrial": { "min_normal": 1.20, "min_stress": 1.05, "note": "UK logistics / industrial", "source": "Typical UK industrial lending", }, # ── SPAIN ───────────────────────────────────────────────────────────────── "spain_commercial": { "min_normal": 1.25, "min_stress": 1.10, "note": "Spain CRE (banca española / European debt funds)", "source": "Typical Spain commercial real estate lending", }, "spain_residential": { "min_normal": 1.20, "min_stress": 1.05, "note": "Spain residential rental", "source": "Typical Spain residential lending", }, "spain_industrial": { "min_normal": 1.25, "min_stress": 1.10, "note": "Spain industrial / logistics", "source": "Typical Spain industrial lending", }, # ── GERMANY ─────────────────────────────────────────────────────────────── "germany_commercial": { "min_normal": 1.20, "min_stress": 1.05, "note": "Germany CRE (Pfandbrief-eligible lender standard)", "source": "Germany Pfandbrief bank / typical CRE lending", }, "germany_residential": { "min_normal": 1.20, "min_stress": 1.05, "note": "Germany residential rental (Wohnimmobilien)", "source": "Typical German residential lending", }, # ── FRANCE ──────────────────────────────────────────────────────────────── "france_commercial": { "min_normal": 1.20, "min_stress": 1.05, "note": "France CRE", "source": "Typical France commercial real estate lending", }, # ── INDIA ───────────────────────────────────────────────────────────────── "india_commercial": { "min_normal": 1.35, "min_stress": 1.15, "note": "India CRE (aligned with RBI guidelines for CRE lending)", "source": "RBI CRE lending guidelines / typical Indian bank standard", }, "india_residential": { "min_normal": 1.30, "min_stress": 1.10, "note": "India residential / housing finance", "source": "NHB / typical Indian housing finance company", }, # ── UAE ─────────────────────────────────────────────────────────────────── "uae_commercial": { "min_normal": 1.25, "min_stress": 1.10, "note": "UAE CRE (local and international bank standard)", "source": "Typical UAE commercial real estate lending", }, "uae_residential": { "min_normal": 1.25, "min_stress": 1.10, "note": "UAE residential", "source": "UAE Central Bank mortgage regulations", }, # ── INFRASTRUCTURE ──────────────────────────────────────────────────────── "infrastructure": { "min_normal": 1.40, "min_stress": 1.20, "note": "Infrastructure / project finance (PPP / concession standard)", "source": "MDB / project finance lender standard", }, "infrastructure_social": { "min_normal": 1.30, "min_stress": 1.15, "note": "Social infrastructure (hospitals, schools, availability-based)", "source": "Typical social infrastructure project finance", }, # ── DEFAULT ─────────────────────────────────────────────────────────────── "default": { "min_normal": 1.25, "min_stress": 1.10, "note": "Industry default (jurisdiction not specified or not recognized)", "source": "General industry reference", }, } # ─── Helper: Resolve threshold key ──────────────────────────────────────────── # Jurisdictions the detector recognises but for which NO calibrated DSCR threshold # exists. They resolve to "default" and the output says so explicitly # (threshold_calibrated: false). Kept in sync with data/jurisdiction_matrix.json # by tests/test_v16.py. v15 listed "singapore" here (no reference file, silently # defaulted) and rejected "canada"/"netherlands" that the detector supports. CALIBRATED_JURISDICTIONS = ["usa", "uk", "spain", "germany", "france", "india", "uae"] REFERENCE_ONLY_JURISDICTIONS = ["canada", "netherlands"] JURISDICTION_CHOICES = CALIBRATED_JURISDICTIONS + REFERENCE_ONLY_JURISDICTIONS + ["default"] def resolve_key(jurisdiction: str, asset_type: str) -> str: """Return the best matching threshold key.""" # Direct match key = f"{jurisdiction}_{asset_type}" if key in THRESHOLDS: return key # Infrastructure shortcut if asset_type in ("infrastructure", "infrastructure_social"): return asset_type if asset_type in THRESHOLDS else "infrastructure" # Jurisdiction + commercial fallback key_commercial = f"{jurisdiction}_commercial" if key_commercial in THRESHOLDS: return key_commercial return "default" def threshold_is_calibrated(thr_key: str, jurisdiction: str) -> bool: """True only when the threshold used is specific to the requested jurisdiction.""" if thr_key.startswith("infrastructure"): return True return thr_key.startswith(f"{jurisdiction}_") # ─── Core Calculation ────────────────────────────────────────────────────────── def evaluate_dscr( noi: float, debt_service: float, jurisdiction: str = "default", asset_type: str = "commercial", ) -> dict: if debt_service <= 0: return {"error": "debt_service must be greater than 0"} if noi < 0: return {"error": "NOI is negative — property is loss-making. DSCR not meaningful."} dscr = noi / debt_service thr_key = resolve_key(jurisdiction, asset_type) thr = THRESHOLDS[thr_key] margin_vs_normal = dscr - thr["min_normal"] margin_vs_stress = dscr - thr["min_stress"] # Status determination if dscr >= thr["min_normal"]: status = "GREEN" status_detail = ( f"DSCR {dscr:.2f}x exceeds minimum normal threshold of {thr['min_normal']:.2f}x " f"by {margin_vs_normal:+.2f}x. Adequate debt service coverage." ) elif dscr >= thr["min_stress"]: status = "YELLOW" status_detail = ( f"DSCR {dscr:.2f}x is below normal minimum ({thr['min_normal']:.2f}x) " f"but above stress minimum ({thr['min_stress']:.2f}x). " f"Marginal coverage — review cash flow assumptions and sensitivity." ) else: status = "RED" status_detail = ( f"DSCR {dscr:.2f}x is below stress minimum of {thr['min_stress']:.2f}x. " f"Debt service coverage is INSUFFICIENT even under stress threshold. " f"ESCALATE — do not proceed without restructuring debt or improving NOI." ) # Breakeven calculation breakeven_noi = debt_service * thr["min_normal"] noi_gap = breakeven_noi - noi return { "inputs": { "noi": noi, "debt_service": debt_service, "jurisdiction": jurisdiction, "asset_type": asset_type, }, "result": { "dscr": round(dscr, 3), "dscr_formatted": f"{dscr:.2f}x", }, "threshold_applied": { "key": thr_key, "min_normal": thr["min_normal"], "min_stress": thr["min_stress"], "note": thr["note"], "source": thr["source"], "threshold_calibrated": threshold_is_calibrated(thr_key, jurisdiction), "calibration_note": ( "Jurisdiction-specific threshold." if threshold_is_calibrated(thr_key, jurisdiction) else f"NO calibrated DSCR threshold exists for '{jurisdiction}'; industry default applied. " "State this limitation in the output and verify with the lender." ), }, "margins": { "vs_normal_threshold": f"{margin_vs_normal:+.3f}x", "vs_stress_threshold": f"{margin_vs_stress:+.3f}x", }, "breakeven": { "noi_required_for_green": round(breakeven_noi, 0), "noi_gap_to_green": round(noi_gap, 0), "noi_gap_pct": f"{(noi_gap / breakeven_noi * 100):.1f}%" if noi_gap > 0 else "0.0% — already GREEN", }, "status": status, "status_detail": status_detail, "escalation_required": status == "RED", } # ─── CLI ────────────────────────────────────────────────────────────────────── def main(): parser = argparse.ArgumentParser( description="Calculate DSCR with jurisdiction-calibrated thresholds." ) parser.add_argument("--noi", type=float, required=True, help="Annual Net Operating Income") parser.add_argument("--debt_service", type=float, required=True, help="Annual debt service (P&I or IO)") parser.add_argument( "--jurisdiction", default="default", choices=JURISDICTION_CHOICES, help=("Country/jurisdiction of the property (default: default). " "canada/netherlands are accepted but use industry-default thresholds " "(threshold_calibrated=false in the output)."), ) parser.add_argument( "--asset_type", default="commercial", choices=["commercial", "residential", "industrial", "infrastructure", "infrastructure_social", "mixed"], help="Asset type (default: commercial)", ) args = parser.parse_args() result = evaluate_dscr(args.noi, args.debt_service, args.jurisdiction, args.asset_type) print(json.dumps(result, indent=2, ensure_ascii=False)) if __name__ == "__main__": main()