regit-curves 1.0.0

Audit-grade interest-rate yield curve bootstrap and interpolation in pure Rust. Single- and multi-curve (OIS-discounted), discount/zero/forward/par views, full primary-source derivations. Zero dependencies.
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
// Copyright 2026 Regit.io — Nicolas Koenig
// SPDX-License-Identifier: Apache-2.0

//! Overnight-indexed (OIS) swap instrument.
//!
//! An OIS exchanges, on each payment period, a fixed coupon against the
//! daily-compounded realisation of an overnight rate over the same period. By
//! no-arbitrage in the OIS-discounted world the expected realised compounded
//! overnight rate over `[t_{i-1}, t_i]` is the simply-compounded OIS forward
//! rate over the same interval (Mercurio 2009, §3.2). Because OIS is a
//! *single-curve* instrument — the OIS curve both projects the float leg's
//! forwards and discounts every cash flow — the float-leg present value
//! telescopes exactly:
//!
//! ```text
//! PV_float = sum_i D(t_i) * tau_i * F_i
//!          = sum_i D(t_i) * (D(t_{i-1}) / D(t_i) - 1)
//!          = sum_i (D(t_{i-1}) - D(t_i))
//!          = D(t_0) - D(t_N),
//! ```
//!
//! where `F_i = (D(t_{i-1}) / D(t_i) - 1) / tau_i` is the OIS forward rate
//! over the period under the swap's day-count and `t_0` is the swap start,
//! `t_N` the swap maturity. The fixed-leg present value is
//!
//! ```text
//! PV_fixed = rate * sum_i tau_i * D(t_i).
//! ```
//!
//! Equating the two legs yields the par-OIS-rate identity used by the
//! bootstrap engine:
//!
//! ```text
//! rate * sum_i tau_i * D(t_i) = D(t_0) - D(t_N).
//! ```
//!
//! This is the same single-curve form as a vanilla fixed-float swap. The
//! identity is what pins the OIS discount curve to its quoted par OIS rates.
//!
//! # References
//!
//! - Mercurio, F., *Interest Rates and The Credit Crunch: New Formulas and
//!   Market Models*, Bloomberg Portfolio Research Paper No. 2010-01-FRONTIERS
//!   (Feb 2009), §3.2. Float-leg telescoping argument in the OIS-discounted
//!   world.
//! - Hagan, P. S. & West, G., "Interpolation methods for curve construction",
//!   *Applied Mathematical Finance* 13(2):89-129 (2006), §2. Single-curve
//!   par-swap-rate equation.

use crate::errors::BootstrapError;
use crate::types::{Date, Daycount, Frequency};

use super::schedule::SwapSchedule;
use super::{CurveSnapshot, InstrumentLike};

/// A par-quoted overnight-indexed swap.
///
/// Fields:
///
/// - `start` — swap effective date (`t_0`).
/// - `maturity` — swap maturity (`t_N` = the last payment date).
/// - `rate` — par OIS rate (decimal, e.g. `0.03` for 3%). Negative rates
///   are permitted — they have been quoted on EUR / CHF OIS markets.
/// - `freq` — payment frequency (typically [`Frequency::Annual`] for tenors
///   above one year; [`Frequency::OnceAtMaturity`] is the market convention
///   for tenors of one year or less, where there is a single bullet payment
///   at maturity).
/// - `daycount` — day-count convention used to compute the period accruals
///   `tau_i` on both the fixed leg and (notionally) the float leg.
/// - `schedule` — the precomputed [`SwapSchedule`] of period boundary dates.
///
/// Constructed via [`OisSwap::new`] (which builds the regular schedule
/// internally) or [`OisSwap::with_schedule`] (which accepts a caller-built
/// schedule for irregular cases such as stub periods).
///
/// # Examples
///
/// ```
/// use regit_curves::instruments::OisSwap;
/// use regit_curves::types::{Date, Daycount, Frequency};
///
/// let start    = Date::from_ymd(2024, 1, 2).unwrap();
/// let maturity = Date::from_ymd(2029, 1, 2).unwrap();
/// // 5y annual OIS quoted at 3% on Act/360.
/// let swap = OisSwap::new(start, maturity, 0.03, Frequency::Annual, Daycount::Act360).unwrap();
/// assert_eq!(swap.schedule.len(), 5);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct OisSwap {
    /// Swap effective date — `t_0`, the start of the first accrual period.
    pub start: Date,
    /// Swap maturity — `t_N`, the last payment date.
    pub maturity: Date,
    /// Par OIS rate (decimal).
    pub rate: f64,
    /// Payment frequency (used to build the regular schedule).
    pub freq: Frequency,
    /// Day-count convention used to compute period accruals on both legs.
    pub daycount: Daycount,
    /// Precomputed schedule of period boundary dates.
    pub schedule: SwapSchedule,
}

impl OisSwap {
    /// Constructs an OIS swap with a regular schedule generated from
    /// `(start, maturity, freq)`.
    ///
    /// Validation:
    ///
    /// - `rate` must be finite.
    /// - `start` must be strictly before `maturity`.
    /// - The regular schedule generator must succeed — see
    ///   [`SwapSchedule::from_regular`] for the regularity constraint.
    ///
    /// Negative rates are accepted (the par-OIS-rate identity is linear in
    /// `rate` and remains well-posed under any finite quote).
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::InvalidInstrument`] if `rate` is not finite, if
    ///   `start >= maturity`, or if [`SwapSchedule::from_regular`] returns an
    ///   error.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::OisSwap;
    /// use regit_curves::types::{Date, Daycount, Frequency};
    /// use regit_curves::BootstrapError;
    ///
    /// let start    = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2029, 1, 2).unwrap();
    /// assert!(
    ///     OisSwap::new(start, maturity, 0.03, Frequency::Annual, Daycount::Act360).is_ok()
    /// );
    /// // Inverted dates rejected:
    /// assert!(matches!(
    ///     OisSwap::new(maturity, start, 0.03, Frequency::Annual, Daycount::Act360).unwrap_err(),
    ///     BootstrapError::InvalidInstrument { .. },
    /// ));
    /// ```
    pub fn new(
        start: Date,
        maturity: Date,
        rate: f64,
        freq: Frequency,
        daycount: Daycount,
    ) -> Result<Self, BootstrapError> {
        if !rate.is_finite() {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "OIS swap rate must be finite",
            });
        }
        if start.days_between(maturity) <= 0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "OIS swap start must precede maturity",
            });
        }
        let schedule = SwapSchedule::from_regular(start, maturity, freq)?;
        Ok(Self {
            start,
            maturity,
            rate,
            freq,
            daycount,
            schedule,
        })
    }

    /// Constructs an OIS swap from an already-built schedule.
    ///
    /// Use this entry point when the schedule is irregular (e.g. stub first
    /// or last period, or business-day-adjusted dates). The supplied schedule
    /// must agree with `(start, maturity)`: its first boundary date must
    /// equal `start` and its last must equal `maturity`.
    ///
    /// Validation matches [`OisSwap::new`]; additionally the schedule's
    /// endpoints are checked against `(start, maturity)`.
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::InvalidInstrument`] if `rate` is not finite, if
    ///   `start >= maturity`, or if the schedule's start / maturity do not
    ///   match the supplied dates.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::{OisSwap, SwapSchedule};
    /// use regit_curves::types::{Date, Daycount, Frequency};
    ///
    /// let start    = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2026, 1, 2).unwrap();
    /// let sch = SwapSchedule::from_regular(start, maturity, Frequency::Annual).unwrap();
    /// let swap = OisSwap::with_schedule(
    ///     start, maturity, 0.03, Frequency::Annual, Daycount::Act360, sch,
    /// ).unwrap();
    /// assert_eq!(swap.schedule.len(), 2);
    /// ```
    pub fn with_schedule(
        start: Date,
        maturity: Date,
        rate: f64,
        freq: Frequency,
        daycount: Daycount,
        schedule: SwapSchedule,
    ) -> Result<Self, BootstrapError> {
        if !rate.is_finite() {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "OIS swap rate must be finite",
            });
        }
        if start.days_between(maturity) <= 0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "OIS swap start must precede maturity",
            });
        }
        if schedule.start() != start || schedule.maturity() != maturity {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "schedule endpoints must match (start, maturity)",
            });
        }
        Ok(Self {
            start,
            maturity,
            rate,
            freq,
            daycount,
            schedule,
        })
    }

    /// Present value of the fixed leg:
    ///
    /// ```text
    /// PV_fixed = rate * sum_i tau_i * D(t_i),
    /// ```
    ///
    /// with `tau_i` the period accruals under [`OisSwap::daycount`] and
    /// `D(t_i)` the curve's discount factor at each period's payment date.
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::Type`] if the day-count convention cannot produce
    ///   a year fraction (e.g. [`Daycount::Business252`] without a calendar).
    /// - [`BootstrapError::InvalidInstrument`] if the curve snapshot is empty
    ///   or returns a non-positive discount factor.
    ///
    /// # Examples
    ///
    /// ```
    /// # use regit_curves::instruments::OisSwap;
    /// # use regit_curves::types::{Date, Daycount, Frequency};
    /// let start = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2025, 1, 2).unwrap();
    /// let swap = OisSwap::new(
    ///     start, maturity, 0.03, Frequency::Annual, Daycount::Act360,
    /// ).unwrap();
    /// assert_eq!(swap.rate, 0.03);
    /// ```
    pub(crate) fn fixed_leg_pv(
        &self,
        _reference_date: Date,
        curve: &CurveSnapshot<'_>,
    ) -> Result<f64, BootstrapError> {
        let mut annuity = 0.0_f64;
        for i in 0..self.schedule.len() {
            let period_start = self.schedule.period_start(i);
            let period_end = self.schedule.period_end(i);
            let tau = self.daycount.year_fraction(period_start, period_end)?;
            let t_pay = curve
                .daycount
                .year_fraction(curve.reference_date, period_end)?;
            let d_pay = curve
                .discount_at(t_pay)
                .ok_or(BootstrapError::InvalidInstrument {
                    at_index: 0,
                    reason: "curve snapshot is empty",
                })?;
            if !d_pay.is_finite() || d_pay <= 0.0 {
                return Err(BootstrapError::InvalidInstrument {
                    at_index: 0,
                    reason: "non-positive discount factor in curve snapshot",
                });
            }
            annuity += tau * d_pay;
        }
        Ok(self.rate * annuity)
    }

    /// Present value of the float leg under OIS discounting:
    ///
    /// ```text
    /// PV_float = D(t_0) - D(t_N),
    /// ```
    ///
    /// the telescoped sum of period forwards (see the module-level derivation
    /// citing Mercurio 2009 §3.2).
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::Type`] if the curve's day-count cannot produce a
    ///   year fraction for `start` or `maturity`.
    /// - [`BootstrapError::InvalidInstrument`] if the curve snapshot is empty
    ///   or returns a non-positive discount factor.
    ///
    /// # Examples
    ///
    /// ```
    /// # use regit_curves::instruments::OisSwap;
    /// # use regit_curves::types::{Date, Daycount, Frequency};
    /// let start = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2025, 1, 2).unwrap();
    /// let swap = OisSwap::new(
    ///     start, maturity, 0.03, Frequency::Annual, Daycount::Act360,
    /// ).unwrap();
    /// assert_eq!(swap.start, start);
    /// assert_eq!(swap.maturity, maturity);
    /// ```
    pub(crate) fn float_leg_pv(
        &self,
        _reference_date: Date,
        curve: &CurveSnapshot<'_>,
    ) -> Result<f64, BootstrapError> {
        let t_start = curve
            .daycount
            .year_fraction(curve.reference_date, self.start)?;
        let t_maturity = curve
            .daycount
            .year_fraction(curve.reference_date, self.maturity)?;
        let d_start = curve
            .discount_at(t_start)
            .ok_or(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "curve snapshot is empty",
            })?;
        let d_maturity =
            curve
                .discount_at(t_maturity)
                .ok_or(BootstrapError::InvalidInstrument {
                    at_index: 0,
                    reason: "curve snapshot is empty",
                })?;
        if !d_start.is_finite() || d_start <= 0.0 || !d_maturity.is_finite() || d_maturity <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "non-positive discount factor in curve snapshot",
            });
        }
        Ok(d_start - d_maturity)
    }
}

impl InstrumentLike for OisSwap {
    #[inline]
    fn pillar(&self) -> Date {
        self.maturity
    }

    fn residual(
        &self,
        reference_date: Date,
        curve: &CurveSnapshot<'_>,
    ) -> Result<f64, BootstrapError> {
        // Residual: PV_fixed - PV_float. Zero at the bootstrap solution.
        // Equivalent to rate * sum_i tau_i * D(t_i) - (D(t_0) - D(t_N)).
        let fixed = self.fixed_leg_pv(reference_date, curve)?;
        let float = self.float_leg_pv(reference_date, curve)?;
        Ok(fixed - float)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::instruments::CurveSnapshot;

    fn d(y: i32, m: u32, day: u32) -> Date {
        Date::from_ymd(y, m, day).unwrap()
    }

    // ─── Construction & validation ───────────────────────────────────────

    #[test]
    fn new_accepts_5y_annual_swap() {
        let start = d(2024, 1, 2);
        let maturity = d(2029, 1, 2);
        let swap =
            OisSwap::new(start, maturity, 0.03, Frequency::Annual, Daycount::Act360).unwrap();
        assert_eq!(swap.start, start);
        assert_eq!(swap.maturity, maturity);
        assert!((swap.rate - 0.03).abs() < 1e-15);
        assert_eq!(swap.freq, Frequency::Annual);
        assert_eq!(swap.daycount, Daycount::Act360);
        assert_eq!(swap.schedule.len(), 5);
    }

    #[test]
    fn new_accepts_6m_once_at_maturity() {
        // Short OIS: a single bullet payment at maturity.
        let start = d(2024, 1, 2);
        let maturity = d(2024, 7, 2);
        let swap = OisSwap::new(
            start,
            maturity,
            0.025,
            Frequency::OnceAtMaturity,
            Daycount::Act360,
        )
        .unwrap();
        assert_eq!(swap.schedule.len(), 1);
        assert_eq!(swap.schedule.period_start(0), start);
        assert_eq!(swap.schedule.period_end(0), maturity);
    }

    #[test]
    fn new_accepts_negative_rate() {
        // EUR / CHF OIS quoted negative is routine.
        let start = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let swap =
            OisSwap::new(start, maturity, -0.002, Frequency::Annual, Daycount::Act360).unwrap();
        assert!(swap.rate < 0.0);
    }

    #[test]
    fn new_rejects_nan_rate() {
        let err = OisSwap::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            f64::NAN,
            Frequency::Annual,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_inf_rate() {
        let err = OisSwap::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            f64::INFINITY,
            Frequency::Annual,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_inverted_dates() {
        let err = OisSwap::new(
            d(2029, 1, 2),
            d(2024, 1, 2),
            0.03,
            Frequency::Annual,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_equal_start_and_maturity() {
        let s = d(2024, 1, 2);
        let err = OisSwap::new(s, s, 0.03, Frequency::Annual, Daycount::Act360).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_propagates_irregular_schedule_error() {
        // 13 months at semi-annual cadence is not regular.
        let err = OisSwap::new(
            d(2024, 1, 2),
            d(2025, 2, 2),
            0.03,
            Frequency::SemiAnnual,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    // ─── with_schedule entry point ───────────────────────────────────────

    #[test]
    fn with_schedule_accepts_matching_schedule() {
        let start = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let sch = SwapSchedule::from_regular(start, maturity, Frequency::Annual).unwrap();
        let swap = OisSwap::with_schedule(
            start,
            maturity,
            0.03,
            Frequency::Annual,
            Daycount::Act360,
            sch,
        )
        .unwrap();
        assert_eq!(swap.schedule.len(), 2);
    }

    #[test]
    fn with_schedule_rejects_mismatched_endpoints() {
        let start = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let other = d(2027, 1, 2);
        let sch = SwapSchedule::from_regular(start, other, Frequency::Annual).unwrap();
        let err = OisSwap::with_schedule(
            start,
            maturity,
            0.03,
            Frequency::Annual,
            Daycount::Act360,
            sch,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn with_schedule_rejects_nan_rate() {
        let start = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let sch = SwapSchedule::from_regular(start, maturity, Frequency::Annual).unwrap();
        let err = OisSwap::with_schedule(
            start,
            maturity,
            f64::NAN,
            Frequency::Annual,
            Daycount::Act360,
            sch,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    // ─── Pillar accessor ─────────────────────────────────────────────────

    #[test]
    fn pillar_is_maturity_date() {
        let start = d(2024, 1, 2);
        let maturity = d(2029, 1, 2);
        let swap =
            OisSwap::new(start, maturity, 0.03, Frequency::Annual, Daycount::Act360).unwrap();
        assert_eq!(swap.pillar(), maturity);
    }

    // ─── Residual against a flat curve ───────────────────────────────────

    /// Builds a hand-rolled flat continuously-compounded discount curve
    /// `D(t) = exp(-r * t)` over a quarterly grid spanning 30 years.
    fn flat_curve(reference_date: Date, daycount: Daycount, r: f64) -> (Vec<f64>, Vec<f64>) {
        let mut times = Vec::new();
        let mut discounts = Vec::new();
        for i in 0..=120 {
            let date = Date::from_serial(reference_date.serial() + i * 91);
            let t = daycount.year_fraction(reference_date, date).unwrap();
            times.push(t);
            discounts.push((-r * t).exp());
        }
        (times, discounts)
    }

    /// Computes the par OIS rate consistent with a flat continuously-
    /// compounded curve `D(t) = exp(-r_c * t)`:
    ///
    /// ```text
    /// r_par = (D(t_0) - D(t_N)) / sum_i tau_i * D(t_i).
    /// ```
    fn par_ois_rate_flat(swap: &OisSwap, reference: Date, r_c: f64) -> f64 {
        let dc = swap.daycount;
        let t0 = dc.year_fraction(reference, swap.start).unwrap();
        let tn = dc.year_fraction(reference, swap.maturity).unwrap();
        let numerator = (-r_c * t0).exp() - (-r_c * tn).exp();
        let mut annuity = 0.0_f64;
        for i in 0..swap.schedule.len() {
            let s = swap.schedule.period_start(i);
            let e = swap.schedule.period_end(i);
            let tau = dc.year_fraction(s, e).unwrap();
            let t_pay = dc.year_fraction(reference, e).unwrap();
            annuity += tau * (-r_c * t_pay).exp();
        }
        numerator / annuity
    }

    #[test]
    fn par_ois_rate_zeroes_residual_5y_annual() {
        // Numerical test target from the spec: flat r_c = 3%, 5y annual on
        // Act/360. Build the par rate analytically, feed it to the swap,
        // assert residual == 0 to 1e-10.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let r_c = 0.03_f64;
        let start = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, daycount, r_c);

        // Build a placeholder swap to derive the par rate from the schedule.
        let placeholder = OisSwap::new(start, maturity, 0.0, Frequency::Annual, daycount).unwrap();
        let r_par = par_ois_rate_flat(&placeholder, reference, r_c);

        let swap = OisSwap::new(start, maturity, r_par, Frequency::Annual, daycount).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };
        let residual = swap.residual(reference, &snapshot).unwrap();
        assert!(
            residual.abs() < 1e-10,
            "OIS residual on flat curve must be zero to 1e-10, got {residual}",
        );
    }

    #[test]
    fn par_ois_rate_zeroes_residual_6m_once_at_maturity() {
        // Short OIS: single bullet payment.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let r_c = 0.025_f64;
        let start = reference;
        let maturity = d(2024, 7, 2);
        let (times, discounts) = flat_curve(reference, daycount, r_c);

        let placeholder =
            OisSwap::new(start, maturity, 0.0, Frequency::OnceAtMaturity, daycount).unwrap();
        let r_par = par_ois_rate_flat(&placeholder, reference, r_c);

        let swap =
            OisSwap::new(start, maturity, r_par, Frequency::OnceAtMaturity, daycount).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };
        let residual = swap.residual(reference, &snapshot).unwrap();
        assert!(
            residual.abs() < 1e-10,
            "short-OIS residual on flat curve must be zero to 1e-10, got {residual}",
        );
    }

    #[test]
    fn residual_sign_responds_to_rate_perturbation() {
        // Over-quoting the par rate makes the fixed leg too rich, so the
        // residual (fixed - float) becomes positive.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let r_c = 0.03_f64;
        let start = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, daycount, r_c);

        let placeholder = OisSwap::new(start, maturity, 0.0, Frequency::Annual, daycount).unwrap();
        let r_par = par_ois_rate_flat(&placeholder, reference, r_c);

        let swap =
            OisSwap::new(start, maturity, r_par + 0.005, Frequency::Annual, daycount).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };
        let residual = swap.residual(reference, &snapshot).unwrap();
        assert!(residual > 1e-6);
    }

    #[test]
    fn fixed_and_float_legs_match_at_par() {
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let r_c = 0.03_f64;
        let start = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, daycount, r_c);

        let placeholder = OisSwap::new(start, maturity, 0.0, Frequency::Annual, daycount).unwrap();
        let r_par = par_ois_rate_flat(&placeholder, reference, r_c);
        let swap = OisSwap::new(start, maturity, r_par, Frequency::Annual, daycount).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };
        let fixed = swap.fixed_leg_pv(reference, &snapshot).unwrap();
        let float = swap.float_leg_pv(reference, &snapshot).unwrap();
        assert!((fixed - float).abs() < 1e-10);
    }

    #[test]
    fn float_leg_telescopes_to_d_start_minus_d_maturity() {
        // PV_float = D(t_0) - D(t_N). Verify against the flat curve directly.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let r_c = 0.03_f64;
        let start = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, daycount, r_c);

        let swap = OisSwap::new(start, maturity, 0.03, Frequency::Annual, daycount).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };
        let float = swap.float_leg_pv(reference, &snapshot).unwrap();
        let t0 = daycount.year_fraction(reference, start).unwrap();
        let tn = daycount.year_fraction(reference, maturity).unwrap();
        let expected = (-r_c * t0).exp() - (-r_c * tn).exp();
        assert!((float - expected).abs() < 1e-12);
    }

    #[test]
    fn residual_errors_on_empty_curve_snapshot() {
        let reference = d(2024, 1, 2);
        let swap = OisSwap::new(
            reference,
            d(2029, 1, 2),
            0.03,
            Frequency::Annual,
            Daycount::Act360,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: Daycount::Act360,
            times: &[],
            discounts: &[],
        };
        let err = swap.residual(reference, &snapshot).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn residual_linear_in_rate() {
        // residual(rate) = rate * annuity - (D(t_0) - D(t_N)) is affine in
        // rate. Perturb by dr and verify the residual shifts by dr * annuity.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let r_c = 0.03_f64;
        let start = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, daycount, r_c);
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };

        let placeholder = OisSwap::new(start, maturity, 0.0, Frequency::Annual, daycount).unwrap();
        let r_par = par_ois_rate_flat(&placeholder, reference, r_c);

        let swap_a = OisSwap::new(start, maturity, r_par, Frequency::Annual, daycount).unwrap();
        let swap_b =
            OisSwap::new(start, maturity, r_par + 0.01, Frequency::Annual, daycount).unwrap();
        let r_a = swap_a.residual(reference, &snapshot).unwrap();
        let r_b = swap_b.residual(reference, &snapshot).unwrap();
        // Expected shift: 0.01 * annuity, where annuity is computed from the
        // same flat curve.
        let mut annuity = 0.0_f64;
        for i in 0..swap_a.schedule.len() {
            let s = swap_a.schedule.period_start(i);
            let e = swap_a.schedule.period_end(i);
            let tau = daycount.year_fraction(s, e).unwrap();
            let t_pay = daycount.year_fraction(reference, e).unwrap();
            annuity += tau * (-r_c * t_pay).exp();
        }
        assert!(((r_b - r_a) - 0.01 * annuity).abs() < 1e-12);
    }
}