Repository navigation
Expand file tree
/
Copy pathanalytics_v2.py
More file actions
404 lines (349 loc) · 15.4 KB
/
Copy pathanalytics_v2.py
File metadata and controls
404 lines (349 loc) · 15.4 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
analytics_v2.py — DDCSE v2 Enhanced Analytics
================================================
Upgrades over v1:
- Multi-covenant compliance runner (not just NLR)
- Buffer severity scoring (0–100) for portfolio-level heat maps
- Cross-default cascade using NetworkX with weighted propagation paths
- Macro shock sensitivity matrix (EBITDA compression × debt increase)
- Variance-from-threshold trend detection
"""
from __future__ import annotations
import math
from dataclasses import dataclass, field
from typing import Literal
try:
import networkx as nx
NX_AVAILABLE = True
except ImportError:
NX_AVAILABLE = False
# ──────────────────────────────────────────────────────────────────────────────
# DATA STRUCTURES
# ──────────────────────────────────────────────────────────────────────────────
@dataclass
class CovenantResult:
name: str
cov_type: str
threshold: float
actual: float
operator: Literal["lte", "gte"]
unit: str | None
compliant: bool
buffer_distance: float # positive = headroom; negative = breach depth
buffer_pct: float # as % of threshold (buffer / threshold)
warning_band: float # 10% of threshold by default
status: Literal["COMPLIANT", "WATCHLIST", "WARNING", "BREACH"]
severity_score: float # 0 = no risk, 100 = deep breach
@property
def is_warning(self) -> bool:
return self.status in ("WATCHLIST", "WARNING")
@dataclass
class EntityNode:
"""Represents a debt entity in the NetworkX cross-default graph."""
entity_id: str
label: str
face_value_usd_m: float
seniority: str
rate: str
status: Literal["breach", "warning", "compliant"]
has_cross_default_clause: bool
covenant_results: list[CovenantResult] = field(default_factory=list)
@dataclass
class CascadeResult:
origin: str # entity_id that breached first
propagation_path: list[str] # ordered chain of affected entities
total_debt_at_risk_usd_m: float
entities_affected: int
direct_breach_entities: list[str]
technical_cross_defaults: list[str]
cascade_depth: int # longest path from origin
# ──────────────────────────────────────────────────────────────────────────────
# COVENANT EVALUATOR
# ──────────────────────────────────────────────────────────────────────────────
_WARNING_BAND_PCT = 0.10 # within 10% of threshold = WATCHLIST/WARNING
def evaluate_covenant(
name: str,
cov_type: str,
threshold: float,
actual: float,
operator: Literal["lte", "gte"],
unit: str | None = None,
warning_band_pct: float = _WARNING_BAND_PCT,
) -> CovenantResult:
"""
AST-safe covenant evaluation. No eval() — pure arithmetic.
Severity score:
COMPLIANT > 10% buffer → 0–30
WATCHLIST 0–10% buffer → 30–60
BREACH 0–25% depth → 60–85
BREACH > 25% depth → 85–100
"""
if threshold == 0:
raise ValueError(f"Covenant '{name}': threshold cannot be zero")
warning_band = abs(threshold) * warning_band_pct
if operator == "lte":
compliant = actual <= threshold
buffer_distance = threshold - actual # positive = headroom
else: # gte
compliant = actual >= threshold
buffer_distance = actual - threshold # positive = headroom
buffer_pct = buffer_distance / abs(threshold)
# Status classification
if compliant:
if buffer_pct <= warning_band_pct:
status: Literal["COMPLIANT","WATCHLIST","WARNING","BREACH"] = "WATCHLIST"
else:
status = "COMPLIANT"
else:
status = "BREACH"
# Severity score
if status == "COMPLIANT":
# Buffer pct capped at 50% for scoring purposes
severity = max(0, 30 - (min(buffer_pct, 0.50) / 0.50) * 30)
elif status == "WATCHLIST":
# 0–10% buffer band → 30–60
severity = 30 + (1 - buffer_pct / warning_band_pct) * 30
else:
# Breach: depth as pct of threshold
breach_depth = abs(buffer_pct)
severity = min(100, 60 + breach_depth / 0.25 * 25)
return CovenantResult(
name=name,
cov_type=cov_type,
threshold=threshold,
actual=actual,
operator=operator,
unit=unit,
compliant=compliant,
buffer_distance=round(buffer_distance, 4),
buffer_pct=round(buffer_pct, 4),
warning_band=round(warning_band, 4),
status=status,
severity_score=round(severity, 1),
)
def evaluate_package(covenant_specs: list[dict]) -> list[CovenantResult]:
"""
Evaluate a full covenant package (list of CovenantSpec dicts from real_cases.py).
Returns one CovenantResult per covenant.
"""
results = []
for spec in covenant_specs:
r = evaluate_covenant(
name=spec["name"],
cov_type=spec["type"],
threshold=spec["threshold"],
actual=spec["actual"],
operator=spec["operator"],
unit=spec.get("unit"),
)
results.append(r)
return results
def portfolio_severity_score(results: list[CovenantResult]) -> float:
"""
Aggregate severity score for a covenant package: max severity × breach_fraction.
Range 0–100.
"""
if not results:
return 0.0
max_sev = max(r.severity_score for r in results)
breach_frac = sum(1 for r in results if r.status == "BREACH") / len(results)
return round(min(100, max_sev * (0.5 + 0.5 * breach_frac)), 1)
# ──────────────────────────────────────────────────────────────────────────────
# NETWORKX CROSS-DEFAULT CASCADE
# ──────────────────────────────────────────────────────────────────────────────
def build_debt_graph(entities: list[EntityNode], edges: list[tuple[str, str]]) -> "nx.DiGraph":
"""
Build a directed NetworkX graph where:
- nodes = debt entities (HoldCo, OpCo, subs)
- edges = structural / guarantee / cross-default linkages
- direction = parent → child (guarantee flows downward, cross-defaults propagate upward)
Parameters
----------
entities : list of EntityNode describing each tranche/entity
edges : list of (parent_id, child_id) structural links
"""
if not NX_AVAILABLE:
raise ImportError("networkx is required for cascade analysis: pip install networkx")
G = nx.DiGraph()
for e in entities:
G.add_node(
e.entity_id,
label=e.label,
face_value=e.face_value_usd_m,
seniority=e.seniority,
status=e.status,
cross_default=e.has_cross_default_clause,
)
for parent, child in edges:
G.add_edge(parent, child, relation="structural_guarantee")
return G
def simulate_cascade(
G: "nx.DiGraph",
breach_entity_id: str,
max_depth: int = 5,
) -> CascadeResult:
"""
BFS propagation of a covenant breach through the cross-default graph.
A technical cross-default is triggered in an entity if:
(a) a structural parent or child has cross_default_clause = True, AND
(b) that parent/child is in breach or becomes in-scope via cascade.
Returns a CascadeResult with the full propagation map.
"""
if not NX_AVAILABLE:
raise ImportError("networkx is required")
if breach_entity_id not in G:
raise ValueError(f"Entity '{breach_entity_id}' not in graph")
visited: set[str] = {breach_entity_id}
queue: list[tuple[str, int]] = [(breach_entity_id, 0)]
propagation_path: list[str] = [breach_entity_id]
technical_cross_defaults: list[str] = []
total_at_risk = G.nodes[breach_entity_id].get("face_value", 0)
while queue:
current, depth = queue.pop(0)
if depth >= max_depth:
continue
# Propagate to all neighbours (both directions = cross-default can go either way)
neighbours = list(G.predecessors(current)) + list(G.successors(current))
for nbr in neighbours:
if nbr in visited:
continue
nbr_data = G.nodes[nbr]
if nbr_data.get("cross_default", False):
visited.add(nbr)
queue.append((nbr, depth + 1))
propagation_path.append(nbr)
technical_cross_defaults.append(nbr)
total_at_risk += nbr_data.get("face_value", 0)
return CascadeResult(
origin=breach_entity_id,
propagation_path=propagation_path,
total_debt_at_risk_usd_m=round(total_at_risk, 1),
entities_affected=len(propagation_path),
direct_breach_entities=[breach_entity_id],
technical_cross_defaults=technical_cross_defaults,
cascade_depth=max(
(nx.shortest_path_length(G.to_undirected(), breach_entity_id, t)
for t in technical_cross_defaults),
default=0,
),
)
def build_envision_graph() -> "nx.DiGraph":
"""
Pre-built Envision Healthcare debt graph based on Ch.11 First-Day Declaration.
"""
entities = [
EntityNode("holdco_rcf", "HoldCo — $400M RCF", 400, "Super Senior / 1L", "SOFR+600", "breach", True),
EntityNode("opco_a_tlb", "OpCo A — $1,270M TLB", 1270, "1st Lien Term", "SOFR+575", "warning", True),
EntityNode("opco_b_notes","OpCo B — $750M Sr Notes", 750, "2nd Lien", "8.75% Fx", "compliant", False),
EntityNode("sub1_abl", "Sub 1 — $200M ABL", 200, "Super Senior ABL", "SOFR+200", "breach", True),
EntityNode("sub2_mezz", "Sub 2 — $180M Mezz", 180, "Mezzanine", "12% PIK", "warning", True),
EntityNode("sub3_equip", "Sub 3 — $90M Equip.", 90, "Equipment Notes", "7.5% Fx", "compliant", False),
]
edges = [
("holdco_rcf", "opco_a_tlb"),
("holdco_rcf", "opco_b_notes"),
("opco_a_tlb", "sub1_abl"),
("opco_a_tlb", "sub2_mezz"),
("opco_b_notes", "sub3_equip"),
]
return build_debt_graph(entities, edges)
# ──────────────────────────────────────────────────────────────────────────────
# MACRO SHOCK SENSITIVITY MATRIX
# ──────────────────────────────────────────────────────────────────────────────
def macro_shock_matrix(
total_debt: float,
cash: float,
ebitda: float,
nlr_threshold: float,
ebitda_shocks: list[float] | None = None,
debt_shocks: list[float] | None = None,
) -> dict:
"""
Generates an (EBITDA compression × Debt increase) sensitivity matrix for NLR.
Returns a dict with keys "rows", "cols", "matrix" for easy rendering.
Each cell: {"nlr": float, "breached": bool, "buffer": float}
"""
ebitda_shocks = ebitda_shocks or [0.0, 0.10, 0.20, 0.30, 0.40, 0.50]
debt_shocks = debt_shocks or [0.0, 0.05, 0.10, 0.20, 0.30]
matrix = []
for e_shock in ebitda_shocks:
row = []
stressed_ebitda = ebitda * (1 - e_shock)
for d_shock in debt_shocks:
stressed_debt = total_debt * (1 + d_shock)
if stressed_ebitda <= 0:
nlr = math.inf
breached = True
buffer = -math.inf
else:
nlr = (stressed_debt - cash) / stressed_ebitda
breached = nlr > nlr_threshold
buffer = nlr_threshold - nlr
row.append({
"ebitda_shock": e_shock,
"debt_shock": d_shock,
"stressed_ebitda": round(stressed_ebitda, 1),
"stressed_debt": round(stressed_debt, 1),
"nlr": round(nlr, 2) if not math.isinf(nlr) else None,
"breached": breached,
"buffer": round(buffer, 2) if not math.isinf(buffer) else None,
})
matrix.append(row)
return {
"rows": [f"EBITDA –{int(s*100)}%" for s in ebitda_shocks],
"cols": [f"Debt +{int(s*100)}%" for s in debt_shocks],
"matrix": matrix,
"base_nlr": round((total_debt - cash) / ebitda, 2),
"threshold": nlr_threshold,
}
# ──────────────────────────────────────────────────────────────────────────────
# TREND EROSION DETECTOR
# ──────────────────────────────────────────────────────────────────────────────
def detect_trend_erosion(
historical_ratios: list[float],
threshold: float,
operator: Literal["lte", "gte"],
lookback_quarters: int = 4,
) -> dict:
"""
Detects whether a covenant ratio is directionally eroding toward breach.
Returns:
- direction: "deteriorating" | "improving" | "stable"
- quarters_to_breach_estimate: int | None
- velocity: change per quarter (avg of last lookback_quarters)
- current_buffer: float
"""
if len(historical_ratios) < 2:
return {"direction": "insufficient_data"}
recent = historical_ratios[-lookback_quarters:]
deltas = [recent[i+1] - recent[i] for i in range(len(recent)-1)]
avg_velocity = sum(deltas) / len(deltas) if deltas else 0.0
current = historical_ratios[-1]
if operator == "lte":
buffer = threshold - current
deteriorating = avg_velocity > 0 # ratio climbing toward threshold
else:
buffer = current - threshold
deteriorating = avg_velocity < 0 # ratio falling toward threshold
if abs(avg_velocity) < 0.05:
direction = "stable"
elif deteriorating:
direction = "deteriorating"
else:
direction = "improving"
# Quarters to breach estimate
qtb = None
if direction == "deteriorating" and avg_velocity != 0 and buffer > 0:
if operator == "lte":
qtb = math.ceil(buffer / avg_velocity)
else:
qtb = math.ceil(buffer / abs(avg_velocity))
return {
"direction": direction,
"velocity_per_quarter": round(avg_velocity, 3),
"current_buffer": round(buffer, 3),
"quarters_to_breach_estimate": qtb,
"lookback_quarters": lookback_quarters,
"current_ratio": round(current, 3),
"threshold": threshold,
}