-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcounterfactual_engine.py
More file actions
434 lines (369 loc) · 18.2 KB
/
counterfactual_engine.py
File metadata and controls
434 lines (369 loc) · 18.2 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""
Counterfactual reasoning engine — "what would have happened if...?"
Implements:
- Counterfactual trade analysis: what if we had entered earlier/later?
- Parameter sensitivity: what if the lookback was X instead of Y?
- Regime counterfactual: what if the regime had been different?
- Portfolio counterfactual: what if allocation was different?
- Event removal: what if a specific event (news, earnings) hadn't happened?
- Synthetic counterfactual via causal inference (potential outcomes)
- Counterfactual P&L: reconstruct alternate histories
- Scenario comparison: actual vs counterfactual equity curves
- Regret analysis: which decisions had highest opportunity cost?
- Learning: distill counterfactual insights into future decision rules
"""
from __future__ import annotations
import math
import numpy as np
from dataclasses import dataclass, field
from typing import Optional, Callable
# ── Counterfactual Result ─────────────────────────────────────────────────────
@dataclass
class CounterfactualResult:
"""Result of a counterfactual analysis."""
name: str
description: str
actual_pnl: float
counterfactual_pnl: float
opportunity_cost: float # CF - actual (positive = missed opportunity)
actual_sharpe: float
counterfactual_sharpe: float
actual_max_dd: float
counterfactual_max_dd: float
actual_equity_curve: np.ndarray
counterfactual_equity_curve: np.ndarray
insight: str # key takeaway
confidence: float # 0-1 how reliable is this counterfactual
# ── Counterfactual Engine ─────────────────────────────────────────────────────
class CounterfactualEngine:
"""Engine for counterfactual trade analysis."""
def __init__(self, seed: int = 42):
self.rng = np.random.default_rng(seed)
def _equity_curve(self, returns: np.ndarray) -> np.ndarray:
return np.cumprod(1 + returns)
def _sharpe(self, returns: np.ndarray) -> float:
if len(returns) < 5 or returns.std() < 1e-10:
return 0.0
return float(returns.mean() / returns.std() * math.sqrt(252))
def _max_dd(self, returns: np.ndarray) -> float:
eq = self._equity_curve(returns)
peak = np.maximum.accumulate(eq)
dd = (peak - eq) / (peak + 1e-10)
return float(dd.max())
# ── Trade Timing Counterfactual ───────────────────────────────────────
def timing_counterfactual(
self,
signal: np.ndarray,
returns: np.ndarray,
actual_entry_idx: int,
actual_exit_idx: int,
offsets: list[int] = None,
) -> list[CounterfactualResult]:
"""What if we entered/exited at different times?"""
if offsets is None:
offsets = [-5, -3, -1, 1, 3, 5]
n = len(returns)
actual_ret = returns[actual_entry_idx:actual_exit_idx]
actual_pnl = float(np.sum(actual_ret))
actual_sharpe = self._sharpe(actual_ret)
actual_dd = self._max_dd(actual_ret)
actual_eq = self._equity_curve(actual_ret)
results = []
for offset in offsets:
cf_entry = max(0, actual_entry_idx + offset)
cf_exit = min(n, actual_exit_idx + offset)
if cf_entry >= cf_exit:
continue
cf_ret = returns[cf_entry:cf_exit]
cf_pnl = float(np.sum(cf_ret))
cf_sharpe = self._sharpe(cf_ret)
cf_dd = self._max_dd(cf_ret)
cf_eq = self._equity_curve(cf_ret)
insight = (
f"{'Earlier' if offset < 0 else 'Later'} by {abs(offset)} days: "
f"PnL {'better' if cf_pnl > actual_pnl else 'worse'} by "
f"{abs(cf_pnl - actual_pnl)*100:.1f}%"
)
results.append(CounterfactualResult(
name=f"timing_offset_{offset:+d}",
description=f"Entry/exit shifted by {offset} days",
actual_pnl=actual_pnl,
counterfactual_pnl=cf_pnl,
opportunity_cost=cf_pnl - actual_pnl,
actual_sharpe=actual_sharpe,
counterfactual_sharpe=cf_sharpe,
actual_max_dd=actual_dd,
counterfactual_max_dd=cf_dd,
actual_equity_curve=actual_eq,
counterfactual_equity_curve=cf_eq,
insight=insight,
confidence=0.9,
))
return results
# ── Parameter Sensitivity Counterfactual ──────────────────────────────
def parameter_counterfactual(
self,
signal_generator: Callable[[dict], np.ndarray],
base_params: dict,
param_name: str,
param_values: list,
returns: np.ndarray,
) -> list[CounterfactualResult]:
"""What if we used different parameter values?"""
# Actual with base params
actual_signal = signal_generator(base_params)
actual_strat_ret = actual_signal * returns
actual_pnl = float(np.sum(actual_strat_ret))
actual_sharpe = self._sharpe(actual_strat_ret)
actual_dd = self._max_dd(actual_strat_ret)
actual_eq = self._equity_curve(actual_strat_ret)
results = []
for val in param_values:
cf_params = {**base_params, param_name: val}
cf_signal = signal_generator(cf_params)
cf_ret = cf_signal * returns
cf_pnl = float(np.sum(cf_ret))
results.append(CounterfactualResult(
name=f"{param_name}={val}",
description=f"What if {param_name} was {val} instead of {base_params.get(param_name)}?",
actual_pnl=actual_pnl,
counterfactual_pnl=cf_pnl,
opportunity_cost=cf_pnl - actual_pnl,
actual_sharpe=actual_sharpe,
counterfactual_sharpe=self._sharpe(cf_ret),
actual_max_dd=actual_dd,
counterfactual_max_dd=self._max_dd(cf_ret),
actual_equity_curve=actual_eq,
counterfactual_equity_curve=self._equity_curve(cf_ret),
insight=f"{param_name}={val}: Sharpe {self._sharpe(cf_ret):.2f} vs actual {actual_sharpe:.2f}",
confidence=0.85,
))
return results
# ── Allocation Counterfactual ─────────────────────────────────────────
def allocation_counterfactual(
self,
actual_weights: np.ndarray,
alternative_weights: list[tuple[str, np.ndarray]],
asset_returns: np.ndarray, # (T, N)
) -> list[CounterfactualResult]:
"""What if allocation had been different?"""
actual_ret = asset_returns @ actual_weights
actual_pnl = float(np.sum(actual_ret))
actual_sharpe = self._sharpe(actual_ret)
actual_dd = self._max_dd(actual_ret)
actual_eq = self._equity_curve(actual_ret)
results = []
for name, alt_w in alternative_weights:
cf_ret = asset_returns @ alt_w
cf_pnl = float(np.sum(cf_ret))
cf_sharpe = self._sharpe(cf_ret)
insight = f"{name}: Sharpe {cf_sharpe:.2f} vs actual {actual_sharpe:.2f}"
if cf_sharpe > actual_sharpe:
insight += " — BETTER risk-adjusted return"
else:
insight += " — worse risk-adjusted return"
results.append(CounterfactualResult(
name=f"alloc_{name}",
description=f"Counterfactual allocation: {name}",
actual_pnl=actual_pnl,
counterfactual_pnl=cf_pnl,
opportunity_cost=cf_pnl - actual_pnl,
actual_sharpe=actual_sharpe,
counterfactual_sharpe=cf_sharpe,
actual_max_dd=actual_dd,
counterfactual_max_dd=self._max_dd(cf_ret),
actual_equity_curve=actual_eq,
counterfactual_equity_curve=self._equity_curve(cf_ret),
insight=insight,
confidence=0.95,
))
return results
# ── Regime Counterfactual ─────────────────────────────────────────────
def regime_counterfactual(
self,
returns: np.ndarray,
regime_labels: np.ndarray,
strategy_returns: np.ndarray,
hypothetical_regime: str,
regime_return_distributions: dict[str, tuple[float, float]], # regime -> (mean, std)
) -> CounterfactualResult:
"""What if the regime had been different?"""
actual_ret = strategy_returns.copy()
actual_pnl = float(np.sum(actual_ret))
actual_sharpe = self._sharpe(actual_ret)
# Generate counterfactual returns from hypothetical regime
mu, sigma = regime_return_distributions.get(hypothetical_regime, (0.0, 0.01))
n = len(returns)
cf_market = self.rng.normal(mu / 252, sigma / math.sqrt(252), n)
# Counterfactual strategy: same signal applied to different market
signal_ratio = strategy_returns / (returns + 1e-10)
signal_ratio = np.clip(signal_ratio, -5, 5)
cf_strat = signal_ratio * cf_market
cf_pnl = float(np.sum(cf_strat))
cf_sharpe = self._sharpe(cf_strat)
return CounterfactualResult(
name=f"regime_{hypothetical_regime}",
description=f"What if regime was {hypothetical_regime}?",
actual_pnl=actual_pnl,
counterfactual_pnl=cf_pnl,
opportunity_cost=cf_pnl - actual_pnl,
actual_sharpe=actual_sharpe,
counterfactual_sharpe=cf_sharpe,
actual_max_dd=self._max_dd(actual_ret),
counterfactual_max_dd=self._max_dd(cf_strat),
actual_equity_curve=self._equity_curve(actual_ret),
counterfactual_equity_curve=self._equity_curve(cf_strat),
insight=f"In {hypothetical_regime} regime: Sharpe would be {cf_sharpe:.2f}",
confidence=0.5, # lower confidence — hypothetical
)
# ── Event Removal ─────────────────────────────────────────────────────
def event_removal_counterfactual(
self,
returns: np.ndarray,
event_dates: list[int], # indices of event days
window: int = 3, # days around event to replace
) -> CounterfactualResult:
"""What if specific events hadn't happened?"""
cf_returns = returns.copy()
# Replace event windows with average non-event returns
event_mask = np.zeros(len(returns), dtype=bool)
for idx in event_dates:
for d in range(max(0, idx - window), min(len(returns), idx + window + 1)):
event_mask[d] = True
non_event_mean = float(returns[~event_mask].mean())
non_event_std = float(returns[~event_mask].std())
cf_returns[event_mask] = self.rng.normal(non_event_mean, non_event_std, int(event_mask.sum()))
actual_pnl = float(np.sum(returns))
cf_pnl = float(np.sum(cf_returns))
event_pnl = float(np.sum(returns[event_mask]))
return CounterfactualResult(
name="event_removal",
description=f"Removed {len(event_dates)} events ({int(event_mask.sum())} days)",
actual_pnl=actual_pnl,
counterfactual_pnl=cf_pnl,
opportunity_cost=cf_pnl - actual_pnl,
actual_sharpe=self._sharpe(returns),
counterfactual_sharpe=self._sharpe(cf_returns),
actual_max_dd=self._max_dd(returns),
counterfactual_max_dd=self._max_dd(cf_returns),
actual_equity_curve=self._equity_curve(returns),
counterfactual_equity_curve=self._equity_curve(cf_returns),
insight=f"Events contributed {event_pnl*100:.1f}% to total PnL ({event_pnl/max(abs(actual_pnl), 1e-10)*100:.0f}% of total)",
confidence=0.7,
)
# ── Regret Analysis ───────────────────────────────────────────────────
def regret_analysis(
self,
decisions: list[dict], # each: {'name', 'actual_return', 'best_alternative_return'}
) -> dict:
"""Compute regret metrics across a series of decisions."""
if not decisions:
return {"total_regret": 0.0}
regrets = []
for d in decisions:
actual = d.get("actual_return", 0)
best = d.get("best_alternative_return", 0)
regret = max(best - actual, 0)
regrets.append({
"name": d.get("name", ""),
"actual": actual,
"best_alternative": best,
"regret": regret,
})
regret_values = [r["regret"] for r in regrets]
total_regret = float(sum(regret_values))
avg_regret = float(np.mean(regret_values))
# Hindsight optimal
optimal_total = float(sum(d.get("best_alternative_return", 0) for d in decisions))
actual_total = float(sum(d.get("actual_return", 0) for d in decisions))
# Worst decisions
regrets.sort(key=lambda x: x["regret"], reverse=True)
return {
"total_regret": total_regret,
"avg_regret_per_decision": avg_regret,
"actual_total_return": actual_total,
"hindsight_optimal_return": optimal_total,
"regret_as_pct_of_optimal": float(total_regret / max(abs(optimal_total), 1e-10) * 100),
"worst_decisions": regrets[:5],
"n_decisions": len(decisions),
"n_regretful": int(sum(1 for r in regret_values if r > 0)),
}
# ── Synthetic Counterfactual (Potential Outcomes) ─────────────────────
def synthetic_control_counterfactual(
self,
treated_returns: np.ndarray, # returns of treated (actual) unit
control_returns: np.ndarray, # (T, N) returns of control pool
treatment_start: int, # when treatment (trade) began
) -> CounterfactualResult:
"""
Abadie-style synthetic control: what would treated unit have done
without the treatment (trade/strategy)?
"""
T, N = control_returns.shape
pre_treatment = slice(0, treatment_start)
# Fit weights on pre-treatment period
y_pre = treated_returns[pre_treatment]
X_pre = control_returns[pre_treatment]
# Constrained regression: weights sum to 1, non-negative
w = np.ones(N) / N
for _ in range(200):
resid = y_pre - X_pre @ w
grad = -2 * X_pre.T @ resid / len(y_pre)
w -= 0.01 * grad
w = np.maximum(w, 0)
w /= w.sum() + 1e-10
# Counterfactual: synthetic control post-treatment
synthetic_full = control_returns @ w
actual_post = treated_returns[treatment_start:]
cf_post = synthetic_full[treatment_start:]
# Treatment effect
effect = actual_post - cf_post
cum_effect = float(np.sum(effect))
return CounterfactualResult(
name="synthetic_control",
description=f"Synthetic control counterfactual from {N} control units",
actual_pnl=float(np.sum(actual_post)),
counterfactual_pnl=float(np.sum(cf_post)),
opportunity_cost=float(-cum_effect), # negative = actual was better
actual_sharpe=self._sharpe(actual_post),
counterfactual_sharpe=self._sharpe(cf_post),
actual_max_dd=self._max_dd(actual_post),
counterfactual_max_dd=self._max_dd(cf_post),
actual_equity_curve=self._equity_curve(treated_returns),
counterfactual_equity_curve=self._equity_curve(synthetic_full),
insight=f"Treatment effect: {cum_effect*100:.2f}% cumulative",
confidence=0.6,
)
# ── Learning from Counterfactuals ─────────────────────────────────────
def distill_insights(self, results: list[CounterfactualResult]) -> dict:
"""Extract actionable insights from counterfactual analyses."""
if not results:
return {"n_analyses": 0}
# Best and worst counterfactuals
by_opp_cost = sorted(results, key=lambda r: r.opportunity_cost, reverse=True)
# Patterns
timing_better = sum(1 for r in results if "timing" in r.name and r.opportunity_cost > 0)
timing_total = sum(1 for r in results if "timing" in r.name)
alloc_better = sum(1 for r in results if "alloc" in r.name and r.opportunity_cost > 0)
alloc_total = sum(1 for r in results if "alloc" in r.name)
avg_opp_cost = float(np.mean([r.opportunity_cost for r in results]))
rules = []
if timing_total > 0 and timing_better / timing_total > 0.6:
rules.append("Timing decisions frequently suboptimal — consider systematic entry/exit rules")
if alloc_total > 0 and alloc_better / alloc_total > 0.6:
rules.append("Allocation decisions frequently suboptimal — consider model-based allocation")
for r in by_opp_cost[:3]:
if r.opportunity_cost > 0.01:
rules.append(f"[{r.name}] {r.insight}")
return {
"n_analyses": len(results),
"avg_opportunity_cost": avg_opp_cost,
"total_opportunity_cost": float(sum(r.opportunity_cost for r in results)),
"best_counterfactual": by_opp_cost[0].name if by_opp_cost else None,
"best_opportunity_cost": float(by_opp_cost[0].opportunity_cost) if by_opp_cost else 0.0,
"decision_rules": rules,
"confidence_weighted_insight": float(
sum(r.opportunity_cost * r.confidence for r in results) /
max(sum(r.confidence for r in results), 1e-10)
),
}