regit-curves 1.0.1

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
// Copyright 2026 Regit.io — Nicolas Koenig
// SPDX-License-Identifier: Apache-2.0

//! Short-term interest-rate (STIR) future instrument.
//!
//! A STIR future is quoted as a **price** (e.g. `95.0` for an implied rate of
//! `5%`). The implied **forward** rate over the futures' underlying rate
//! period `[start, end]` is obtained from the quoted price by subtracting the
//! caller-supplied **convexity adjustment** (in rate units, i.e. decimal):
//!
//! ```text
//! r_quoted = (100 - price) / 100
//! r_fwd    = r_quoted - convexity_adjustment
//! ```
//!
//! Once the forward rate is known the instrument pins the discount curve on
//! `[start, end]` by the same simply-compounded identity used for an FRA:
//!
//! ```text
//! D(start) / D(end) = 1 + r_fwd * tau(start, end),
//! ```
//!
//! where `tau` is the year fraction under the future's day-count convention.
//!
//! # Convexity adjustment policy
//!
//! `convexity_adjustment` is a **caller-supplied scalar in rate units**. The
//! model that produces it — typically a short-rate model (Hull-White,
//! Black-Karasinski) or an HJM / SABR construction calibrated to caps or
//! swaptions — is **out of scope for this crate**. See `WORKING.md` §6, open
//! question 1: convexity-adjustment models live in a future, separately
//! audited crate. Passing `0.0` disables the adjustment.
//!
//! A futures price near or above `100` is permitted (negative-rate
//! environments — EUR, CHF, JPY); no clamping is applied.
//!
//! # References
//!
//! - Hagan, P. S. & West, G., "Interpolation methods for curve construction",
//!   *Applied Mathematical Finance* 13(2):89-129 (2006), §2. The forward-rate
//!   pricing identity used here.
//! - Hull, J. C., *Options, Futures, and Other Derivatives*, 10th edition,
//!   Pearson (2018), §6.3 — "Eurodollar Futures". Standard reference for the
//!   convexity-adjustment relationship between futures and forward rates.
//! - Andersen, L. B. G. & Piterbarg, V. V., *Interest Rate Modeling*, Volume I:
//!   Foundations and Vanilla Models, Atlantic Financial Press (2010), §6.3.
//!   Detailed treatment of futures-vs-forward convexity under Gaussian
//!   short-rate dynamics. The convexity-adjustment **model** itself is not
//!   implemented here — only consumed as a caller-supplied scalar.

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

use super::{CurveSnapshot, InstrumentLike};

/// A short-term interest-rate (STIR) future.
///
/// Fields:
///
/// - `start` — period start: the futures expiry / IMM date and the first day
///   of the underlying rate period.
/// - `end` — period end: `start + index_tenor` (e.g. `start + 3M` for a
///   3-month-LIBOR / SOFR future).
/// - `price` — quoted futures price. A price of `95.0` implies a **quoted**
///   rate of `5%` (i.e. `(100 - 95) / 100`). Prices near or above `100` are
///   accepted (negative-rate environment).
/// - `convexity_adjustment` — caller-supplied scalar in rate units, subtracted
///   from the quoted rate to give the **forward** rate. Pass `0.0` to
///   disable. See the module header for the policy.
/// - `daycount` — the day-count convention used to compute the accrual `tau`.
///
/// Constructed via [`Future::new`], which validates: `price.is_finite()`,
/// `convexity_adjustment.is_finite()`, `start < end`.
///
/// # Examples
///
/// ```
/// use regit_curves::instruments::Future;
/// use regit_curves::types::{Date, Daycount};
///
/// // 3-month IMM Mar 2024 STIR future: 2024-03-20 -> 2024-06-19 (91 days),
/// // quoted at 95.00 with a 5bp convexity adjustment.
/// let start = Date::from_ymd(2024, 3, 20).unwrap();
/// let end   = Date::from_ymd(2024, 6, 19).unwrap();
/// let fut = Future::new(start, end, 95.0, 0.0005, Daycount::Act360).unwrap();
/// // Quoted rate: 5%. Implied forward rate: 5% - 5bp = 4.95%.
/// assert!((fut.quoted_rate() - 0.05).abs() < 1e-15);
/// assert!((fut.implied_forward_rate() - 0.0495).abs() < 1e-15);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Future {
    /// Period start — the futures expiry / IMM date and the first day of the
    /// underlying rate period.
    pub start: Date,
    /// Period end — `start + index_tenor` (e.g. `start + 3M`).
    pub end: Date,
    /// Quoted futures price (e.g. `95.0` implies a quoted rate of `5%`).
    pub price: f64,
    /// Convexity adjustment in rate units (decimal). Subtracted from the
    /// quoted rate to obtain the forward rate. Caller-supplied; the model
    /// that produces it is out of scope for this crate. Pass `0.0` to
    /// disable.
    pub convexity_adjustment: f64,
    /// Day-count convention used to compute the accrual `tau`.
    pub daycount: Daycount,
}

impl Future {
    /// Constructs a STIR future after validating its invariants.
    ///
    /// Validation:
    ///
    /// - `price` must be finite. Prices near or above `100` are permitted
    ///   (negative-rate quotes).
    /// - `convexity_adjustment` must be finite. Pass `0.0` to disable.
    /// - `start` must be strictly before `end` (a future must have a
    ///   non-degenerate underlying rate period).
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::InvalidInstrument`] if `price` or
    ///   `convexity_adjustment` is not finite, or if `start >= end`.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::Future;
    /// use regit_curves::types::{Date, Daycount};
    /// use regit_curves::BootstrapError;
    ///
    /// let start = Date::from_ymd(2024, 3, 20).unwrap();
    /// let end   = Date::from_ymd(2024, 6, 19).unwrap();
    /// assert!(Future::new(start, end, 95.0, 0.0, Daycount::Act360).is_ok());
    /// // Inverted dates rejected:
    /// assert!(matches!(
    ///     Future::new(end, start, 95.0, 0.0, Daycount::Act360).unwrap_err(),
    ///     BootstrapError::InvalidInstrument { .. },
    /// ));
    /// ```
    pub fn new(
        start: Date,
        end: Date,
        price: f64,
        convexity_adjustment: f64,
        daycount: Daycount,
    ) -> Result<Self, BootstrapError> {
        if !price.is_finite() {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "future price must be finite",
            });
        }
        if !convexity_adjustment.is_finite() {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "future convexity adjustment must be finite",
            });
        }
        if start.days_between(end) <= 0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "future start must be strictly before end",
            });
        }
        Ok(Self {
            start,
            end,
            price,
            convexity_adjustment,
            daycount,
        })
    }

    /// Implied rate from the quoted price alone, ignoring the convexity
    /// adjustment:
    ///
    /// ```text
    /// r_quoted = (100 - price) / 100.
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::Future;
    /// use regit_curves::types::{Date, Daycount};
    ///
    /// let start = Date::from_ymd(2024, 3, 20).unwrap();
    /// let end   = Date::from_ymd(2024, 6, 19).unwrap();
    /// let fut = Future::new(start, end, 95.0, 0.0005, Daycount::Act360).unwrap();
    /// assert!((fut.quoted_rate() - 0.05).abs() < 1e-15);
    /// ```
    #[inline]
    #[must_use]
    pub fn quoted_rate(&self) -> f64 {
        (100.0 - self.price) / 100.0
    }

    /// Implied **forward** rate over `[start, end]`:
    ///
    /// ```text
    /// r_fwd = (100 - price) / 100 - convexity_adjustment.
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::Future;
    /// use regit_curves::types::{Date, Daycount};
    ///
    /// let start = Date::from_ymd(2024, 3, 20).unwrap();
    /// let end   = Date::from_ymd(2024, 6, 19).unwrap();
    /// // Price 95.0 -> 5% quoted; subtract 5bp convexity -> 4.95%.
    /// let fut = Future::new(start, end, 95.0, 0.0005, Daycount::Act360).unwrap();
    /// assert!((fut.implied_forward_rate() - 0.0495).abs() < 1e-15);
    /// ```
    #[inline]
    #[must_use]
    pub fn implied_forward_rate(&self) -> f64 {
        self.quoted_rate() - self.convexity_adjustment
    }

    /// Year fraction across the future's underlying rate period
    /// `[start, end]`.
    ///
    /// # Errors
    ///
    /// - [`TypeError::InvalidTenor`] if the day-count convention requires a
    ///   calendar it has not been supplied (e.g. [`Daycount::Business252`]).
    /// - [`TypeError::NonPositiveRange`] only if the constructor was bypassed
    ///   (the constructor validates `start < end`).
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::Future;
    /// use regit_curves::types::{Date, Daycount};
    ///
    /// let start = Date::from_ymd(2024, 3, 20).unwrap();
    /// let end   = Date::from_ymd(2024, 6, 19).unwrap();
    /// let fut = Future::new(start, end, 95.0, 0.0, Daycount::Act360).unwrap();
    /// // 91 days under Act/360.
    /// assert!((fut.accrual().unwrap() - 91.0 / 360.0).abs() < 1e-15);
    /// ```
    pub fn accrual(&self) -> Result<f64, TypeError> {
        self.daycount.year_fraction(self.start, self.end)
    }

    /// Returns the discount factor implied at [`Future::end`] given the
    /// discount factor at [`Future::start`]:
    ///
    /// ```text
    /// D(end) = D(start) / (1 + r_fwd * tau).
    /// ```
    ///
    /// `r_fwd` is [`Future::implied_forward_rate`].
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::InvalidInstrument`] if `discount_at_start` is
    ///   non-finite or non-positive, or if `1 + r_fwd * tau` is non-positive
    ///   (pathological deeply-negative-rate input).
    /// - [`BootstrapError::Type`] if the day-count query fails.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::Future;
    /// use regit_curves::types::{Date, Daycount};
    ///
    /// let start = Date::from_ymd(2024, 3, 20).unwrap();
    /// let end   = Date::from_ymd(2024, 6, 19).unwrap();
    /// let fut = Future::new(start, end, 95.0, 0.0005, Daycount::Act360).unwrap();
    /// let d_end = fut.implied_discount(1.0).unwrap();
    /// // r_fwd = 0.0495; tau = 91/360.
    /// let expected = 1.0 / (1.0 + 0.0495 * 91.0 / 360.0);
    /// assert!((d_end - expected).abs() < 1e-15);
    /// ```
    pub fn implied_discount(&self, discount_at_start: f64) -> Result<f64, BootstrapError> {
        if !discount_at_start.is_finite() || discount_at_start <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "discount factor at start must be finite and positive",
            });
        }
        let tau = self.accrual()?;
        let growth = 1.0 + self.implied_forward_rate() * tau;
        if !growth.is_finite() || growth <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "non-positive accrual factor (1 + r_fwd * tau)",
            });
        }
        Ok(discount_at_start / growth)
    }
}

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

    fn residual(
        &self,
        _reference_date: Date,
        curve: &CurveSnapshot<'_>,
    ) -> Result<f64, BootstrapError> {
        let tau = self.accrual()?;
        // Curve-side year fractions are taken under the curve's own day-count
        // (which is independent of the instrument's `daycount`).
        let t_start = curve
            .daycount
            .year_fraction(curve.reference_date, self.start)?;
        let t_end = curve
            .daycount
            .year_fraction(curve.reference_date, self.end)?;
        let d_start = curve
            .discount_at(t_start)
            .ok_or(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "curve snapshot is empty",
            })?;
        let d_end = curve
            .discount_at(t_end)
            .ok_or(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "curve snapshot is empty",
            })?;
        if d_end <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "non-positive discount factor in curve snapshot",
            });
        }
        // Residual: D(start) / D(end) - (1 + r_fwd * tau).
        // Zero at the bootstrap solution.
        Ok(d_start / d_end - (1.0 + self.implied_forward_rate() * tau))
    }
}

#[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_valid_future() {
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0005,
            Daycount::Act360,
        )
        .unwrap();
        assert_eq!(fut.start, d(2024, 3, 20));
        assert_eq!(fut.end, d(2024, 6, 19));
        assert!((fut.price - 95.0).abs() < 1e-15);
        assert!((fut.convexity_adjustment - 0.0005).abs() < 1e-15);
    }

    #[test]
    fn new_accepts_zero_convexity_adjustment() {
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        assert!((fut.implied_forward_rate() - fut.quoted_rate()).abs() < 1e-15);
    }

    #[test]
    fn new_accepts_negative_rate_environment_price_above_100() {
        // EUR / CHF / JPY STIR futures: a price above 100 implies a negative
        // quoted rate. We do not clamp.
        let fut =
            Future::new(d(2024, 3, 20), d(2024, 6, 19), 100.5, 0.0, Daycount::Act360).unwrap();
        assert!(fut.quoted_rate() < 0.0);
        assert!((fut.quoted_rate() + 0.005).abs() < 1e-15);
    }

    #[test]
    fn new_accepts_price_exactly_100() {
        let fut =
            Future::new(d(2024, 3, 20), d(2024, 6, 19), 100.0, 0.0, Daycount::Act360).unwrap();
        assert!((fut.quoted_rate() - 0.0).abs() < 1e-15);
    }

    #[test]
    fn new_rejects_nan_price() {
        let err = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            f64::NAN,
            0.0,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_inf_price() {
        let err = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            f64::INFINITY,
            0.0,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_nan_convexity_adjustment() {
        let err = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            f64::NAN,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_inf_convexity_adjustment() {
        let err = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            f64::NEG_INFINITY,
            Daycount::Act360,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_inverted_dates() {
        let err =
            Future::new(d(2024, 6, 19), d(2024, 3, 20), 95.0, 0.0, Daycount::Act360).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_equal_dates() {
        // A future must have a strictly non-degenerate underlying rate period
        // (unlike a deposit, which permits a zero-day accrual).
        let err =
            Future::new(d(2024, 3, 20), d(2024, 3, 20), 95.0, 0.0, Daycount::Act360).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    // ─── quoted_rate / implied_forward_rate ──────────────────────────────

    #[test]
    fn quoted_rate_matches_price_definition() {
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        assert!((fut.quoted_rate() - 0.05).abs() < 1e-15);
    }

    #[test]
    fn implied_forward_rate_subtracts_convexity_adjustment() {
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0005,
            Daycount::Act360,
        )
        .unwrap();
        // 5% - 5bp = 4.95%.
        assert!((fut.implied_forward_rate() - 0.0495).abs() < 1e-15);
    }

    // ─── Year-fraction helper ────────────────────────────────────────────

    #[test]
    fn accrual_matches_imm_mar_2024_act360() {
        // IMM Mar 2024 -> IMM Jun 2024: 2024-03-20 to 2024-06-19 = 91 days.
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        let tau = fut.accrual().unwrap();
        assert!((tau - 91.0 / 360.0).abs() < 1e-15);
    }

    #[test]
    fn accrual_propagates_business252_error() {
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0,
            Daycount::Business252,
        )
        .unwrap();
        let err = fut.accrual().unwrap_err();
        assert!(matches!(err, TypeError::InvalidTenor { .. }));
    }

    // ─── Pricing identity: implied_discount ──────────────────────────────

    #[test]
    fn implied_discount_basic_formula() {
        // IMM Mar 2024 future at 95.0 with 5bp convexity adjustment.
        // r_fwd = 4.95%; tau = 91/360.
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0005,
            Daycount::Act360,
        )
        .unwrap();
        let d_end = fut.implied_discount(1.0).unwrap();
        let tau = 91.0_f64 / 360.0;
        let expected = 1.0 / (1.0 + 0.0495 * tau);
        assert!((d_end - expected).abs() < 1e-15);
    }

    #[test]
    fn implied_discount_zero_convexity_matches_quoted_rate() {
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        let d_end = fut.implied_discount(1.0).unwrap();
        let tau = 91.0_f64 / 360.0;
        let expected = 1.0 / (1.0 + 0.05 * tau);
        assert!((d_end - expected).abs() < 1e-15);
    }

    #[test]
    fn implied_discount_scales_linearly_in_d_start() {
        // D(end) = D(start) / (1 + r * tau), so D(end) is linear in D(start).
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        let d1 = fut.implied_discount(1.0).unwrap();
        let d2 = fut.implied_discount(0.5).unwrap();
        assert!((d2 - 0.5 * d1).abs() < 1e-15);
    }

    #[test]
    fn implied_discount_rejects_non_finite_d_start() {
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        assert!(matches!(
            fut.implied_discount(f64::NAN).unwrap_err(),
            BootstrapError::InvalidInstrument { .. },
        ));
        assert!(matches!(
            fut.implied_discount(f64::INFINITY).unwrap_err(),
            BootstrapError::InvalidInstrument { .. },
        ));
    }

    #[test]
    fn implied_discount_rejects_non_positive_d_start() {
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        assert!(matches!(
            fut.implied_discount(0.0).unwrap_err(),
            BootstrapError::InvalidInstrument { .. },
        ));
        assert!(matches!(
            fut.implied_discount(-0.5).unwrap_err(),
            BootstrapError::InvalidInstrument { .. },
        ));
    }

    #[test]
    fn implied_discount_rejects_non_positive_growth() {
        // Pathological deeply-negative forward rate where (1 + r_fwd * tau) <= 0.
        // tau = 91/360 ≈ 0.2528; price = 1000 gives quoted_rate = -9.0, so
        // 1 + (-9.0) * 0.2528 ≈ -1.275.
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            1000.0,
            0.0,
            Daycount::Act360,
        )
        .unwrap();
        let err = fut.implied_discount(1.0).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn implied_discount_propagates_business252_error() {
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0,
            Daycount::Business252,
        )
        .unwrap();
        let err = fut.implied_discount(1.0).unwrap_err();
        assert!(matches!(err, BootstrapError::Type(_)));
    }

    // ─── Residual against a self-consistent curve ────────────────────────

    /// Builds a hand-rolled tabulated discount curve consistent with a given
    /// forward rate `r_fwd` on `[start, end]` and a flat continuously-
    /// compounded rate `r_c` elsewhere. Specifically we lay down knots at the
    /// curve anchor, at `start`, and at `end` such that
    /// `D(start) / D(end) = 1 + r_fwd * tau(start, end)` is honoured exactly.
    fn curve_consistent_with_forward(
        reference: Date,
        daycount: Daycount,
        start: Date,
        end: Date,
        r_fwd: f64,
        r_c: f64,
    ) -> (Vec<f64>, Vec<f64>) {
        let t_start = daycount.year_fraction(reference, start).unwrap();
        let t_end = daycount.year_fraction(reference, end).unwrap();
        let tau = daycount.year_fraction(start, end).unwrap();
        let d_start = (-r_c * t_start).exp();
        let d_end = d_start / (1.0 + r_fwd * tau);
        // Anchor at reference (D = 1) plus the two pillar points; this is all
        // the residual computation looks up.
        (vec![0.0, t_start, t_end], vec![1.0, d_start, d_end])
    }

    #[test]
    fn future_residual_is_zero_on_self_consistent_curve() {
        // IMM Mar 2024 numerical test target from the pass spec:
        // start = 2024-03-20, end = 2024-06-19 (91 days, Act/360),
        // price = 95.0, convexity_adjustment = 0.0005.
        // r_fwd = 5% - 0.05% = 4.95%.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let start = d(2024, 3, 20);
        let end = d(2024, 6, 19);
        let fut = Future::new(start, end, 95.0, 0.0005, daycount).unwrap();
        let r_fwd = fut.implied_forward_rate();
        assert!((r_fwd - 0.0495).abs() < 1e-15);

        let (times, discounts) =
            curve_consistent_with_forward(reference, daycount, start, end, r_fwd, 0.04);
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };
        let residual = fut.residual(reference, &snapshot).unwrap();
        assert!(
            residual.abs() < 1e-12,
            "residual on self-consistent curve must be zero to 1e-12, got {residual}",
        );
    }

    #[test]
    fn future_residual_sign_responds_to_price_perturbation() {
        // A lower price quotes a HIGHER implied rate. With the curve held
        // fixed at the par r_fwd, raising the implied rate by overshooting
        // the price increases `1 + r_fwd * tau` and drives the residual
        // `D(start)/D(end) - (1 + r_fwd * tau)` negative.
        let reference = d(2024, 1, 2);
        let daycount = Daycount::Act360;
        let start = d(2024, 3, 20);
        let end = d(2024, 6, 19);
        let par_fut = Future::new(start, end, 95.0, 0.0005, daycount).unwrap();
        let r_fwd_par = par_fut.implied_forward_rate();

        let (times, discounts) =
            curve_consistent_with_forward(reference, daycount, start, end, r_fwd_par, 0.04);
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount,
            times: &times,
            discounts: &discounts,
        };

        // Drop the quoted price by 50bp -> implied rate rises by 50bp ->
        // residual goes negative.
        let mispriced = Future::new(start, end, 94.5, 0.0005, daycount).unwrap();
        let residual = mispriced.residual(reference, &snapshot).unwrap();
        assert!(residual < -1e-6);
    }

    #[test]
    fn future_residual_errors_on_empty_curve_snapshot() {
        let reference = d(2024, 1, 2);
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: Daycount::Act360,
            times: &[],
            discounts: &[],
        };
        let err = fut.residual(reference, &snapshot).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn future_residual_propagates_business252_error() {
        // The instrument's `accrual` is queried before any curve lookup, so
        // a `Business252` instrument surfaces the day-count error first.
        let reference = d(2024, 1, 2);
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0,
            Daycount::Business252,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: Daycount::Act360,
            times: &[0.0_f64, 1.0],
            discounts: &[1.0_f64, 0.95],
        };
        let err = fut.residual(reference, &snapshot).unwrap_err();
        assert!(matches!(err, BootstrapError::Type(_)));
    }

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

    #[test]
    fn future_pillar_is_end_date() {
        let fut = Future::new(d(2024, 3, 20), d(2024, 6, 19), 95.0, 0.0, Daycount::Act360).unwrap();
        assert_eq!(fut.pillar(), d(2024, 6, 19));
    }

    // ─── Round-trip identity check ───────────────────────────────────────

    #[test]
    fn future_discount_roundtrip_through_growth_factor() {
        // If we know D(start), implied_discount produces D(end) such that
        // D(start) / D(end) == 1 + r_fwd * tau exactly.
        let fut = Future::new(
            d(2024, 3, 20),
            d(2024, 6, 19),
            95.0,
            0.0005,
            daycount_for_test(),
        )
        .unwrap();
        let d_s = 0.9876;
        let d_e = fut.implied_discount(d_s).unwrap();
        let tau = fut.accrual().unwrap();
        assert!((d_s / d_e - (1.0 + 0.0495 * tau)).abs() < 1e-15);
    }

    fn daycount_for_test() -> Daycount {
        Daycount::Act360
    }
}