File size: 15,541 Bytes
3b01fc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fd88a5
 
 
 
 
 
 
3b01fc7
 
 
 
 
 
 
 
 
 
 
 
7fd88a5
3b01fc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fd88a5
 
 
 
 
 
3b01fc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fd88a5
 
 
3b01fc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/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()