tradefloor 0.11.0

A reproducible evaluation environment for financial AI agents.
Documentation
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
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
//! The engine's half of what derivatives read: the price index
//! (`index_level_listed`), the live VIX (`vix_intraday_live`) and the
//! forecast (`forecast_horizon_sessions`).
//!
//! Each reads the engine's state and writes only its own fields, so a model
//! with these switches on prints the prices the same model prints with them
//! off. None of them takes a draw.

use super::*;
use crate::derivatives::Forecast;
use crate::market::index_value::{
    calculate_market_index, rebased_divisor, IndexConstituent, IndexLevel, INDEX_BASE,
};
use crate::market::index_var::{
    index_conditional_variance_terms_with_states, intraday_variance_factor,
    name_noise_variance_scaled, name_session_variance, vix_from_variance, NameConstants,
    NameVariance,
};

/// The policy grid a meeting moves the rate on, percentage points.
const GUIDANCE_GRID: f64 = 0.25;

/// The prior over a scheduled meeting's decision (`policy_guidance_signal_sd`):
/// `(steps, weight)`, the decision being the published shadow plus `steps`
/// times [`GUIDANCE_GRID`]. The model's own: on 64 histories of 21 years of
/// the pt-v22 futures line (10,040 eves), the share of eves at which the
/// decision the meeting delivered was the published shadow plus each step,
/// with a half count on each of -8 to 8 so none has no weight. The decision
/// is the published shadow at 93.2% of eves (se 0.6%), one step off at 4.9%;
/// the Fed's own reading at every close sits off it at the same shares.
const GUIDANCE_PRIOR: [(f64, f64); 17] = [
    (-8.0, 4.975867e-05),
    (-7.0, 4.975867e-05),
    (-6.0, 4.975867e-05),
    (-5.0, 4.975867e-05),
    (-4.0, 3.483107e-04),
    (-3.0, 1.243967e-03),
    (-2.0, 6.319351e-03),
    (-1.0, 2.333682e-02),
    (0.0, 9.315321e-01),
    (1.0, 2.522765e-02),
    (2.0, 1.030004e-02),
    (3.0, 1.144449e-03),
    (4.0, 1.492760e-04),
    (5.0, 4.975867e-05),
    (6.0, 4.975867e-05),
    (7.0, 4.975867e-05),
    (8.0, 4.975867e-05),
];

/// Five-point Gauss-Hermite nodes and weights (physicists' form, the
/// weights summing to the square root of pi).
const GAUSS_HERMITE_5: [(f64, f64); 5] = [
    (-2.020_182_870_456_086, 0.019_953_242_059_045_913),
    (-0.958_572_464_613_818_5, 0.393_619_323_152_241_2),
    (0.0, 0.945_308_720_482_941_9),
    (0.958_572_464_613_818_5, 0.393_619_323_152_241_2),
    (2.020_182_870_456_086, 0.019_953_242_059_045_913),
];

/// The day's market factor in the forecast's expected close, integrated by
/// four equally likely nodes: the means of a standard normal's four
/// equal-probability bins, rescaled so the rule's variance is one. The close
/// is quadratic in the day's factor with a sign asymmetry, which any
/// symmetric unit-variance rule integrates exactly, and each sign keeps two
/// nodes for the lagged wire's faces.
const FACTOR_NODES: [f64; 4] = [-1.370_224_249_159_755, -0.349_979_295_122_705,
                                0.349_979_295_122_705, 1.370_224_249_159_755];

/// Forty-point Gauss-Hermite nodes and weights (physicists' form), the
/// positive half; the rule is symmetric. Fine enough for the stress premium,
/// which has a kink at its knee, and for a skewed spread's tail.
const GAUSS_HERMITE_40_HALF: [(f64, f64); 20] = [
    (0.17453721459758237, 0.3386432774255891),
    (0.5238747138322772, 0.2657282518773773),
    (0.8740066123570881, 0.1633787327132714),
    (1.225480109046289, 0.07847460586540442),
    (1.5788698949316138, 0.029312565536172383),
    (1.9347914722822959, 0.008460888008258134),
    (2.2939171418750837, 0.0018714968295979483),
    (2.6569959984428957, 0.00031385359454133175),
    (3.0248798839012845, 3.936933981092491e-5),
    (3.398558265859628, 3.6315761506930256e-6),
    (3.7792067534352234, 2.411144163670531e-7),
    (4.1682570668325, 1.1212360832275839e-8),
    (4.567502072844395, 3.525620791365422e-10),
    (4.9792609785452555, 7.156528052690362e-12),
    (5.406654247970128, 8.805707645216109e-14),
    (5.8540950560304, 6.008358789490818e-16),
    (6.328255351220082, 1.9891810121165008e-18),
    (6.840237305249355, 2.567593365411684e-21),
    (7.411582531485469, 8.544056963775433e-25),
    (8.09876113925085, 2.5910437138470347e-29),
];

/// The shape of the log VIX's spread about the forecast's path: a standard
/// variable, skewed to `forecast_vix_dispersion_skew`, on the forty nodes of
/// a standard normal. Each node is `(shape, weight)`, the weights summing to
/// one and the shapes to mean 0 and variance 1 under them. At a skew of 0.0
/// the shape is the normal itself; off it, a skew-normal, the normal's
/// weights times `2 Phi(a z)`, with `a` the one whose skewness under the
/// rule is the skew. Its tails stay normal, so every power of the VIX the
/// forecast takes an expectation of stays finite; its skewness reaches
/// about 0.99.
fn spread_shape(skew: f64) -> Vec<(f64, f64)> {
    let root_pi = crate::mathx::sqrt(std::f64::consts::PI);
    let mut normal = Vec::with_capacity(40);
    for &(x, w) in GAUSS_HERMITE_40_HALF.iter().rev() {
        normal.push((-std::f64::consts::SQRT_2 * x, w / root_pi));
    }
    for &(x, w) in GAUSS_HERMITE_40_HALF.iter() {
        normal.push((std::f64::consts::SQRT_2 * x, w / root_pi));
    }
    // The skew-normal of shape `a`, standardised under the rule itself, so a
    // node's shape is exactly a number of the spread's sds; and its
    // skewness under the rule.
    let skewed = |a: f64| -> (Vec<(f64, f64)>, f64) {
        let mut nodes = normal.clone();
        if a != 0.0 {
            let mut total = 0.0;
            for node in nodes.iter_mut() {
                node.1 *= crate::mathx::erfc(-a * node.0 / std::f64::consts::SQRT_2);
                total += node.1;
            }
            for node in nodes.iter_mut() {
                node.1 /= total;
            }
        }
        let mean: f64 = nodes.iter().map(|(z, w)| w * z).sum();
        let var: f64 = nodes.iter().map(|(z, w)| w * (z - mean) * (z - mean)).sum();
        let sd = crate::mathx::sqrt(var);
        let mut third = 0.0;
        for node in nodes.iter_mut() {
            node.0 = (node.0 - mean) / sd;
            third += node.1 * node.0 * node.0 * node.0;
        }
        (nodes, third)
    };
    if skew == 0.0 {
        return skewed(0.0).0;
    }
    let (mut lo, mut hi) = (0.0, 50.0);
    for _ in 0..60 {
        let mid = 0.5 * (lo + hi);
        if skewed(mid).1 < skew.abs() {
            lo = mid;
        } else {
            hi = mid;
        }
    }
    // A left skew is the right one reflected.
    skewed(skew.signum() * 0.5 * (lo + hi)).0
}

/// The log of `V / mean` at each node of a spread of log variance `var`
/// with shape `shape` ([`spread_shape`]), shifted so that `E[V]` is the mean
/// under the rule. Each entry is `(log ratio, weight)`.
fn spread_at(shape: &[(f64, f64)], var: f64) -> Vec<(f64, f64)> {
    let sd = crate::mathx::sqrt(crate::mathx::max(0.0, var));
    let total: f64 = shape.iter().map(|(s, w)| w * crate::mathx::exp(sd * s)).sum();
    let shift = -crate::mathx::log(total);
    shape.iter().map(|(s, w)| (shift + sd * s, *w)).collect()
}

/// `E[g(V)]` for `V = mean * exp(x)` over the nodes of [`spread_at`].
fn spread_expect(nodes: &[(f64, f64)], mean: f64, g: impl Fn(f64) -> f64) -> f64 {
    let mut total = 0.0;
    for &(x, w) in nodes {
        total += w * g(mean * crate::mathx::exp(x));
    }
    total
}

/// `E[g(X)]` for `X` lognormal with mean `mean` and log variance `var`, by
/// five-point Gauss-Hermite; `g(mean)` at a variance of 0.0.
fn lognormal_expect(mean: f64, var: f64, g: impl Fn(f64) -> f64) -> f64 {
    if !(var > 0.0) || !(mean > 0.0) {
        return g(mean);
    }
    let sd = crate::mathx::sqrt(var);
    let mu = crate::mathx::log(mean) - 0.5 * var;
    let mut total = 0.0;
    for &(node, weight) in GAUSS_HERMITE_5.iter() {
        total += weight * g(crate::mathx::exp(mu + sd * std::f64::consts::SQRT_2 * node));
    }
    total / crate::mathx::sqrt(std::f64::consts::PI)
}

/// `E[max(X - k, 0)]` for `X` lognormal with mean `mean` and log variance
/// `var`: `mean N(d1) - k N(d2)`.
fn lognormal_excess(mean: f64, var: f64, k: f64) -> f64 {
    if !(var > 0.0) || !(mean > 0.0) || !(k > 0.0) {
        return if mean > k { mean - k } else { 0.0 };
    }
    let sd = crate::mathx::sqrt(var);
    let d1 = (crate::mathx::log(mean / k) + 0.5 * var) / sd;
    let d2 = d1 - sd;
    let cdf = |x: f64| 0.5 * crate::mathx::erfc(-x / std::f64::consts::SQRT_2);
    mean * cdf(d1) - k * cdf(d2)
}

/// A slot the forecast follows: a public, solvent name with a
/// capitalisation, as the variance identity reads it.
struct ForecastName {
    slot: usize,
    name: NameVariance,
    constants: NameConstants,
    sector_base: f64,
    garch_beta: f64,
}

impl Engine {
    // ── The index (`index_level_listed`) ──────────────────────────────────

    fn index_on(&self) -> bool {
        self.params.index_level_listed != 0.0
    }

    /// The roster as [`calculate_market_index`] reads it: every public name
    /// at `price * shares_outstanding`, marked when bankrupt; the public,
    /// solvent names' ids as the components; and their capitalisation,
    /// summed in roster order as that function sums it.
    pub(super) fn index_constituents(&self) -> (Vec<IndexConstituent>, Vec<String>, f64) {
        let mut roster = Vec::with_capacity(self.companies.len());
        let mut ids = Vec::with_capacity(self.companies.len());
        let mut cap = 0.0;
        for c in &self.companies {
            if !c.is_public {
                continue;
            }
            let mut k = IndexConstituent::new(c.id.clone(), c.stock.price * c.stock.shares_outstanding);
            if c.is_bankrupt {
                k.is_bankrupt = true;
            } else {
                ids.push(c.id.clone());
                cap += k.market_cap;
            }
            roster.push(k);
        }
        (roster, ids, cap)
    }

    /// The level on the prices standing, the last close's level being what
    /// a degenerate roster holds.
    pub(super) fn index_level_now(&self) -> f64 {
        let (roster, ids, _) = self.index_constituents();
        let previous = if self.index_close > 0.0 { self.index_close } else { INDEX_BASE };
        calculate_market_index(&roster, &ids, previous, self.index_divisor).value
    }

    /// At an open: the divisor, once, so the session opens at the base.
    pub(super) fn index_open(&mut self) {
        if !self.index_on() || self.index_divisor > 0.0 {
            return;
        }
        let (_, _, cap) = self.index_constituents();
        if let Some(d) = rebased_divisor(cap, INDEX_BASE) {
            self.index_divisor = d;
        }
    }

    /// At a close, on the session's last prints: the close level.
    pub(super) fn index_mark_close(&mut self) {
        if !self.index_on() || !(self.index_divisor > 0.0) {
            return;
        }
        self.index_close = self.index_level_now();
    }

    /// Before a listing, a delisting or a change of status: the
    /// constituents' capitalisation and the level, at the prices standing.
    /// `None` with the switch off or before the base is set.
    pub(super) fn index_before_change(&self) -> Option<(f64, f64)> {
        if !self.index_on() || !(self.index_divisor > 0.0) {
            return None;
        }
        let (roster, ids, cap) = self.index_constituents();
        let previous = if self.index_close > 0.0 { self.index_close } else { INDEX_BASE };
        Some((cap, calculate_market_index(&roster, &ids, previous, self.index_divisor).value))
    }

    /// After the change: the divisor that holds the level where it was on
    /// the constituents now standing. Nothing when the capitalisation did
    /// not move, or when nothing is left to divide.
    pub(super) fn index_rebase(&mut self, before: Option<(f64, f64)>) {
        let Some((cap_before, level)) = before else {
            return;
        };
        let (_, _, cap) = self.index_constituents();
        if cap.to_bits() == cap_before.to_bits() {
            return;
        }
        if let Some(d) = rebased_divisor(cap, level) {
            self.index_divisor = d;
        }
    }

    /// The price index (`index_level_listed`): `None` with the switch off.
    ///
    /// The level is `sum(price * shares_outstanding) / divisor` over the
    /// public, solvent names on the prices as they stand. The divisor is set
    /// at the first open so that session opens at
    /// [`crate::market::INDEX_BASE`], and reset by [`Engine::add_company`],
    /// [`Engine::remove_company`] and [`Engine::set_status`] so the level
    /// does not move when the constituents do. A name edited through
    /// [`Engine::companies_mut`] is read as it stands and resets nothing.
    pub fn index_level(&self) -> Option<IndexLevel> {
        if !self.index_on() {
            return None;
        }
        let constituents = self.companies.iter().filter(|c| c.is_public && !c.is_bankrupt).count();
        let divisor = if self.index_divisor > 0.0 { Some(self.index_divisor) } else { None };
        let close = if self.index_close > 0.0 { Some(self.index_close) } else { None };
        let level = if divisor.is_some() { self.index_level_now() } else { INDEX_BASE };
        Some(IndexLevel { level, close, divisor, constituents })
    }

    /// The index's state for the snapshot: `[divisor, close]`, 0.0 for each
    /// not yet set. `None` with the switch off.
    pub fn index_state(&self) -> Option<[f64; 2]> {
        if self.index_on() {
            Some([self.index_divisor, self.index_close])
        } else {
            None
        }
    }

    /// Put the index's state back (a restore). Refused with the switch off,
    /// and for a divisor or level that is negative or not finite.
    pub fn set_index_state(&mut self, state: Option<[f64; 2]>) -> Result<(), String> {
        match state {
            Some(_) if !self.index_on() => Err(
                "this snapshot carries the price index (index_divisor), which only an \
                 engine with index_level_listed on keeps, and this engine's model has it off"
                    .to_string()),
            Some([d, c]) => {
                if !(d.is_finite() && d >= 0.0 && c.is_finite() && c >= 0.0) {
                    return Err(format!(
                        "this snapshot's index_divisor is [{d}, {c}]; the divisor and the close \
                         level are finite and not negative, 0.0 where not yet set"));
                }
                self.index_divisor = d;
                self.index_close = c;
                Ok(())
            }
            None => {
                self.index_divisor = 0.0;
                self.index_close = 0.0;
                Ok(())
            }
        }
    }

    // ── The live VIX (`vix_intraday_live`) ────────────────────────────────

    /// The VIX as published live (`vix_intraday_live`): within a session the
    /// projection of the VIX tonight's close will publish, given the session
    /// so far; outside one, and between a pin and the next tick, the
    /// published VIX. `None` with the switch off.
    pub fn live_vix(&self) -> Option<f64> {
        if self.params.vix_intraday_live == 0.0 {
            return None;
        }
        Some(match self.vix_live {
            Some(v) => v,
            None => self.published_vix(),
        })
    }

    /// The live VIX's projection a session holds, for the snapshot: `None`
    /// outside a session and with the switch off.
    pub fn vix_live_mark(&self) -> Option<f64> {
        if self.params.vix_intraday_live == 0.0 {
            None
        } else {
            self.vix_live
        }
    }

    /// Put the live VIX's projection back (a restore). Refused with the
    /// switch off and for a projection that is not finite and positive.
    pub fn set_vix_live_mark(&mut self, mark: Option<f64>) -> Result<(), String> {
        match mark {
            Some(_) if self.params.vix_intraday_live == 0.0 => Err(
                "this snapshot carries the live VIX (vix_live), which only an engine with \
                 vix_intraday_live on keeps, and this engine's model has it off"
                    .to_string()),
            Some(v) if !(v.is_finite() && v > 0.0) => Err(format!(
                "this snapshot's vix_live is {v}; the live VIX is finite and positive")),
            m => {
                self.vix_live = m;
                Ok(())
            }
        }
    }

    // ── The forecast (`forecast_horizon_sessions`) ────────────────────────

    /// The forecast the last close computed (`forecast_horizon_sessions`):
    /// `None` with the dial at 0.0 and before the first close.
    pub fn forecast(&self) -> Option<&Forecast> {
        if self.params.forecast_horizon_sessions == 0.0 {
            None
        } else {
            self.forecast.as_ref()
        }
    }

    /// Put a forecast back (a restore). Refused with the dial at 0.0 and for
    /// a forecast whose horizon is not the dial's.
    pub fn set_forecast(&mut self, forecast: Option<Forecast>) -> Result<(), String> {
        let h = self.params.forecast_horizon_sessions;
        match forecast {
            Some(_) if h == 0.0 => Err(
                "this snapshot carries a forecast, which only an engine with \
                 forecast_horizon_sessions set keeps, and this engine's is 0"
                    .to_string()),
            Some(f) if f.horizon() as f64 != h => Err(format!(
                "this snapshot's forecast runs {} sessions and this engine's \
                 forecast_horizon_sessions is {h}",
                f.horizon())),
            f => {
                self.forecast = f;
                Ok(())
            }
        }
    }

    /// Compute the forecast on the state standing: at a close for the close
    /// whose day count is `game_day`, or after a pin for the close the
    /// standing forecast was taken at (nothing before the first close).
    /// Nothing with the dial at 0.0.
    pub(super) fn refresh_forecast(&mut self, game_day: Option<i64>) {
        if self.params.forecast_horizon_sessions == 0.0 {
            return;
        }
        let day = match game_day.or_else(|| self.forecast.as_ref().map(|f| f.day)) {
            Some(d) => d,
            None => return,
        };
        self.forecast = Some(self.compute_forecast(day));
    }

    /// The market's belief over the five phases, `phase_cycle` order: the
    /// cycle nowcast where the model runs one, and otherwise the whole
    /// weight on the published phase.
    fn forecast_belief(&self) -> [f64; 5] {
        if self.params.cycle_nowcast_accuracy != 0.0 {
            let total: f64 = self.cycle_nowcast.iter().sum();
            if total > 0.0 && total.is_finite() {
                return self.cycle_nowcast;
            }
        }
        let phases = crate::economy::cycle::phase_cycle();
        let published = self.published_cycle_phase();
        let mut b = [0.0; 5];
        let k = phases.iter().position(|p| *p == published).unwrap_or(0);
        b[k] = 1.0;
        b
    }

    /// Each phase's exit rate per session, from the cycle's own mean
    /// sojourns: the nowcast's predict step.
    fn forecast_exit_rates(&self) -> [f64; 5] {
        if self.params.cycle_nowcast_accuracy != 0.0 || self.params.corporate_spread_cycle != 0.0 {
            return self.cycle_nowcast_terms.0;
        }
        let (mean, _) = crate::economy::cycle::stationary_phase_shares_for(&self.cycle_spec());
        let mut lambda = [0.0; 5];
        for j in 0..5 {
            lambda[j] = if mean[j] > 0.0 { crate::mathx::min(1.0, 1.0 / mean[j]) } else { 0.0 };
        }
        lambda
    }

    /// The names the forecast follows, with the weights the identity reads.
    fn forecast_names(&self) -> Vec<ForecastName> {
        let mut total = 0.0;
        for c in self.companies.iter() {
            if c.is_public && !c.is_bankrupt && c.stock.market_cap > 0.0 {
                total += c.stock.market_cap;
            }
        }
        if !(total > 0.0) {
            return Vec::new();
        }
        let bases = self.sector_base_variances();
        let mut out = Vec::new();
        for (slot, c) in self.companies.iter().enumerate() {
            if !c.is_public || c.is_bankrupt || c.stock.market_cap <= 0.0 {
                continue;
            }
            let name = NameVariance {
                weight: c.stock.market_cap / total,
                beta: c.stock.beta.unwrap_or(1.0),
                sector: self.sector_keys.iter().position(|k| *k == c.sector).unwrap_or(usize::MAX),
                garch_variance: c.stock.garch_variance,
                market_cap: c.stock.market_cap,
            };
            out.push(ForecastName {
                slot,
                name,
                constants: NameConstants::of(&self.params, &name),
                sector_base: bases.get(slot).copied().unwrap_or(c.stock.garch_variance),
                garch_beta: crate::market::garch::garch_beta_for(&self.params, c.stock.market_cap),
            });
        }
        out
    }

    /// The cycle's volatility regimes the belief weighs: for each phase,
    /// its weight, the level the factor's baseline is scaled by and the VIX
    /// coupling's scale (`market_vol_cycle_ratio`). One regime of weight one
    /// with the dial at 0.0.
    fn forecast_regimes(&self, belief: &[f64; 5], phase_regimes: &[(f64, f64); 5]) -> Vec<(f64, f64, f64)> {
        if self.params.market_vol_cycle_ratio == 0.0 {
            return vec![(1.0, 1.0, 1.0)];
        }
        let mut out: Vec<(f64, f64, f64)> = Vec::new();
        for (j, &w) in belief.iter().enumerate() {
            if !(w > 0.0) {
                continue;
            }
            let (level, scale) = phase_regimes[j];
            match out.iter_mut().find(|r| r.1.to_bits() == level.to_bits() && r.2.to_bits() == scale.to_bits()) {
                Some(r) => r.0 += w,
                None => out.push((w, level, scale)),
            }
        }
        if out.is_empty() {
            out.push((1.0, 1.0, 1.0));
        }
        out
    }

    /// Each phase's level and VIX scale for [`Engine::forecast_regimes`]:
    /// the cycle multiplier's target in the phase, read once a forecast,
    /// since nothing it reads moves over the horizon.
    fn forecast_phase_regimes(&self) -> [(f64, f64); 5] {
        let phases = crate::economy::cycle::phase_cycle();
        let mut out = [(1.0, 1.0); 5];
        if self.params.market_vol_cycle_ratio == 0.0 {
            return out;
        }
        for (j, phase) in phases.iter().enumerate() {
            let l = self.market_vol_cycle_target_log_for(*phase);
            let m = crate::mathx::exp(l);
            out[j] = (m * m, self.market_vol_cycle_vix_scale_at(Some(l)));
        }
        out
    }

    /// The identity on a forecast state: the index's one-session variance
    /// and its names', at the VIX `vix`. The factor's variance is `faces`:
    /// its expectation after an up day and after a down day, which the
    /// lagged wire's two faces read (`down` is the bit where it is known, and
    /// both faces are then the one variance). The names read their average.
    #[allow(clippy::too_many_arguments)]
    fn forecast_identity(
        &self,
        names: &[ForecastName],
        garch: &[f64],
        sector_states: &[f64],
        idio: &[f64],
        faces: (f64, f64),
        vix: f64,
        universe_stress: f64,
        down: Option<bool>,
        economy: &EconomyState,
    ) -> (f64, Vec<f64>) {
        let factor_variance = 0.5 * (faces.0 + faces.1);
        let p = &self.params;
        let k = intraday_variance_factor();
        let target = crate::market::tick::sector_sigma_at(p, economy, self.vix_anchor);
        let sigmas: Vec<f64> = if self.sector_state_on() {
            sector_states.iter().map(|s| if *s > 0.0 { target * crate::mathx::sqrt(*s) } else { target }).collect()
        } else {
            vec![target; self.sector_keys.len()]
        };
        let ratio = vix / self.vix_anchor;
        let rate_scale = if p.jump_vix_coupling == 0.0 {
            1.0
        } else {
            (1.0 - p.jump_vix_coupling) + ((p.jump_vix_coupling * ratio) * ratio)
        };
        let spike = crate::market::tick::crisis_spike_for(p, vix, universe_stress);
        let list: Vec<NameVariance> = names
            .iter()
            .zip(garch.iter())
            .map(|(n, g)| NameVariance { garch_variance: *g, ..n.name })
            .collect();
        let excitations: Vec<f64> = if p.jump_idio_excitation == 0.0 {
            Vec::new()
        } else {
            names.iter().map(|n| self.jump_excitation.get(n.slot).copied().unwrap_or(0.0)).collect()
        };
        let one = |bit: bool, v: f64| {
            index_conditional_variance_terms_with_states(
                p, &list, self.sector_keys.len(), v, &sigmas, &excitations, idio,
                rate_scale, spike, bit, k)
                .total()
        };
        let index = match down {
            Some(bit) => one(bit, factor_variance),
            None if p.market_beta_down_asym_lag == 0.0 => one(false, factor_variance),
            None => 0.5 * (one(false, faces.0) + one(true, faces.1)),
        };
        let per_name = names
            .iter()
            .enumerate()
            .map(|(i, n)| {
                let sigma = if n.name.sector < sigmas.len() { sigmas[n.name.sector] } else { 0.0 };
                let r = idio.get(i).copied().unwrap_or(1.0);
                name_session_variance(
                    p, &n.constants, n.name.beta, garch[i], factor_variance, sigma, r, rate_scale, k)
            })
            .collect();
        (index, per_name)
    }

    /// The policy rate expected at each close `1..=horizon` sessions after
    /// the close whose day count is `day`, meeting by meeting. The next
    /// meeting moves the rate by the change the meeting function makes on the
    /// economy as published (the shadow `policy_anticipation` prices), less
    /// `forecast_policy_shadow_discount` of it. Each meeting after it repeats
    /// `forecast_policy_persistence` of the one before's shadow-driven
    /// change, and every meeting closes `forecast_policy_reversion` of the
    /// gap to `forecast_policy_neutral`. Meetings fall on the calendar's next
    /// date and then at the mean of the ladder's normal cadence. The ladder
    /// is discrete, so these three are a projection fitted on the model's own
    /// held-out histories rather than an iteration of its law.
    /// The day count the next scheduled meeting is held at, for a forecast
    /// taken for `day`: the first close whose timestamp reaches the date on
    /// the calendar, and no earlier than `day + 1`. Its decision is the rate
    /// the close of session `first - 1` sets.
    fn next_meeting_day(&self, day: i64) -> i64 {
        let minutes = 24 * 60;
        let next = self.central_bank.next_meeting_date;
        let first = if next <= 0 { day + 1 } else { (next + minutes - 1) / minutes };
        if first < day + 1 {
            day + 1
        } else {
            first
        }
    }

    /// The session, counted from 0, whose close holds the next scheduled
    /// meeting, as the standing forecast's policy path reads it: `None`
    /// without a forecast (`forecast_horizon_sessions`).
    pub fn next_meeting_session(&self) -> Option<i64> {
        let f = self.forecast()?;
        Some(self.next_meeting_day(f.day) - 1)
    }

    /// The next meeting's change as the economy as published implies it and
    /// as the Fed's own reading (the economy the meeting reads) does,
    /// percentage points, on the standing forecast's close: the two silent
    /// shadows `policy_anticipation_guidance` blends, `(published, own)`.
    /// `None` without a forecast (`forecast_horizon_sessions`). No stream is
    /// touched and nothing is written.
    pub fn policy_shadows(&self) -> Option<(f64, f64)> {
        let f = self.forecast()?;
        let request = Self::meeting_request(f.day);
        let options = self.policy_options(false);
        Some((self.shadow_published_change(&request, &options), self.shadow_own_change(&request, &options)))
    }

    /// The request a shadow meeting for the close after `day` reads.
    fn meeting_request(day: i64) -> DayAdvanceRequest<'static> {
        DayAdvanceRequest {
            volatility: 1.0,
            active_shocks: &[],
            market_return_pct: 0.0,
            game_day: day + 1,
            timestamp: (day + 1) * 24 * 60,
        }
    }

    /// The next meeting's change as the market sees it through the Fed's
    /// guidance (`policy_guidance_signal_sd`): the Fed's own reading of the
    /// meeting held at `meeting`, `reading`, plus the meeting's signal noise,
    /// and the posterior mean over the decisions it can make, the 25 bp grid
    /// around the published shadow `published`, weighted by
    /// [`GUIDANCE_PRIOR`]. Percentage points.
    fn guidance_posterior(&self, reading: f64, published: f64, meeting: i64) -> f64 {
        let sd = self.params.policy_guidance_signal_sd / 100.0;
        let z = crate::rng::GameRng::keyed(
            crate::rng::guidance_signal_key(self.root_seed),
            crate::rng::GUIDANCE_SIGNAL_TAG as u64,
            meeting as u64,
        )
        .next_normal();
        let signal = reading + sd * z;
        let r0 = self.economy.federal_funds_rate;
        let mut points = [(0.0, 0.0); GUIDANCE_PRIOR.len()];
        let mut top = f64::NEG_INFINITY;
        for (point, (steps, weight)) in points.iter_mut().zip(GUIDANCE_PRIOR.iter()) {
            let change = crate::mathx::clamp(r0 + published + GUIDANCE_GRID * steps, 0.0, 8.0) - r0;
            let x = (signal - change) / sd;
            let log = crate::mathx::log(*weight) - 0.5 * x * x;
            *point = (change, log);
            top = crate::mathx::max(top, log);
        }
        let (mut sum, mut total) = (0.0, 0.0);
        for (change, log) in points {
            let w = crate::mathx::exp(log - top);
            sum += w * change;
            total += w;
        }
        sum / total
    }

    fn forecast_policy_path(&self, day: i64, horizon: usize) -> Vec<f64> {
        let p = &self.params;
        let r0 = self.economy.federal_funds_rate;
        let request = Self::meeting_request(day);
        let options = self.policy_options(false);
        let first = self.next_meeting_day(day);
        // THE GUIDANCE'S SIGNAL (`policy_guidance_signal_sd`): the Fed's own
        // reading reaches the price through a noisy signal, so its share q is
        // the posterior mean over the grid, not the reading. On a meeting's
        // eve the reading is the decision the eve holds, and the meeting is
        // priced at the market's expectation of it whole. A branch: at 0.0
        // the shadow is the blend as it stood.
        let (shadow, held) = if p.policy_guidance_signal_sd != 0.0 {
            let published = self.shadow_published_change(&request, &options);
            let eve = if first == day + 1 { self.eve_decision } else { None };
            let reading = match eve {
                Some(decision) => decision,
                None => self.shadow_own_change(&request, &options),
            };
            let q = p.policy_anticipation_guidance;
            let seen = (1.0 - q) * published + q * self.guidance_posterior(reading, published, first);
            (seen, eve.map(|_| seen))
        } else {
            (self.shadow_meeting_change(&request, &options), None)
        };
        // The normal cadence is 42 to 55 calendar days, uniform.
        let cal = self.macro_calendar();
        let mut cadence = 0.0;
        for j in 0..14 {
            cadence += cal.scale_days(42 + j) as f64;
        }
        let cadence = cadence / 14.0;
        let last = day + horizon as i64;
        let mut path = vec![r0; horizon];
        let mut rate = r0;
        let mut push = (1.0 - p.forecast_policy_shadow_discount) * shadow;
        let mut j = 0;
        loop {
            let at = first + (j as f64 * cadence).round() as i64;
            if at > last {
                break;
            }
            let mut change = push;
            if p.forecast_policy_reversion != 0.0 {
                change += p.forecast_policy_reversion * (p.forecast_policy_neutral - rate);
            }
            if j == 0 {
                if let Some(c) = held {
                    change = c;
                }
            }
            rate += change;
            for slot in path.iter_mut().skip((at - day - 1) as usize) {
                *slot = rate;
            }
            push = if p.forecast_policy_persistence != 0.0 {
                p.forecast_policy_persistence * push
            } else {
                0.0
            };
            j += 1;
        }
        path
    }

    /// The forecast for the close whose day count is `day`, on the state
    /// standing. See `ModelParams::forecast_horizon_sessions`.
    ///
    /// Two tracks run side by side. The first iterates the laws on the
    /// expected VIX: the VIX's own path, its index variance read-back and
    /// the economy the step reads come from it. The second carries the same
    /// states for the variances the forecast reports, with each convex VIX
    /// coupling taken as its expectation over the VIX's spread about the
    /// first track (`forecast_vix_dispersion`, skewed by
    /// `forecast_vix_dispersion_skew`) and the market factor's return
    /// memory's multiplier as its expectation over the memory's own spread.
    /// The first track's VIX is the state's expectation; the published VIX
    /// reported is the published quote's expectation over the same spread
    /// ([`Engine::forecast_published_vix`]).
    fn compute_forecast(&self, day: i64) -> Forecast {
        let p = &self.params;
        let horizon = p.forecast_horizon_sessions as usize;
        let phases = crate::economy::cycle::phase_cycle();
        let exits = self.forecast_exit_rates();
        let mut belief = self.forecast_belief();

        // The economy as public: the believed phase, the published growth,
        // and a phase old enough that the step takes no phase-change shock
        // it cannot know of.
        let mut econ = self.economy.clone();
        let mut best = 0;
        for j in 1..5 {
            if belief[j] > belief[best] {
                best = j;
            }
        }
        econ.cycle_phase = phases[best];
        econ.gdp_growth = self.published_gdp_growth();
        if econ.months_in_current_phase < 1.0 {
            econ.months_in_current_phase = 1.0;
        }

        let names = self.forecast_names();
        let phase_regimes = self.forecast_phase_regimes();
        let mut garch: Vec<f64> = names.iter().map(|n| n.name.garch_variance).collect();
        let mut garch_e = garch.clone();
        let mut sector_states: Vec<f64> = if self.sector_state_on() {
            self.sector_variance.iter().map(|s| if *s > 0.0 { *s } else { 1.0 }).collect()
        } else {
            Vec::new()
        };
        let mut idio: Vec<f64> = if self.idio_state_on() {
            names.iter().map(|n| self.idio_ratio_at(n.slot)).collect()
        } else {
            Vec::new()
        };
        let mut mv = self.market_vol;
        let mut mv_e = self.market_vol;
        // Each track's factor variance after an up and a down day; known
        // (and one) at the close the forecast starts from.
        let mut faces = (mv.variance(), mv.variance());
        let mut faces_e = faces;
        let mut slow = self.vix_anchor_slow;
        let mut stress = self.vix_stress_memory;
        let mut fear = self.vix_fear;
        // The host's premium on the VIX target, which the next close reads
        // as it stands, fading on its half-life a close at a time; the floor
        // is held (`daily_inputs` carries it).
        let mut premium = self.vix_target_premium;
        let premium_fade = if self.vix_target_premium_half_life != 0.0 {
            crate::mathx::pow(0.5, 1.0 / self.vix_target_premium_half_life)
        } else {
            1.0
        };
        let mut universe_stress = self.universe_stress;
        let mut down = Some(self.market_vol.prev_day_down());
        let (mut var_oil, mut var_inventory) = (0.0, 0.0);
        // Oil's long factor (`oil_target_drift_sd`): the mean and variance of
        // its log level, so the step reads the log of the level's
        // expectation, `m + v / 2`, which stays where it is with no pull.
        let (mut drift_mean, mut drift_var) = (self.oil_target_drift, 0.0);
        // `oil_forecast_clamp`: the level carriers' log variance about the
        // expected path (the long factor's and the dollar's), and the
        // dollar's own variance, for the clamped expectation the forecast
        // publishes; `oil_forecast_opec_spread` reads them too. Nothing is
        // read with both switches off.
        let (mut carry_var, mut usd_var) = (0.0f64, 0.0f64);
        // Under the long factor a bound's truncation moves the factor, so the
        // expected truncation at one horizon joins the factor's expected path
        // at the next (`oil_expected_log_truncation`).
        let (mut absorb_pending, mut absorb_so_far) = (0.0f64, 0.0f64);
        // `oil_forecast_cycle_growth`: growth's expectation over the cycle's
        // phases, for oil's block, rather than the believed phase held.
        let mut cycle = if p.oil_forecast_cycle_growth != 0.0 {
            Some(CycleGrowth::new(self, &belief, &econ, day))
        } else {
            None
        };
        let policy = self.forecast_policy_path(day, horizon);
        let k_curve = intraday_variance_factor();
        // The spread's shape (`forecast_vix_dispersion_skew`), and the stress
        // memory's sd about its path: the memory's own average of the log
        // VIX's spread, moving with it.
        let shape = if p.forecast_vix_dispersion != 0.0 {
            spread_shape(p.forecast_vix_dispersion_skew)
        } else {
            Vec::new()
        };
        let skewed = p.forecast_vix_dispersion != 0.0 && p.forecast_vix_dispersion_skew != 0.0;
        let mut stress_sd = 0.0;
        // The VIX's slow level as the published VIX's log average nowcasts
        // it (`forecast_vix_level_nowcast`); 0.0 with the dial off.
        let level_now = self.vix_level_nowcast();

        let mut out = Forecast {
            day,
            vix: Vec::with_capacity(horizon),
            index_variance: Vec::with_capacity(horizon),
            policy_rate: policy.clone(),
            oil: Vec::with_capacity(horizon),
            name_variance: vec![Vec::new(); self.companies.len()],
        };
        for n in &names {
            out.name_variance[n.slot] = Vec::with_capacity(horizon);
        }

        for (step, &rate) in policy.iter().enumerate() {
            let vix = econ.vix;
            // The log VIX's variance about its expectation after `step`
            // closes, and the VIX whose square is the expected square.
            let spread = self.forecast_vix_log_variance(step);
            // Off zero skew, the spread's nodes: the log of V / vix at each.
            let nodes = if skewed && spread > 0.0 { spread_at(&shape, spread) } else { Vec::new() };
            let vix_sq = if !nodes.is_empty() {
                let second: f64 = nodes.iter().map(|(x, w)| w * crate::mathx::exp(2.0 * x)).sum();
                vix * crate::mathx::sqrt(second)
            } else if spread > 0.0 {
                vix * crate::mathx::exp(0.5 * spread)
            } else {
                vix
            };
            let lev = self.forecast_leverage_factor(step);
            let mut econ_sq = econ.clone();
            econ_sq.vix = vix_sq;

            // The session t + step + 1: the variances the forecast reports,
            // on the expectation track; and the index's variance on the
            // first track, which sizes the session's return the VIX reads.
            let (v_report, per_name) = self.forecast_identity(
                &names, &garch_e, &sector_states, &idio, (faces_e.0 * lev, faces_e.1 * lev), vix_sq,
                universe_stress, down, &econ_sq);
            out.index_variance.push(v_report);
            for (n, v) in names.iter().zip(per_name.iter()) {
                out.name_variance[n.slot].push(*v);
            }
            let (v_session, _) = self.forecast_identity(
                &names, &garch, &sector_states, &idio, faces, vix, universe_stress, down, &econ);

            // Its close. The factor on both tracks: the expected update over
            // the day's factor (four nodes) and the believed phase's regimes.
            let regimes = self.forecast_regimes(&belief, &phase_regimes);
            let (next_mv, next_faces) = self.forecast_factor_close(&mv, &regimes, vix, 0.0, &[]);
            let (next_mv_e, next_faces_e) = self.forecast_factor_close(&mv_e, &regimes, vix, spread, &nodes);

            // Each name's GARCH on both tracks: the expected GJR step on the
            // session's noise.
            if p.garch_cascade_components < 1.0 {
                let coupled = |x: f64| {
                    if p.garch_vix_coupling == 0.0 {
                        1.0
                    } else {
                        let cpl = p.garch_vix_coupling;
                        1.0 - cpl + crate::market::garch::vix_coupled_response(p, cpl, x / self.vix_anchor)
                    }
                };
                let coupled_e = if p.garch_vix_coupling == 0.0 {
                    1.0
                } else if !nodes.is_empty() {
                    spread_expect(&nodes, vix, coupled)
                } else {
                    lognormal_expect(vix, spread, coupled)
                };
                let target = crate::market::tick::sector_sigma_at(p, &econ, self.vix_anchor);
                let target_e = crate::market::tick::sector_sigma_at(p, &econ_sq, self.vix_anchor);
                for (i, n) in names.iter().enumerate() {
                    let state = |t: f64| {
                        if self.sector_state_on() {
                            t * crate::mathx::sqrt(sector_states.get(n.name.sector).copied().unwrap_or(1.0))
                        } else {
                            t
                        }
                    };
                    let r = idio.get(i).copied().unwrap_or(1.0);
                    let base = n.sector_base * coupled(vix);
                    let base_e = if p.garch_vix_coupling == 0.0 {
                        n.sector_base
                    } else {
                        n.sector_base * coupled_e
                    };
                    garch[i] = self.forecast_garch_step(
                        n, garch[i], mv.variance(), state(target), r, base, k_curve);
                    garch_e[i] = self.forecast_garch_step(
                        n, garch_e[i], mv_e.variance() * lev, state(target_e), r, base_e, k_curve);
                }
            }
            // The sector and own-variance ratios revert to one.
            let (ga, gc) = (p.garch_floor_multiple, p.garch_ceiling_multiple);
            if self.sector_state_on() {
                let (a, b) = (p.sector_vol_alpha, p.sector_vol_beta);
                for s in sector_states.iter_mut() {
                    *s = crate::mathx::max(crate::mathx::min((1.0 - a - b) + (a + b) * *s, gc), ga);
                }
            }
            if self.idio_state_on() {
                let (a, b) = (p.idio_vol_alpha, p.idio_vol_beta);
                for r in idio.iter_mut() {
                    *r = crate::mathx::max(crate::mathx::min((1.0 - a - b) + (a + b) * *r, gc), ga);
                }
            }
            // The universe's stress at the close's VIX.
            let from_vix = if vix > p.crisis_vix_threshold { vix - p.crisis_vix_threshold } else { 0.0 };
            let mut from_regime = 0.0;
            for (j, w) in belief.iter().enumerate() {
                from_regime += w * p.regime_stress_points * phases[j].stress_intensity();
            }
            universe_stress = crate::mathx::max(
                crate::mathx::max(from_vix, from_regime), p.universe_stress_decay * universe_stress);
            mv = next_mv;
            mv_e = next_mv_e;
            faces = next_faces;
            faces_e = next_faces_e;
            down = None;

            // The step's read-back, at the VIX before the step. The fast
            // return memory (`market_vol_leverage_fast`) at its window's mean,
            // as the close's read-back takes it; the faces themselves with
            // that dial off.
            let faces_vix = if p.market_vol_leverage_fast == 0.0 {
                faces
            } else {
                let q = mv.vix_window_ratio(p);
                (faces.0 * q, faces.1 * q)
            };
            let (v_next, _) = self.forecast_identity(
                &names, &garch, &sector_states, &idio, faces_vix, vix, universe_stress, None, &econ);
            let mut scale_bar = 0.0;
            for &(w, _, scale) in &regimes {
                scale_bar += w * scale;
            }
            let implied = vix_from_variance(p.vix_variance_premium, v_next);
            if p.vix_anchor_memory != 0.0 && p.vix_level_identity != 0.0 {
                let anchor = self.vix_anchor * scale_bar;
                let anchor = if p.vix_anchor_centre != 0.0 {
                    anchor * crate::mathx::exp(-p.vix_anchor_centre)
                } else {
                    anchor
                };
                if implied > 0.0 && anchor > 0.0 {
                    let h = p.vix_anchor_memory;
                    let d = crate::mathx::log(implied / anchor);
                    slow = (1.0 - h) * slow + h * d;
                    if p.vix_stress_premium != 0.0 {
                        let hs = p.vix_stress_memory_rate();
                        stress = (1.0 - hs) * stress + hs * d;
                        let after = self.forecast_vix_log_variance(step + 1);
                        stress_sd = (1.0 - hs) * stress_sd + hs * crate::mathx::sqrt(after);
                    }
                }
            }

            // The VIX: the step on the session's return, integrated by the
            // eight nodes, with its draws at their means and the jump's mean.
            let next_day = day + step as i64 + 1;
            let request = DayAdvanceRequest {
                volatility: 1.0,
                active_shocks: &[],
                market_return_pct: 0.0,
                game_day: next_day,
                timestamp: next_day * 24 * 60,
            };
            let mut inputs = self.daily_inputs(&request, 0.0, v_next, slow, scale_bar);
            inputs.vix_anchor_level = self.vix_anchor * scale_bar;
            // The slow level's nowcast (`forecast_vix_level_nowcast`): the
            // level's expectation at this close multiplies the anchor, as the
            // level itself does; the implied VIX below takes it too.
            let level = if level_now != 0.0 {
                crate::mathx::exp(level_now * crate::mathx::pow(p.vix_level_persistence, (step + 1) as f64))
            } else {
                1.0
            };
            if level_now != 0.0 {
                inputs.vix_anchor_level *= level;
            }
            inputs.vix_target_premium = premium;
            if premium != 0.0 {
                premium *= premium_fade;
                if premium.abs() < crate::engine::VIX_TARGET_PREMIUM_CLEARED_UNDER {
                    premium = 0.0;
                }
            }
            inputs.vix_implied_from_market = if p.vix_level_identity != 0.0 {
                if level_now != 0.0 { implied * level } else { implied }
            } else if p.vix_realised_vol_weight == 0.0 {
                0.0
            } else {
                p.market_vol_vix_anchor * mv.sigma_daily() / p.market_factor_sigma
            };
            // The fear memory, advanced on the step's inputs at the expected
            // VIX, as the close advances it (`vix_fear_uptake`).
            if p.vix_fear_uptake != 0.0 {
                fear = crate::economy::daily::advance_vix_fear(fear, &inputs, vix, p.vix_fear_half_life);
                inputs.vix_fear = fear;
            }
            let sd_ret = 100.0 * crate::mathx::sqrt(crate::mathx::max(0.0, v_session));
            let mut vix_next = 0.0;
            for &z in live_mark_nodes().iter() {
                let mut node = inputs;
                node.market_day_return_pct = z * sd_ret;
                let stepped = crate::economy::daily::vix_close(
                    &econ, &node, 0.0, &mut crate::economy::daily::MeanDraws);
                vix_next += stepped / 8.0 + self.forecast_vix_jump_mean(&econ, &node) / 8.0;
            }

            // The rest of the economy: the step itself with its draws at
            // their means, then the VIX above, the policy path, and oil's
            // expected inventory push, OPEC decision and the dollar's
            // expected safe-haven drift over the VIX's spread.
            let opec_day = econ.oil_last_opec_day;
            let oil_before = econ.oil_price;
            let kappa = p.oil_inventory_reversion;
            let sd_before = crate::mathx::sqrt(var_inventory);
            // Inventory's noise variance a session: the standing 0.25, or
            // `oil_inventory_noise_sd` squared. A branch, so the shipped
            // arithmetic is kept to the bit.
            let inventory_var = if p.oil_inventory_noise_sd == 0.0 {
                0.25
            } else {
                p.oil_inventory_noise_sd * p.oil_inventory_noise_sd
            };
            let var_after = (1.0 - kappa) * (1.0 - kappa) * var_inventory + inventory_var;
            // The convenience premium (`oil_convenience_yield`) is convex in
            // inventory, so the step reads its expectation over the
            // inventory's spread, before the step and after it, rather than
            // its value on the expected path: the expected price is then the
            // premium-free path times the premium's expectation.
            if p.oil_convenience_yield != 0.0 {
                inputs.oil_convenience_inventory_var_before = var_inventory;
                inputs.oil_convenience_inventory_var = var_after;
            }
            if p.oil_target_drift_sd != 0.0 {
                let sigma = p.oil_target_drift_sd;
                let pull = 1.0 - p.oil_target_drift_reversion;
                let before = drift_mean + 0.5 * drift_var;
                drift_mean = pull * drift_mean - 0.5 * sigma * sigma + absorb_pending;
                absorb_pending = 0.0;
                drift_var = pull * pull * drift_var + sigma * sigma;
                inputs.oil_target_drift = Some((before, drift_mean + 0.5 * drift_var));
            }
            if let Some(c) = &cycle {
                inputs.oil_growth = Some(c.growth());
            }
            let mut next = update_economy_daily(&econ, &inputs, &mut crate::economy::daily::MeanDraws);
            if let Some(c) = cycle.as_mut() {
                c.step(&inputs, request.volatility, v_session, request.timestamp);
            }
            var_inventory = var_after;
            let sd_inv = crate::mathx::sqrt(var_inventory);
            let gap = |level: f64, sd: f64| {
                crate::economy::daily::inventory_pressure_expected(level, sd)
                    - crate::economy::daily::inventory_pressure(level)
            };
            // The step read the pressure at the inventory's expected path;
            // its expectation over inventory's spread is what the price
            // expects. Under `oil_inventory_level_gain` the step read the
            // change in the pressure, so the gap is the change in the gaps.
            let mut pushed = if p.oil_inventory_level_gain == 0.0 {
                gap(next.oil_inventory_level, sd_inv)
            } else {
                p.oil_inventory_level_gain
                    * (gap(next.oil_inventory_level, sd_inv) - gap(econ.oil_inventory_level, sd_before))
            };
            next.oil_price += pushed;
            if next.oil_last_opec_day != opec_day {
                // `oil_forecast_opec_spread`: the decision over the price's
                // whole spread at the step's start (the clamp's log variance,
                // every part as it stood before this step), read lognormal.
                // A branch, so 0.0 keeps the short factor's normal to the bit.
                let opec = if p.oil_forecast_opec_spread != 0.0 {
                    let log_var = if oil_before > 0.0 { var_oil / (oil_before * oil_before) } else { 0.0 }
                        + carry_var
                        + p.oil_convenience_yield * p.oil_convenience_yield * (sd_before * sd_before) / (50.0 * 50.0)
                        + p.oil_dollar_elasticity * p.oil_dollar_elasticity * usd_var / (100.0 * 100.0);
                    crate::economy::daily::opec_expected_impact_lognormal(oil_before, log_var, p.oil_opec_symmetry)
                } else {
                    crate::economy::daily::opec_expected_impact(
                        oil_before, crate::mathx::sqrt(var_oil), p.oil_opec_symmetry)
                };
                next.oil_price += opec;
                pushed += opec;
            }
            // Under `oil_pushes_in_target` the expected pushes join the part
            // of the price that decays at 0.03, as the step's own do.
            if p.oil_pushes_in_target != 0.0 {
                next.oil_push_level += pushed;
            }
            // Oil's own spread about its expectation, for the OPEC rule's
            // expected decision: the law's reversion and noise.
            // A branch, so the standing law keeps its arithmetic to the bit.
            var_oil = if p.oil_mean_reversion == 0.0 && p.oil_noise_sd == 0.0 && p.oil_noise_log_sd == 0.0 {
                0.97 * 0.97 * var_oil + 4.0
            } else {
                let keep = if p.oil_mean_reversion == 0.0 { 0.97 } else { 1.0 - p.oil_mean_reversion };
                // Under `oil_noise_log_sd` the noise is a share of the
                // price, read at the price the step started from.
                let noise = if p.oil_noise_log_sd != 0.0 {
                    p.oil_noise_log_sd * oil_before
                } else if p.oil_noise_sd == 0.0 {
                    2.0
                } else {
                    p.oil_noise_sd
                };
                keep * keep * var_oil + noise * noise
            };
            if spread > 0.0 {
                let k = p.usd_crisis_vix_threshold;
                let at_mean = if vix > k { vix - k } else { 0.0 };
                let excess = if !nodes.is_empty() {
                    spread_expect(&nodes, vix, |x| if x > k { x - k } else { 0.0 })
                } else {
                    lognormal_excess(vix, spread, k)
                };
                let haven = crate::economy::daily::usd_safe_haven_gain(p.usd_safe_haven_gain) * (excess - at_mean);
                // Under `oil_dollar_elasticity` the law rescales the oil
                // price's reverting part by the day's whole dollar move, the
                // bid included, so the expected bid's excess over the bid at
                // the expected VIX moves oil by the same ratio.
                if p.oil_dollar_elasticity != 0.0 && next.usd_index > 0.0 {
                    let push = if p.oil_pushes_in_target != 0.0 { next.oil_push_level } else { 0.0 };
                    let ratio = crate::mathx::pow((next.usd_index + haven) / next.usd_index, -p.oil_dollar_elasticity);
                    next.oil_price = (next.oil_price - push) * ratio + push;
                }
                next.usd_index += haven;
                // Under `usd_mean_reversion` the expected bid joins the part of
                // the dollar that decays at 0.02, as the step's own does.
                if p.usd_mean_reversion != 0.0 {
                    next.usd_haven_level += haven;
                }
            }
            next.vix = vix_next;
            next.federal_funds_rate = rate;
            econ = next;

            out.vix.push(self.forecast_published_vix(&shape, econ.vix, stress, stress_sd, step + 1));
            // `oil_forecast_clamp`: publish the expectation truncated at the
            // clamps over the price's spread; the recursion keeps stepping
            // the unclamped expectation, so the clamp is read once.
            // The carriers' variances step under the clamp and under
            // `oil_forecast_opec_spread`, which reads them at the next step.
            if p.oil_forecast_clamp != 0.0 || p.oil_forecast_opec_spread != 0.0 {
                let sigma = p.oil_target_drift_sd;
                let pull = 1.0 - p.oil_target_drift_reversion;
                carry_var = pull * pull * carry_var + sigma * sigma;
                // The dollar's own spread, on its law: the standing 0.02 and
                // 0.3, or `usd_mean_reversion` and `usd_noise_sd`.
                let usd_keep = 1.0 - if p.usd_mean_reversion == 0.0 { 0.02 } else { p.usd_mean_reversion };
                let usd_noise = if p.usd_noise_sd == 0.0 { 0.3 } else { p.usd_noise_sd };
                usd_var = usd_keep * usd_keep * usd_var + usd_noise * usd_noise;
            }
            if p.oil_forecast_clamp != 0.0 {
                let s = econ.oil_price;
                let kappa_c = p.oil_convenience_yield;
                let inventory_log_var = var_inventory / (50.0 * 50.0);
                let log_var = carry_var
                    + kappa_c * kappa_c * inventory_log_var
                    + p.oil_dollar_elasticity * p.oil_dollar_elasticity * usd_var / (100.0 * 100.0);
                let bounds = crate::economy::daily::oil_bounds(p.oil_price_floor, p.oil_price_ceiling);
                // The price at this horizon rests on a bound it reaches; under
                // the long factor that truncation then moves the factor, which
                // the next step's expected path carries.
                // Read as lognormal: the price's spread is right-skewed. Its log
                // variance is the short factor's (in dollars, over the price)
                // and the carriers'. The marginal's expected truncation at a
                // horizon is the whole of it to that horizon, so the factor's
                // path takes its change.
                let log_total = if s > 0.0 { var_oil / (s * s) + log_var } else { 0.0 };
                if p.oil_target_drift_sd != 0.0 {
                    let to_here = crate::economy::daily::oil_lognormal_log_truncation(s, log_total, bounds);
                    absorb_pending = to_here - absorb_so_far;
                    absorb_so_far = to_here;
                }
                out.oil.push(crate::economy::daily::oil_lognormal_censored(s, log_total, bounds));
            } else {
                out.oil.push(econ.oil_price);
            }

            // The belief, one session on.
            let mut pred = [0.0; 5];
            for (j, (&lam, &pj)) in exits.iter().zip(belief.iter()).enumerate() {
                pred[j] += pj * (1.0 - lam);
                pred[(j + 1) % 5] += pj * lam;
            }
            belief = pred;
        }
        out
    }

    /// The published VIX expected `closes` closes ahead, from the first
    /// track's VIX `vix` and stress memory `stress`. The quote is the state
    /// times `exp(premium)`, the premium a capped hinge in the stress memory
    /// (`vix_stress_premium`), so its expectation is not the quote at the
    /// track's state. With `forecast_vix_dispersion` set it is taken over the
    /// VIX's spread after these closes ([`spread_shape`], [`spread_at`]),
    /// the first track's VIX being the state's mean, and the stress memory
    /// at each node `stress_sd` of its own sds from `stress`, the same number
    /// as the VIX's: the memory is an average of the VIX's log excursions,
    /// so it moves with them, and `stress_sd` is that average of the VIX's
    /// spread. With the dial or the premium at 0.0 it is the quote at the
    /// track's state.
    fn forecast_published_vix(&self, shape: &[(f64, f64)], vix: f64, stress: f64, stress_sd: f64,
                              closes: usize) -> f64 {
        let var = self.forecast_vix_log_variance(closes);
        if self.params.vix_stress_premium == 0.0 || shape.is_empty() || !(var > 0.0) {
            return self.published_vix_at(vix, stress);
        }
        let nodes = spread_at(shape, var);
        let mut total = 0.0;
        for (&(x, w), &(z, _)) in nodes.iter().zip(shape.iter()) {
            total += w * self.published_vix_at(vix * crate::mathx::exp(x), stress + stress_sd * z);
        }
        total
    }

    /// The log VIX's variance about the forecast after `closes` closes:
    /// `sd^2 * (1 - 0.5^(2 closes / half-life))` under
    /// `forecast_vix_dispersion`, its long-horizon level from the first close
    /// at a half-life of 0.0, and 0.0 with the dial off.
    fn forecast_vix_log_variance(&self, closes: usize) -> f64 {
        let p = &self.params;
        let sd = p.forecast_vix_dispersion;
        if sd == 0.0 || closes == 0 {
            return 0.0;
        }
        let hl = p.forecast_vix_dispersion_half_life;
        let share = if hl == 0.0 {
            1.0
        } else {
            1.0 - crate::mathx::pow(0.5, 2.0 * closes as f64 / hl)
        };
        sd * sd * share
    }

    /// The expected over the typical multiplier the market factor's return
    /// memory (`market_vol_leverage`) applies `closes` closes on: the memory
    /// is a sum of the days' standardised factors, so its spread about its
    /// expectation grows to its stationary spread `s` as `1 - phi^(2 closes)`,
    /// and `E[exp(k l)]` is `exp(k E[l] + k^2 var / 2)`. 1.0 with the memory
    /// off.
    fn forecast_leverage_factor(&self, closes: usize) -> f64 {
        let p = &self.params;
        let slower = if p.market_vol_leverage == 0.0 || closes == 0 {
            1.0
        } else {
            let k = p.market_vol_leverage;
            let phi = crate::mathx::pow(0.5, 1.0 / p.market_vol_leverage_half_life);
            let a = p.market_vol_leverage_down;
            let var_u = 0.5 * (1.0 + (1.0 - a) * (1.0 - a)) - a * a / (2.0 * std::f64::consts::PI);
            let s2 = (1.0 - phi) / (1.0 + phi) * var_u;
            let grown = 1.0 - crate::mathx::pow(phi, 2.0 * closes as f64);
            crate::mathx::exp(0.5 * k * k * s2 * grown)
        };
        // The fast return memory (`market_vol_leverage_fast`) the same way,
        // at its own half-life; the slower memory's factor alone with it off.
        if p.market_vol_leverage_fast == 0.0 || closes == 0 {
            return slower;
        }
        let k = p.market_vol_leverage_fast;
        let phi = crate::mathx::pow(0.5, 1.0 / p.market_vol_leverage_fast_half_life);
        let s2 = crate::market::factor_vol::MarketVarianceState::leverage_fast_s2(p);
        let grown = 1.0 - crate::mathx::pow(phi, 2.0 * closes as f64);
        slower * crate::mathx::exp(0.5 * k * k * s2 * grown)
    }

    /// One close of the market factor's variance state in expectation: the
    /// update over the four nodes of the day's factor and each believed
    /// regime, at the VIX `vix`. With `spread` off zero the target's VIX
    /// coupling is taken as its expectation over the VIX's spread of that log
    /// variance (a lognormal, or `nodes` when the spread is skewed), by
    /// scaling the regime's level by the ratio of the expected coupling to
    /// the coupling at the expected VIX.
    fn forecast_factor_close(
        &self,
        mv: &MarketVarianceState,
        regimes: &[(f64, f64, f64)],
        vix: f64,
        spread: f64,
        nodes: &[(f64, f64)],
    ) -> (MarketVarianceState, (f64, f64)) {
        let p = &self.params;
        let sd_f = crate::mathx::sqrt(crate::mathx::max(0.0, mv.variance()));
        // The VIX the close's target reads: the smoothed one, as
        // `close_day_scaled` steps it.
        let smoothed = if p.market_vol_vix_smooth == 0.0 {
            vix
        } else {
            let alpha = 2.0 / (p.market_vol_vix_smooth + 1.0);
            let prev = mv.snapshot().5;
            let prev = if prev < 0.0 { vix } else { prev };
            prev + alpha * (vix - prev)
        };
        let c = p.market_vol_vix_coupling;
        let c_slow = if p.market_vol_slow_vix_damp == 0.0 { c } else { c * (1.0 - p.market_vol_slow_vix_damp) };
        let w_slow = p.market_vol_slow_weight;
        let (mut variance, mut fast, mut slow_part, mut lev) = (0.0, 0.0, 0.0, 0.0);
        let mut lev_fast = 0.0;
        let (mut up, mut down) = (0.0, 0.0);
        let mut smoothed_out = -1.0;
        for &(w, level, scale) in regimes {
            let denominator = self.vix_anchor * scale;
            let level = if spread > 0.0 && c != 0.0 {
                let at = crate::market::factor_vol::vix_response(p, smoothed / denominator);
                let response = |x: f64| crate::market::factor_vol::vix_response(p, x / denominator);
                let expected = if nodes.is_empty() {
                    lognormal_expect(smoothed, spread, response)
                } else {
                    spread_expect(nodes, smoothed, response)
                };
                let ratio_fast = (1.0 - c + c * expected) / (1.0 - c + c * at);
                let ratio_slow = (1.0 - c_slow + c_slow * expected) / (1.0 - c_slow + c_slow * at);
                level * ((1.0 - w_slow) * ratio_fast + w_slow * ratio_slow)
            } else {
                level
            };
            for &z in FACTOR_NODES.iter() {
                let mut s = mv.with_day_factor(z * sd_f);
                let _ = s.close_day_scaled(p, denominator, vix, level);
                let snap = s.snapshot();
                variance += w / 4.0 * snap.0;
                // Each face is the two nodes of its sign.
                if z < 0.0 {
                    down += w / 2.0 * snap.0;
                } else {
                    up += w / 2.0 * snap.0;
                }
                fast += w / 4.0 * snap.2;
                slow_part += w / 4.0 * snap.3;
                // The smoothed VIX reads the VIX alone, the same on every node.
                smoothed_out = snap.5;
                lev += w / 4.0 * s.leverage_memory();
                lev_fast += w / 4.0 * s.leverage_fast_memory();
            }
        }
        let mut next = MarketVarianceState::restore_with_components(
            variance, 0.0, fast, slow_part, 0.0, smoothed_out);
        next.set_leverage_memory(lev);
        // 0.0 with `market_vol_leverage_fast` off, which the field already is.
        next.set_leverage_fast_memory(lev_fast);
        (next, (up, down))
    }

    /// One name's GARCH variance one close on, in expectation: the GJR
    /// update averaged over the session's noise at plus and minus its sd,
    /// which is exact for the update's quadratic and its sign asymmetry.
    #[allow(clippy::too_many_arguments)]
    fn forecast_garch_step(
        &self,
        n: &ForecastName,
        h: f64,
        factor_variance: f64,
        sector_sigma: f64,
        idio_ratio: f64,
        base: f64,
        k: f64,
    ) -> f64 {
        let p = &self.params;
        let noise = name_noise_variance_scaled(
            p, &n.constants, n.name.beta, h, factor_variance, sector_sigma, idio_ratio, k);
        let eps = crate::mathx::sqrt(crate::mathx::max(0.0, noise));
        let up = crate::market::garch::update_garch_variance_for(p, n.garch_beta, h, eps, base);
        let dn = crate::market::garch::update_garch_variance_for(p, n.garch_beta, h, -eps, base);
        0.5 * (up + dn)
    }

    /// The mean of the VIX's own jump on the step `inputs` describes:
    /// its arrival rate times its mean size, as `vix_and_yields` draws it.
    fn forecast_vix_jump_mean(&self, economy: &EconomyState, inputs: &DailyInputs) -> f64 {
        if inputs.vix_jump_intensity == 0.0 && inputs.vix_jump_return_intensity == 0.0 {
            return 0.0;
        }
        let clamp = inputs.vix_return_clamp;
        let ret = crate::mathx::max(-clamp, crate::mathx::min(clamp, inputs.market_day_return_pct));
        let rate = if inputs.vix_jump_return_intensity != 0.0 {
            inputs.vix_jump_intensity + inputs.vix_jump_return_intensity * crate::mathx::max(0.0, -ret)
        } else {
            inputs.vix_jump_intensity
        };
        let innovation_on = inputs.vix_innovation_sigma != 0.0 || inputs.vix_innovation_return_sigma != 0.0;
        let size = if inputs.vix_jump_level_scale != 0.0 {
            let unit = if innovation_on {
                let s0 = inputs.vix_innovation_sigma;
                let sr = inputs.vix_innovation_return_sigma * ret;
                economy.vix * crate::mathx::sqrt(s0 * s0 + sr * sr)
            } else {
                economy.vix
            };
            inputs.vix_jump_level_scale * unit
        } else {
            inputs.vix_jump_scale
        };
        crate::mathx::min(1.0, rate / 252.0) * size
    }
}


/// The first day of the economy's long run for the forecast's cycle
/// (`oil_forecast_cycle_growth`), on the run's own day numbering: far past
/// any session a run reaches, so the keyed draws the run takes by day are
/// not the long run's, beside the keys and generators of its own.
const CYCLE_AGE_LAW_ORIGIN: i64 = 1 << 34;

/// The long run's warm-up and its length, in days. Precision, not a value
/// of the model: the run starts from the close's state, which the warm-up
/// forgets, and the age law is its occupancy over the rest.
const CYCLE_AGE_LAW_WARM_DAYS: i64 = 252 * 50;
const CYCLE_AGE_LAW_DAYS: i64 = 252 * 10_000;

/// Draws at the middle of their range for a projected meeting: a uniform
/// of one half and a normal of zero, as the market's shadow meeting reads.
struct MidDraws;

impl crate::rng::Rng for MidDraws {
    fn next_f64(&mut self) -> f64 {
        0.5
    }
    fn next_normal(&mut self) -> f64 {
        0.0
    }
}

impl Engine {
    /// Each phase's stationary age law under the FULL law, for the start of
    /// the forecast's cycle (`oil_forecast_cycle_growth`): the hazard and the
    /// ladder on the economy's own state, as the law transitions. There is no
    /// closed form ([`crate::economy::cycle`], the stationary law), so it is
    /// read as the occupancy of the economy's own long run: a copy of this
    /// engine run alone, as before day zero (the cycle reads
    /// `cycle_equity_hazard_opening` for the index it does not have), with
    /// every generator and counter key its own (`rng::AGE_LAW_TAG`) and its
    /// days numbered from [`CYCLE_AGE_LAW_ORIGIN`]. Per phase, `phase_cycle`
    /// order, in bins of a month: each bin's mass-weighted age in completed
    /// days and its mass. Bins under a billionth are left out. Worked out
    /// at the first forecast, from the state it stands on, once per seed and
    /// model, and kept for the process; nothing the run reads is touched.
    /// An engine restored from a snapshot works it out from the state it
    /// restores, so its forecast can differ from the original's in the long
    /// run's sampling.
    fn cycle_age_law(&self, day: i64) -> Vec<Vec<(f64, f64)>> {
        type Key = (u64, String);
        type Law = Vec<Vec<(f64, f64)>>;
        static MEMO: std::sync::Mutex<Vec<(Key, Law)>> = std::sync::Mutex::new(Vec::new());
        let key: Key = (self.root_seed, self.model_fingerprint().to_string());
        if let Some(v) = MEMO.lock().ok().and_then(|m| m.iter().find(|(k, _)| *k == key).map(|(_, v)| v.clone())) {
            return v;
        }
        let tag = crate::rng::AGE_LAW_TAG;
        let root = self.root_seed;
        let mut e = self.clone();
        e.market_rng = GameRng::surgery(root, stream::MARKET, tag);
        e.economy_rng = GameRng::surgery(root, stream::ECONOMY, tag);
        e.external_rng = GameRng::surgery(root, stream::EXTERNAL, tag);
        e.jump_rng = GameRng::surgery(root, stream::JUMPS, tag);
        e.volume_rng = GameRng::surgery(root, stream::VOLUME, tag);
        e.volume_idio_rng = GameRng::surgery(root, stream::VOLUME_IDIO, tag);
        e.news_rng = GameRng::surgery(root, stream::NEWS, tag);
        e.overnight_rng = GameRng::surgery(root, stream::OVERNIGHT, tag);
        e.market_vol_level_rng = GameRng::surgery(root, stream::MARKET_VOL_LEVEL, tag);
        e.crisis_epicentre_rng = GameRng::surgery(root, stream::CRISIS_EPICENTRE, tag);
        e.cycle_nowcast_rng = GameRng::surgery(root, stream::CYCLE_NOWCAST, tag);
        e.earnings_key = crate::rng::age_law_key(self.earnings_key);
        e.skew_noise_key = crate::rng::age_law_key(self.skew_noise_key);
        e.oil_drift_key = crate::rng::age_law_key(self.oil_drift_key);
        e.core_trend_key = crate::rng::age_law_key(self.core_trend_key);
        e.spread_noise_key = crate::rng::age_law_key(self.spread_noise_key);
        e.term_premium_key = crate::rng::age_law_key(self.term_premium_key);
        e.cycle_publication.key = crate::rng::age_law_key(self.cycle_publication.key);
        e.economy_alone = true;
        // The absolute clocks move with the days, as the burn-in's do.
        let shift = CYCLE_AGE_LAW_ORIGIN - day;
        e.central_bank.next_meeting_date += shift * 24 * 60;
        e.central_bank.last_meeting_date += shift * 24 * 60;
        e.economy.oil_last_opec_day += shift;
        let spec = self.cycle_spec();
        let phases = crate::economy::cycle::phase_cycle();
        let mut counts: Vec<Vec<f64>> = vec![Vec::new(); 5];
        for j in 1..=CYCLE_AGE_LAW_WARM_DAYS + CYCLE_AGE_LAW_DAYS {
            let d = CYCLE_AGE_LAW_ORIGIN + j;
            e.advance_day(&DayAdvanceRequest {
                volatility: 1.0,
                active_shocks: &[],
                market_return_pct: 0.0,
                game_day: d,
                timestamp: d * 24 * 60,
            });
            if j > CYCLE_AGE_LAW_WARM_DAYS {
                let k = phases.iter().position(|ph| *ph == e.economy.cycle_phase).unwrap_or(0);
                let a = crate::mathx::max(0.0, (e.economy.months_in_current_phase * spec.month_days).round()) as usize;
                if counts[k].len() <= a {
                    counts[k].resize(a + 1, 0.0);
                }
                counts[k][a] += 1.0;
            }
        }
        let month = crate::mathx::max(1.0, spec.month_days.round()) as usize;
        let mut out = Vec::with_capacity(5);
        for c in &counts {
            let total: f64 = c.iter().sum();
            let mut bins: Vec<(f64, f64)> = Vec::new();
            for chunk in c.chunks(month).enumerate() {
                let (b, ws) = chunk;
                let (mut mass, mut age) = (0.0, 0.0);
                for (i, w) in ws.iter().enumerate() {
                    mass += w;
                    age += w * (b * month + i) as f64;
                }
                if total > 0.0 && mass / total > 1e-9 {
                    bins.push((age / mass, mass / total));
                }
            }
            out.push(bins);
        }
        if let Ok(mut m) = MEMO.lock() {
            if m.len() > 8 {
                m.clear();
            }
            m.push((key, out.clone()));
        }
        out
    }
}

/// One phase's own economy in the forecast's cycle
/// (`oil_forecast_cycle_growth`): the state the law steps in that phase,
/// the central bank that meets on it, and the variance of the market's gap
/// about the state's (`spread_equity_gap`).
#[derive(Clone)]
struct CycleTrack {
    economy: EconomyState,
    bank: crate::economy::CentralBankState,
    gap_var: f64,
}

impl CycleTrack {
    /// Mass entering the phase joins its state at the mass-weighted mean:
    /// every continuous field, and the gap's variance as a mixture's. The
    /// phase, its clock, the OPEC day and the central bank are the
    /// receiving phase's.
    fn join(&mut self, w: f64, from: &CycleTrack, wf: f64) {
        let t = w + wf;
        if !(t > 0.0) {
            return;
        }
        let (a, b) = (w / t, wf / t);
        let (g0, g1) = (self.economy.spread_equity_gap, from.economy.spread_equity_gap);
        let g = a * g0 + b * g1;
        self.gap_var = a * (self.gap_var + g0 * g0) + b * (from.gap_var + g1 * g1) - g * g;
        if self.gap_var < 0.0 {
            self.gap_var = 0.0;
        }
        let x = &mut self.economy;
        let y = &from.economy;
        macro_rules! mix {
            ($($f:ident),* $(,)?) => { $( x.$f = a * x.$f + b * y.$f; )* };
        }
        mix!(federal_funds_rate, prime_rate, corporate_bond_yield, treasury_yield_10y, treasury_yield_2y,
             mortgage_rate_30y, cpi, inflation_rate, core_inflation, gdp_growth, gdp, unemployment_rate,
             jobs_created, labor_force_participation, usd_index, oil_price, gold_price, copper_price,
             housing_index, home_starts_monthly, housing_transaction_volume, long_term_unemployment_rate,
             structural_unemployment, consumer_confidence, business_confidence, fear_greed_index, vix,
             tariff_rate, trade_balance, oil_inventory_level, wage_growth, previous_day_market_return,
             rolling_market_return_30d, earnings_cycle, vix_feedback, intermeeting_return, fed_put,
             fed_put_owed, fed_put_mcap_prev, spread_equity_gap, spread_priced_gap, earnings_anticipation,
             earnings_lookthrough, unemployment_impulse, oil_push_level, usd_haven_level,
             oil_inflation_level, zlb_unmet, oil_bound_log_ratio, qe_pe_boost, qe_assets_ratio,
             fiscal_stimulus, government_debt_to_gdp, phase_gdp_target, recession_probability);
        for (u, v) in x.gdp_trend.iter_mut().zip(y.gdp_trend.iter()) {
            *u = a * *u + b * *v;
        }
        if let (Some(u), Some(v)) = (x.market_pe, y.market_pe) {
            x.market_pe = Some(a * u + b * v);
        }
    }
}

/// Growth's expectation over the cycle's phases for oil's forecast
/// (`oil_forecast_cycle_growth`), on the law's own transitions. Each phase
/// holds cohorts of the belief's mass, each with its own growth and age:
/// the mass on a phase at the start, spread over the phase's stationary age
/// law under the full law (the age is not public;
/// [`Engine::cycle_age_law`], in months), and the mass that enters a phase
/// over the horizon, a fresh sojourn. Each phase also carries its own
/// economy ([`CycleTrack`]), from the close's: at each close the law steps
/// it in that phase, its draws at their means, at its cohorts' mean growth;
/// its central bank meets on it by the law's rule, the draws at the middle
/// of their range; and the market's gap steps on it with its variance about
/// that path, the forecast's own session variance of the index. Each
/// cohort's growth moves with the step, and each cohort leaves with the
/// probability the law's transition check compares against on that
/// phase's stepped economy at the cohort's age: the hazard, the ladder from
/// the phase's own state (the market's fall over the gap's spread,
/// inflation, rates, the curve, valuation, unemployment, growth), the clamp
/// and the per-day conversion
/// ([`crate::economy::cycle::transition_probability_for`]). Mass entering a
/// phase takes the step's mean shock on a phase's first session, and its
/// state joins the phase's at the mass-weighted mean. No value is set
/// here: every part is the law's.
struct CycleGrowth {
    phases: [crate::economy::CyclePhase; 5],
    spec: crate::economy::CycleSpec,
    /// Each phase's cohorts: age in completed sessions, mass and growth.
    cohorts: Vec<Vec<(f64, f64, f64)>>,
    tracks: Vec<Option<CycleTrack>>,
    /// The meeting's options as the close stands; the bank's and the
    /// curve's inflation are each track's own.
    options: crate::economy::PolicyOptions,
    core: (bool, bool),
}

impl CycleGrowth {
    fn new(engine: &Engine, belief: &[f64; 5], econ: &EconomyState, day: i64) -> Self {
        let phases = crate::economy::cycle::phase_cycle();
        let spec = engine.cycle_spec();
        let ages = engine.cycle_age_law(day);
        let total: f64 = belief.iter().sum();
        let mut cohorts = vec![Vec::new(); 5];
        let mut tracks = vec![None; 5];
        for k in 0..5 {
            let b = if total > 0.0 { belief[k] / total } else { 0.0 };
            if b > 0.0 {
                for &(age, w) in &ages[k] {
                    cohorts[k].push((age, b * w, econ.gdp_growth));
                }
                let mut economy = econ.clone();
                economy.cycle_phase = phases[k];
                tracks[k] = Some(CycleTrack { economy, bank: engine.central_bank, gap_var: 0.0 });
            }
        }
        let options = crate::economy::PolicyOptions {
            path_before: engine.priced_policy_path(),
            eve_change: None,
            ..engine.policy_options(false)
        };
        let core = (engine.params.fed_core_inflation != 0.0, engine.params.treasury_core_inflation != 0.0);
        CycleGrowth { phases, spec, cohorts, tracks, options, core }
    }

    /// The growth oil's block reads at the next step: the cohorts' mean.
    fn growth(&self) -> f64 {
        let (mut m, mut g) = (0.0, 0.0);
        for c in self.cohorts.iter().flatten() {
            m += c.1;
            g += c.1 * c.2;
        }
        if m > 0.0 {
            g / m
        } else {
            0.0
        }
    }

    /// One close: each phase's economy stepped by the law at its cohorts'
    /// mean growth and one point above it (the step is affine in growth but
    /// for its clamp and the pull's doubling against the phase, so each
    /// cohort moves on the line through the two); each cohort's leavers, at
    /// the law's transition probability on that stepped economy, enter the
    /// next phase; then the phase's bank meets. `variance` is the forecast's
    /// variance of the session's return, the return the gap steps on.
    fn step(&mut self, inputs: &DailyInputs, volatility: f64, variance: f64, timestamp: i64) {
        let decay = inputs.yields.spread_equity_decay;
        let mut own = *inputs;
        own.oil_growth = None;
        let mut leaving = [(0.0, 0.0); 5];
        let mut stepped_tracks: Vec<Option<CycleTrack>> = vec![None; 5];
        for k in 0..5 {
            let (mut m, mut g) = (0.0, 0.0);
            for c in &self.cohorts[k] {
                m += c.1;
                g += c.1 * c.2;
            }
            let track = match &self.tracks[k] {
                Some(t) if m > 0.0 => t,
                _ => continue,
            };
            let bar = g / m;
            let mut at = track.economy.clone();
            at.cycle_phase = self.phases[k];
            at.gdp_growth = bar + 1.0;
            let s1 = update_economy_daily(&at, &own, &mut crate::economy::daily::MeanDraws).gdp_growth;
            at.gdp_growth = bar;
            let stepped = update_economy_daily(&at, &own, &mut crate::economy::daily::MeanDraws);
            let s0 = stepped.gdp_growth;
            let slope = s1 - s0;
            let gap_var = if decay != 0.0 { decay * decay * (track.gap_var + variance) } else { 0.0 };
            for c in self.cohorts[k].iter_mut() {
                c.0 += 1.0;
                c.2 = s0 + slope * (c.2 - bar);
                let q = crate::economy::cycle::transition_probability_for(
                    &stepped, self.phases[k], c.0, &self.spec, gap_var);
                let out = c.1 * q;
                c.1 -= out;
                leaving[k].0 += out;
                leaving[k].1 += out * c.2;
            }
            self.cohorts[k].retain(|c| c.1 > 1e-12);
            let mut options = self.options;
            let core = stepped.inflation_rate - stepped.oil_inflation_level;
            if self.core.0 {
                options.policy_inflation = Some(core);
            }
            if self.core.1 {
                options.curve_inflation = Some(core);
            }
            let met = crate::economy::update_central_bank_with(&track.bank, &stepped, timestamp, &mut MidDraws, &options);
            stepped_tracks[k] = Some(CycleTrack { economy: met.economy, bank: met.central_bank, gap_var });
        }
        let mut staying = [0.0; 5];
        for k in 0..5 {
            staying[k] = self.cohorts[k].iter().map(|c| c.1).sum();
            if stepped_tracks[k].is_some() {
                self.tracks[k] = stepped_tracks[k].clone();
            }
        }
        for k in 0..5 {
            let (m, g) = leaving[k];
            if m > 0.0 {
                let next = (k + 1) % 5;
                let shock = crate::economy::daily::phase_entry_growth_shock(self.phases[next], 0.5, volatility);
                self.cohorts[next].push((0.0, m, g / m + shock));
                let from = stepped_tracks[k].as_ref().expect("a phase with leavers was stepped");
                match self.tracks[next].as_mut() {
                    Some(t) if staying[next] > 0.0 => t.join(staying[next], from, m),
                    _ => {
                        let mut t = from.clone();
                        t.economy.cycle_phase = self.phases[next];
                        self.tracks[next] = Some(t);
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::economy::{
        create_initial_central_bank_state, create_initial_economy_state, InitialEconomyOptions,
    };
    use crate::market::TickStock;

    fn company(id: &str, price: f64, shares: f64) -> TickCompany {
        TickCompany {
            id: id.to_string(),
            ticker: id.to_string(),
            sector: "technology".to_string(),
            is_bankrupt: false,
            is_public: true,
            sector_volatility: Some(1.0),
            sector_avg_pe: Some(32.0),
            eps: Some(4.0),
            book_value_per_share: Some(20.0),
            revenue_growth: Some(0.1),
            stock: TickStock {
                price,
                previous_close: price,
                previous_tick_price: None,
                open: price,
                high: price,
                low: price,
                volume: 0.0,
                avg_volume: 1e6,
                shares_outstanding: shares,
                market_cap: price * shares,
                mispricing_s: None,
                mispricing_s_prev_close: None,
                mispricing_momentum: None,
                fair_value_offset: None,
                buyback_log_shares: None,
                dividend: None,
                maker_inventory: None,
                garch_variance: 0.015 * 0.015,
                garch_cascade: [0.015 * 0.015; crate::market::garch::CASCADE_MAX],
                last_daily_return: None,
                beta: Some(1.0),
                short_interest: 0.0,
                float: shares,
            },
        }
    }

    fn engine(params: ModelParams) -> Engine {
        Engine::with_params(
            11,
            vec![company("A", 100.0, 1e8), company("B", 50.0, 3e8), company("C", 220.0, 5e7)],
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            vec!["technology".to_string(), "energy".to_string(), "healthcare".to_string()],
            params,
        )
    }

    fn with(dials: &[(&str, f64)]) -> ModelParams {
        let mut p = crate::params::PT_V21;
        for (name, value) in dials {
            p = p.with_override(name, *value).unwrap();
        }
        p
    }

    /// `sum(price * shares) / divisor` over the public, solvent names, by hand.
    fn by_hand(e: &Engine, divisor: f64) -> f64 {
        let cap: f64 = e
            .companies()
            .iter()
            .filter(|c| c.is_public && !c.is_bankrupt)
            .map(|c| c.stock.price * c.stock.shares_outstanding)
            .sum();
        cap / divisor
    }

    fn close_enough(a: f64, b: f64) -> bool {
        (a - b).abs() <= 1e-12 * b.abs()
    }

    #[test]
    fn off_there_is_no_index_no_live_vix_and_no_forecast() {
        let mut e = engine(crate::params::PT_V21);
        let mut buffer = SessionBuffer::new();
        e.run_numbered_day(0, 390, &mut buffer);
        assert!(e.index_level().is_none());
        assert!(e.index_state().is_none());
        assert!(e.live_vix().is_none());
        assert!(e.forecast().is_none());
    }

    #[test]
    fn the_index_keeps_its_level_through_a_listing_a_delisting_and_a_bankruptcy() {
        let mut e = engine(with(&[("index_level_listed", 1.0)]));
        let before = e.index_level().unwrap();
        assert_eq!(before.level, INDEX_BASE);
        assert_eq!(before.divisor, None);
        assert_eq!(before.close, None);
        // The base: the first open, on the prices before any tick.
        e.set_current_day(0);
        e.open_market();
        let d0 = e.index_level().unwrap().divisor.unwrap();
        let cap0: f64 = e.companies().iter().map(|c| c.stock.price * c.stock.shares_outstanding).sum();
        assert_eq!(d0.to_bits(), (cap0 / INDEX_BASE).to_bits());
        assert!(close_enough(e.index_level().unwrap().level, INDEX_BASE));
        let mut buffer = SessionBuffer::new();
        e.run_session(&SessionRequest::new(GameTime::new(9, 30, 3), 390), &mut buffer);
        let live = e.index_level().unwrap();
        assert!(close_enough(live.level, by_hand(&e, d0)));
        assert_ne!(live.level, INDEX_BASE, "the session moved no price");
        e.close_day(1);
        let closed = e.index_level().unwrap();
        assert!(close_enough(closed.close.unwrap(), live.level), "the close is the last prints'");

        // A delisting between sessions, at the prices then standing.
        let level = e.index_level().unwrap().level;
        e.remove_company(1).unwrap();
        let after = e.index_level().unwrap();
        assert!(close_enough(after.level, level));
        assert_eq!(after.constituents, 2);
        assert!(close_enough(after.divisor.unwrap(), by_hand(&e, 1.0) / level));
        // A listing.
        e.add_company(company("D", 80.0, 2e8));
        let after = e.index_level().unwrap();
        assert!(close_enough(after.level, level));
        assert_eq!(after.constituents, 3);
        // A bankruptcy, through the status setter.
        e.set_status(&[false, true, false], &[true, true, true]).unwrap();
        let after = e.index_level().unwrap();
        assert!(close_enough(after.level, level));
        assert_eq!(after.constituents, 2);
        // And the next session prices the survivors on the new divisor.
        let d1 = after.divisor.unwrap();
        e.set_current_day(1);
        e.open_market();
        e.run_session(&SessionRequest::new(GameTime::new(9, 30, 3), 390), &mut buffer);
        assert!(close_enough(e.index_level().unwrap().level, by_hand(&e, d1)));
    }

    #[test]
    fn the_index_state_round_trips_and_is_refused_where_the_switch_is_off() {
        let mut e = engine(with(&[("index_level_listed", 1.0)]));
        let mut buffer = SessionBuffer::new();
        e.run_numbered_day(0, 390, &mut buffer);
        let state = e.index_state().unwrap();
        let mut other = engine(with(&[("index_level_listed", 1.0)]));
        other.set_index_state(Some(state)).unwrap();
        assert_eq!(other.index_state(), Some(state));
        let mut off = engine(crate::params::PT_V21);
        assert!(off.set_index_state(Some(state)).is_err());
        assert!(off.set_vix_live_mark(Some(20.0)).is_err());
    }

    /// The live VIX at the session's last minute is the VIX the close
    /// publishes with its draws at their means, bit for bit, on a model
    /// whose close steps nothing the projection does not (the sector and
    /// own-variance states off). After the close it is the published VIX.
    #[test]
    fn the_last_minute_is_the_close_at_its_means() {
        let p = with(&[
            ("vix_intraday_live", 1.0),
            ("sector_vol_alpha", 0.0),
            ("sector_vol_beta", 0.0),
            ("idio_vol_alpha", 0.0),
            ("idio_vol_beta", 0.0),
            ("idio_vol_jump_bump", 0.0),
            ("vix_level_sigma", 0.0),
        ]);
        let mut e = engine(p);
        let mut buffer = SessionBuffer::new();
        for day in 0..3 {
            e.run_numbered_day(day, 390, &mut buffer);
        }
        e.set_current_day(3);
        e.open_market();
        let opening = e.live_vix().unwrap();
        e.run_session(&SessionRequest::new(GameTime::new(9, 30, 3), 390), &mut buffer);
        let last = e.live_vix().unwrap();
        assert_ne!(last.to_bits(), opening.to_bits(), "the session moved the live VIX");
        let noise = e.daily_innovation_column();
        let innovations: Vec<Option<f64>> = noise.into_iter().map(Some).collect();
        let variances = e.sector_base_variances();
        e.close_market(&DayCloseRequest {
            daily_innovations: &innovations,
            sector_base_variances: &variances,
            avg_volume: crate::market::AvgVolumePolicy::Hold,
        });
        let request = DayAdvanceRequest {
            volatility: 1.0,
            active_shocks: &[],
            market_return_pct: 0.0,
            game_day: 4,
            timestamp: 4 * 24 * 60,
        };
        e.advance_day_with(&request, &mut crate::economy::daily::MeanDraws);
        assert_eq!(last.to_bits(), e.published_vix().to_bits());
        assert_eq!(e.live_vix().unwrap().to_bits(), e.published_vix().to_bits());
    }

    #[test]
    fn the_guidance_reaches_the_path_through_its_signal_and_rounding_the_eve_recovers_nothing() {
        let base = [
            ("forecast_horizon_sessions", 63.0),
            ("index_level_listed", 1.0),
            ("fed_eve_decision", 1.0),
            ("policy_anticipation_guidance", 1.0),
        ];
        let mut dials = base.to_vec();
        dials.push(("policy_guidance_signal_sd", 6.4759));
        let mut e = engine(with(&dials));
        let mut off = engine(with(&base));
        let (mut buffer, mut other) = (SessionBuffer::new(), SessionBuffer::new());
        let mut eve: Option<(f64, f64)> = None;
        let mut misses = Vec::new();
        for day in 0..2000 {
            e.run_numbered_day(day, 390, &mut buffer);
            off.run_numbered_day(day, 390, &mut other);
            // The forecast feeds only the contracts: no stock price moves.
            for (a, b) in e.companies().iter().zip(off.companies().iter()) {
                assert_eq!(a.stock.price.to_bits(), b.stock.price.to_bits(), "day {day}");
            }
            let rate = e.economy.federal_funds_rate;
            if let Some((priced, then)) = eve.take() {
                misses.push((rate - then, priced));
            }
            let f = e.forecast().unwrap();
            if e.next_meeting_day(f.day) == f.day + 1 {
                // What the signal lets the market expect of the decision.
                eve = Some((f.policy_rate[0] - rate, rate));
            }
        }
        let n = misses.len() as f64;
        assert!(n >= 50.0, "{n} eves");
        let surprise: Vec<f64> = misses.iter().map(|(d, p)| d - p).collect();
        let mean = surprise.iter().sum::<f64>() / n;
        let sd = (surprise.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / (n - 1.0)).sqrt();
        assert!(mean.abs() < 0.02 && sd > 0.005 && sd < 0.08, "mean {mean} sd {sd} on {n}");
        // Rounding the eve's price to the grid and trading toward it.
        let edge: Vec<f64> = misses
            .iter()
            .map(|(d, p)| {
                let toward = (p / GUIDANCE_GRID).round() * GUIDANCE_GRID - p;
                if toward == 0.0 { 0.0 } else { toward.signum() * (d - p) }
            })
            .collect();
        let m = edge.iter().sum::<f64>() / n;
        let s = (edge.iter().map(|x| (x - m) * (x - m)).sum::<f64>() / (n - 1.0)).sqrt();
        assert!(m < 2.0 * s / n.sqrt(), "the grid rule's edge {m} (se {})", s / n.sqrt());
    }

    #[test]
    fn the_forecast_runs_its_horizon_and_its_first_variance_is_the_identitys() {
        let mut e = engine(with(&[("forecast_horizon_sessions", 63.0), ("index_level_listed", 1.0)]));
        let mut buffer = SessionBuffer::new();
        for day in 0..3 {
            e.run_numbered_day(day, 390, &mut buffer);
        }
        let f = e.forecast().unwrap().clone();
        assert_eq!(f.horizon(), 63);
        assert_eq!(f.day, 3);
        for series in [&f.vix, &f.index_variance, &f.policy_rate, &f.oil] {
            assert_eq!(series.len(), 63);
            assert!(series.iter().all(|v| v.is_finite() && *v >= 0.0));
        }
        assert!(f.name_variance.iter().all(|row| row.len() == 63));
        // Entry 0 is the next session's variance as the identity reads it.
        let now = e.index_conditional_variance_terms_now().total();
        assert!(close_enough(f.index_variance[0], now), "{} {now}", f.index_variance[0]);
        // A pure function of the state: a copy computes the same one, and
        // so does a restore of it.
        let mut copy = e.clone();
        copy.refresh_forecast(None);
        assert_eq!(copy.forecast().unwrap(), &f);
        let words = f.to_words();
        let back = crate::derivatives::Forecast::from_words(&words).unwrap();
        assert_eq!(back, f);
    }

    /// The rule's mean, variance and skewness of a list of `(value, weight)`.
    fn moments(nodes: &[(f64, f64)]) -> (f64, f64, f64) {
        let mean: f64 = nodes.iter().map(|(x, w)| w * x).sum();
        let var: f64 = nodes.iter().map(|(x, w)| w * (x - mean) * (x - mean)).sum();
        let third: f64 = nodes.iter().map(|(x, w)| w * (x - mean).powi(3)).sum();
        (mean, var, third / var.powf(1.5))
    }

    #[test]
    fn the_spread_is_standard_skewed_as_asked_and_keeps_the_mean() {
        let weights: f64 = spread_shape(0.0).iter().map(|(_, w)| w).sum();
        assert!((weights - 1.0).abs() < 1e-12);
        for skew in [0.0, 0.81, 0.95, -0.5] {
            let shape = spread_shape(skew);
            let (mean, var, sk) = moments(&shape);
            assert!(mean.abs() < 1e-12 && (var - 1.0).abs() < 1e-12, "{skew}: {mean} {var}");
            assert!((sk - skew).abs() < 1e-9, "{skew}: skewness {sk}");
            // The nodes are a spread of the asked log variance whose
            // exponential has mean one, so `vix` stays the mean.
            let nodes = spread_at(&shape, 0.09);
            let (_, var_x, _) = moments(&nodes);
            assert!((var_x - 0.09).abs() < 1e-12);
            assert!((spread_expect(&nodes, 18.0, |v| v) - 18.0).abs() < 1e-12);
        }
        // At no skew the second moment is the lognormal's.
        let nodes = spread_at(&spread_shape(0.0), 0.09);
        let second = spread_expect(&nodes, 1.0, |v| v * v);
        assert!((second - crate::mathx::exp(0.09)).abs() < 1e-12);
        // Skewed, a steep power's expectation is above the lognormal's and
        // finite: the tails stay normal (the variance target reads the VIX
        // to the fourth power).
        let lognormal = spread_expect(&nodes, 1.0, |v| v.powi(4));
        let skewed = spread_expect(&spread_at(&spread_shape(0.95), 0.09), 1.0, |v| v.powi(4));
        assert!(skewed > lognormal && skewed < 1.5 * lognormal, "{skewed} {lognormal}");
    }

    #[test]
    fn the_published_vix_is_the_quotes_expectation_over_the_spread() {
        let dials = [
            ("forecast_horizon_sessions", 63.0),
            ("forecast_vix_dispersion", 0.3),
            ("forecast_vix_dispersion_half_life", 31.5),
            ("forecast_vix_dispersion_skew", 0.81),
        ];
        let e = engine(with(&dials));
        let shape = spread_shape(0.81);
        // Calm: the stress memory under the knee at the state, so the quote
        // is the state, and its expectation adds the premium's right tail.
        let (vix, calm) = (16.0, 0.1);
        assert_eq!(e.published_vix_at(vix, calm), vix);
        let near = e.forecast_published_vix(&shape, vix, calm, 0.0, 1);
        let far = e.forecast_published_vix(&shape, vix, calm, 0.3, 126);
        assert!((near - vix).abs() < 1e-9, "{near}");
        assert!(far > vix * 1.005, "{far}");
        // More of the memory's spread, more of the premium.
        assert!(e.forecast_published_vix(&shape, vix, calm, 0.2, 126) < far);
        // Stressed: the premium is near its cap at the state, and the
        // expectation reads its concave top, so it is below the quote.
        let hot = 2.0;
        let quote = e.published_vix_at(40.0, hot);
        assert!(e.forecast_published_vix(&shape, 40.0, hot, 0.05, 1) <= quote * 1.0001);
        // With the premium off it is the state, whatever the spread.
        let off = engine(with(&[dials[0], dials[1], dials[2], dials[3], ("vix_stress_premium", 0.0)]));
        assert_eq!(off.forecast_published_vix(&shape, vix, calm, 0.3, 126), vix);
        // With the dispersion off, the quote at the state.
        assert_eq!(e.forecast_published_vix(&[], vix, 2.0, 0.3, 126), e.published_vix_at(vix, 2.0));
    }
}