libitofin 0.12.0

A ground-up Rust port of QuantLib: quantitative-finance primitives for pricing, risk, and numerical methods.
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
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
//! ISDA credit-default-swap pricing engine.
//!
//! Port of `ql/pricingengines/credit/isdacdsengine.{hpp,cpp}`: [`IsdaCdsEngine`]
//! prices a [`CreditDefaultSwap`](crate::instruments::CreditDefaultSwap) the way
//! the ISDA standard model does, integrating both legs over the pillar dates of
//! the two curves it is built with rather than over the premium schedule alone.
//! Three flags - [`NumericalFix`], [`AccrualBias`] and
//! [`ForwardsInCouponPeriod`] - select which of that model's known
//! approximations the engine reproduces, so that it can be graded against the
//! standard model's C code and not only against the theory. They are documented
//! against the two references named at `isdacdsengine.hpp:36-47`: [1] OpenGamma's
//! note on the ISDA model and [2] Markit's proposed numerical fix.
//!
//! The model is specified against curves of a fixed shape, so the engine refuses
//! anything else outright (`isdacdsengine.cpp:62-98`).
//!
//! Deviations, documented per D5/D10:
//! - The C++ global `Settings::instance()` (`isdacdsengine.cpp:70`) becomes an
//!   explicit [`Settings`] handle the engine is built with, as for
//!   [`MidPointCdsEngine`](super::MidPointCdsEngine); an unset evaluation date is
//!   an `Err` rather than a system-clock fall back.
//! - The three range checks on the flags (`isdacdsengine.cpp:54-60`) have no
//!   counterpart: they reject a `NumericalFix` that is neither `None` nor
//!   `Taylor`, which a C++ enum permits and a Rust one does not.
//! - The C++ `dynamic_pointer_cast<FixedRateCoupon>` on each premium flow
//!   (`isdacdsengine.cpp:205`) becomes
//!   [`CashFlow::as_coupon`](crate::cashflow::CashFlow::as_coupon), as it does for
//!   [`MidPointCdsEngine`](super::MidPointCdsEngine): C++ dereferences the
//!   resulting null pointer when a flow is not a coupon and the port reports it
//!   (D4). The narrowing stops at `dyn Coupon` rather than at the fixed-rate
//!   coupon, which is every member the kernel reads; a floating-rate coupon in
//!   the leg would therefore price here where C++ is undefined.
//! - The C++ `default:` arm of the side switch (`isdacdsengine.cpp:322-323`) has
//!   no counterpart: [`ProtectionSide`] has exactly the two variants, so there
//!   is no unknown side to reject.

use crate::cashflow::{CashFlow, Leg};
use crate::cashflows::Coupon;
use crate::errors::QlResult;
use crate::event::{Event, event_has_occurred};
use crate::instruments::{
    CdsArguments, CdsEngine, CdsResults, Claim, FaceValueClaim, ProtectionSide,
};
use crate::patterns::observable::{AsObservable, Observable};
use crate::pricingengine::{Arguments, PricingEngine, Results};
use crate::settings::Settings;
use crate::shared::Shared;
use crate::termstructures::credit::defaulttermstructure::DefaultProbabilityTermStructure;
use crate::termstructures::yieldtermstructure::YieldTermStructure;
use crate::time::date::Date;
use crate::time::daycounter::DayCounter;
use crate::time::daycounters::actual360::Actual360;
use crate::time::daycounters::actual365fixed::Actual365Fixed;
use crate::types::{Rate, Real};
use crate::{fail, handle::Handle, require};

use super::isda_node_grid;

/// The one-basis-point move the leg sensitivities are quoted against
/// (`isdacdsengine.cpp:349`), as for
/// [`MidPointCdsEngine`](super::MidPointCdsEngine).
const BASIS_POINT: Rate = 1.0e-4;

/// How the engine keeps the integrands' `f_i + h_i` denominators away from zero
/// (`isdacdsengine.hpp:66-70`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericalFix {
    /// No fix: `10^-50` is added to the denominators instead ([1] footnote 26).
    /// C++ spells this variant `None` (`hpp:67`); it is renamed here to keep
    /// clear of [`Option::None`], which every use site has in scope.
    NoFix,
    /// A Taylor expansion replaces the quotient once `f_i + h_i < 10^-4` ([2]).
    Taylor,
}

/// Whether the premium leg carries the standard model's half-day accrual bias
/// (`isdacdsengine.hpp:72-76`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccrualBias {
    /// The second, erroneous term of [1] formula (50) is included, as the
    /// standard model's C code does before version 1.8.2.
    HalfDayBias,
    /// It is left out, as from 1.8.2 on.
    NoBias,
}

/// How the engine treats forward rates inside a coupon period
/// (`isdacdsengine.hpp:78-83`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ForwardsInCouponPeriod {
    /// The second, erroneous term of [1] formula (52) is included.
    Flat,
    /// It is left out, which with [`AccrualBias::NoBias`] is the theoretically
    /// correct setting (`isdacdsengine.hpp:59-61`).
    Piecewise,
}

/// ISDA standard-model engine for credit-default swaps.
///
/// The client is responsible for supplying curves built to the ISDA
/// specification; the engine checks the properties it can and refuses the rest
/// (`isdacdsengine.hpp:85-96`).
pub struct IsdaCdsEngine {
    base: CdsEngine,
    probability: Handle<dyn DefaultProbabilityTermStructure>,
    recovery_rate: Real,
    discount_curve: Handle<dyn YieldTermStructure>,
    include_settlement_date_flows: Option<bool>,
    numerical_fix: NumericalFix,
    accrual_bias: AccrualBias,
    forwards_in_coupon_period: ForwardsInCouponPeriod,
    settings: Shared<Settings<Date>>,
}

impl IsdaCdsEngine {
    /// Builds the engine over the two curve handles it registers with
    /// (`isdacdsengine.cpp:36-50`), on the C++ default flags `Taylor` /
    /// `HalfDayBias` / `Piecewise` (`isdacdsengine.hpp:101-103`).
    ///
    /// The arguments are [`MidPointCdsEngine::new`](super::MidPointCdsEngine::new)'s,
    /// so the two engines are interchangeable at a call site that prices on
    /// either.
    pub fn new(
        probability: Handle<dyn DefaultProbabilityTermStructure>,
        recovery_rate: Real,
        discount_curve: Handle<dyn YieldTermStructure>,
        include_settlement_date_flows: Option<bool>,
        settings: Shared<Settings<Date>>,
    ) -> IsdaCdsEngine {
        let base = CdsEngine::new(CdsArguments::default(), CdsResults::default());
        probability.register_observer(&base.observer());
        discount_curve.register_observer(&base.observer());
        IsdaCdsEngine {
            base,
            probability,
            recovery_rate,
            discount_curve,
            include_settlement_date_flows,
            numerical_fix: NumericalFix::Taylor,
            accrual_bias: AccrualBias::HalfDayBias,
            forwards_in_coupon_period: ForwardsInCouponPeriod::Piecewise,
            settings,
        }
    }

    /// Chooses the three fidelity flags, which the C++ constructor takes as
    /// trailing defaulted arguments (`isdacdsengine.hpp:98-104`).
    pub fn with_fidelity(
        mut self,
        numerical_fix: NumericalFix,
        accrual_bias: AccrualBias,
        forwards_in_coupon_period: ForwardsInCouponPeriod,
    ) -> IsdaCdsEngine {
        self.numerical_fix = numerical_fix;
        self.accrual_bias = accrual_bias;
        self.forwards_in_coupon_period = forwards_in_coupon_period;
        self
    }

    /// `isdacdsengine.cpp:62-157`: the ISDA-compatibility checks, then the
    /// integration grid and the constants the leg kernels run on.
    fn validated(&self) -> QlResult<IsdaContext> {
        require!(
            !self.discount_curve.is_empty(),
            "no discount term structure set"
        );
        require!(
            !self.probability.is_empty(),
            "no probability term structure set"
        );
        let discount = self.discount_curve.current_link()?;
        let probability = self.probability.current_link()?;
        require_act_365_fixed(discount.day_counter(), "yield")?;
        require_act_365_fixed(probability.day_counter(), "probability")?;

        let Some(eval_date) = self.settings.evaluation_date() else {
            fail!("no evaluation date set: the ISDA CDS engine needs today's date");
        };
        let reference = discount.reference_date()?;
        require!(
            reference == eval_date,
            "yield term structure reference date ({reference}) should be evaluation date ({eval_date})"
        );
        let reference = probability.reference_date()?;
        require!(
            reference == eval_date,
            "probability term structure reference date ({reference}) should be evaluation date ({eval_date})"
        );

        let arguments = self.base.arguments();
        require!(
            arguments.settles_accrual,
            "ISDA engine not compatible with non accrual paying CDS"
        );
        require!(
            arguments.pays_at_default_time,
            "ISDA engine not compatible with end period payment"
        );
        let Some(claim) = arguments.claim.as_ref() else {
            fail!("claim not set");
        };
        require!(
            claim.as_any().is_some_and(|any| any.is::<FaceValueClaim>()),
            "ISDA engine not compatible with non face value claim"
        );
        let (Some(maturity), Some(start)) = (arguments.maturity, arguments.protection_start) else {
            fail!("maturity or protection start date not set");
        };

        Ok(IsdaContext {
            discount,
            probability,
            eval_date,
            effective_protection_start: start.max(eval_date + 1),
            nodes: isda_node_grid(&self.discount_curve, &self.probability, maturity)?,
            n_fix: if self.numerical_fix == NumericalFix::NoFix {
                1.0e-50
            } else {
                0.0
            },
            recovery_rate: self.recovery_rate,
            include_settlement_date_flows: self.include_settlement_date_flows,
            accrual_bias: self.accrual_bias,
            forwards_in_coupon_period: self.forwards_in_coupon_period,
            maturity,
        })
    }

    /// `isdacdsengine.cpp:159-197`: the protection leg, integrated over the
    /// grid nodes from the day before the effective protection start out to the
    /// maturity, and scaled by what a default would claim.
    ///
    /// The value is positive here, whatever the C++ comment at `:159` says:
    /// `h_hat` is the fall in the log survival probability over a step and so is
    /// non-negative, as is the `N (1 - R)` it ends up scaled by. The sign the
    /// protection side gives the leg belongs to the results tail (`:311-324`),
    /// exactly as it does for
    /// [`MidPointCdsEngine`](super::MidPointCdsEngine) (`midpointcdsengine.cpp:143`).
    ///
    /// C++ rolls a `d1` into `d0` alongside `P0` and `Q0` (`:192-194`) that
    /// nothing reads after the two curve lookups it seeds (`:163-164`); the port
    /// keeps the lookups and drops the date.
    ///
    /// The claim is asked for its amount on the null date, as in C++ (`:196`).
    /// The engine settles face-value claims alone (`:97-98`) and those ignore
    /// the date, so nothing is lost by having no default date to offer.
    fn protection_leg_npv(
        &self,
        context: &IsdaContext,
        claim: &dyn Claim,
        notional: Real,
    ) -> QlResult<Real> {
        let opening = context.effective_protection_start - 1;
        let mut p0 = context.discount.discount_date(opening, false)?;
        let mut q0 = context
            .probability
            .survival_probability_date(opening, false)?;
        let mut protection_npv = 0.0;

        let mut index = context
            .nodes
            .partition_point(|node| *node <= context.effective_protection_start);
        while index < context.nodes.len() {
            let past_maturity = context.nodes[index] > context.maturity;
            let d1 = if past_maturity {
                context.maturity
            } else {
                context.nodes[index]
            };
            let p1 = context.discount.discount_date(d1, false)?;
            let q1 = context.probability.survival_probability_date(d1, false)?;
            let f_hat = p0.ln() - p1.ln();
            let h_hat = q0.ln() - q1.ln();
            let fhphh = f_hat + h_hat;

            protection_npv +=
                if fhphh < TAYLOR_THRESHOLD && self.numerical_fix == NumericalFix::Taylor {
                    let fhphhq = fhphh * fhphh;
                    p0 * q0
                        * h_hat
                        * (1.0 - 0.5 * fhphh + 1.0 / 6.0 * fhphhq - 1.0 / 24.0 * fhphhq * fhphh
                            + 1.0 / 120.0 * fhphhq * fhphhq)
                } else {
                    h_hat / (fhphh + context.n_fix) * (p0 * q0 - p1 * q1)
                };

            p0 = p1;
            q0 = q1;
            if past_maturity {
                break;
            }
            index += 1;
        }

        Ok(protection_npv * claim.amount(&Date::null(), notional, context.recovery_rate))
    }

    /// `isdacdsengine.cpp:201-287`: the premium leg - each live coupon carried
    /// by the survival to the day before it pays, plus what a default part way
    /// through a period would accrue.
    ///
    /// The survival factor and its one-day offset (`:218`) are the standard
    /// model's, not a discounting identity: a coupon is paid only if the name
    /// survived the day before the payment date, which is a day earlier than
    /// the date the coupon is discounted from.
    ///
    /// The specification fixes the premium leg's day count as well as the
    /// curves' (`:205-211`), so the ISDA `365/360` scaling at `:282` is the
    /// conversion between the two conventions it allows.
    fn premium_leg_npv(&self, context: &IsdaContext, leg: &Leg, notional: Real) -> QlResult<Real> {
        let mut premium_npv = 0.0;
        let mut default_accrual_npv = 0.0;

        for (position, flow) in leg.iter().enumerate() {
            let Some(coupon) = flow.as_coupon() else {
                fail!("premium leg flow #{} is not a coupon", position + 1);
            };
            let day_counter = coupon.day_counter();
            require!(
                day_counter == Actual365Fixed::new()
                    || day_counter == Actual360::new()
                    || day_counter == Actual360::with_last_day(true),
                "ISDA engine requires a coupon day counter Act/365Fixed or Act/360 ({day_counter})"
            );

            if !flow.has_occurred(
                &self.settings,
                Some(context.effective_protection_start),
                context.include_settlement_date_flows,
            )? {
                premium_npv += coupon.amount()?
                    * context.discount.discount_date(flow.date(), false)?
                    * context
                        .probability
                        .survival_probability_date(flow.date() - 1, false)?;
            }

            if event_has_occurred(
                coupon.accrual_end_date(),
                &self.settings,
                Some(context.effective_protection_start),
                Some(false),
            )? {
                continue;
            }
            default_accrual_npv += self.default_accrual(context, coupon, flow.date())?
                * notional
                * coupon.rate()?
                * 365.0
                / 360.0;
        }

        Ok(premium_npv + default_accrual_npv)
    }

    /// `isdacdsengine.cpp:223-280`: what a default inside one coupon's period
    /// accrues, per unit of notional and of coupon rate.
    ///
    /// The period is integrated from the day before its accrual starts - or
    /// before the protection does, whichever is later - to the day before it
    /// pays (`:225-227`), subdivided at the grid's own nodes when the engine was
    /// asked to see the forwards inside the period as piecewise (`:231-241`).
    /// Only the caller's guard on the accrual end (`:223-224`) keeps that
    /// subdivision well formed: it holds the period open past the effective
    /// protection start, which with a payment date no earlier than the accrual
    /// end puts the opening date strictly before the closing one, so the node
    /// range between them cannot run backwards.
    ///
    /// `tstart` is measured from the unclamped accrual start (`:228-230`), a day
    /// earlier than the accrual it weights, which is what the half-day bias
    /// half-corrects.
    fn default_accrual(
        &self,
        context: &IsdaContext,
        coupon: &dyn Coupon,
        payment_date: Date,
    ) -> QlResult<Real> {
        let start = coupon
            .accrual_start_date()
            .max(context.effective_protection_start)
            - 1;
        let end = payment_date - 1;
        let tstart = context
            .discount
            .time_from_reference(coupon.accrual_start_date() - 1)?
            - match context.accrual_bias {
                AccrualBias::HalfDayBias => 1.0 / 730.0,
                AccrualBias::NoBias => 0.0,
            };

        let mut local_nodes = vec![start];
        if context.forwards_in_coupon_period == ForwardsInCouponPeriod::Piecewise {
            let opening = context.nodes.partition_point(|node| *node <= start);
            let closing = context.nodes.partition_point(|node| *node < end);
            local_nodes.extend_from_slice(&context.nodes[opening..closing]);
        }
        local_nodes.push(end);

        let mut accrual = 0.0;
        let mut t0 = context.discount.time_from_reference(local_nodes[0])?;
        let mut p0 = context.discount.discount_date(local_nodes[0], false)?;
        let mut q0 = context
            .probability
            .survival_probability_date(local_nodes[0], false)?;
        for node in &local_nodes[1..] {
            let t1 = context.discount.time_from_reference(*node)?;
            let p1 = context.discount.discount_date(*node, false)?;
            let q1 = context
                .probability
                .survival_probability_date(*node, false)?;
            let f_hat = p0.ln() - p1.ln();
            let h_hat = q0.ln() - q1.ln();
            let fhphh = f_hat + h_hat;

            accrual += if fhphh < TAYLOR_THRESHOLD && self.numerical_fix == NumericalFix::Taylor {
                let fhphhq = fhphh * fhphh;
                h_hat
                    * p0
                    * q0
                    * ((t0 - tstart)
                        * (1.0 - 0.5 * fhphh + 1.0 / 6.0 * fhphhq - 1.0 / 24.0 * fhphhq * fhphh)
                        + (t1 - t0)
                            * (0.5 - 1.0 / 3.0 * fhphh + 1.0 / 8.0 * fhphhq
                                - 1.0 / 30.0 * fhphhq * fhphh))
            } else {
                (h_hat / (fhphh + context.n_fix))
                    * ((t1 - t0) * ((p0 * q0 - p1 * q1) / (fhphh + context.n_fix) - p1 * q1)
                        + (t0 - tstart) * (p0 * q0 - p1 * q1))
            };

            t0 = t1;
            p0 = p1;
            q0 = q1;
        }
        Ok(accrual)
    }
}

/// Below this the integrands' quotients are replaced by their Taylor expansions
/// (`isdacdsengine.cpp:183`, `:256`), when the engine was asked for them.
const TAYLOR_THRESHOLD: Real = 1.0e-4;

/// `isdacdsengine.cpp:77-84`: the specification fixes both curves on
/// Act/365 (Fixed), which C++ checks by comparing the day counters themselves.
/// A curve carrying none has no C++ counterpart - an absent one there is an
/// empty `DayCounter`, which compares unequal and trips this same check - and is
/// reported as `none`.
fn require_act_365_fixed(day_counter: Option<DayCounter>, curve: &str) -> QlResult<()> {
    match day_counter {
        Some(day_counter) if day_counter == Actual365Fixed::new() => Ok(()),
        Some(day_counter) => {
            fail!("{curve} term structure day counter ({day_counter}) should be Act/365(Fixed)")
        }
        None => fail!("{curve} term structure day counter (none) should be Act/365(Fixed)"),
    }
}

/// What the leg kernels run on once the compatibility checks have passed: the
/// C++ locals of `isdacdsengine.cpp:66-157`, gathered so that the kernels can be
/// written against them without reshaping the engine.
struct IsdaContext {
    discount: Shared<dyn YieldTermStructure>,
    probability: Shared<dyn DefaultProbabilityTermStructure>,
    /// The one field neither leg reads: the results tail measures the upfront
    /// payment and the accrual rebate against it (`isdacdsengine.cpp:292`,
    /// `:304`).
    eval_date: Date,
    effective_protection_start: Date,
    nodes: Vec<Date>,
    n_fix: Real,
    recovery_rate: Real,
    include_settlement_date_flows: Option<bool>,
    accrual_bias: AccrualBias,
    forwards_in_coupon_period: ForwardsInCouponPeriod,
    maturity: Date,
}

impl AsObservable for IsdaCdsEngine {
    fn observable(&self) -> &Observable {
        self.base.observable()
    }
}

impl PricingEngine for IsdaCdsEngine {
    fn arguments_mut(&mut self) -> &mut dyn Arguments {
        self.base.arguments_mut()
    }

    fn results(&self) -> &dyn Results {
        self.base.results()
    }

    fn reset(&mut self) {
        self.base.reset();
    }

    /// `isdacdsengine.cpp:52-364`: the validation and the setup (`:54-157`),
    /// both leg integrations (`:159-287`), and the results tail (`:289-364`)
    /// that signs the legs by protection side and derives the fair quotes from
    /// them.
    ///
    /// The fair spread's guard is C++'s verbatim: it tests the coupon leg alone
    /// (`:331`) yet divides by the coupon leg plus the accrual rebate (`:333`),
    /// so a contract whose coupon leg is worth nothing reports none even where
    /// the rebate alone would carry the quotient. The mid-point engine holds the
    /// same quirk (`midpointcdsengine.cpp:152-155`).
    fn calculate(&mut self) -> QlResult<()> {
        let context = self.validated()?;
        let (side, notional, spread, upfront, upfront_payment, accrual_rebate) = {
            let arguments = self.base.arguments();
            let (Some(side), Some(notional), Some(spread)) =
                (arguments.side, arguments.notional, arguments.spread)
            else {
                fail!("side, notional or spread not set");
            };
            let Some(upfront_payment) = arguments.upfront_payment.as_ref() else {
                fail!("upfront payment not set");
            };
            (
                side,
                notional,
                spread,
                arguments.upfront,
                Shared::clone(upfront_payment),
                arguments.accrual_rebate.as_ref().map(Shared::clone),
            )
        };

        let (mut default_leg_npv, mut coupon_leg_npv) = {
            let arguments = self.base.arguments();
            let Some(claim) = arguments.claim.as_ref() else {
                fail!("claim not set");
            };
            (
                self.protection_leg_npv(&context, &**claim, notional)?,
                self.premium_leg_npv(&context, &arguments.leg, notional)?,
            )
        };

        let mut upfront_pvo1 = 0.0;
        let mut upfront_npv = 0.0;
        if !upfront_payment.has_occurred(
            &self.settings,
            Some(context.eval_date),
            context.include_settlement_date_flows,
        )? {
            upfront_pvo1 = context
                .discount
                .discount_date(upfront_payment.date(), false)?;
            if upfront_payment.amount()? != 0.0 {
                upfront_npv = upfront_pvo1 * upfront_payment.amount()?;
            }
        }

        let mut accrual_rebate_npv = 0.0;
        if let Some(rebate) = accrual_rebate.as_ref()
            && rebate.amount()? != 0.0
            && !rebate.has_occurred(
                &self.settings,
                Some(context.eval_date),
                context.include_settlement_date_flows,
            )?
        {
            accrual_rebate_npv =
                context.discount.discount_date(rebate.date(), false)? * rebate.amount()?;
        }

        let mut upfront_sign = 1.0;
        match side {
            ProtectionSide::Seller => {
                default_leg_npv *= -1.0;
                accrual_rebate_npv *= -1.0;
            }
            ProtectionSide::Buyer => {
                coupon_leg_npv *= -1.0;
                upfront_npv *= -1.0;
                upfront_sign = -1.0;
            }
        }

        let fair_spread = if coupon_leg_npv != 0.0 {
            Some(-default_leg_npv * spread / (coupon_leg_npv + accrual_rebate_npv))
        } else {
            None
        };
        let upfront_sensitivity = upfront_pvo1 * notional;
        let fair_upfront = if upfront_sensitivity != 0.0 {
            Some(
                -upfront_sign * (default_leg_npv + coupon_leg_npv + accrual_rebate_npv)
                    / upfront_sensitivity,
            )
        } else {
            None
        };
        let coupon_leg_bps = if spread != 0.0 {
            Some(coupon_leg_npv * BASIS_POINT / spread)
        } else {
            None
        };
        let upfront_bps = match upfront {
            Some(upfront) if upfront != 0.0 => Some(upfront_npv * BASIS_POINT / upfront),
            _ => None,
        };

        let results = self.base.results_mut();
        results.instrument.value =
            Some(default_leg_npv + coupon_leg_npv + upfront_npv + accrual_rebate_npv);
        results.instrument.error_estimate = None;
        results.default_leg_npv = Some(default_leg_npv);
        results.coupon_leg_npv = Some(coupon_leg_npv);
        results.upfront_npv = Some(upfront_npv);
        results.accrual_rebate_npv = Some(accrual_rebate_npv);
        results.fair_spread = fair_spread;
        results.fair_upfront = fair_upfront;
        results.coupon_leg_bps = coupon_leg_bps;
        results.upfront_bps = upfront_bps;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    //! Oracle: the ISDA-compatibility block of `IsdaCdsEngine::calculate`
    //! (`isdacdsengine.cpp:54-98`), which is about which inputs the engine
    //! refuses and with which message.
    //!
    //! Every case starts from one fixture the engine accepts and corrupts a
    //! single dimension of it, and compares the message rather than only that an
    //! error came back: the checks run in a fixed order, so a case that broke two
    //! dimensions at once would pass on the wrong guard. The uncorrupted fixture
    //! is a case of its own and prices, which is what shows every guard above it
    //! passed.

    use super::*;
    use crate::instrument::Instrument;
    use crate::instruments::{Claim, CreditDefaultSwap, ProtectionSide};
    use crate::interestrate::Compounding;
    use crate::shared::shared;
    use crate::termstructures::credit::flathazardrate::FlatHazardRate;
    use crate::termstructures::yields::FlatForward;
    use crate::time::businessdayconvention::BusinessDayConvention;
    use crate::time::calendars::weekendsonly::WeekendsOnly;
    use crate::time::date::Month;
    use crate::time::daycounters::actual360::Actual360;
    use crate::time::frequency::Frequency;
    use crate::time::schedule::MakeSchedule;

    fn today() -> Date {
        Date::new(15, Month::June, 2026)
    }

    fn act365f() -> DayCounter {
        Actual365Fixed::new()
    }

    fn discount(reference: Date, day_counter: DayCounter) -> Handle<dyn YieldTermStructure> {
        Handle::new(shared(FlatForward::with_rate(
            reference,
            0.03,
            day_counter,
            Compounding::Continuous,
            Frequency::Annual,
        )) as Shared<dyn YieldTermStructure>)
    }

    fn credit(
        reference: Date,
        day_counter: DayCounter,
    ) -> Handle<dyn DefaultProbabilityTermStructure> {
        Handle::new(
            shared(FlatHazardRate::with_rate(reference, 0.02, day_counter))
                as Shared<dyn DefaultProbabilityTermStructure>,
        )
    }

    /// A claim outside the downcast seam, standing in for the claims the ISDA
    /// model does not settle (`isdacdsengine.cpp:97-98`).
    struct WholeNotionalClaim;

    impl Claim for WholeNotionalClaim {
        fn amount(&self, _default_date: &Date, notional: Real, _recovery_rate: Real) -> Real {
            notional
        }
    }

    /// Arms an engine over the two curves with a contract the ISDA model
    /// covers, then corrupts one dimension of the arguments.
    fn armed(
        discount: Handle<dyn YieldTermStructure>,
        credit: Handle<dyn DefaultProbabilityTermStructure>,
        corrupt: impl FnOnce(&mut CdsArguments),
    ) -> IsdaCdsEngine {
        let settings = shared(Settings::new());
        settings.set_evaluation_date(today());
        let mut engine = IsdaCdsEngine::new(credit, 0.4, discount, None, Shared::clone(&settings));
        let schedule = MakeSchedule::new()
            .from(today())
            .to(Date::new(15, Month::June, 2028))
            .with_frequency(Frequency::Semiannual)
            .with_calendar(WeekendsOnly::new())
            .build();
        let cds = CreditDefaultSwap::new(
            ProtectionSide::Seller,
            10_000_000.0,
            0.01,
            schedule,
            BusinessDayConvention::Following,
            Actual360::new(),
            true,
            true,
            settings,
        )
        .expect("the contract is well formed");
        cds.setup_arguments(engine.base.arguments_mut())
            .expect("the contract fills the arguments");
        corrupt(engine.base.arguments_mut());
        engine
    }

    /// What `calculate` reports for such an engine.
    fn refusal(
        discount: Handle<dyn YieldTermStructure>,
        credit: Handle<dyn DefaultProbabilityTermStructure>,
        corrupt: impl FnOnce(&mut CdsArguments),
    ) -> String {
        armed(discount, credit, corrupt)
            .calculate()
            .expect_err("the corrupted dimension is refused")
            .message()
            .to_string()
    }

    /// The fixture every argument-side case corrupts: both curves Act/365
    /// (Fixed) at the evaluation date, as the specification asks.
    fn compatible(corrupt: impl FnOnce(&mut CdsArguments)) -> String {
        refusal(
            discount(today(), act365f()),
            credit(today(), act365f()),
            corrupt,
        )
    }

    /// `isdacdsengine.cpp:77-84`.
    #[test]
    fn a_curve_that_does_not_count_act_365_fixed_is_refused() {
        assert_eq!(
            refusal(
                discount(today(), Actual360::new()),
                credit(today(), act365f()),
                |_| {}
            ),
            "yield term structure day counter (Actual/360) should be Act/365(Fixed)"
        );
        assert_eq!(
            refusal(
                discount(today(), act365f()),
                credit(today(), Actual360::new()),
                |_| {}
            ),
            "probability term structure day counter (Actual/360) should be Act/365(Fixed)"
        );
    }

    /// `isdacdsengine.cpp:85-92`: the date the curves are held against is the
    /// evaluation date the engine was threaded with (D5), not a clock.
    #[test]
    fn a_curve_referenced_off_the_evaluation_date_is_refused() {
        let tomorrow = today() + 1;
        assert_eq!(
            refusal(
                discount(tomorrow, act365f()),
                credit(today(), act365f()),
                |_| {}
            ),
            format!(
                "yield term structure reference date ({tomorrow}) should be evaluation date ({})",
                today()
            )
        );
        assert_eq!(
            refusal(
                discount(today(), act365f()),
                credit(tomorrow, act365f()),
                |_| {}
            ),
            format!(
                "probability term structure reference date ({tomorrow}) should be evaluation date ({})",
                today()
            )
        );
    }

    /// `isdacdsengine.cpp:93-98`: the three contract features the ISDA model
    /// does not cover, the last read through the [`Claim`] downcast seam.
    #[test]
    fn a_contract_feature_the_isda_model_does_not_cover_is_refused() {
        assert_eq!(
            compatible(|arguments| arguments.settles_accrual = false),
            "ISDA engine not compatible with non accrual paying CDS"
        );
        assert_eq!(
            compatible(|arguments| arguments.pays_at_default_time = false),
            "ISDA engine not compatible with end period payment"
        );
        assert_eq!(
            compatible(|arguments| {
                arguments.claim = Some(shared(WholeNotionalClaim) as Shared<dyn Claim>);
            }),
            "ISDA engine not compatible with non face value claim"
        );
    }

    /// The fixture every case above corrupts, left alone: it clears every guard
    /// and prices, which is what makes each of those cases a single-dimension
    /// corruption.
    #[test]
    fn a_compatible_contract_prices() {
        let mut engine = armed(
            discount(today(), act365f()),
            credit(today(), act365f()),
            |_| {},
        );
        engine.calculate().expect("the fixture clears every guard");
        assert!(engine.base.results().instrument.value.is_some());
    }

    /// What the checks leave behind for the kernels of #796: the flags the
    /// caller chose (`isdacdsengine.hpp:98-104`), the `10^-50` the no-fix
    /// variant puts into the denominators (`:157`), the protection start pushed
    /// past the evaluation date (`:100-102`) and the integration grid
    /// (`:150-156`), which two flat curves leave as the maturity alone.
    #[test]
    fn the_checks_leave_the_kernels_the_flags_and_the_grid() {
        let fixture = || {
            armed(
                discount(today(), act365f()),
                credit(today(), act365f()),
                |_| {},
            )
        };
        let defaulted = fixture().validated().expect("the fixture is compatible");
        assert_eq!(defaulted.n_fix, 0.0);
        assert_eq!(defaulted.accrual_bias, AccrualBias::HalfDayBias);
        assert_eq!(
            defaulted.forwards_in_coupon_period,
            ForwardsInCouponPeriod::Piecewise
        );
        assert_eq!(defaulted.effective_protection_start, today() + 1);
        assert_eq!(defaulted.nodes, vec![Date::new(15, Month::June, 2028)]);

        let chosen = fixture()
            .with_fidelity(
                NumericalFix::NoFix,
                AccrualBias::NoBias,
                ForwardsInCouponPeriod::Flat,
            )
            .validated()
            .expect("the fixture is compatible");
        assert_eq!(chosen.n_fix, 1.0e-50);
        assert_eq!(chosen.accrual_bias, AccrualBias::NoBias);
        assert_eq!(
            chosen.forwards_in_coupon_period,
            ForwardsInCouponPeriod::Flat
        );
    }
}

#[cfg(test)]
mod protection_leg {
    //! Oracle: the protection-leg integration (`isdacdsengine.cpp:159-199`).
    //!
    //! The grid this walks is not something a contract can be asked for, so
    //! every case grades it against a closed form instead of against a repriced
    //! contract. On curves of a flat continuous rate `r` and a flat hazard `h`,
    //! a step's exact integrand `h_hat / (f_hat + h_hat) (P0 Q0 - P1 Q1)`
    //! collapses to `h/(r+h) (P0 Q0 - P1 Q1)`, so the sum telescopes to
    //! `h/(r+h) (P0 Q0 - P_end Q_end)` whatever the nodes in between are. That
    //! value is arrived at without walking anything, which is what makes it an
    //! oracle for the walk rather than a restatement of it.
    //!
    //! The discount curve is interpolated rather than flat even though it prices
    //! flat: `isda_node_grid` takes the grid from the curves' own pillars, so
    //! two flat curves leave it as the maturity alone and nothing about the walk
    //! over it could be seen (which is what the scaffold's own grid case pins).
    //!
    //! The full numeric gate for this engine is the Markit grid of #798; these
    //! are the mechanical pins that gate cannot localise.

    use super::*;
    use crate::cashflows::SimpleCashFlow;
    use crate::math::interpolations::loglinear::LogLinear;
    use crate::shared::shared;
    use crate::termstructures::credit::flathazardrate::FlatHazardRate;
    use crate::termstructures::yields::InterpolatedDiscountCurve;
    use crate::time::date::{Month, SerialNumber};

    const NOTIONAL: Real = 10_000_000.0;
    const RECOVERY: Real = 0.4;

    pub(super) fn today() -> Date {
        Date::new(15, Month::June, 2026)
    }

    pub(super) fn act365f() -> DayCounter {
        Actual365Fixed::new()
    }

    /// Act/365 (Fixed) time from the evaluation date, which both curves count
    /// in and which the closed form below is stated in.
    pub(super) fn years(days: SerialNumber) -> Real {
        Real::from(days) / 365.0
    }

    /// A log-linear discount curve pillared at `offsets` days from today, its
    /// factors set to `exp(-rate t)`. It prices as a flat continuous curve - log
    /// linear in the discount factor is linear in `-rate t` - while contributing
    /// those pillars to the ISDA grid.
    fn nodal_discount(rate: Real, offsets: &[SerialNumber]) -> Handle<dyn YieldTermStructure> {
        let dates = offsets.iter().map(|days| today() + *days).collect();
        let discounts = offsets
            .iter()
            .map(|days| (-rate * years(*days)).exp())
            .collect();
        Handle::new(shared(
            InterpolatedDiscountCurve::<LogLinear>::new(dates, discounts, act365f(), None)
                .expect("the pillars increase and open at a discount factor of 1"),
        ) as Shared<dyn YieldTermStructure>)
    }

    /// The protection leg the walk should sum to, in closed form. The
    /// integration opens at the day before the effective protection start, which
    /// for a protection start on or before today is today itself, where the time
    /// is zero and both curves are `1`.
    fn analytic(rate: Real, hazard: Real, end: SerialNumber) -> Real {
        let total = rate + hazard;
        hazard / total * (1.0 - (-total * years(end)).exp()) * NOTIONAL * (1.0 - RECOVERY)
    }

    /// The protection leg of a contract on those curves, read out of the priced
    /// results.
    ///
    /// The arguments are set directly rather than through a contract: the
    /// protection leg reads only four of them, and a schedule would tie the
    /// maturity to a coupon date and so hide the clamp below. The rest are the
    /// least the results tail needs, on the buyer's side, which is the one that
    /// leaves the protection leg its own sign (`isdacdsengine.cpp:311-324`).
    fn protection_leg(
        rate: Real,
        hazard: Real,
        offsets: &[SerialNumber],
        maturity: SerialNumber,
        numerical_fix: NumericalFix,
    ) -> Real {
        let settings = shared(Settings::new());
        settings.set_evaluation_date(today());
        let credit = Handle::new(
            shared(FlatHazardRate::with_rate(today(), hazard, act365f()))
                as Shared<dyn DefaultProbabilityTermStructure>,
        );
        let mut engine = IsdaCdsEngine::new(
            credit,
            RECOVERY,
            nodal_discount(rate, offsets),
            None,
            settings,
        )
        .with_fidelity(
            numerical_fix,
            AccrualBias::HalfDayBias,
            ForwardsInCouponPeriod::Piecewise,
        );

        let arguments = engine.base.arguments_mut();
        arguments.notional = Some(NOTIONAL);
        arguments.claim = Some(shared(FaceValueClaim) as Shared<dyn Claim>);
        arguments.protection_start = Some(today());
        arguments.maturity = Some(today() + maturity);
        arguments.settles_accrual = true;
        arguments.pays_at_default_time = true;
        arguments.side = Some(ProtectionSide::Buyer);
        arguments.spread = Some(0.0);
        arguments.upfront_payment = Some(shared(
            SimpleCashFlow::new(0.0, today()).expect("a flow of nothing is well formed"),
        ));

        engine.calculate().expect("the arguments are complete");
        engine
            .base
            .results()
            .default_leg_npv
            .expect("the protection leg is valued")
    }

    /// `isdacdsengine.cpp:162-195`: the walk over a grid of several pillars sums
    /// to the closed form, and does so positive - the C++ comment at `:159`
    /// notwithstanding.
    #[test]
    fn the_walk_over_the_grid_sums_to_the_closed_form() {
        let value = protection_leg(0.03, 0.02, &[0, 30, 180, 400], 400, NumericalFix::NoFix);
        let expected = analytic(0.03, 0.02, 400);

        assert!(expected > 0.0, "a leg worth nothing would be vacuous");
        assert!(
            (value - expected).abs() <= 1.0e-12 * expected,
            "the walk summed to {value} rather than to the closed form {expected}"
        );
    }

    /// `isdacdsengine.cpp:170-172`: a pillar past the maturity is integrated to
    /// the maturity instead and ends the walk, rather than overshooting it.
    #[test]
    fn a_pillar_past_the_maturity_integrates_to_the_maturity() {
        let value = protection_leg(0.03, 0.02, &[0, 30, 180, 400], 300, NumericalFix::NoFix);
        let expected = analytic(0.03, 0.02, 300);
        let overshoot = analytic(0.03, 0.02, 400);

        assert!(
            overshoot - expected > 1.0e-3 * expected,
            "a grid that stopped at the maturity anyway would be vacuous"
        );
        assert!(
            (value - expected).abs() <= 1.0e-12 * expected,
            "the walk ran to {value} rather than to the maturity's {expected}"
        );
    }

    /// `isdacdsengine.cpp:183-190`: the Taylor series and the quotient it stands
    /// in for agree, and both meet the closed form, on a grid whose every step
    /// falls under the threshold.
    ///
    /// Two-day steps at a combined rate of `0.015` put `f + h` at `8.2e-5`,
    /// under the `1e-4` the series is selected below. It is graded against the
    /// closed form rather than only against the quotient, which is what makes
    /// the coefficients load bearing: raising the `1/6` term to `1/5` moves the
    /// sum by `2e-10` relative, and the `1/2` term by `1.4e-5`.
    #[test]
    fn the_taylor_series_meets_the_quotient_and_the_closed_form() {
        let offsets = [0, 1, 2, 3, 4, 5, 6, 7, 8];
        let taylor = protection_leg(0.01, 0.005, &offsets, 8, NumericalFix::Taylor);
        let quotient = protection_leg(0.01, 0.005, &offsets, 8, NumericalFix::NoFix);
        let expected = analytic(0.01, 0.005, 8);

        assert!(expected > 0.0, "a leg worth nothing would be vacuous");
        assert!(
            (taylor - expected).abs() <= 1.0e-12 * expected,
            "the series summed to {taylor} rather than to the closed form {expected}"
        );
        assert!(
            (quotient - expected).abs() <= 1.0e-12 * expected,
            "the quotient summed to {quotient} rather than to the closed form {expected}"
        );
        assert!(
            (taylor - quotient).abs() <= 1.0e-12 * expected,
            "the two arms parted by {} at the crossover",
            taylor - quotient
        );
    }

    /// What the numerical fix is for ([1] footnote 26, [2]), and the one case in
    /// which the two arms are told apart at all: a discount rate that cancels
    /// the hazard leaves `f + h` at zero, where the quotient divides a rounding
    /// error by `10^-50` and the series returns the limit.
    ///
    /// The limit of `h/(r+h) (1 - e^{-(r+h)t})` as `r + h` goes to zero is
    /// `h t`, which is what the series must reproduce. The quotient is asserted
    /// only to be nowhere near it, since what it returns is not a number this
    /// port should be pinned to.
    #[test]
    fn the_series_returns_the_limit_the_quotient_cannot() {
        let hazard = 0.02;
        let offsets = [0, 1, 2];
        let expected = hazard * years(2) * NOTIONAL * (1.0 - RECOVERY);
        let taylor = protection_leg(-hazard, hazard, &offsets, 2, NumericalFix::Taylor);
        let quotient = protection_leg(-hazard, hazard, &offsets, 2, NumericalFix::NoFix);

        assert!(
            (taylor - expected).abs() <= 1.0e-12 * expected,
            "the series returned {taylor} rather than the limit {expected}"
        );
        assert!(
            (quotient - expected).abs() > 0.5 * expected,
            "the quotient returned {quotient}, near enough the limit {expected} that the arms \
             cannot be told apart here"
        );
    }
}

#[cfg(test)]
mod premium_leg {
    //! Oracle: the premium-leg integration (`isdacdsengine.cpp:201-287`).
    //!
    //! The two kernels of this leg have no closed form the way the protection
    //! leg does, so what is pinned here is what the Markit grid of #798 would
    //! fail on without saying where: the survival factor and its one-day offset,
    //! which discounting alone would not produce; the day counters the
    //! specification allows; and that each of the two fidelity flags reaches the
    //! number at all, since a flag read but never applied prices identically to
    //! one that was never read.
    //!
    //! The curves are the protection leg's, for the same reason: the grid the
    //! accrual subdivides is the discount curve's own pillars, and two flat
    //! curves leave it as the maturity alone, where the piecewise flag has
    //! nothing to insert and could not be seen.

    use super::protection_leg::{act365f, today, years};
    use super::*;
    use crate::cashflow::CashFlow;
    use crate::cashflows::FixedRateCoupon;
    use crate::instrument::Instrument;
    use crate::instruments::{CreditDefaultSwap, ProtectionSide};
    use crate::interestrate::{Compounding, InterestRate};
    use crate::math::interpolations::loglinear::LogLinear;
    use crate::shared::shared;
    use crate::termstructures::credit::flathazardrate::FlatHazardRate;
    use crate::termstructures::yields::InterpolatedDiscountCurve;
    use crate::time::businessdayconvention::BusinessDayConvention;
    use crate::time::calendars::weekendsonly::WeekendsOnly;
    use crate::time::date::{Month, SerialNumber};
    use crate::time::daycounters::thirty360::{Convention, Thirty360};
    use crate::time::frequency::Frequency;
    use crate::time::schedule::MakeSchedule;

    const NOTIONAL: Real = 10_000_000.0;
    const SPREAD: Real = 0.01;
    const RECOVERY: Real = 0.4;
    const HAZARD: Real = 0.02;

    /// Pillars spread through the two years the contract runs, so that every
    /// coupon period has at least one strictly inside it.
    const PILLARS: [SerialNumber; 6] = [0, 30, 100, 200, 400, 800];

    /// The forward rate over each segment between those pillars.
    const FORWARDS: [Real; 5] = [0.01, 0.05, 0.02, 0.07, 0.03];

    /// A log-linear discount curve whose forward rate changes from pillar to
    /// pillar, which is what the piecewise flag needs to be seen at all: over a
    /// segment of constant forward the accrual integral is additive, so
    /// subdividing a flat curve returns the very same number.
    pub(super) fn stepped_discount() -> Handle<dyn YieldTermStructure> {
        let mut dates = vec![today()];
        let mut discounts = vec![1.0];
        let mut log_discount: Real = 0.0;
        for (segment, forward) in FORWARDS.iter().enumerate() {
            log_discount -= forward * (years(PILLARS[segment + 1]) - years(PILLARS[segment]));
            dates.push(today() + PILLARS[segment + 1]);
            discounts.push(log_discount.exp());
        }
        Handle::new(shared(
            InterpolatedDiscountCurve::<LogLinear>::new(dates, discounts, act365f(), None)
                .expect("the pillars increase and open at a discount factor of 1"),
        ) as Shared<dyn YieldTermStructure>)
    }

    /// That curve's discount factor, read off a curve built again rather than
    /// out of the engine's own.
    pub(super) fn discount_factor(date: Date) -> Real {
        stepped_discount()
            .current_link()
            .expect("the handle is linked")
            .discount_date(date, false)
            .expect("the date is inside the curve")
    }

    /// The flat hazard curve's survival probability, by hand.
    fn survival(date: Date) -> Real {
        (-HAZARD * years(date - today())).exp()
    }

    /// An engine over the nodal curves, armed with a two-year semiannual
    /// contract counted in `day_counter`.
    fn armed(
        day_counter: DayCounter,
        accrual_bias: AccrualBias,
        forwards_in_coupon_period: ForwardsInCouponPeriod,
    ) -> IsdaCdsEngine {
        let settings = shared(Settings::new());
        settings.set_evaluation_date(today());
        let credit = Handle::new(
            shared(FlatHazardRate::with_rate(today(), HAZARD, act365f()))
                as Shared<dyn DefaultProbabilityTermStructure>,
        );
        let mut engine = IsdaCdsEngine::new(
            credit,
            RECOVERY,
            stepped_discount(),
            None,
            Shared::clone(&settings),
        )
        .with_fidelity(
            NumericalFix::Taylor,
            accrual_bias,
            forwards_in_coupon_period,
        );

        let schedule = MakeSchedule::new()
            .from(today())
            .to(Date::new(15, Month::June, 2028))
            .with_frequency(Frequency::Semiannual)
            .with_calendar(WeekendsOnly::new())
            .build();
        let cds = CreditDefaultSwap::new(
            ProtectionSide::Seller,
            NOTIONAL,
            SPREAD,
            schedule,
            BusinessDayConvention::Following,
            day_counter,
            true,
            true,
            settings,
        )
        .expect("the contract is well formed");
        cds.setup_arguments(engine.base.arguments_mut())
            .expect("the contract fills the arguments");
        engine
    }

    /// The premium leg of a priced contract. Every fixture here is on the
    /// seller's side, which is the one that leaves the premium leg its own sign
    /// (`isdacdsengine.cpp:311-324`).
    fn coupon_leg(engine: &mut IsdaCdsEngine) -> Real {
        engine.calculate().expect("the contract prices");
        engine
            .base
            .results()
            .coupon_leg_npv
            .expect("the premium leg is valued")
    }

    /// `isdacdsengine.cpp:205-211`: the specification fixes the premium leg's
    /// day count on the three conventions it names, and refuses the rest.
    #[test]
    fn a_coupon_counted_outside_the_isda_conventions_is_refused() {
        for accepted in [act365f(), Actual360::new(), Actual360::with_last_day(true)] {
            assert!(
                armed(
                    accepted.clone(),
                    AccrualBias::HalfDayBias,
                    ForwardsInCouponPeriod::Piecewise,
                )
                .calculate()
                .is_ok(),
                "a leg counted in {accepted} should price"
            );
        }

        assert_eq!(
            armed(
                Thirty360::with_convention(Convention::BondBasis),
                AccrualBias::HalfDayBias,
                ForwardsInCouponPeriod::Piecewise,
            )
            .calculate()
            .expect_err("the day counter is outside the specification")
            .message(),
            "ISDA engine requires a coupon day counter Act/365Fixed or Act/360 \
             (30/360 (Bond Basis))"
        );
    }

    /// `isdacdsengine.cpp:214-219`: a coupon is discounted to its payment date
    /// but carried by the survival to the day *before* it.
    ///
    /// The leg is one coupon whose accrual closed before the protection opens,
    /// which the accrual guard (`:223-224`) drops, so the premium leg is that
    /// coupon alone and can be read against the curves directly. The two mutants
    /// the identity has to exclude - the survival read on the payment date, and
    /// no survival factor at all - are computed alongside it and asserted to
    /// differ, so neither could pass this fixture.
    #[test]
    fn a_coupon_is_carried_by_the_survival_to_the_day_before_it_pays() {
        let payment = today() + 30;
        let mut engine = armed(
            act365f(),
            AccrualBias::HalfDayBias,
            ForwardsInCouponPeriod::Piecewise,
        );
        let coupon = shared(FixedRateCoupon::new(
            payment,
            NOTIONAL,
            InterestRate::new(SPREAD, act365f(), Compounding::Simple, Frequency::Annual)
                .expect("a simple annual rate is well formed"),
            today() - 180,
            today() - 1,
            None,
            None,
            None,
        ));
        let amount = Coupon::amount(&*coupon).expect("the coupon accrues an amount");
        engine.base.arguments_mut().leg = vec![coupon as Shared<dyn CashFlow>];

        let discount = discount_factor(payment);
        let expected = amount * discount * survival(payment - 1);
        let on_the_payment_date = amount * discount * survival(payment);
        let without_survival = amount * discount;

        assert!(
            (expected - on_the_payment_date).abs() > 1.0e-9 * expected,
            "a fixture whose survival does not move over a day could not see the offset"
        );
        assert!(
            (expected - without_survival).abs() > 1.0e-3 * expected,
            "a fixture surviving with certainty could not see the factor at all"
        );
        assert!(
            (coupon_leg(&mut engine) - expected).abs() <= 1.0e-12 * expected,
            "the premium leg came to {} rather than to {expected}",
            coupon_leg(&mut engine)
        );
    }

    /// `isdacdsengine.cpp:228-230`: the half-day bias reaches the accrual.
    ///
    /// The two settings differ by the `1/730` of a year the biased one shifts
    /// `tstart` back by, which is worth about a day's accrual on each period, so
    /// the biased leg is worth strictly more.
    #[test]
    fn the_half_day_bias_moves_the_accrual() {
        let biased = coupon_leg(&mut armed(
            act365f(),
            AccrualBias::HalfDayBias,
            ForwardsInCouponPeriod::Piecewise,
        ));
        let unbiased = coupon_leg(&mut armed(
            act365f(),
            AccrualBias::NoBias,
            ForwardsInCouponPeriod::Piecewise,
        ));

        assert!(unbiased > 0.0, "a leg worth nothing would be vacuous");
        assert!(
            biased > unbiased,
            "the biased leg came to {biased}, not above the unbiased {unbiased}"
        );
    }

    /// `isdacdsengine.cpp:231-241`: subdividing a coupon period at the grid's
    /// own pillars reaches the accrual.
    ///
    /// The fixture's pillars fall strictly inside the coupon periods, which is
    /// what the two settings part over: with none inside, both integrate each
    /// period in one step and price identically.
    #[test]
    fn the_pillars_inside_a_coupon_period_move_the_accrual() {
        let piecewise = coupon_leg(&mut armed(
            act365f(),
            AccrualBias::NoBias,
            ForwardsInCouponPeriod::Piecewise,
        ));
        let flat = coupon_leg(&mut armed(
            act365f(),
            AccrualBias::NoBias,
            ForwardsInCouponPeriod::Flat,
        ));

        assert!(flat > 0.0, "a leg worth nothing would be vacuous");
        assert!(
            (piecewise - flat).abs() > 1.0e-12 * flat,
            "subdividing the periods left the leg at {piecewise}, apart from {flat} by nothing"
        );
    }
}

#[cfg(test)]
mod results_tail {
    //! Oracle: the results tail (`isdacdsengine.cpp:289-364`).
    //!
    //! The numeric acceptance for this engine is the Markit grid of #798; what
    //! is pinned here is the wiring that grid would fail on without saying
    //! where - which leg each protection side negates, the identity the value
    //! sums to, the two fair quotes and the sentinels the sensitivities fall
    //! back on - and the one place the port keeps a C++ quirk rather than the
    //! formula the quirk was meant to be.
    //!
    //! Every case prices one contract whose accrual opened three months before
    //! the evaluation date and whose trade date is the evaluation date itself,
    //! so that the accrual rebate is worth a quarter's coupon rather than
    //! nothing. That is what the fair-spread case needs: the divisor is the
    //! coupon leg plus the rebate, which a rebate worth nothing could not be
    //! told from the coupon leg alone.

    use super::premium_leg::{discount_factor, stepped_discount};
    use super::protection_leg::{act365f, today};
    use super::*;
    use crate::instrument::Instrument;
    use crate::instruments::{CdsTerms, CreditDefaultSwap};
    use crate::shared::shared;
    use crate::termstructures::credit::flathazardrate::FlatHazardRate;
    use crate::time::businessdayconvention::BusinessDayConvention;
    use crate::time::calendars::weekendsonly::WeekendsOnly;
    use crate::time::date::Month;
    use crate::time::frequency::Frequency;
    use crate::time::schedule::MakeSchedule;

    const NOTIONAL: Real = 10_000_000.0;
    const SPREAD: Rate = 0.01;
    const UPFRONT: Rate = 0.05;
    const RECOVERY: Real = 0.4;
    const HAZARD: Real = 0.02;

    /// A two-year semiannual contract on the premium leg's curves, quoted on an
    /// upfront and a running spread and traded today, straddling the evaluation
    /// date inside its first coupon period.
    fn armed(
        side: ProtectionSide,
        spread: Rate,
        upfront: Rate,
        upfront_date: Option<Date>,
    ) -> (IsdaCdsEngine, CreditDefaultSwap) {
        let settings = shared(Settings::new());
        settings.set_evaluation_date(today());
        let credit = Handle::new(
            shared(FlatHazardRate::with_rate(today(), HAZARD, act365f()))
                as Shared<dyn DefaultProbabilityTermStructure>,
        );
        let mut engine = IsdaCdsEngine::new(
            credit,
            RECOVERY,
            stepped_discount(),
            None,
            Shared::clone(&settings),
        );
        let schedule = MakeSchedule::new()
            .from(Date::new(15, Month::March, 2026))
            .to(Date::new(15, Month::March, 2028))
            .with_frequency(Frequency::Semiannual)
            .with_calendar(WeekendsOnly::new())
            .build();
        let cds = CreditDefaultSwap::with_upfront_and_terms(
            side,
            NOTIONAL,
            upfront,
            spread,
            schedule,
            BusinessDayConvention::Following,
            Actual360::new(),
            CdsTerms {
                settles_accrual: true,
                pays_at_default_time: true,
                trade_date: Some(today()),
                upfront_date,
                ..CdsTerms::default()
            },
            settings,
        )
        .expect("the contract is well formed");
        cds.setup_arguments(engine.base.arguments_mut())
            .expect("the contract fills the arguments");
        (engine, cds)
    }

    /// The four signed legs a priced fixture leaves behind, in the order the
    /// value sums them (`isdacdsengine.cpp:326`).
    fn legs(results: &CdsResults) -> (Real, Real, Real, Real) {
        (
            results
                .default_leg_npv
                .expect("the protection leg is valued"),
            results.coupon_leg_npv.expect("the premium leg is valued"),
            results.upfront_npv.expect("the upfront is valued"),
            results
                .accrual_rebate_npv
                .expect("the accrual rebate is valued"),
        )
    }

    /// `isdacdsengine.cpp:326-328`: the value is the four signed legs, and the
    /// engine offers no error estimate.
    ///
    /// The sum is asserted exactly, in the order C++ adds it in: the legs are
    /// read back out of the results, so anything but the same additions would
    /// be a different number, not a rounding of this one.
    #[test]
    fn the_value_sums_the_four_signed_legs() {
        for side in [ProtectionSide::Seller, ProtectionSide::Buyer] {
            let (mut engine, _) = armed(side, SPREAD, UPFRONT, None);
            engine.calculate().expect("the contract prices");
            let results = engine.base.results();
            let (default_leg, coupon_leg, upfront, rebate) = legs(results);

            for leg in [default_leg, coupon_leg, upfront, rebate] {
                assert!(
                    leg != 0.0 && leg.is_finite(),
                    "a leg worth {leg} could not tell one sum from another"
                );
            }
            assert_eq!(
                results.instrument.value,
                Some(default_leg + coupon_leg + upfront + rebate)
            );
            assert_eq!(results.instrument.error_estimate, None);
        }
    }

    /// `isdacdsengine.cpp:311-324`: the seller negates the protection leg and
    /// the rebate, the buyer the premium leg and the upfront.
    ///
    /// Each leg is pinned on its own rather than only through the sum, which
    /// two legs swapped between the arms would leave untouched; and since the
    /// two sides negate disjoint halves of the same four, the contract is worth
    /// exactly the opposite to each of them, bit for bit.
    #[test]
    fn each_side_negates_its_own_two_legs() {
        let (mut seller, _) = armed(ProtectionSide::Seller, SPREAD, UPFRONT, None);
        seller.calculate().expect("the contract prices");
        let (mut buyer, _) = armed(ProtectionSide::Buyer, SPREAD, UPFRONT, None);
        buyer.calculate().expect("the contract prices");

        let sold = legs(seller.base.results());
        let bought = legs(buyer.base.results());

        assert!(sold.0 < 0.0 && sold.1 > 0.0 && sold.2 > 0.0 && sold.3 < 0.0);
        assert!(bought.0 > 0.0 && bought.1 < 0.0 && bought.2 < 0.0 && bought.3 > 0.0);
        assert_eq!(sold.0, -bought.0);
        assert_eq!(sold.1, -bought.1);
        assert_eq!(sold.2, -bought.2);
        assert_eq!(sold.3, -bought.3);
        assert_eq!(
            seller.base.results().instrument.value,
            buyer.base.results().instrument.value.map(|value| -value)
        );
    }

    /// `isdacdsengine.cpp:331-337`, quirk included: the guard tests the premium
    /// leg alone yet the divisor is the premium leg plus the accrual rebate.
    ///
    /// The two divisors are told apart here, which is what makes this the quirk
    /// and not a restatement: the fixture's rebate is worth about a percent of
    /// its premium leg, so dividing by the premium leg alone moves the fair
    /// spread far past any rounding. The rebate is asserted to carry that
    /// weight before the quirk is asserted at all, since a rebate worth nothing
    /// would make the two formulas the same expression.
    #[test]
    fn the_fair_spread_divides_by_the_premium_leg_and_the_rebate() {
        let (mut engine, _) = armed(ProtectionSide::Seller, SPREAD, UPFRONT, None);
        engine.calculate().expect("the contract prices");
        let results = engine.base.results();
        let (default_leg, coupon_leg, _, rebate) = legs(results);

        assert!(
            rebate.abs() > 1.0e-4 * coupon_leg.abs(),
            "a rebate of {rebate} against a premium leg of {coupon_leg} could not tell the \
             divisors apart"
        );
        let faithful = -default_leg * SPREAD / (coupon_leg + rebate);
        let premium_leg_alone = -default_leg * SPREAD / coupon_leg;

        assert_eq!(results.fair_spread, Some(faithful));
        assert!(
            (faithful - premium_leg_alone).abs() > 1.0e-6 * faithful.abs(),
            "the two divisors agreed to {faithful}, so the quirk is not pinned"
        );
    }

    /// `isdacdsengine.cpp:339-347`: the fair upfront is what the three other
    /// signed legs come to, per unit of the upfront's own sensitivity, against
    /// the sign the protection side gives that sensitivity.
    ///
    /// The sensitivity is rebuilt from the contract's own settlement date and a
    /// curve built again, rather than from the upfront NPV the same tail
    /// wrote, so the discount factor is an input to this case and not an
    /// output of it.
    #[test]
    fn the_fair_upfront_prices_the_other_three_legs() {
        for (side, upfront_sign) in [(ProtectionSide::Seller, 1.0), (ProtectionSide::Buyer, -1.0)] {
            let (mut engine, cds) = armed(side, SPREAD, UPFRONT, None);
            engine.calculate().expect("the contract prices");
            let results = engine.base.results();
            let (default_leg, coupon_leg, _, rebate) = legs(results);

            let sensitivity =
                discount_factor(Event::date(cds.upfront_payment().as_ref())) * NOTIONAL;
            assert!(
                sensitivity > 0.0,
                "an upfront still owed should carry a sensitivity"
            );
            let expected: Real = -upfront_sign * (default_leg + coupon_leg + rebate) / sensitivity;
            let fair_upfront = results.fair_upfront.expect("the fair upfront is available");

            assert!(
                expected != 0.0 && expected.is_finite(),
                "a fair upfront of {expected} would be vacuous"
            );
            assert!(
                (fair_upfront - expected).abs() <= 1.0e-12 * expected.abs(),
                "the fair upfront came to {fair_upfront} rather than to {expected}"
            );
        }
    }

    /// `isdacdsengine.cpp:341-346`: an upfront the cash settlement date has
    /// already seen leaves no sensitivity, and so no fair upfront.
    ///
    /// The rebate settles on that same date (`creditdefaultswap.cpp:543-546`),
    /// so it is worth nothing here too, which is what leaves the guard the only
    /// thing that could return the sentinel.
    #[test]
    fn an_upfront_already_settled_prices_no_fair_upfront() {
        let (mut engine, _) = armed(ProtectionSide::Seller, SPREAD, UPFRONT, Some(today() - 1));
        engine.calculate().expect("the contract prices");
        let results = engine.base.results();

        assert_eq!(results.upfront_npv, Some(0.0));
        assert_eq!(results.accrual_rebate_npv, Some(0.0));
        assert_eq!(results.fair_upfront, None);
        assert!(results.fair_spread.is_some());
    }

    /// `isdacdsengine.cpp:349-364`: each sensitivity is a basis point of its own
    /// leg per unit of its own quote, and reports the sentinel where that quote
    /// is nothing to divide by.
    #[test]
    fn the_sensitivities_follow_the_quotes_they_divide_by() {
        let (mut quoted, _) = armed(ProtectionSide::Seller, SPREAD, UPFRONT, None);
        quoted.calculate().expect("the contract prices");
        let results = quoted.base.results();
        let (_, coupon_leg, upfront, _) = legs(results);

        assert_eq!(results.coupon_leg_bps, Some(coupon_leg * 1.0e-4 / SPREAD));
        assert_eq!(results.upfront_bps, Some(upfront * 1.0e-4 / UPFRONT));

        let (mut unquoted, _) = armed(ProtectionSide::Seller, 0.0, 0.0, None);
        unquoted.calculate().expect("the contract prices");
        let results = unquoted.base.results();

        assert_eq!(results.coupon_leg_npv, Some(0.0));
        assert_eq!(results.coupon_leg_bps, None);
        assert_eq!(results.upfront_bps, None);
        assert_eq!(results.fair_spread, None);
    }
}

/// The ISDA/Markit acceptance oracle: the 20-case upfront grid
/// (`creditdefaultswap.cpp:567-722`) and the two single-record reconciliations
/// (`:759-861` and `:863-960`), all priced through the contract's own
/// [`implied_hazard_rate`](crate::instruments::CreditDefaultSwap::implied_hazard_rate)
/// rather than a hazard rate handed in.
#[cfg(test)]
mod markit_oracle {
    use super::*;
    use crate::currency::Currency;
    use crate::event::Event;
    use crate::indexes::iborindex::IborIndex;
    use crate::instrument::Instrument;
    use crate::instruments::{MakeCreditDefaultSwap, PricingModel, ProtectionSide};
    use crate::math::interpolations::loglinear::LogLinear;
    use crate::pricingengine::PricingEngine;
    use crate::shared::{SharedMut, shared, shared_mut};
    use crate::termstructures::bootstraphelper::RateHelper;
    use crate::termstructures::bootstraptraits::Discount;
    use crate::termstructures::credit::flathazardrate::FlatHazardRate;
    use crate::termstructures::yields::{DepositRateHelper, PiecewiseYieldCurve, SwapRateHelper};
    use crate::time::businessdayconvention::BusinessDayConvention;
    use crate::time::calendars::weekendsonly::WeekendsOnly;
    use crate::time::date::Month;
    use crate::time::daycounters::actual360::Actual360;
    use crate::time::daycounters::actual365fixed::Actual365Fixed;
    use crate::time::daycounters::thirty360::{Convention, Thirty360};
    use crate::time::frequency::Frequency;
    use crate::time::period::Period;
    use crate::time::timeunit::TimeUnit;
    use crate::types::Integer;

    const NOTIONAL: Real = 10_000_000.0;

    /// Boost's `BOOST_CHECK_CLOSE`, which `QL_CHECK_CLOSE` forwards to: the
    /// tolerance is a *percentage*, and the strong form requires the relative
    /// difference to clear it against both operands. A `tolerance` of 1e-6 is
    /// therefore a relative 1e-8, not a relative 1e-6.
    fn assert_close(actual: Real, expected: Real, tolerance: Real, what: &str) {
        let fraction = tolerance / 100.0;
        let difference = (actual - expected).abs();
        assert!(
            difference <= fraction * actual.abs() && difference <= fraction * expected.abs(),
            "{what}: {actual} is not within {tolerance}% of {expected} \
             (relative {})",
            difference / expected.abs()
        );
    }

    /// The ISDA-convention forecasting index the C++ fixture builds inline
    /// (`creditdefaultswap.cpp:616-618` and `:796-798`).
    ///
    /// The deposit helpers take one of these too: C++ reaches the same
    /// conventions through `DepositRateHelper`'s explicit-convention
    /// constructor, which synthesises exactly this index internally
    /// (`ratehelpers.cpp:41-50`), while the port's helper takes the index
    /// outright. The forwarding handle is left empty because both helpers'
    /// builders re-point their own clone of the index at the curve being
    /// bootstrapped.
    ///
    /// C++ leaves that synthesised index an empty `Currency()`, which has no
    /// counterpart here; the currency the fixture passes is inert, since the
    /// swap helper is given its fixed-leg frequency and day counter outright
    /// and so never reaches the currency defaults.
    fn isda_ibor(
        tenor: Period,
        currency: Currency,
        settings: &Shared<Settings<Date>>,
    ) -> IborIndex {
        IborIndex::new(
            "IsdaIbor".to_string(),
            tenor,
            2,
            currency,
            WeekendsOnly::new(),
            BusinessDayConvention::ModifiedFollowing,
            false,
            Actual360::new(),
            Handle::empty(),
            Shared::clone(settings),
        )
    }

    /// The ISDA-compliant discount curve: deposits in months, swaps in years,
    /// bootstrapped log-linearly on discount factors over Act/365F
    /// (`creditdefaultswap.cpp:628-632`).
    ///
    /// C++ builds it with a moving reference date at zero settlement days; the
    /// port's piecewise curve takes the reference date outright, and the caller
    /// passes the evaluation date, which is what zero settlement days on a
    /// business day resolve to.
    fn isda_curve(
        reference: Date,
        deposits: &[(Integer, Real)],
        swaps: &[(Integer, Real)],
        float_tenor: Period,
        fixed_frequency: Frequency,
        currency: Currency,
        settings: &Shared<Settings<Date>>,
    ) -> Handle<dyn YieldTermStructure> {
        let mut helpers: Vec<Shared<dyn RateHelper>> = Vec::new();
        for (months, quote) in deposits {
            let index = isda_ibor(
                Period::new(*months, TimeUnit::Months),
                currency.clone(),
                settings,
            );
            helpers.push(DepositRateHelper::from_rate(*quote, &index) as Shared<dyn RateHelper>);
        }
        let float_index = isda_ibor(float_tenor, currency, settings);
        for (years, quote) in swaps {
            helpers.push(SwapRateHelper::from_rate(
                *quote,
                Period::new(*years, TimeUnit::Years),
                WeekendsOnly::new(),
                fixed_frequency,
                BusinessDayConvention::ModifiedFollowing,
                Thirty360::with_convention(Convention::BondBasis),
                &float_index,
            ) as Shared<dyn RateHelper>);
        }
        let curve = PiecewiseYieldCurve::<Discount, LogLinear>::new(
            reference,
            helpers,
            Actual365Fixed::new(),
            LogLinear,
        )
        .expect("the ISDA rate helpers bootstrap");
        Handle::new(curve as Shared<dyn YieldTermStructure>)
    }

    /// The engine the fixture prices on, over the flat hazard rate `h` and the
    /// three fidelity flags C++ spells out (`creditdefaultswap.cpp:686-688`).
    fn isda_engine(
        hazard_rate: Rate,
        recovery: Real,
        discount: &Handle<dyn YieldTermStructure>,
        settings: &Shared<Settings<Date>>,
    ) -> SharedMut<dyn PricingEngine> {
        let probability = Handle::new(shared(FlatHazardRate::moving_with_rate(
            0,
            WeekendsOnly::new(),
            hazard_rate,
            Actual365Fixed::new(),
            Shared::clone(settings),
        )) as Shared<dyn DefaultProbabilityTermStructure>);
        shared_mut(
            IsdaCdsEngine::new(
                probability,
                recovery,
                discount.clone(),
                None,
                Shared::clone(settings),
            )
            .with_fidelity(
                NumericalFix::Taylor,
                AccrualBias::HalfDayBias,
                ForwardsInCouponPeriod::Piecewise,
            ),
        ) as SharedMut<dyn PricingEngine>
    }

    /// `creditdefaultswap.cpp:583-588`: the Markit deposit quotes, in months.
    const USD_DEPOSITS: [(Integer, Real); 6] = [
        (1, 0.003081),
        (2, 0.005525),
        (3, 0.007163),
        (6, 0.012413),
        (9, 0.014),
        (12, 0.015488),
    ];

    /// `creditdefaultswap.cpp:598-611`: the Markit swap quotes, in years.
    const USD_SWAPS: [(Integer, Real); 14] = [
        (2, 0.011907),
        (3, 0.01699),
        (4, 0.021198),
        (5, 0.02444),
        (6, 0.026937),
        (7, 0.028967),
        (8, 0.030504),
        (9, 0.031719),
        (10, 0.03279),
        (12, 0.034535),
        (15, 0.036217),
        (20, 0.036981),
        (25, 0.037246),
        (30, 0.037605),
    ];

    /// `creditdefaultswap.cpp:643-664`: the ISDA-model upfronts on a ten-million
    /// notional, in the term-date / spread / recovery order the loop visits.
    const MARKIT_VALUES: [Real; 20] = [
        -97798.29358,
        -97776.11889,
        914971.5977,
        894985.6298,
        -186921.3594,
        -186839.8148,
        1646623.672,
        1579803.626,
        -274298.9203,
        -274122.4725,
        2279730.93,
        2147972.527,
        -592420.2297,
        -591571.2294,
        3993550.206,
        3545843.418,
        -797501.1422,
        -795915.9787,
        4702034.688,
        4042340.999,
    ];

    /// `creditdefaultswap.cpp:567-722`: 20 conventional trades reprice to the
    /// Markit upfronts, and both sides of each are worth nothing once the fair
    /// upfront is paid.
    ///
    /// Each case implies a flat hazard rate off a quoted trade through the ISDA
    /// branch of
    /// [`implied_hazard_rate`](crate::instruments::CreditDefaultSwap::implied_hazard_rate),
    /// then prices a 1% conventional trade of the same maturity on that rate.
    ///
    /// ## Tolerance
    ///
    /// C++ picks its tolerance off `IborCoupon::Settings::usingAtParCoupons()`
    /// (`creditdefaultswap.cpp:666-669`): 1e-6 with at-par coupons, 1e-3 with
    /// indexed ones, because indexed coupons leave the bootstrapped risk-free
    /// curve "a bit off". The port threads that flag on
    /// [`Settings`](crate::settings::Settings::using_at_par_coupons) instead of
    /// a singleton, defaulting to at-par, and the fixture leaves it there - so
    /// the at-par arm is the live one and 1e-6 is the tolerance pinned. All 20
    /// cases clear it with room: the worst relative error is 1.2e-9 against the
    /// 1e-8 that a 1e-6 *percentage* allows, so no case is excluded.
    ///
    /// The tight arm is load-bearing rather than merely available. Corrupting
    /// the premium leg's survival offset (`isdacdsengine.cpp:218`) moves case 0
    /// by a relative 4.0e-6, which the loose 1e-3 arm - a relative 1e-5 - would
    /// have let through.
    #[test]
    fn the_markit_grid_reproduces_the_isda_upfronts() {
        const TOLERANCE: Real = 1.0e-6;
        let settings = shared(Settings::<Date>::new());
        let trade_date = Date::new(21, Month::May, 2009);
        settings.set_evaluation_date(trade_date);
        let discount = isda_curve(
            trade_date,
            &USD_DEPOSITS,
            &USD_SWAPS,
            Period::new(3, TimeUnit::Months),
            Frequency::Semiannual,
            Currency::usd(),
            &settings,
        );

        let term_dates = [
            Date::new(20, Month::June, 2010),
            Date::new(20, Month::June, 2011),
            Date::new(20, Month::June, 2012),
            Date::new(20, Month::June, 2016),
            Date::new(20, Month::June, 2019),
        ];
        let mut case = 0;
        for term_date in term_dates {
            for spread in [0.001, 0.1] {
                for recovery in [0.2, 0.4] {
                    let trade = |running: Rate| {
                        MakeCreditDefaultSwap::from_term_date(
                            term_date,
                            running,
                            Shared::clone(&settings),
                        )
                        .with_nominal(NOTIONAL)
                    };
                    let hazard_rate = trade(spread)
                        .build()
                        .expect("the quoted trade builds")
                        .implied_hazard_rate(
                            0.0,
                            &discount,
                            Actual365Fixed::new(),
                            recovery,
                            1.0e-10,
                            PricingModel::Isda,
                        )
                        .expect("the quoted trade inverts on the ISDA engine");
                    let engine = isda_engine(hazard_rate, recovery, &discount, &settings);

                    let mut conventional = trade(0.01).build().expect("the trade builds");
                    conventional
                        .base_mut()
                        .set_pricing_engine(SharedMut::clone(&engine));
                    let fair_upfront = conventional.fair_upfront().expect("the trade prices");
                    assert_close(
                        conventional.notional() * fair_upfront,
                        MARKIT_VALUES[case],
                        TOLERANCE,
                        &format!("case {case} ({term_date}, spread {spread}, recovery {recovery})"),
                    );

                    for side in [ProtectionSide::Buyer, ProtectionSide::Seller] {
                        let mut at_fair = trade(0.01)
                            .with_upfront_rate(fair_upfront)
                            .with_side(side)
                            .build()
                            .expect("the trade builds");
                        at_fair
                            .base_mut()
                            .set_pricing_engine(SharedMut::clone(&engine));
                        let npv = at_fair.npv().expect("the trade prices");
                        assert!(
                            npv.abs() <= TOLERANCE,
                            "case {case} {side:?} is worth {npv} at its own fair upfront"
                        );
                    }
                    case += 1;
                }
            }
        }
    }

    /// `creditdefaultswap.cpp:770-771` and `:875-876`: the EUR deposit quotes,
    /// in months. Both reconciliation cases build the same curve.
    const EUR_DEPOSITS: [(Integer, Real); 4] = [
        (1, -0.0056),
        (3, -0.005440),
        (6, -0.005190),
        (12, -0.004930),
    ];

    /// `creditdefaultswap.cpp:781-793` and `:886-898`: the EUR swap quotes, in
    /// years.
    const EUR_SWAPS: [(Integer, Real); 13] = [
        (2, -0.004820),
        (3, -0.004420),
        (4, -0.003990),
        (5, -0.003520),
        (6, -0.002970),
        (7, -0.002370),
        (8, -0.001760),
        (9, -0.001140),
        (10, -0.000540),
        (12, 0.000570),
        (15, 0.001880),
        (20, 0.002940),
        (30, 0.002820),
    ];

    /// `creditdefaultswap.cpp:765` and `:869`: today, on both cases.
    fn reconcile_value_date() -> Date {
        Date::new(26, Month::July, 2021)
    }

    const RECONCILE_NOMINAL: Real = 1.0e6;
    const RECONCILE_RECOVERY: Real = 0.4;
    const RECONCILE_TOLERANCE: Real = 1.0e-3;
    const CONVENTIONAL_SPREAD: Rate = 0.006713;

    /// The EUR curve of both reconciliation cases, and the engine over the flat
    /// hazard rate implied off a trade quoted at the conventional spread
    /// (`creditdefaultswap.cpp:764-826`, which `:868-931` repeats verbatim).
    ///
    /// The evaluation date is set before the helpers are built, since they date
    /// themselves off it.
    fn reconcile_engine(settings: &Shared<Settings<Date>>) -> (SharedMut<dyn PricingEngine>, Date) {
        let value_date = reconcile_value_date();
        settings.set_evaluation_date(value_date);
        let discount = isda_curve(
            value_date,
            &EUR_DEPOSITS,
            &EUR_SWAPS,
            Period::new(6, TimeUnit::Months),
            Frequency::Annual,
            Currency::eur(),
            settings,
        );
        let maturity = Date::new(20, Month::June, 2026);
        let hazard_rate = MakeCreditDefaultSwap::from_term_date(
            maturity,
            CONVENTIONAL_SPREAD,
            Shared::clone(settings),
        )
        .with_nominal(RECONCILE_NOMINAL)
        .build()
        .expect("the quoted trade builds")
        .implied_hazard_rate(
            0.0,
            &discount,
            Actual365Fixed::new(),
            RECONCILE_RECOVERY,
            1.0e-10,
            PricingModel::Isda,
        )
        .expect("the quoted trade inverts on the ISDA engine");
        (
            isda_engine(hazard_rate, RECONCILE_RECOVERY, &discount, settings),
            maturity,
        )
    }

    /// `creditdefaultswap.cpp:759-861`: one Markit record traded today
    /// reconciles - its value, its upfront, and the thousand of accrual it
    /// rebates, read both off the legs and off the rebate flow itself.
    ///
    /// `df` is C++'s own: the ratio of the upfront to the value, which carries
    /// the discount to cash settlement, and which the second and third
    /// assertions then divide back out.
    #[test]
    fn a_traded_today_record_reconciles_with_its_accrual_rebate() {
        const MARKIT_VALUE: Real = -16070.7;
        const EXPECTED_ACCRUAL: Real = 1000.0;
        let settings = shared(Settings::<Date>::new());
        let (engine, maturity) = reconcile_engine(&settings);

        let mut conventional =
            MakeCreditDefaultSwap::from_term_date(maturity, 0.01, Shared::clone(&settings))
                .with_nominal(RECONCILE_NOMINAL)
                .build()
                .expect("the conventional trade builds");
        conventional.base_mut().set_pricing_engine(engine);

        let npv = conventional.npv().expect("the trade prices");
        let calculated_upfront =
            conventional.notional() * conventional.fair_upfront().expect("the trade prices");
        let df = calculated_upfront / npv;
        let derived_accrual = df
            * (npv
                - conventional.default_leg_npv().expect("the trade prices")
                - conventional.coupon_leg_npv().expect("the trade prices"));
        let rebate = conventional
            .accrual_rebate()
            .expect("a rebating trade carries the flow");
        let calculated_accrual = rebate.amount().expect("the rebate is a known amount");
        let settlement_date = Event::date(rebate.as_ref());

        assert_close(npv, MARKIT_VALUE, RECONCILE_TOLERANCE, "the value");
        assert_close(
            calculated_upfront,
            df * MARKIT_VALUE,
            RECONCILE_TOLERANCE,
            "the upfront",
        );
        assert_close(
            derived_accrual,
            EXPECTED_ACCRUAL,
            RECONCILE_TOLERANCE,
            "the accrual derived from the legs",
        );
        assert_close(
            calculated_accrual,
            EXPECTED_ACCRUAL,
            RECONCILE_TOLERANCE,
            "the accrual on the rebate flow",
        );
        assert_eq!(
            settlement_date,
            WeekendsOnly::new().advance(
                reconcile_value_date(),
                3,
                TimeUnit::Days,
                BusinessDayConvention::Following,
                false,
            )
        );
    }

    /// `creditdefaultswap.cpp:863-960`: the same record traded two years ago
    /// settled its rebate long before today, so the value drops by exactly the
    /// thousand the rebate carried and the legs account for all of what is left.
    #[test]
    fn a_record_traded_in_the_past_reconciles_without_its_accrual_rebate() {
        const MARKIT_VALUE: Real = -17070.77;
        const EXPECTED_ACCRUAL: Real = 0.0;
        let settings = shared(Settings::<Date>::new());
        let (engine, maturity) = reconcile_engine(&settings);

        let mut conventional =
            MakeCreditDefaultSwap::from_term_date(maturity, 0.01, Shared::clone(&settings))
                .with_nominal(RECONCILE_NOMINAL)
                .with_trade_date(Date::new(20, Month::July, 2019))
                .build()
                .expect("the conventional trade builds");
        conventional.base_mut().set_pricing_engine(engine);

        let npv = conventional.npv().expect("the trade prices");
        let calculated_accrual = npv
            - conventional.default_leg_npv().expect("the trade prices")
            - conventional.coupon_leg_npv().expect("the trade prices");

        assert_close(npv, MARKIT_VALUE, RECONCILE_TOLERANCE, "the value");
        assert_close(
            calculated_accrual,
            EXPECTED_ACCRUAL,
            RECONCILE_TOLERANCE,
            "the accrual derived from the legs",
        );
    }
}

/// The oracle the Python `IsdaCdsEngine` facade (#812) is pinned against: a
/// three-year contract on two flat Act/365F curves referenced at the evaluation
/// date, priced through the instrument the way a caller prices one.
///
/// The fixture lives here rather than only in the binding because the pinned
/// number has to come out of the constructor the facade wraps.
/// `crates/itofin-py/tests/test_isda_cds.py` rebuilds it byte-for-byte - the
/// same dates, curves, schedule and contract terms - and asserts the same three
/// literals, so a facade that reached a different engine, or dropped one of the
/// arguments it forwards, shows up there as a mismatch rather than as a
/// plausible number nothing grades.
///
/// The schedule is built through the same builder calls `PySchedule::new`
/// (`itofin-py/src/time.rs:528-536`) makes, including the rule and the
/// termination convention the refusal fixture leaves defaulted, and its shape
/// is asserted alongside the value so a drift in the dates is told apart from a
/// drift in the pricing. Two flat curves leave the integration grid as the
/// maturity alone (`isdanodegrid.rs:122-124`), so this is a single-node ISDA
/// reprice - still the engine's own kernels, and still distinct from the
/// mid-point engine's period-by-period integration.
#[cfg(test)]
mod binding_oracle {
    use super::*;
    use crate::instrument::Instrument;
    use crate::instruments::{CreditDefaultSwap, ProtectionSide};
    use crate::interestrate::Compounding;
    use crate::pricingengine::PricingEngine;
    use crate::shared::{SharedMut, shared, shared_mut};
    use crate::termstructures::credit::flathazardrate::FlatHazardRate;
    use crate::termstructures::yields::FlatForward;
    use crate::time::businessdayconvention::BusinessDayConvention;
    use crate::time::calendars::target::Target;
    use crate::time::date::Month;
    use crate::time::dategenerationrule::DateGeneration;
    use crate::time::daycounters::actual360::Actual360;
    use crate::time::frequency::Frequency;
    use crate::time::schedule::MakeSchedule;

    const NOTIONAL: Real = 10_000_000.0;
    const SPREAD: Rate = 0.01;
    const RECOVERY: Real = 0.4;
    const HAZARD: Real = 0.02;
    const DISCOUNT_RATE: Rate = 0.03;

    /// What the fixture prices to, recorded from this test's own run to the
    /// precision an `f64` round-trips at. `test_isda_cds.py` carries the same
    /// three numbers.
    const NPV: Real = -52927.18294373818;
    const COUPON_LEG_NPV: Real = 281656.6267407311;
    const DEFAULT_LEG_NPV: Real = -334583.8096844693;

    /// Wide enough to survive a platform's `exp` differing in its last bit: the
    /// values run to `10^5`, so this is a relative `2 * 10^-13`, while the
    /// literals were recorded on one machine and are asserted on whichever one
    /// CI happens to run. Still some nine orders of magnitude below anything a
    /// mis-wired engine, curve or contract term would move the price by.
    const TOLERANCE: Real = 1.0e-8;

    fn today() -> Date {
        Date::new(15, Month::June, 2026)
    }

    fn maturity() -> Date {
        Date::new(15, Month::June, 2029)
    }

    /// The contract of the fixture, with the ISDA engine already installed.
    fn priced() -> CreditDefaultSwap {
        let settings = shared(Settings::new());
        settings.set_evaluation_date(today());
        let discount = Handle::new(shared(FlatForward::with_rate(
            today(),
            DISCOUNT_RATE,
            Actual365Fixed::new(),
            Compounding::Continuous,
            Frequency::Annual,
        )) as Shared<dyn YieldTermStructure>);
        let credit = Handle::new(shared(FlatHazardRate::with_rate(
            today(),
            HAZARD,
            Actual365Fixed::new(),
        )) as Shared<dyn DefaultProbabilityTermStructure>);
        let schedule = MakeSchedule::new()
            .from(today())
            .to(maturity())
            .with_frequency(Frequency::Quarterly)
            .with_calendar(Target::new())
            .with_convention(BusinessDayConvention::Following)
            .with_termination_date_convention(BusinessDayConvention::Following)
            .with_rule(DateGeneration::Forward)
            .build();
        assert_eq!(schedule.dates().len(), 13);
        assert_eq!(schedule.dates()[0], today());
        assert_eq!(schedule.dates()[12], maturity());
        let mut cds = CreditDefaultSwap::new(
            ProtectionSide::Seller,
            NOTIONAL,
            SPREAD,
            schedule,
            BusinessDayConvention::Following,
            Actual360::new(),
            true,
            true,
            Shared::clone(&settings),
        )
        .expect("the contract is well formed");
        let engine = shared_mut(IsdaCdsEngine::new(
            credit, RECOVERY, discount, None, settings,
        )) as SharedMut<dyn PricingEngine>;
        cds.base_mut().set_pricing_engine(engine);
        cds
    }

    /// The value and its two legs, which the Python oracle asserts to the same
    /// three literals.
    ///
    /// The legs are pinned alongside the value so that a Python-side mismatch
    /// says which half of the price moved rather than only that the total did.
    #[test]
    fn the_flat_curve_fixture_prices_to_the_pinned_value() {
        let mut cds = priced();
        assert!((cds.npv().expect("the contract prices") - NPV).abs() <= TOLERANCE);
        assert!(
            (cds.coupon_leg_npv().expect("the premium leg is valued") - COUPON_LEG_NPV).abs()
                <= TOLERANCE
        );
        assert!(
            (cds.default_leg_npv().expect("the protection leg is valued") - DEFAULT_LEG_NPV).abs()
                <= TOLERANCE
        );
    }
}