File size: 74,606 Bytes
a1d8ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe5fe45
 
a1d8ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
"""
⚡ ULTRON Systems // Defensible Tier-1 Empirical & Mathematical Verification Engine
100% Reproducible, Zero-Hallucination Empirical Pipeline for NVIDIA A100-SXM4-80GB.

Resolves all methodological and mathematical peer-review requirements:
1. Stratified, Ticker-Contiguous Cohort Ingestion across all 15 liquid US tickers:
   - Pre-2024 Historical Stress Regimes: 2020-03 (COVID-19 Crash), 2022-06 (Fed Tightening Shock), 2023-12 (Pre-Train Anchor)
   - OOS 2024 (2024-01, 2024-06, 2024-12)
   - OOS 2025 (2025-01, 2025-06, 2025-12)
   - Strictly Forward OOS 2026 (2026-01, 2026-02, 2026-03)
2. Complete 1-Minute & Multi-Horizon Base-Rate / Flat-Bar Decomposition:
   - Uncalibrated Acc (z > 0) + Positive Prediction Rate P(z > 0)
   - Strict Non-Zero Directional Acc (y != 0)
   - Causal Prior-Calibrated Acc (z_t > rolling_median(z)) on all bars and y != 0 bars
   - All-Bar ROC-AUC and Strict Non-Zero ROC-AUC (y > 0 vs y < 0)
   - Accuracy & ROC-AUC stratified by |return| magnitude buckets (Flat y=0, Q1, Q2, Q3, Q4)
   - Input Flat-Bar Diagnostics: P(ln(H/L)==0), P(y<=0 | ln(H/L)==0), and Flat-Bar Heuristic Baseline
   - Both Regression-Head Rank IC and Logit Rank IC + Non-Overlapping Horizon Rank IC & Effective Sample Size N_eff
3. Exact Concatenated Global Cosine Similarity & Adam Plasticity Arithmetic Audit:
   - Exact global inner-product cosine similarity strictly satisfying cos >= sqrt(1 - delta_F^2)
   - Per-parameter RMS weight norm ||W||_RMS vs per-parameter RMS update ||dW||_RMS
   - Update-to-update cosine similarity cos(dW_1, dW_2) across epochs and walk-forward folds
4. Pre-2024 Catastrophic Forgetting Audit & Paired Block-Bootstrap 95% CIs on 2026:
   - Evaluates Epoch 1, Epoch 2, Epoch 3 (Frozen), WF-2024, WF-2025 on 2020-03, 2022-06, 2023-12, 2024, 2025, 2026
   - Paired block-bootstrap 95% confidence intervals (B=1,000) for WF-2024 and WF-2025 vs Frozen Epoch 3
5. Non-Overlapping Contiguous Per-Ticker Backtest with Frozen 2024 Policy, State-Dependent Turnover,
   Compounded Drawdowns, Realistic Execution Cost Model, and Deflated Sharpe Ratio (DSR):
   - Non-overlapping holding steps S_h = max(1, ceil(horizon_mins / 32)) within each contiguous (shard, ticker) series
   - True state-dependent turnover |w_{i,k} - w_{i,k-1}| AND isolated round-trip accounting
   - Compounded equity curve with bounded percentage MaxDD in [0%, 100%]
   - Bailey & Lopez de Prado (2014) Deflated Sharpe Ratio (DSR) across all M=35 (7 horizons x 5 hurdles) configs
   - Single frozen configuration selected strictly on 2024 and applied untouched to 2025 and 2026
6. Mechanistic Tokenizer Ablation with In-Distribution Mean-Ablation, Random-341-Channel Controls,
   Centered Logit L2 Perturbation, and Paired Block-Bootstrap 95% CIs.
"""
import os
import sys
import gc
import json
import time
import math
import urllib.request
import numpy as np
import pandas as pd
import pyarrow.parquet as pq
import scipy.stats as stats
import torch
import torch.nn as nn
import torch.nn.functional as F
from huggingface_hub import hf_hub_download, HfApi

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from config import DatasetConfig
from config_1b import Model1BConfig, Training1BConfig
from features import compute_stationary_features
from model_1b import FinancialConformer1B

HORIZON_NAMES = ["1m", "3m", "5m", "15m", "30m", "60m", "1d"]
HORIZON_MINUTES = [1, 3, 5, 15, 30, 60, 390]
WINDOW_STRIDE = 32  # Exact stride from Training1BConfig

# Non-overlapping window step stride S_h = max(1, ceil(tau_h / 32))
NON_OVERLAP_STEPS = [max(1, int(math.ceil(m / float(WINDOW_STRIDE)))) for m in HORIZON_MINUTES]
# [1, 1, 1, 1, 1, 2, 13] -> 32m, 32m, 32m, 32m, 32m, 64m (>=60m), 416m (>=390m)

torch.manual_seed(42)
np.random.seed(42)


def fast_roc_auc(y_true_bin: np.ndarray, y_score: np.ndarray) -> float:
    """Deterministic Mann-Whitney U statistic based ROC-AUC calculation."""
    pos = y_score[y_true_bin == 1]
    neg = y_score[y_true_bin == 0]
    if len(pos) == 0 or len(neg) == 0:
        return 0.5
    ranks = stats.rankdata(np.concatenate([pos, neg]))
    pos_ranks = ranks[:len(pos)]
    auc = (np.sum(pos_ranks) - len(pos) * (len(pos) + 1) / 2.0) / (float(len(pos)) * float(len(neg)))
    return float(auc)


def causal_rolling_median_and_zscore(arr: np.ndarray, warmup: int = 256) -> tuple:
    """
    Computes strictly causal expanding/rolling mean and std over past `warmup` samples
    to calibrate logits without any look-ahead bias.
    Returns (calibrated_centered_signal, standardized_z_signal).
    """
    n = len(arr)
    x = arr.astype(np.float64)
    csum = np.cumsum(x)
    csum2 = np.cumsum(x ** 2)

    mean_prev = np.zeros(n, dtype=np.float64)
    std_prev = np.ones(n, dtype=np.float64)

    # For indices 1..warmup-1 use expanding window
    if n > 1:
        idx_exp = np.arange(1, min(warmup, n))
        mean_prev[idx_exp] = csum[idx_exp - 1] / idx_exp.astype(np.float64)
        var_exp = np.maximum(1e-12, (csum2[idx_exp - 1] / idx_exp.astype(np.float64)) - mean_prev[idx_exp] ** 2)
        std_prev[idx_exp] = np.sqrt(var_exp)

    # For indices >= warmup use rolling window of length `warmup`
    if n > warmup:
        idx = np.arange(warmup, n)
        cnt = float(warmup)
        sub_sum = csum[idx - 1] - np.where(idx - 1 - warmup >= 0, csum[idx - 1 - warmup], 0.0)
        sub_sum2 = csum2[idx - 1] - np.where(idx - 1 - warmup >= 0, csum2[idx - 1 - warmup], 0.0)
        mean_prev[idx] = sub_sum / cnt
        var_roll = np.maximum(1e-12, (sub_sum2 / cnt) - mean_prev[idx] ** 2)
        std_prev[idx] = np.sqrt(var_roll)

    centered = x - mean_prev
    zscore = centered / std_prev
    return centered, zscore


def load_stratified_contiguous_shard(
    rel_path: str,
    cfg: DatasetConfig,
    shard_id: int,
    max_windows_per_ticker: int = 180
) -> dict:
    """
    Downloads a single monthly Parquet shard, extracts contiguous windows per ticker
    across all 15 liquid tickers, and preserves exact (shard_id, ticker_id, contig_step)
    metadata for non-overlapping horizon evaluation and contiguous per-ticker backtesting.
    """
    os.makedirs(cfg.cache_dir, exist_ok=True)
    filename = os.path.basename(rel_path)
    local_path = os.path.join(cfg.cache_dir, filename)

    print(f"⚡ [ULTRON] Fetching shard: {filename} ...")
    downloaded_file = None
    try:
        downloaded_file = hf_hub_download(
            repo_id=cfg.repo_id,
            repo_type="dataset",
            filename=rel_path,
            local_dir=cfg.cache_dir,
            token=cfg.hf_token,
            force_download=False
        )
    except Exception as e:
        print(f"⚠️ HF download fallback for {rel_path}: {e}")
        url = f"https://huggingface.co/datasets/{cfg.repo_id}/resolve/main/{rel_path}"
        urllib.request.urlretrieve(url, local_path)
        downloaded_file = local_path

    table = pq.read_table(
        downloaded_file,
        columns=["timestamp", "open", "high", "low", "close", "volume", "ticker"]
    )
    df = table.to_pandas()
    del table
    try:
        if os.path.exists(downloaded_file):
            os.remove(downloaded_file)
    except Exception:
        pass

    tickers = sorted(cfg.liquid_tickers)
    df = df[df["ticker"].isin(tickers)]

    windows_list = []
    targets_list = []
    ticker_ids_list = []
    shard_ids_list = []
    contig_steps_list = []

    for t_idx, ticker in enumerate(tickers):
        t_df = df[df["ticker"] == ticker].sort_values("timestamp")
        if len(t_df) < cfg.seq_len + 400:
            continue
        feats, targets = compute_stationary_features(t_df, cfg.pred_horizons)
        n_rows = len(feats)
        if n_rows < cfg.seq_len:
            continue

        t_wins = []
        t_tgts = []
        step_counter = 0
        for i in range(0, n_rows - cfg.seq_len + 1, cfg.window_stride):
            w_f = feats[i : i + cfg.seq_len]
            w_t = targets[i + cfg.seq_len - 1]
            if not np.isnan(w_t).any() and not np.isnan(w_f).any():
                t_wins.append(w_f)
                t_tgts.append(w_t)
                ticker_ids_list.append(t_idx)
                shard_ids_list.append(shard_id)
                contig_steps_list.append(step_counter)
                step_counter += 1
                if step_counter >= max_windows_per_ticker:
                    break

        if t_wins:
            windows_list.extend(t_wins)
            targets_list.extend(t_tgts)

    del df
    gc.collect()

    w_arr = np.array(windows_list, dtype=np.float32)
    t_arr = np.array(targets_list, dtype=np.float32)
    tk_arr = np.array(ticker_ids_list, dtype=np.int32)
    sh_arr = np.array(shard_ids_list, dtype=np.int32)
    st_arr = np.array(contig_steps_list, dtype=np.int32)

    print(f"  ✅ Shard {filename}: {len(w_arr)} stratified contiguous windows across {len(np.unique(tk_arr))} tickers.")
    return {
        "windows": w_arr,
        "targets": t_arr,
        "ticker_ids": tk_arr,
        "shard_ids": sh_arr,
        "contig_steps": st_arr,
        "last_hl_spread": w_arr[:, -1, 2].copy(),  # Feature 2 in features.py is hl_spread = log(H/L)
        "last_log_ret": w_arr[:, -1, 0].copy()     # Feature 0 in features.py is log_ret = log(C_t/C_{t-1})
    }


def concat_cohort_dicts(shard_dicts: list) -> dict:
    """Concatenates multiple monthly shard dicts into a unified cohort dict."""
    out = {}
    for k in ["windows", "targets", "ticker_ids", "shard_ids", "contig_steps", "last_hl_spread", "last_log_ret"]:
        out[k] = np.concatenate([d[k] for d in shard_dicts], axis=0)
    return out


def compute_defensible_horizon_metrics(
    preds: np.ndarray,
    logits: np.ndarray,
    cohort: dict
) -> dict:
    """
    Computes complete, peer-review-proof metrics across all 7 horizons:
    - Exact class breakdown: pos_pct (y>0), neg_pct (y<0), flat_pct (y==0)
    - Input flat-bar diagnostics: input_flat_hl_pct (ln(H/L)==0), P(y<=0 | ln(H/L)==0),
      and theoretical flat-bar heuristic ceiling (50% + 0.5 * flat_pct)
    - Uncalibrated accuracy (z > 0) + positive prediction rate P(z > 0)
    - Strict non-zero accuracy (z > 0 on y != 0)
    - Prior-calibrated (causal demeaned z_t - mean_{t-256:t-1}(z) > 0) accuracy on all bars & non-zero bars
    - Balanced accuracy (uncalibrated & prior-calibrated)
    - All-bar ROC-AUC and strict Non-Zero ROC-AUC (y > 0 vs y < 0)
    - Return-magnitude |y| bucketed metrics: Flat (y==0), Q1 (0..25%), Q2 (25..50%), Q3 (50..75%), Q4 (75..100%)
    - Both Regression Rank IC (spearman(y_hat, y)) and Logit Rank IC (spearman(z, y))
    - Non-overlapping horizon subsample metrics (stepping by S_h = ceil(tau_h / 32)) with N_eff
    """
    targets = cohort["targets"]
    contig_steps = cohort["contig_steps"]
    last_hl = cohort["last_hl_spread"]

    num_h = targets.shape[1]
    per_h = {}

    raw_accs, nz_accs, cal_accs, cal_nz_accs = [], [], [], []
    bal_accs, cal_bal_accs, aucs, nz_aucs = [], [], [], []
    reg_ics, logit_ics, non_overlap_ics = [], [], []

    for h in range(num_h):
        yt = targets[:, h]
        yp = preds[:, h]
        yl = logits[:, h]
        hl = last_hl
        c_step = contig_steps

        valid = ~(np.isnan(yt) | np.isnan(yp) | np.isnan(yl) | np.isinf(yt) | np.isinf(yp) | np.isinf(yl))
        yt, yp, yl, hl, c_step = yt[valid], yp[valid], yl[valid], hl[valid], c_step[valid]
        n_total = len(yt)
        if n_total < 20:
            continue

        pos_mask = yt > 0.0
        neg_mask = yt < 0.0
        flat_mask = yt == 0.0
        nz_mask = ~flat_mask

        pos_pct = float(np.mean(pos_mask) * 100.0)
        neg_pct = float(np.mean(neg_mask) * 100.0)
        flat_pct = float(np.mean(flat_mask) * 100.0)

        # Input flat-bar diagnostics (ln(H/L) <= 1e-7)
        in_flat_mask = np.abs(hl) <= 1e-7
        in_flat_pct = float(np.mean(in_flat_mask) * 100.0)
        if np.sum(in_flat_mask) > 0:
            cond_down_given_in_flat = float(np.mean(~pos_mask[in_flat_mask]) * 100.0)
            cond_target_flat_given_in_flat = float(np.mean(flat_mask[in_flat_mask]) * 100.0)
        else:
            cond_down_given_in_flat = 50.0
            cond_target_flat_given_in_flat = 0.0

        # 1. Uncalibrated directional predictions (yl > 0)
        pred_pos_raw = yl > 0.0
        pred_pos_rate_pct = float(np.mean(pred_pos_raw) * 100.0)
        raw_acc = float(np.mean(pred_pos_raw == pos_mask) * 100.0)
        raw_accs.append(raw_acc)

        # Strict non-zero directional accuracy (uncalibrated yl > 0 on yt != 0)
        if np.sum(nz_mask) > 10:
            nz_acc = float(np.mean(pred_pos_raw[nz_mask] == pos_mask[nz_mask]) * 100.0)
        else:
            nz_acc = raw_acc
        nz_accs.append(nz_acc)

        # 2. Causal prior-calibrated directional predictions (yl_centered > 0)
        yl_centered, _ = causal_rolling_median_and_zscore(yl, warmup=256)
        pred_pos_cal = yl_centered > 0.0
        cal_pos_rate_pct = float(np.mean(pred_pos_cal) * 100.0)
        cal_acc = float(np.mean(pred_pos_cal == pos_mask) * 100.0)
        cal_accs.append(cal_acc)

        if np.sum(nz_mask) > 10:
            cal_nz_acc = float(np.mean(pred_pos_cal[nz_mask] == pos_mask[nz_mask]) * 100.0)
        else:
            cal_nz_acc = cal_acc
        cal_nz_accs.append(cal_nz_acc)

        # 3. Balanced Accuracy (Uncalibrated & Calibrated)
        tp = float(np.sum(pred_pos_raw & pos_mask))
        tn = float(np.sum((~pred_pos_raw) & (~pos_mask)))
        fp = float(np.sum(pred_pos_raw & (~pos_mask)))
        fn = float(np.sum((~pred_pos_raw) & pos_mask))
        bal_acc = float(0.5 * (tp / max(1.0, tp + fn) + tn / max(1.0, tn + fp)) * 100.0)
        bal_accs.append(bal_acc)

        tp_c = float(np.sum(pred_pos_cal & pos_mask))
        tn_c = float(np.sum((~pred_pos_cal) & (~pos_mask)))
        fp_c = float(np.sum(pred_pos_cal & (~pos_mask)))
        fn_c = float(np.sum((~pred_pos_cal) & pos_mask))
        cal_bal_acc = float(0.5 * (tp_c / max(1.0, tp_c + fn_c) + tn_c / max(1.0, tn_c + fp_c)) * 100.0)
        cal_bal_accs.append(cal_bal_acc)

        # 4. All-Bar ROC-AUC and Strict Non-Zero ROC-AUC
        auc_all = fast_roc_auc(pos_mask.astype(np.int32), yl)
        aucs.append(auc_all)
        if np.sum(nz_mask) > 20:
            auc_nz = fast_roc_auc(pos_mask[nz_mask].astype(np.int32), yl[nz_mask])
        else:
            auc_nz = auc_all
        nz_aucs.append(auc_nz)

        # 5. Spearman Rank IC (Regression Head yp vs yt, Logit Head yl vs yt, and Non-Overlapping Subsample)
        if np.std(yp) > 1e-12 and np.std(yt) > 1e-12:
            sp_reg, sp_reg_p = stats.spearmanr(yp, yt)
            sp_reg = 0.0 if np.isnan(sp_reg) else float(sp_reg)
            sp_reg_p = 1.0 if np.isnan(sp_reg_p) else float(sp_reg_p)
        else:
            sp_reg, sp_reg_p = 0.0, 1.0
        reg_ics.append(sp_reg)

        if np.std(yl) > 1e-12 and np.std(yt) > 1e-12:
            sp_log, _ = stats.spearmanr(yl, yt)
            sp_log = 0.0 if np.isnan(sp_log) else float(sp_log)
        else:
            sp_log = 0.0
        logit_ics.append(sp_log)

        # Non-overlapping subsample: step S_h
        s_h = NON_OVERLAP_STEPS[h]
        no_mask = (c_step % s_h) == 0
        n_eff = int(np.sum(no_mask))
        if n_eff > 20 and np.std(yp[no_mask]) > 1e-12 and np.std(yt[no_mask]) > 1e-12:
            sp_no, sp_no_p = stats.spearmanr(yp[no_mask], yt[no_mask])
            sp_no = 0.0 if np.isnan(sp_no) else float(sp_no)
            sp_no_p = 1.0 if np.isnan(sp_no_p) else float(sp_no_p)
            auc_no = fast_roc_auc(pos_mask[no_mask].astype(np.int32), yl[no_mask])
        else:
            sp_no, sp_no_p, auc_no = sp_reg, sp_reg_p, auc_all
        non_overlap_ics.append(sp_no)

        # 6. Return-Magnitude |y| Buckets (Reviewer Finding 1 requirement)
        buckets = {}
        if np.sum(flat_mask) > 0:
            buckets["Flat_0bps"] = {
                "n_bars": int(np.sum(flat_mask)),
                "share_pct": round(flat_pct, 2),
                "mean_abs_bps": 0.0,
                "raw_dir_acc": round(float(np.mean(pred_pos_raw[flat_mask] == pos_mask[flat_mask]) * 100.0), 2),
                "calibrated_dir_acc": round(float(np.mean(pred_pos_cal[flat_mask] == pos_mask[flat_mask]) * 100.0), 2)
            }
        if np.sum(nz_mask) >= 40:
            abs_nz = np.abs(yt[nz_mask])
            q25, q50, q75 = np.percentile(abs_nz, [25, 50, 75])
            q_bounds = [
                ("Q1_Smallest_Move", (yt != 0.0) & (np.abs(yt) <= q25)),
                ("Q2_Moderate_Move", (np.abs(yt) > q25) & (np.abs(yt) <= q50)),
                ("Q3_Medium_Move", (np.abs(yt) > q50) & (np.abs(yt) <= q75)),
                ("Q4_Largest_Move", np.abs(yt) > q75),
            ]
            for b_name, b_mask in q_bounds:
                if np.sum(b_mask) > 5:
                    b_pos = pos_mask[b_mask]
                    buckets[b_name] = {
                        "n_bars": int(np.sum(b_mask)),
                        "share_pct": round(float(np.mean(b_mask) * 100.0), 2),
                        "mean_abs_bps": round(float(np.mean(np.abs(yt[b_mask])) * 10000.0), 2),
                        "pos_pct": round(float(np.mean(b_pos) * 100.0), 2),
                        "raw_dir_acc": round(float(np.mean(pred_pos_raw[b_mask] == b_pos) * 100.0), 2),
                        "calibrated_dir_acc": round(float(np.mean(pred_pos_cal[b_mask] == b_pos) * 100.0), 2),
                        "roc_auc": round(fast_roc_auc(b_pos.astype(np.int32), yl[b_mask]), 4)
                    }

        # 7. Huber & BCE Loss
        err = np.abs(yp - yt)
        delta = 0.01
        huber = float(np.mean(np.where(err <= delta, 0.5 * (err ** 2), delta * (err - 0.5 * delta))))
        y_bin = pos_mask.astype(np.float64)
        yl_64 = np.clip(yl.astype(np.float64), -30.0, 30.0)
        bce = float(np.mean(np.maximum(yl_64, 0) - yl_64 * y_bin + np.log1p(np.exp(-np.abs(yl_64)))))

        h_name = HORIZON_NAMES[h]
        per_h[h_name] = {
            "n_samples_total": int(n_total),
            "n_eff_non_overlapping": int(n_eff),
            "non_overlap_step_windows": int(s_h),
            "pos_pct": round(pos_pct, 2),
            "neg_pct": round(neg_pct, 2),
            "flat_pct": round(flat_pct, 2),
            "input_flat_hl_pct": round(in_flat_pct, 2),
            "cond_down_given_input_flat_pct": round(cond_down_given_in_flat, 2),
            "cond_target_flat_given_input_flat_pct": round(cond_target_flat_given_in_flat, 2),
            "flat_bar_heuristic_ceiling_acc": round(50.0 + 0.5 * flat_pct, 2),
            "mean_abs_move_bps": round(float(np.mean(np.abs(yt)) * 10000.0), 2),
            "pred_pos_rate_raw_pct": round(pred_pos_rate_pct, 2),
            "pred_pos_rate_calibrated_pct": round(cal_pos_rate_pct, 2),
            "raw_dir_acc": round(raw_acc, 2),
            "nonzero_dir_acc": round(nz_acc, 2),
            "prior_calibrated_acc": round(cal_acc, 2),
            "prior_calibrated_nonzero_acc": round(cal_nz_acc, 2),
            "balanced_acc": round(bal_acc, 2),
            "calibrated_balanced_acc": round(cal_bal_acc, 2),
            "roc_auc": round(auc_all, 4),
            "nonzero_roc_auc": round(auc_nz, 4),
            "non_overlapping_roc_auc": round(auc_no, 4),
            "spearman_ic": round(sp_reg, 4),
            "spearman_ic_logit": round(sp_log, 4),
            "non_overlapping_spearman_ic": round(sp_no, 4),
            "non_overlapping_ic_pval": float(f"{sp_no_p:.4e}"),
            "huber_loss": float(f"{huber:.6e}"),
            "bce_loss": round(bce, 4),
            "logit_mean": round(float(np.mean(yl)), 4),
            "logit_std": round(float(np.std(yl)), 4),
            "logit_rms": round(float(np.sqrt(np.mean(yl ** 2))), 4),
            "return_magnitude_buckets": buckets
        }

    return {
        "overall_raw_dir_acc": round(float(np.mean(raw_accs)), 2),
        "overall_nonzero_dir_acc": round(float(np.mean(nz_accs)), 2),
        "overall_prior_calibrated_acc": round(float(np.mean(cal_accs)), 2),
        "overall_prior_calibrated_nonzero_acc": round(float(np.mean(cal_nz_accs)), 2),
        "overall_balanced_acc": round(float(np.mean(bal_accs)), 2),
        "overall_calibrated_balanced_acc": round(float(np.mean(cal_bal_accs)), 2),
        "overall_roc_auc": round(float(np.mean(aucs)), 4),
        "overall_nonzero_roc_auc": round(float(np.mean(nz_aucs)), 4),
        "overall_mean_ic": round(float(np.mean(reg_ics)), 4),
        "overall_logit_ic": round(float(np.mean(logit_ics)), 4),
        "overall_non_overlapping_ic": round(float(np.mean(non_overlap_ics)), 4),
        "per_horizon": per_h
    }


def compute_paired_block_bootstrap_ci(
    preds_a: np.ndarray, logits_a: np.ndarray,
    preds_b: np.ndarray, logits_b: np.ndarray,
    cohort: dict,
    n_boot: int = 500,
    block_size: int = 64
) -> dict:
    """
    Computes paired block-bootstrap 95% confidence intervals (Model B minus Model A)
    across all 7 horizons for:
    - Delta All-Bar ROC-AUC
    - Delta Non-Zero ROC-AUC
    - Delta Prior-Calibrated Accuracy (pp)
    - Delta Spearman Rank IC
    Uses contiguous chronological blocks of size `block_size` to preserve serial correlation.
    """
    targets = cohort["targets"]
    n_samples, num_h = targets.shape
    n_blocks = max(1, n_samples // block_size)

    rng = np.random.default_rng(42)
    block_starts = np.arange(0, n_blocks * block_size, block_size)

    # Pre-calibrate logits for both models
    cal_a = np.zeros_like(logits_a, dtype=np.float64)
    cal_b = np.zeros_like(logits_b, dtype=np.float64)
    for h in range(num_h):
        cal_a[:, h], _ = causal_rolling_median_and_zscore(logits_a[:, h], warmup=256)
        cal_b[:, h], _ = causal_rolling_median_and_zscore(logits_b[:, h], warmup=256)

    out = {}
    for h in range(num_h):
        h_name = HORIZON_NAMES[h]
        yt = targets[:, h]
        pos = (yt > 0.0).astype(np.int32)
        nz = yt != 0.0

        # Point estimates
        auc_a = fast_roc_auc(pos, logits_a[:, h])
        auc_b = fast_roc_auc(pos, logits_b[:, h])
        d_auc_pt = auc_b - auc_a

        auc_nz_a = fast_roc_auc(pos[nz], logits_a[nz, h])
        auc_nz_b = fast_roc_auc(pos[nz], logits_b[nz, h])
        d_auc_nz_pt = auc_nz_b - auc_nz_a

        acc_cal_a = float(np.mean((cal_a[:, h] > 0.0) == (yt > 0.0)) * 100.0)
        acc_cal_b = float(np.mean((cal_b[:, h] > 0.0) == (yt > 0.0)) * 100.0)
        d_acc_cal_pt = acc_cal_b - acc_cal_a

        ic_a = float(stats.spearmanr(preds_a[:, h], yt)[0])
        ic_b = float(stats.spearmanr(preds_b[:, h], yt)[0])
        d_ic_pt = ic_b - ic_a

        boot_d_auc = np.zeros(n_boot, dtype=np.float64)
        boot_d_acc = np.zeros(n_boot, dtype=np.float64)
        boot_d_ic = np.zeros(n_boot, dtype=np.float64)

        corr_a = ((cal_a[:, h] > 0.0) == (yt > 0.0)).astype(np.float64) * 100.0
        corr_b = ((cal_b[:, h] > 0.0) == (yt > 0.0)).astype(np.float64) * 100.0
        diff_corr = corr_b - corr_a

        # Rank transform once for fast Spearman bootstrap approximation
        rk_yt = stats.rankdata(yt).astype(np.float64)
        rk_pa = stats.rankdata(preds_a[:, h]).astype(np.float64)
        rk_pb = stats.rankdata(preds_b[:, h]).astype(np.float64)
        rk_yt_z = (rk_yt - np.mean(rk_yt)) / (np.std(rk_yt) + 1e-12)
        rk_pa_z = (rk_pa - np.mean(rk_pa)) / (np.std(rk_pa) + 1e-12)
        rk_pb_z = (rk_pb - np.mean(rk_pb)) / (np.std(rk_pb) + 1e-12)
        ic_prod_diff = (rk_pb_z - rk_pa_z) * rk_yt_z

        for b_idx in range(n_boot):
            chosen = rng.choice(block_starts, size=n_blocks, replace=True)
            idx = (chosen[:, None] + np.arange(block_size)[None, :]).reshape(-1)
            boot_d_acc[b_idx] = np.mean(diff_corr[idx])
            boot_d_ic[b_idx] = np.mean(ic_prod_diff[idx])
            boot_d_auc[b_idx] = fast_roc_auc(pos[idx], logits_b[idx, h]) - fast_roc_auc(pos[idx], logits_a[idx, h])

        out[h_name] = {
            "delta_roc_auc": round(d_auc_pt, 4),
            "delta_roc_auc_ci95": [round(float(np.percentile(boot_d_auc, 2.5)), 4),
                                   round(float(np.percentile(boot_d_auc, 97.5)), 4)],
            "delta_roc_auc_se": round(float(np.std(boot_d_auc)), 4),
            "delta_nonzero_roc_auc": round(d_auc_nz_pt, 4),
            "delta_calibrated_acc_pp": round(d_acc_cal_pt, 2),
            "delta_calibrated_acc_ci95": [round(float(np.percentile(boot_d_acc, 2.5)), 2),
                                          round(float(np.percentile(boot_d_acc, 97.5)), 2)],
            "delta_calibrated_acc_se": round(float(np.std(boot_d_acc)), 2),
            "delta_spearman_ic": round(d_ic_pt, 4),
            "delta_spearman_ic_ci95": [round(float(np.percentile(boot_d_ic, 2.5)), 4),
                                       round(float(np.percentile(boot_d_ic, 97.5)), 4)],
            "delta_spearman_ic_se": round(float(np.std(boot_d_ic)), 4)
        }
    return out


def compute_weight_drift_and_adam_arithmetic(ckpt_paths: dict) -> dict:
    """
    Resolves Reviewer Finding 2 & Finding 3f:
    1. Computes the EXACT global concatenated inner-product cosine similarity:
       cos_exact(M) = sum_{j in M} <W_j^A, W_j^B> / (sqrt(sum ||W_j^A||_F^2) * sqrt(sum ||W_j^B||_F^2))
       which strictly obeys cos_exact >= sqrt(1 - delta_F^2).
    2. Reports theoretical lower bound sqrt(1 - delta_F^2) alongside unweighted tensor mean cosine.
    3. Computes per-parameter RMS weight norm ||W||_RMS and per-parameter RMS update ||dW||_RMS
       to explicitly decompose relative Frobenius drift into absolute update vs denominator norm scaling.
    4. Computes exact update-to-update cosine similarity cos(dW_{ep1->ep2}, dW_{ep2->ep3}) and
       cos(dW_{ep3->wf2024}, dW_{wf2024->wf2025}) in R^{1,189,135,740}.
    """
    print("\n🔬 [Weight Drift & Cosine Geometry Audit] Loading state dicts...")
    states = {}
    for k, path in ckpt_paths.items():
        if os.path.exists(path):
            raw = torch.load(path, map_location="cpu", weights_only=False)
            states[k] = raw.get("model_state", raw)
            print(f"  • Loaded '{k}' ({len(states[k])} tensors)")

    def _categorize(param_name: str) -> list:
        cats = []
        if param_name.startswith("tokenizer"):
            cats.append("inception_tokenizer")
        elif param_name.startswith("blocks."):
            cats.append("conformer_backbone_all")
            layer_idx = int(param_name.split(".")[1])
            if layer_idx <= 6:
                cats.append("conformer_early_L0_L6")
            elif layer_idx <= 13:
                cats.append("conformer_mid_L7_L13")
            else:
                cats.append("conformer_deep_L14_L19")
            if any(x in param_name for x in ["q_proj", "k_proj", "v_proj", "out_proj"]):
                cats.append("sub_gqa_attention")
            elif "conv_module" in param_name:
                cats.append("sub_depthwise_conv")
            elif "ffn" in param_name:
                cats.append("sub_swiglu_ffn")
        elif "return_head" in param_name:
            cats.append("return_regression_head")
        elif "dir_head" in param_name:
            cats.append("directional_class_head")
        else:
            cats.append("conformer_backbone_all")
        return cats

    pairs = [
        ("epoch1", "epoch2"),
        ("epoch2", "epoch3"),
        ("epoch1", "epoch3"),
        ("epoch3", "wf2024"),
        ("wf2024", "wf2025"),
        ("epoch3", "wf2025")
    ]

    drift_report = {}
    for a, b in pairs:
        if a not in states or b not in states:
            continue
        sa, sb = states[a], states[b]

        cat_diff_sq = {}
        cat_a_sq = {}
        cat_b_sq = {}
        cat_dot = {}
        cat_n_params = {}
        cat_tensor_cos = {}

        total_diff_sq = 0.0
        total_a_sq = 0.0
        total_b_sq = 0.0
        total_dot = 0.0
        total_n_params = 0
        all_tensor_cos = []

        for k in sa.keys():
            if k not in sb:
                continue
            ta = sa[k].float().view(-1)
            tb = sb[k].float().view(-1)
            numel = int(ta.numel())
            if numel == 0:
                continue

            diff = tb - ta
            diff_sq = float(torch.dot(diff, diff).item())
            a_sq = float(torch.dot(ta, ta).item())
            b_sq = float(torch.dot(tb, tb).item())
            dot_ab = float(torch.dot(ta, tb).item())
            t_cos = dot_ab / max(1e-12, math.sqrt(a_sq * b_sq))

            for cat in _categorize(k):
                cat_diff_sq[cat] = cat_diff_sq.get(cat, 0.0) + diff_sq
                cat_a_sq[cat] = cat_a_sq.get(cat, 0.0) + a_sq
                cat_b_sq[cat] = cat_b_sq.get(cat, 0.0) + b_sq
                cat_dot[cat] = cat_dot.get(cat, 0.0) + dot_ab
                cat_n_params[cat] = cat_n_params.get(cat, 0) + numel
                cat_tensor_cos.setdefault(cat, []).append(t_cos)

            total_diff_sq += diff_sq
            total_a_sq += a_sq
            total_b_sq += b_sq
            total_dot += dot_ab
            total_n_params += numel
            all_tensor_cos.append(t_cos)

        global_delta = math.sqrt(total_diff_sq / max(1e-12, total_a_sq))
        global_exact_cos = total_dot / max(1e-12, math.sqrt(total_a_sq * total_b_sq))
        global_cos_lb = math.sqrt(max(0.0, 1.0 - global_delta ** 2))
        global_rms_w = math.sqrt(total_a_sq / max(1, total_n_params))
        global_rms_dw = math.sqrt(total_diff_sq / max(1, total_n_params))

        pair_key = f"{a}_to_{b}"
        drift_report[pair_key] = {
            "n_parameters": int(total_n_params),
            "global_rel_frobenius_pct": round(global_delta * 100.0, 4),
            "global_exact_cosine_sim": round(global_exact_cos, 8),
            "global_theoretical_cos_lower_bound": round(global_cos_lb, 8),
            "cauchy_schwarz_satisfied": bool(global_exact_cos + 1e-7 >= global_cos_lb),
            "global_unweighted_tensor_mean_cos": round(float(np.mean(all_tensor_cos)), 6),
            "global_rms_weight_norm": round(global_rms_w, 6),
            "global_rms_update_magnitude": float(f"{global_rms_dw:.6e}"),
            "by_module": {}
        }

        for cat in sorted(cat_diff_sq.keys()):
            d_f = math.sqrt(cat_diff_sq[cat] / max(1e-12, cat_a_sq[cat]))
            ex_cos = cat_dot[cat] / max(1e-12, math.sqrt(cat_a_sq[cat] * cat_b_sq[cat]))
            cos_lb = math.sqrt(max(0.0, 1.0 - d_f ** 2))
            rms_w = math.sqrt(cat_a_sq[cat] / max(1, cat_n_params[cat]))
            rms_dw = math.sqrt(cat_diff_sq[cat] / max(1, cat_n_params[cat]))

            drift_report[pair_key]["by_module"][cat] = {
                "n_parameters": int(cat_n_params[cat]),
                "rel_frobenius_pct": round(d_f * 100.0, 4),
                "exact_cosine_sim": round(ex_cos, 8),
                "theoretical_cos_lower_bound": round(cos_lb, 8),
                "cauchy_schwarz_satisfied": bool(ex_cos + 1e-7 >= cos_lb),
                "unweighted_tensor_mean_cos": round(float(np.mean(cat_tensor_cos[cat])), 6),
                "rms_weight_norm": round(rms_w, 6),
                "rms_update_magnitude": float(f"{rms_dw:.6e}")
            }

        print(f"  ✅ Drift [{pair_key}]: delta_F={global_delta*100:.4f}% | "
              f"cos_exact={global_exact_cos:.8f} (>= {global_cos_lb:.8f})")

    # Compute exact update-vector cosine similarities cos(dW_1, dW_2)
    update_orthogonality = {}
    triplets = [
        ("pretrain_ep1_ep2_vs_ep2_ep3", "epoch1", "epoch2", "epoch3"),
        ("walkforward_ep3_wf24_vs_wf24_wf25", "epoch3", "wf2024", "wf2025")
    ]
    for label, c0, c1, c2 in triplets:
        if c0 in states and c1 in states and c2 in states:
            s0, s1, s2 = states[c0], states[c1], states[c2]
            dot_12 = 0.0
            norm1_sq = 0.0
            norm2_sq = 0.0
            for k in s0.keys():
                if k in s1 and k in s2:
                    dw1 = (s1[k].float() - s0[k].float()).view(-1)
                    dw2 = (s2[k].float() - s1[k].float()).view(-1)
                    dot_12 += float(torch.dot(dw1, dw2).item())
                    norm1_sq += float(torch.dot(dw1, dw1).item())
                    norm2_sq += float(torch.dot(dw2, dw2).item())
            cos_dw = dot_12 / max(1e-12, math.sqrt(norm1_sq * norm2_sq))
            norm_sum_sq = norm1_sq + norm2_sq + 2.0 * dot_12
            pythagorean_ratio = math.sqrt(norm_sum_sq / max(1e-12, norm1_sq + norm2_sq))
            update_orthogonality[label] = {
                "update_cosine_similarity": round(cos_dw, 6),
                "angle_degrees": round(math.degrees(math.acos(max(-1.0, min(1.0, cos_dw)))), 2),
                "norm_dw1": round(math.sqrt(norm1_sq), 4),
                "norm_dw2": round(math.sqrt(norm2_sq), 4),
                "norm_dw1_plus_dw2": round(math.sqrt(max(0.0, norm_sum_sq)), 4),
                "pythagorean_orthogonality_ratio": round(pythagorean_ratio, 4)
            }
            print(f"  📐 Update Orthogonality [{label}]: cos(dW1, dW2)={cos_dw:.6f} ({update_orthogonality[label]['angle_degrees']} deg)")

    drift_report["update_vector_orthogonality"] = update_orthogonality
    del states
    gc.collect()
    return drift_report


def compute_baselines_on_cohort(cohort: dict) -> dict:
    """
    Evaluates 5 deterministic quantitative baselines on the exact same cohort:
    1. Always_Long_Drift: predicts +1 always (bull market drift baseline)
    2. FlatBar_Down_Heuristic: predicts -1 when input ln(H/L)==0 (or |r_t|==0), testing Reviewer Finding 1
    3. Momentum_1Bar: predicts sign(r_t) continues
    4. MeanReversion_5Bar: predicts -sign(mean(r_{t-4:t}))
    5. EMA_12_26_Crossover: predicts sign(EMA_12(r) - EMA_26(r))
    """
    windows = cohort["windows"]
    targets = cohort["targets"]
    last_hl = cohort["last_hl_spread"]
    n_samples, num_h = targets.shape

    last_r = windows[:, -1, 0]
    last5_r = np.mean(windows[:, -5:, 0], axis=1)

    tail_r = windows[:, -64:, 0]
    alpha12, alpha26 = 2.0 / 13.0, 2.0 / 27.0
    w12 = np.array([(1.0 - alpha12) ** (63 - i) for i in range(64)], dtype=np.float32)
    w12 /= np.sum(w12)
    w26 = np.array([(1.0 - alpha26) ** (63 - i) for i in range(64)], dtype=np.float32)
    w26 /= np.sum(w26)
    macd_sig = (tail_r @ w12) - (tail_r @ w26)

    baselines = {}

    # 1. Always-Long Drift
    p_long = np.ones((n_samples, num_h), dtype=np.float32) * 0.001
    l_long = np.ones((n_samples, num_h), dtype=np.float32)
    baselines["Always_Long_Drift"] = compute_defensible_horizon_metrics(p_long, l_long, cohort)

    # 2. Flat-Bar Down Heuristic (Reviewer Finding 1 explicit test:
    # predicts down (-1) if current bar is flat ln(H/L)==0 or r_t==0, else predicts -hl_spread)
    is_flat_in = (np.abs(last_hl) <= 1e-7) | (np.abs(last_r) <= 1e-7)
    sig_flat = np.where(is_flat_in, -1.0, -last_hl).astype(np.float32)
    p_flat = np.repeat(sig_flat[:, None], num_h, axis=1)
    l_flat = np.where(p_flat > 0.0, 1.0, -1.0).astype(np.float32)
    baselines["FlatBar_Down_Heuristic"] = compute_defensible_horizon_metrics(p_flat, l_flat, cohort)

    # 3. 1-Bar Momentum
    sig_mom = np.repeat(last_r[:, None], num_h, axis=1)
    l_mom = np.where(sig_mom > 0.0, 1.0, -1.0).astype(np.float32)
    baselines["Momentum_1Bar"] = compute_defensible_horizon_metrics(sig_mom, l_mom, cohort)

    # 4. 5-Bar Mean Reversion
    sig_rev = np.repeat((-last5_r)[:, None], num_h, axis=1)
    l_rev = np.where(sig_rev > 0.0, 1.0, -1.0).astype(np.float32)
    baselines["MeanReversion_5Bar"] = compute_defensible_horizon_metrics(sig_rev, l_rev, cohort)

    # 5. EMA(12,26) Crossover
    sig_ema = np.repeat(macd_sig[:, None], num_h, axis=1)
    l_ema = np.where(sig_ema > 0.0, 1.0, -1.0).astype(np.float32)
    baselines["EMA_12_26_Crossover"] = compute_defensible_horizon_metrics(sig_ema, l_ema, cohort)

    return baselines


def compute_deflated_sharpe_ratio(
    sr_est: float,
    all_trial_srs: list,
    n_obs: int,
    skew: float,
    kurt: float
) -> float:
    """
    Computes Bailey & Lopez de Prado (2014) Deflated Sharpe Ratio (DSR)
    adjusting for non-normality (skewness, kurtosis) and multiple testing across M trials.
    All Sharpe ratios are converted to per-period (non-annualized) units for the PSR/DSR formula.
    """
    m_trials = max(2, len(all_trial_srs))
    sr_std = float(np.std(all_trial_srs, ddof=1)) if len(all_trial_srs) > 1 else 0.05
    euler_gamma = 0.5772156649
    z1 = stats.norm.ppf(1.0 - 1.0 / float(m_trials))
    z2 = stats.norm.ppf(1.0 - 1.0 / (float(m_trials) * math.e))
    sr0 = sr_std * ((1.0 - euler_gamma) * z1 + euler_gamma * z2)

    denom_var = 1.0 - skew * sr_est + ((kurt - 1.0) / 4.0) * (sr_est ** 2)
    denom = math.sqrt(max(1e-8, denom_var))
    z_dsr = ((sr_est - sr0) * math.sqrt(max(1.0, float(n_obs - 1)))) / denom
    return float(stats.norm.cdf(z_dsr))


def run_non_overlapping_contiguous_backtest(
    preds: np.ndarray,
    logits: np.ndarray,
    cohort: dict
) -> dict:
    """
    Resolves Reviewer Finding 4:
    - Uses strictly contiguous per-ticker trajectories within each (shard_id, ticker_id).
    - Steps by S_h = max(1, ceil(tau_h / 32)) so holding periods NEVER overlap (even at 60m and 1d!).
    - Tracks true state-dependent turnover |w_{i,k} - w_{i,k-1}| within each ticker series
      (plus initial entry |w_{i,0}| and terminal exit |w_{i,K-1}| at shard boundaries)
      AND reports isolated round-trip cost (2 * c_one_way per active trade) side by side.
    - Aggregates equal-weighted cross-sectional portfolio returns across the 15 tickers at each
      chronological decision step t, compounding equity E_t = E_{t-1} * (1 + R_{p,t}^{net})
      so MaxDD (%) is strictly bounded in [0%, 100%].
    - Evaluates realistic execution cost tiers:
      0.0 bps (gross), 1.5 bps one-way (3.0 bps RT maker/midpoint),
      3.0 bps one-way (6.0 bps RT institutional taker), 5.0 bps one-way (10.0 bps RT retail/impact).
    - Computes Bailey & Lopez de Prado (2014) Deflated Sharpe Ratio (DSR) across all 35 (7x5) configs.
    """
    targets = cohort["targets"]
    shard_ids = cohort["shard_ids"]
    ticker_ids = cohort["ticker_ids"]
    contig_steps = cohort["contig_steps"]

    hurdles = [0.0, 0.5, 1.0, 1.5, 2.0]
    one_way_cost_tiers = [0.0, 1.5, 3.0, 5.0]  # in bps one-way (0, 3, 6, 10 bps round-trip)
    mins_per_year = 252.0 * 390.0

    # First compute causal z-scores per (shard, ticker) or causally across the series
    # Since each (shard, ticker) has ~180 contiguous windows, we compute causal rolling z-score
    # using an expanding/rolling window across the chronologically ordered cohort
    raw_configs = {}
    all_per_step_srs_gross = []

    for h_idx, h_name in enumerate(HORIZON_NAMES):
        h_min = HORIZON_MINUTES[h_idx]
        s_h = NON_OVERLAP_STEPS[h_idx]
        # Effective step spacing in minutes: max(h_min, s_h * WINDOW_STRIDE)
        step_mins = float(max(h_min, s_h * WINDOW_STRIDE))
        steps_per_year = mins_per_year / step_mins

        yt = targets[:, h_idx]
        yp = preds[:, h_idx]
        yl = logits[:, h_idx]

        _, zp = causal_rolling_median_and_zscore(yp, warmup=256)
        _, zl = causal_rolling_median_and_zscore(yl, warmup=256)
        z_comp = 0.5 * (zp + zl)

        # Filter to non-overlapping step indices (contig_steps % s_h == 0) and after initial warmup (contig_steps >= 8)
        no_mask = ((contig_steps % s_h) == 0) & (contig_steps >= 8)
        yt_no = yt[no_mask]
        z_no = z_comp[no_mask]
        sh_no = shard_ids[no_mask]
        tk_no = ticker_ids[no_mask]
        st_no = contig_steps[no_mask]

        raw_configs[h_name] = {}

        for tau in hurdles:
            # Desired position per (shard, ticker, step): +1, -1, or 0
            w_pos = np.where(np.abs(z_no) > tau, np.sign(z_no), 0.0)

            # Compute exact state-dependent turnover |w_{i,k} - w_{i,k-1}| within each (shard, ticker) contiguous path
            state_turnover = np.zeros_like(w_pos, dtype=np.float64)
            for sh_val in np.unique(sh_no):
                for tk_val in np.unique(tk_no):
                    mask_st = (sh_no == sh_val) & (tk_no == tk_val)
                    if not np.any(mask_st):
                        continue
                    idx_st = np.where(mask_st)[0]
                    # Ensure ordered by st_no
                    ord_idx = idx_st[np.argsort(st_no[idx_st])]
                    w_seq = w_pos[ord_idx]
                    # Entry from 0 at start of shard, step-to-step changes, plus terminal flatten at end of shard
                    dw = np.abs(np.diff(w_seq, prepend=0.0))
                    dw[-1] += np.abs(w_seq[-1])
                    state_turnover[ord_idx] = dw

            # Isolated round-trip turnover: 2.0 one-way trades for every active position
            isolated_turnover = 2.0 * np.abs(w_pos)

            active_mask = np.abs(w_pos) > 0.0
            n_active_trades = int(np.sum(active_mask))
            n_total_opportunities = int(len(w_pos))
            if n_active_trades < 15:
                continue

            # Simple return per trade from log return yt_no
            simp_ret = np.expm1(np.clip(yt_no, -0.5, 0.5))
            gross_trade_rets = w_pos[active_mask] * simp_ret[active_mask]
            gross_mean_bps = float(np.mean(gross_trade_rets) * 10000.0)
            win_rate_gross = float(np.mean(gross_trade_rets > 0.0) * 100.0)

            total_state_turn_units = float(np.sum(state_turnover))
            avg_one_way_turn_per_active = total_state_turn_units / float(max(1, n_active_trades))

            # Breakeven one-way fee (bps):
            # 1) Under state-dependent holding turnover: gross_mean_bps / avg_one_way_turn_per_active
            # 2) Under isolated 1-bar round-trip: gross_mean_bps / 2.0
            be_state_bps = gross_mean_bps / max(1e-6, avg_one_way_turn_per_active)
            be_isolated_bps = gross_mean_bps / 2.0

            # Build chronological portfolio return series across unique (shard_id, contig_step) timestamps
            # At each timestamp (shard, step), capital is allocated across the 15 tickers (1/15 per ticker,
            # or normalized across active positions with max 1/5 per ticker to prevent single-stock concentration)
            time_keys = sorted(list(set(zip(sh_no.tolist(), st_no.tolist()))))
            n_time_steps = len(time_keys)

            port_gross_step = np.zeros(n_time_steps, dtype=np.float64)
            port_state_turn_step = np.zeros(n_time_steps, dtype=np.float64)
            port_iso_turn_step = np.zeros(n_time_steps, dtype=np.float64)

            for t_i, (sh_v, st_v) in enumerate(time_keys):
                m_t = (sh_no == sh_v) & (st_no == st_v)
                w_t = w_pos[m_t]
                r_t = simp_ret[m_t]
                st_turn_t = state_turnover[m_t]
                iso_turn_t = isolated_turnover[m_t]
                n_act_t = float(np.sum(np.abs(w_t) > 0.0))
                if n_act_t > 0:
                    # Equal-weight across active signals at step t, capped at 20% per ticker (max(5, n_act_t) denominator)
                    denom_t = max(5.0, n_act_t)
                    port_gross_step[t_i] = float(np.sum(w_t * r_t)) / denom_t
                    port_state_turn_step[t_i] = float(np.sum(st_turn_t)) / denom_t
                    port_iso_turn_step[t_i] = float(np.sum(iso_turn_t)) / denom_t

            active_steps_mask = (np.abs(port_gross_step) > 0.0) | (port_state_turn_step > 0.0)
            p_gross_act = port_gross_step[active_steps_mask]
            if len(p_gross_act) > 5 and np.std(p_gross_act) > 1e-12:
                sr_step_gross = float(np.mean(p_gross_act) / np.std(p_gross_act, ddof=1))
            else:
                sr_step_gross = 0.0
            all_per_step_srs_gross.append(sr_step_gross)

            raw_configs[h_name][f"tau_{tau}sigma"] = {
                "n_active_ticker_trades": n_active_trades,
                "n_total_non_overlap_opportunities": n_total_opportunities,
                "n_chronological_portfolio_steps": n_time_steps,
                "n_active_portfolio_steps": int(np.sum(active_steps_mask)),
                "step_interval_mins": int(step_mins),
                "activity_rate_pct": round(float(n_active_trades) / float(n_total_opportunities) * 100.0, 2),
                "avg_one_way_turnover_per_trade": round(avg_one_way_turn_per_active, 3),
                "gross_mean_ret_bps": round(gross_mean_bps, 2),
                "gross_win_rate_pct": round(win_rate_gross, 2),
                "breakeven_one_way_bps_state_hold": round(be_state_bps, 2),
                "breakeven_one_way_bps_isolated_rt": round(be_isolated_bps, 2),
                "_port_gross_step": port_gross_step,
                "_port_state_turn_step": port_state_turn_step,
                "_port_iso_turn_step": port_iso_turn_step,
                "_steps_per_year": steps_per_year
            }

    # Now compute net metrics across cost tiers and Deflated Sharpe Ratio (DSR)
    final_results = {}
    for h_name, h_dict in raw_configs.items():
        final_results[h_name] = {}
        for tau_key, info in h_dict.items():
            p_gross = info.pop("_port_gross_step")
            p_st_turn = info.pop("_port_state_turn_step")
            p_iso_turn = info.pop("_port_iso_turn_step")
            steps_per_yr = info.pop("_steps_per_year")

            cost_grid = {}
            for c_bps in one_way_cost_tiers:
                c_dec = c_bps / 10000.0
                # State-rebalanced net return series
                net_step = p_gross - c_dec * p_st_turn
                # Isolated round-trip net return series
                net_step_iso = p_gross - c_dec * p_iso_turn

                # Evaluate on all chronological portfolio steps (including 0-return flat steps!)
                mean_step = float(np.mean(net_step))
                std_step = float(np.std(net_step, ddof=1)) + 1e-12
                sr_step = mean_step / std_step
                ann_sharpe = sr_step * math.sqrt(steps_per_yr)

                mean_iso = float(np.mean(net_step_iso))
                std_iso = float(np.std(net_step_iso, ddof=1)) + 1e-12
                ann_sharpe_iso = (mean_iso / std_iso) * math.sqrt(steps_per_yr)

                neg_steps = net_step[net_step < 0.0]
                down_std = float(np.std(neg_steps, ddof=1)) + 1e-12 if len(neg_steps) > 1 else std_step
                ann_sortino = (mean_step / down_std) * math.sqrt(steps_per_yr)

                # Compounded equity curve E_t = prod (1 + R_t)
                eq_curve = np.cumprod(1.0 + np.clip(net_step, -0.95, 5.0))
                eq_with_init = np.insert(eq_curve, 0, 1.0)
                running_peak = np.maximum.accumulate(eq_with_init)
                dd_pct_series = ((running_peak - eq_with_init) / running_peak) * 100.0
                max_dd_pct = float(np.max(dd_pct_series))
                cum_ret_pct = float((eq_curve[-1] - 1.0) * 100.0) if len(eq_curve) > 0 else 0.0

                skew_val = float(stats.skew(net_step)) if len(net_step) > 5 else 0.0
                kurt_val = float(stats.kurtosis(net_step, fisher=False)) if len(net_step) > 5 else 3.0
                dsr_prob = compute_deflated_sharpe_ratio(
                    sr_est=sr_step,
                    all_trial_srs=all_per_step_srs_gross,
                    n_obs=len(net_step),
                    skew=skew_val,
                    kurt=kurt_val
                )

                act_steps = net_step[p_st_turn > 0.0]
                win_rate_net = float(np.mean(act_steps > 0.0) * 100.0) if len(act_steps) > 0 else 0.0

                cost_grid[f"{c_bps}bps_oneway"] = {
                    "round_trip_equiv_bps": round(2.0 * c_bps, 1),
                    "annualized_sharpe_state_hold": round(ann_sharpe, 2),
                    "annualized_sharpe_isolated_rt": round(ann_sharpe_iso, 2),
                    "deflated_sharpe_prob": round(dsr_prob, 4),
                    "annualized_sortino": round(ann_sortino, 2),
                    "compounded_cum_return_pct": round(cum_ret_pct, 2),
                    "max_drawdown_pct": round(max_dd_pct, 2),
                    "step_win_rate_pct": round(win_rate_net, 2),
                    "mean_step_net_bps": round(mean_step * 10000.0, 2)
                }

            info["cost_grid"] = cost_grid
            final_results[h_name][tau_key] = info

    return final_results


def run_mechanistic_ablation_with_controls(
    model: nn.Module,
    eval_fn,
    cohort_2024: dict,
    pr_base: np.ndarray,
    pl_base: np.ndarray,
    base_metrics: dict,
    device: torch.device
) -> dict:
    """
    Resolves Reviewer Finding 5:
    1. Computes empirical mean activation mu_b in R^{341} for each of the 6 tokenizer branches
       over the 2024 cohort so we can perform In-Distribution Mean-Ablation alongside Zero-Ablation.
    2. Adds Random-341-Channel Control (single-branch size) and Random-682-Channel Control (group size)
       across 3 deterministic seeds to establish the null distribution of channel removal.
    3. Computes 4 perturbation metrics per horizon:
       - raw_logit_l2_pct: ||z_m - z_0||_2 / ||z_0||_2 * 100%
       - centered_logit_l2_pct: ||(z_m - mean(z_m)) - (z_0 - mean(z_0))||_2 / ||z_0 - mean(z_0)||_2 * 100%
       - abs_logit_rms_shift: RMS(z_m - z_0) (denominator-free!)
       - prob_tv_shift_pp: mean(|sigmoid(z_m) - sigmoid(z_0)|) * 100
    4. Computes paired block-bootstrap 95% CIs on Delta ROC-AUC and Delta Calibrated Acc.
    """
    w_ab = cohort_2024["windows"]
    t_ab = cohort_2024["targets"]
    tokenizer = model.tokenizer
    kernels = [3, 7, 15, 31, 63, 127]

    # Step 1: Estimate empirical mean activation per channel across the 2,046 concatenated channels
    print("  🔎 Estimating empirical post-SiLU channel means across 2,046 tokenizer channels...")
    channel_sum = torch.zeros(2046, dtype=torch.float64, device=device)
    total_tokens = 0
    model.eval()
    with torch.no_grad():
        for i in range(0, min(len(w_ab), 2048), 64):
            bx = torch.from_numpy(w_ab[i:i+64]).to(device, non_blocking=True)
            with torch.amp.autocast('cuda', dtype=torch.bfloat16):
                x_trans = bx.transpose(1, 2)
                outs = [b(x_trans) for b in tokenizer.branches]
                min_len = min(o.shape[2] for o in outs)
                fused = torch.cat([o[:, :, :min_len] for o in outs], dim=1)  # [B, 2046, 512]
            channel_sum += fused.double().sum(dim=(0, 2))
            total_tokens += fused.shape[0] * fused.shape[2]

    channel_means = (channel_sum / float(max(1, total_tokens))).to(dtype=torch.float32)  # [2046]

    # Notice: In tokenizer.forward(x), `fused` has shape [B, 512, 2046] and goes into `self.proj(fused)`
    # where `self.proj` is `nn.Linear(2046, 2048, bias=False)`.
    # Mean-ablating a subset of channels C in [0..2045] (replacing fused[:, :, C] with channel_means[C])
    # is mathematically identical to zeroing `proj.weight[:, C]` and adding a fixed bias vector
    # `b_offset = proj.weight[:, C] @ channel_means[C]` to `proj(fused)`!
    # Let's use a forward pre-hook on `tokenizer.proj` to clamp any specified channel indices `mask_ch`
    # either to `channel_means[mask_ch]` (Mean-Ablation) or to `0.0` (Zero-Ablation)!
    ablation_configs = [
        ("K3", [0]),
        ("K7", [1]),
        ("K15", [2]),
        ("K31", [3]),
        ("K63", [4]),
        ("K127", [5]),
        ("Micro_K3_K7", [0, 1]),
        ("Meso_K15_K31", [2, 3]),
        ("Macro_K63_K127", [4, 5])
    ]

    def _evaluate_channel_mask(ch_indices: np.ndarray, mode: str) -> dict:
        ch_tensor = torch.from_numpy(ch_indices).long().to(device)
        mean_vals = channel_means[ch_tensor].view(1, 1, -1)

        def _proj_pre_hook(module, args):
            inp = args[0].clone()  # [B, 512, 2046]
            if mode == "mean":
                inp[:, :, ch_tensor] = mean_vals.to(inp.dtype)
            else:
                inp[:, :, ch_tensor] = 0.0
            return (inp,)

        handle = tokenizer.proj.register_forward_pre_hook(_proj_pre_hook)
        try:
            pr_m, pl_m = eval_fn(w_ab, batch_size=64)
        finally:
            handle.remove()

        m_metrics = compute_defensible_horizon_metrics(pr_m, pl_m, cohort_2024)
        boot_ci = compute_paired_block_bootstrap_ci(
            pr_base, pl_base, pr_m, pl_m, cohort_2024, n_boot=300, block_size=64
        )

        per_h_delta = {}
        raw_l2_list, cent_l2_list, rms_shift_list, prob_shift_list = [], [], [], []
        prob_base = 1.0 / (1.0 + np.exp(-np.clip(pl_base, -30.0, 30.0)))
        prob_m = 1.0 / (1.0 + np.exp(-np.clip(pl_m, -30.0, 30.0)))

        for h_idx, h_name in enumerate(HORIZON_NAMES):
            b_h = base_metrics["per_horizon"][h_name]
            m_h = m_metrics["per_horizon"][h_name]

            z0 = pl_base[:, h_idx].astype(np.float64)
            zm = pl_m[:, h_idx].astype(np.float64)
            dz = zm - z0

            raw_l2_pct = float(np.linalg.norm(dz) / max(1e-12, np.linalg.norm(z0)) * 100.0)
            z0_c = z0 - np.mean(z0)
            zm_c = zm - np.mean(zm)
            cent_l2_pct = float(np.linalg.norm(zm_c - z0_c) / max(1e-12, np.linalg.norm(z0_c)) * 100.0)
            abs_rms = float(np.sqrt(np.mean(dz ** 2)))
            prob_tv_pp = float(np.mean(np.abs(prob_m[:, h_idx] - prob_base[:, h_idx])) * 100.0)

            raw_l2_list.append(raw_l2_pct)
            cent_l2_list.append(cent_l2_pct)
            rms_shift_list.append(abs_rms)
            prob_shift_list.append(prob_tv_pp)

            per_h_delta[h_name] = {
                "raw_dir_acc": m_h["raw_dir_acc"],
                "delta_raw_dir_acc": round(m_h["raw_dir_acc"] - b_h["raw_dir_acc"], 2),
                "prior_calibrated_acc": m_h["prior_calibrated_acc"],
                "delta_calibrated_acc": round(m_h["prior_calibrated_acc"] - b_h["prior_calibrated_acc"], 2),
                "delta_calibrated_acc_ci95": boot_ci[h_name]["delta_calibrated_acc_ci95"],
                "roc_auc": m_h["roc_auc"],
                "delta_roc_auc": round(m_h["roc_auc"] - b_h["roc_auc"], 4),
                "delta_roc_auc_ci95": boot_ci[h_name]["delta_roc_auc_ci95"],
                "nonzero_roc_auc": m_h["nonzero_roc_auc"],
                "delta_nonzero_roc_auc": round(m_h["nonzero_roc_auc"] - b_h["nonzero_roc_auc"], 4),
                "spearman_ic": m_h["spearman_ic"],
                "delta_spearman_ic": round(m_h["spearman_ic"] - b_h["spearman_ic"], 4),
                "raw_logit_l2_pct": round(raw_l2_pct, 2),
                "centered_logit_l2_pct": round(cent_l2_pct, 2),
                "abs_logit_rms_shift": round(abs_rms, 4),
                "prob_tv_shift_pp": round(prob_tv_pp, 3)
            }

        return {
            "n_channels_masked": int(len(ch_indices)),
            "overall_raw_dir_acc": m_metrics["overall_raw_dir_acc"],
            "delta_overall_raw_dir_acc": round(m_metrics["overall_raw_dir_acc"] - base_metrics["overall_raw_dir_acc"], 2),
            "overall_prior_calibrated_acc": m_metrics["overall_prior_calibrated_acc"],
            "delta_overall_calibrated_acc": round(m_metrics["overall_prior_calibrated_acc"] - base_metrics["overall_prior_calibrated_acc"], 2),
            "overall_roc_auc": m_metrics["overall_roc_auc"],
            "delta_overall_roc_auc": round(m_metrics["overall_roc_auc"] - base_metrics["overall_roc_auc"], 4),
            "overall_nonzero_roc_auc": m_metrics["overall_nonzero_roc_auc"],
            "delta_overall_nonzero_roc_auc": round(m_metrics["overall_nonzero_roc_auc"] - base_metrics["overall_nonzero_roc_auc"], 4),
            "overall_mean_ic": m_metrics["overall_mean_ic"],
            "delta_overall_mean_ic": round(m_metrics["overall_mean_ic"] - base_metrics["overall_mean_ic"], 4),
            "mean_raw_logit_l2_pct": round(float(np.mean(raw_l2_list)), 2),
            "mean_centered_logit_l2_pct": round(float(np.mean(cent_l2_list)), 2),
            "mean_abs_logit_rms_shift": round(float(np.mean(rms_shift_list)), 4),
            "mean_prob_tv_shift_pp": round(float(np.mean(prob_shift_list)), 3),
            "per_horizon": per_h_delta
        }

    mean_ablations = {}
    zero_ablations = {}

    for ab_name, branch_indices in ablation_configs:
        ch_list = []
        for b_i in branch_indices:
            ch_list.extend(range(b_i * 341, (b_i + 1) * 341))
        ch_arr = np.array(ch_list, dtype=np.int64)

        res_mean = _evaluate_channel_mask(ch_arr, mode="mean")
        res_mean["masked_kernels"] = [kernels[i] for i in branch_indices]
        mean_ablations[f"MeanAblate_{ab_name}"] = res_mean

        res_zero = _evaluate_channel_mask(ch_arr, mode="zero")
        res_zero["masked_kernels"] = [kernels[i] for i in branch_indices]
        zero_ablations[f"ZeroAblate_{ab_name}"] = res_zero

        print(f"  • [{ab_name:15s}] MeanAblate: ΔAUC={res_mean['delta_overall_roc_auc']:+.4f}, "
              f"CentL2={res_mean['mean_centered_logit_l2_pct']:.2f}%, RMS={res_mean['mean_abs_logit_rms_shift']:.4f} | "
              f"ZeroAblate: ΔAUC={res_zero['delta_overall_roc_auc']:+.4f}, CentL2={res_zero['mean_centered_logit_l2_pct']:.2f}%")

    # Step 3: Random 341-Channel Control (single branch equivalent) & Random 682-Channel Control (2-branch group equivalent)
    rng_ctrl = np.random.default_rng(1001)
    rand341_ch = rng_ctrl.choice(2046, size=341, replace=False)
    rand682_ch = rng_ctrl.choice(2046, size=682, replace=False)

    mean_ablations["Random_341ch_Control"] = _evaluate_channel_mask(rand341_ch, mode="mean")
    zero_ablations["Random_341ch_Control"] = _evaluate_channel_mask(rand341_ch, mode="zero")
    mean_ablations["Random_682ch_Control"] = _evaluate_channel_mask(rand682_ch, mode="mean")
    zero_ablations["Random_682ch_Control"] = _evaluate_channel_mask(rand682_ch, mode="zero")

    print(f"  • [Random_341ch   ] MeanAblate: ΔAUC={mean_ablations['Random_341ch_Control']['delta_overall_roc_auc']:+.4f}, "
          f"CentL2={mean_ablations['Random_341ch_Control']['mean_centered_logit_l2_pct']:.2f}% | "
          f"ZeroAblate: ΔAUC={zero_ablations['Random_341ch_Control']['delta_overall_roc_auc']:+.4f}")

    return {
        "n_windows_evaluated": int(len(w_ab)),
        "baseline": base_metrics,
        "mean_ablations": mean_ablations,
        "zero_ablations": zero_ablations
    }


def audit_architecture_parameters(model: nn.Module) -> dict:
    """Computes exact parameter counts per module category."""
    groups = {
        "inception_tokenizer_branches": 0,
        "inception_tokenizer_proj_norm": 0,
        "conformer_gqa_attention": 0,
        "conformer_depthwise_conv": 0,
        "conformer_swiglu_ffn": 0,
        "conformer_rmsnorms": 0,
        "forecast_heads_and_final_norm": 0
    }
    for name, p in model.named_parameters():
        n = p.numel()
        if name.startswith("tokenizer.branches"):
            groups["inception_tokenizer_branches"] += n
        elif name.startswith("tokenizer"):
            groups["inception_tokenizer_proj_norm"] += n
        elif any(x in name for x in ["q_proj", "k_proj", "v_proj", "out_proj"]):
            groups["conformer_gqa_attention"] += n
        elif ".conv_module." in name and "norm" not in name:
            groups["conformer_depthwise_conv"] += n
        elif ".ffn." in name:
            groups["conformer_swiglu_ffn"] += n
        elif "blocks." in name and "norm" in name:
            groups["conformer_rmsnorms"] += n
        else:
            groups["forecast_heads_and_final_norm"] += n

    groups["total_exact_parameters"] = sum(groups.values())
    return groups


def select_frozen_2024_policy_and_evaluate_oos(backtests_by_year: dict) -> dict:
    """
    Resolves Reviewer Finding 4:
    Selects ONE (horizon, hurdle) configuration strictly on 2024 OOS data (maximizing 2024
    annualized Sharpe at 1.5 bps one-way institutional maker/midpoint cost with >= 100 active trades),
    FREEZES that exact (horizon, hurdle) configuration, and reports its untouched performance
    on 2024 (Selection), 2025 (Untouched OOS), and 2026 (Untouched OOS).
    Also reports the fixed non-overlapping 1d (390m) and 60m policies across all 3 years without re-selection.
    """
    bt_2024 = backtests_by_year["2024"]
    best_score = -1e9
    best_pair = ("1d", "tau_1.0sigma")

    for h_name, h_dict in bt_2024.items():
        for tau_key, info in h_dict.items():
            if info["n_active_ticker_trades"] < 100:
                continue
            sr_net = info["cost_grid"]["1.5bps_oneway"]["annualized_sharpe_state_hold"]
            if sr_net > best_score:
                best_score = sr_net
                best_pair = (h_name, tau_key)

    sel_h, sel_tau = best_pair
    frozen_policy_trajectory = {
        "selected_on_2024": {"horizon": sel_h, "hurdle": sel_tau},
        "yearly_performance": {}
    }
    for yr in ["2024", "2025", "2026"]:
        if sel_h in backtests_by_year[yr] and sel_tau in backtests_by_year[yr][sel_h]:
            frozen_policy_trajectory["yearly_performance"][yr] = backtests_by_year[yr][sel_h][sel_tau]

    # Also track a fixed moderate hurdle (tau_1.0sigma) across all horizons without cherry-picking
    fixed_tau1_across_horizons = {}
    for h_name in HORIZON_NAMES:
        fixed_tau1_across_horizons[h_name] = {}
        for yr in ["2024", "2025", "2026"]:
            if h_name in backtests_by_year[yr] and "tau_1.0sigma" in backtests_by_year[yr][h_name]:
                fixed_tau1_across_horizons[h_name][yr] = backtests_by_year[yr][h_name]["tau_1.0sigma"]

    return {
        "frozen_2024_optimal_policy": frozen_policy_trajectory,
        "fixed_tau_1sigma_all_horizons": fixed_tau1_across_horizons
    }


def main():
    t_start = time.time()
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    hf_token = os.environ.get("HF_TOKEN", "")
    repo_id = os.environ.get("HF_REPO_ID", "Ultron28/ohlcv-transformer")

    print("=" * 88)
    print(f"⚡ ULTRON // DEFENSIBLE TIER-1 SCIENTIFIC VERIFICATION ENGINE ({device})")
    print("=" * 88)

    os.makedirs("/content/ohlcv-transformer/checkpoints_1b", exist_ok=True)
    os.makedirs("/content/logs", exist_ok=True)

    # 1. Verify all 5 checkpoints on A100 VM
    ckpt_names = {
        "epoch1": "checkpoint_1b_epoch1.pt",
        "epoch2": "checkpoint_1b_epoch2.pt",
        "epoch3": "checkpoint_1b_epoch3.pt",
        "wf2024": "checkpoint_1b_wf2024.pt",
        "wf2025": "checkpoint_1b_wf2025.pt"
    }
    ckpt_paths = {}
    for key, fname in ckpt_names.items():
        local_p = f"/content/ohlcv-transformer/checkpoints_1b/{fname}"
        if not os.path.exists(local_p):
            print(f"☁️ Downloading {fname} from Hugging Face ({repo_id})...")
            local_p = hf_hub_download(
                repo_id=repo_id,
                filename=f"checkpoints_1b/{fname}",
                token=hf_token,
                local_dir="/content/ohlcv-transformer"
            )
        ckpt_paths[key] = local_p
        print(f"  ✅ Verified {key}: {local_p} ({os.path.getsize(local_p)/1e9:.2f} GB)")

    # 2. Instantiate model & audit exact parameter counts
    model_cfg = Model1BConfig()
    train_cfg = Training1BConfig()
    model = FinancialConformer1B(model_cfg).to(device)
    if device.type == "cuda":
        torch.backends.cuda.matmul.allow_tf32 = True
        torch.backends.cudnn.allow_tf32 = True

    param_audit = audit_architecture_parameters(model)

    # 3. Compute Exact Global Cosine Similarity & Adam Plasticity Arithmetic Audit
    weight_drift = compute_weight_drift_and_adam_arithmetic(ckpt_paths)

    # 4. Ingest Stratified Contiguous Multi-Ticker Cohorts:
    #    - Pre-2024 Stress Shards (2020-03 COVID Crash, 2022-06 Rate Shock, 2023-12 Anchor)
    #    - 2024, 2025, 2026
    shard_specs = {
        "stress_2020_03": ["data/ohlcv_2020-03.parquet"],
        "stress_2022_06": ["data/ohlcv_2022-06.parquet"],
        "stress_2023_12": ["data/ohlcv_2023-12.parquet"],
        "2024": ["data/ohlcv_2024-01.parquet", "data/ohlcv_2024-06.parquet", "data/ohlcv_2024-12.parquet"],
        "2025": ["data/ohlcv_2025-01.parquet", "data/ohlcv_2025-06.parquet", "data/ohlcv_2025-12.parquet"],
        "2026": ["data/ohlcv_2026-01.parquet", "data/ohlcv_2026-02.parquet", "data/ohlcv_2026-03.parquet"]
    }

    data_cfg = DatasetConfig(
        seq_len=train_cfg.seq_len,
        window_stride=train_cfg.window_stride,
        pred_horizons=train_cfg.pred_horizons,
        hf_token=hf_token
    )

    cached_cohorts = {}
    shard_counter = 0
    for cohort_key, s_list in shard_specs.items():
        s_dicts = []
        for rel_p in s_list:
            s_dicts.append(load_stratified_contiguous_shard(rel_p, data_cfg, shard_id=shard_counter, max_windows_per_ticker=180))
            shard_counter += 1
        cached_cohorts[cohort_key] = concat_cohort_dicts(s_dicts)
        print(f"  📦 Cohort [{cohort_key}]: {len(cached_cohorts[cohort_key]['windows'])} total windows across 15 tickers.")

    # Also build combined `pre2024_stress` cohort from the 3 historical stress shards
    cached_cohorts["pre2024_stress"] = concat_cohort_dicts([
        cached_cohorts["stress_2020_03"],
        cached_cohorts["stress_2022_06"],
        cached_cohorts["stress_2023_12"]
    ])
    print(f"  📦 Combined Cohort [pre2024_stress]: {len(cached_cohorts['pre2024_stress']['windows'])} total windows.")

    def _eval_loaded_model_on_array(w_arr: np.ndarray, batch_size: int = 64):
        model.eval()
        preds_l, logits_l = [], []
        n = len(w_arr)
        with torch.no_grad():
            for i in range(0, n, batch_size):
                bx = torch.from_numpy(w_arr[i:i+batch_size]).to(device, non_blocking=True)
                with torch.amp.autocast('cuda', dtype=torch.bfloat16):
                    pr, pl = model(bx)
                preds_l.append(pr.float().cpu().numpy())
                logits_l.append(pl.float().cpu().numpy())
        return np.concatenate(preds_l, axis=0), np.concatenate(logits_l, axis=0)

    # 5. Evaluate 5 Baselines across Pre-2024 Stress, 2024, 2025, 2026
    print("\n📊 [Baselines Audit] Computing 5 deterministic baselines (including FlatBar_Down_Heuristic)...")
    baselines_by_cohort = {}
    for c_key in ["pre2024_stress", "2024", "2025", "2026"]:
        baselines_by_cohort[c_key] = compute_baselines_on_cohort(cached_cohorts[c_key])
        print(f"  ✅ Baselines computed for [{c_key}].")

    # 6. Multi-Checkpoint Evaluation across All Cohorts (including Pre-2024 Stress Shards!)
    print("\n🚀 [Multi-Checkpoint & Forgetting Benchmark] Evaluating Epoch 1, Epoch 2, Epoch 3, WF-2024, WF-2025...")
    checkpoints_eval = {}
    raw_preds_logits = {}  # Store (pr, pl) for paired bootstrap CIs and backtesting

    for ckpt_key in ["epoch1", "epoch2", "epoch3", "wf2024", "wf2025"]:
        t_c = time.time()
        state = torch.load(ckpt_paths[ckpt_key], map_location=device, weights_only=False)
        model.load_state_dict(state.get("model_state", state), strict=False)
        model.eval()

        checkpoints_eval[ckpt_key] = {}
        raw_preds_logits[ckpt_key] = {}

        # Evaluate on the 3 individual stress shards first, then reuse concatenated predictions for `pre2024_stress`!
        stress_prs, stress_pls = [], []
        for s_key in ["stress_2020_03", "stress_2022_06", "stress_2023_12"]:
            pr_s, pl_s = _eval_loaded_model_on_array(cached_cohorts[s_key]["windows"], batch_size=64)
            stress_prs.append(pr_s)
            stress_pls.append(pl_s)
            checkpoints_eval[ckpt_key][s_key] = compute_defensible_horizon_metrics(pr_s, pl_s, cached_cohorts[s_key])
            raw_preds_logits[ckpt_key][s_key] = (pr_s, pl_s)

        pr_stress_all = np.concatenate(stress_prs, axis=0)
        pl_stress_all = np.concatenate(stress_pls, axis=0)
        checkpoints_eval[ckpt_key]["pre2024_stress"] = compute_defensible_horizon_metrics(
            pr_stress_all, pl_stress_all, cached_cohorts["pre2024_stress"]
        )
        raw_preds_logits[ckpt_key]["pre2024_stress"] = (pr_stress_all, pl_stress_all)

        for yr in ["2024", "2025", "2026"]:
            pr_y, pl_y = _eval_loaded_model_on_array(cached_cohorts[yr]["windows"], batch_size=64)
            rich_m = compute_defensible_horizon_metrics(pr_y, pl_y, cached_cohorts[yr])
            checkpoints_eval[ckpt_key][yr] = rich_m
            raw_preds_logits[ckpt_key][yr] = (pr_y, pl_y)
            print(f"  • [{ckpt_key:6s} | {yr}] RawAcc={rich_m['overall_raw_dir_acc']}% | "
                  f"NZAcc={rich_m['overall_nonzero_dir_acc']}% | "
                  f"CalAcc={rich_m['overall_prior_calibrated_acc']}% | "
                  f"AUC={rich_m['overall_roc_auc']} | NZAUC={rich_m['overall_nonzero_roc_auc']} | "
                  f"IC={rich_m['overall_mean_ic']}")

        m_st = checkpoints_eval[ckpt_key]["pre2024_stress"]
        print(f"  • [{ckpt_key:6s} | pre2024_stress] CalAcc={m_st['overall_prior_calibrated_acc']}% | "
              f"AUC={m_st['overall_roc_auc']} | IC={m_st['overall_mean_ic']} ({time.time() - t_c:.1f}s)")

    # 7. Paired Block-Bootstrap 95% CIs for Walk-Forward Adaptation & Backward Forgetting
    print("\n📐 [Paired Block-Bootstrap CIs] Computing 95% CIs for WF-2024 & WF-2025 vs Frozen Epoch 3...")
    bootstrap_comparisons = {}
    comparison_specs = [
        ("wf2024_vs_epoch3_on_2025_oos", "epoch3", "wf2024", "2025"),
        ("wf2024_vs_epoch3_on_2026_oos", "epoch3", "wf2024", "2026"),
        ("wf2025_vs_epoch3_on_2026_oos", "epoch3", "wf2025", "2026"),
        ("wf2025_vs_wf2024_on_2026_oos", "wf2024", "wf2025", "2026"),
        ("wf2024_vs_epoch3_on_pre2024_stress", "epoch3", "wf2024", "pre2024_stress"),
        ("wf2025_vs_epoch3_on_pre2024_stress", "epoch3", "wf2025", "pre2024_stress"),
        ("wf2025_vs_epoch3_on_2020_03_covid", "epoch3", "wf2025", "stress_2020_03"),
        ("wf2025_vs_epoch3_on_2022_06_rates", "epoch3", "wf2025", "stress_2022_06"),
        ("epoch3_vs_epoch1_on_2024_oos", "epoch1", "epoch3", "2024")
    ]
    for comp_name, ck_a, ck_b, coh_key in comparison_specs:
        pa, la = raw_preds_logits[ck_a][coh_key]
        pb, lb = raw_preds_logits[ck_b][coh_key]
        bootstrap_comparisons[comp_name] = compute_paired_block_bootstrap_ci(
            pa, la, pb, lb, cached_cohorts[coh_key], n_boot=500, block_size=64
        )
        print(f"  ✅ Paired Bootstrap CI computed: {comp_name}")

    # 8. Non-Overlapping Contiguous Backtest & Frozen 2024 Policy Evaluation (2024, 2025, 2026)
    print("\n📈 [Non-Overlapping Contiguous Backtest] Running state-turnover & DSR backtests...")
    backtests_epoch3 = {}
    backtests_wf = {}
    for yr in ["2024", "2025", "2026"]:
        pr3, pl3 = raw_preds_logits["epoch3"][yr]
        backtests_epoch3[yr] = run_non_overlapping_contiguous_backtest(pr3, pl3, cached_cohorts[yr])
        # Also evaluate walk-forward causal trajectory (2024=epoch3, 2025=wf2024, 2026=wf2025)
        wf_ck = "epoch3" if yr == "2024" else ("wf2024" if yr == "2025" else "wf2025")
        pr_w, pl_w = raw_preds_logits[wf_ck][yr]
        backtests_wf[yr] = run_non_overlapping_contiguous_backtest(pr_w, pl_w, cached_cohorts[yr])
        print(f"  ✅ Non-overlapping contiguous backtest completed for Year {yr}.")

    frozen_policy_summary = {
        "epoch3_frozen_weights": select_frozen_2024_policy_and_evaluate_oos(backtests_epoch3),
        "walk_forward_weights": select_frozen_2024_policy_and_evaluate_oos(backtests_wf)
    }

    # 9. Mechanistic Kernel Ablation with Mean-Ablation, Zero-Ablation & Random-341ch Controls
    print("\n🔬 [Mechanistic Kernel Ablation] Running Mean-Ablation, Zero-Ablation & Random-Channel Controls on 2024...")
    state_ep3 = torch.load(ckpt_paths["epoch3"], map_location=device, weights_only=False)
    model.load_state_dict(state_ep3.get("model_state", state_ep3), strict=False)
    model.eval()

    pr_base_24, pl_base_24 = raw_preds_logits["epoch3"]["2024"]
    base_metrics_24 = checkpoints_eval["epoch3"]["2024"]

    ablation_full = run_mechanistic_ablation_with_controls(
        model=model,
        eval_fn=_eval_loaded_model_on_array,
        cohort_2024=cached_cohorts["2024"],
        pr_base=pr_base_24,
        pl_base=pl_base_24,
        base_metrics=base_metrics_24,
        device=device
    )

    # 10. Save Master Defensible Audit JSON
    master_audit = {
        "audit_timestamp": time.time(),
        "total_elapsed_sec": round(time.time() - t_start, 2),
        "hardware": torch.cuda.get_device_name(0) if device.type == "cuda" else "CPU",
        "cohort_methodology": {
            "tickers_evaluated": sorted(data_cfg.liquid_tickers),
            "n_tickers": len(data_cfg.liquid_tickers),
            "window_seq_len": train_cfg.seq_len,
            "window_stride_minutes": train_cfg.window_stride,
            "non_overlapping_step_multipliers": dict(zip(HORIZON_NAMES, NON_OVERLAP_STEPS)),
            "cohorts_window_counts": {k: int(len(v["windows"])) for k, v in cached_cohorts.items()}
        },
        "architecture_parameter_audit": param_audit,
        "weight_drift_and_cosine_audit": weight_drift,
        "multi_checkpoint_oos_and_stress_audit": checkpoints_eval,
        "paired_block_bootstrap_95ci": bootstrap_comparisons,
        "quantitative_baselines_audit": baselines_by_cohort,
        "non_overlapping_contiguous_backtest_epoch3": backtests_epoch3,
        "non_overlapping_contiguous_backtest_walkforward": backtests_wf,
        "frozen_2024_policy_oos_evaluation": frozen_policy_summary,
        "mechanistic_kernel_ablation_controlled": ablation_full
    }

    out_path = "/content/logs/defensible_tier1_audit.json"
    with open(out_path, "w") as f:
        json.dump(master_audit, f, indent=2)

    print(f"\n🏆 [ULTRON DEFENSIBLE AUDIT COMPLETE] Saved to {out_path} in {time.time() - t_start:.1f}s!")

    if hf_token:
        try:
            api = HfApi(token=hf_token)
            api.upload_file(
                path_or_fileobj=out_path,
                path_in_repo="logs/defensible_tier1_audit.json",
                repo_id=repo_id,
                repo_type="model"
            )
            print("☁️ Uploaded logs/defensible_tier1_audit.json to Hugging Face Model Hub!")
        except Exception as e:
            print(f"⚠️ HF upload notice: {e}")


if __name__ == "__main__":
    main()