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

//! Coupon-bearing bond instrument.
//!
//! A coupon-bearing bond pays a regular fixed coupon equal to
//! `coupon * tau_i * notional` on each scheduled coupon date `t_i`, and
//! repays the notional principal on the maturity date `t_N`. By
//! no-arbitrage the bond's dirty price equals the present value of the
//! coupon and principal cash flows:
//!
//! ```text
//! dirty_price = clean_price + accrued
//!             = coupon * SUM_i tau_i * D(t_i) * notional
//!               + notional * D(t_N),
//! ```
//!
//! where `tau_i` is the period accrual under [`Bond::daycount`] and `D(t)`
//! is the curve's discount factor evaluated under the curve's own
//! day-count. The residual returned by the instrument's `residual` method
//! is
//!
//! ```text
//! residual = coupon_pv + principal_pv - dirty_price
//!          = coupon * SUM_i tau_i * D(t_i) * notional
//!            + notional * D(t_N)
//!            - (clean_price + accrued).
//! ```
//!
//! Zero at the bootstrap solution. For a **par bond at issue** the quoted
//! clean price equals the notional and the accrued interest is zero, so
//! the par-bond identity reduces to
//!
//! ```text
//! coupon * SUM_i tau_i * D(t_i) + D(t_N) = 1,
//! ```
//!
//! which is the same shape as the OIS-swap par equation.
//!
//! # References
//!
//! - Andersen, L. B. G. & Piterbarg, V. V., *Interest Rate Modeling*,
//!   Volume I: Foundations and Vanilla Models, Atlantic Financial Press
//!   (2010), §5.1-5.2. Bond pricing and bootstrap identity.
//! - ISDA, *2006 ISDA Definitions*, §6 ("Fixed Amounts and Floating
//!   Amounts"). Coupon calculation conventions.

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

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

/// A coupon-bearing bond, quoted by clean price (plus accrued interest).
///
/// Fields:
///
/// - `issue` — issue / settlement date when the curve begins discounting
///   (`t_0`).
/// - `maturity` — maturity date when the principal is repaid (`t_N`).
/// - `coupon` — annualised coupon rate, decimal (e.g. `0.05` for 5%).
/// - `freq` — coupon frequency (typically [`Frequency::SemiAnnual`] for
///   USD / GBP government bonds, [`Frequency::Annual`] for many Eurozone
///   sovereign issues).
/// - `daycount` — day-count convention for the coupon accruals `tau_i`.
/// - `notional` — principal repaid at maturity. Typically `1.0` for a
///   par-quoted bond or `100.0` for percent-of-par quoting.
/// - `clean_price` — quoted clean price, in the same units as `notional`.
///   A par bond quotes at `notional`; a discount bond below; a premium
///   bond above.
/// - `accrued` — accrued interest at settlement, in the same units as
///   `notional`. Zero at issue; non-zero when the bond is quoted between
///   coupon dates. The dirty price is `clean_price + accrued`.
/// - `schedule` — coupon schedule generated from `(issue, maturity, freq)`.
///
/// Constructed via [`Bond::new`] (which builds the regular schedule
/// internally) or [`Bond::with_schedule`] (which accepts a caller-built
/// schedule for irregular cases such as stub periods).
///
/// # Examples
///
/// ```
/// use regit_curves::instruments::Bond;
/// use regit_curves::types::{Date, Daycount, Frequency};
///
/// let issue    = Date::from_ymd(2024, 1, 2).unwrap();
/// let maturity = Date::from_ymd(2029, 1, 2).unwrap();
/// // 5y annual 5% bond on Act/365F, quoted at par with no accrued.
/// let bond = Bond::new(
///     issue,
///     maturity,
///     0.05,
///     Frequency::Annual,
///     Daycount::Act365F,
///     1.0,
///     1.0,
///     0.0,
/// )
/// .unwrap();
/// assert_eq!(bond.schedule.len(), 5);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Bond {
    /// Issue / settlement date — when the curve begins discounting.
    pub issue: Date,
    /// Maturity date — when principal is repaid.
    pub maturity: Date,
    /// Annualised coupon rate (decimal, e.g. `0.05` for 5%).
    pub coupon: f64,
    /// Coupon frequency.
    pub freq: Frequency,
    /// Day-count for coupon accruals.
    pub daycount: Daycount,
    /// Notional principal repaid at maturity (typically `1.0` for a
    /// par-quoted bond or `100.0` for percent-of-par quoting).
    pub notional: f64,
    /// Quoted clean price (in the same units as `notional`). A par bond
    /// quotes at `notional` (clean price = 1.0 for `notional = 1.0`, or
    /// 100.0 for `notional = 100.0`); a discount bond quotes below par; a
    /// premium bond quotes above.
    pub clean_price: f64,
    /// Accrued interest at settlement (in the same units as `notional`).
    /// Conventionally zero at issue; non-zero if the bond is quoted
    /// between coupon dates. The dirty price is `clean_price + accrued`.
    pub accrued: f64,
    /// Coupon schedule generated from `(issue, maturity, freq)`.
    pub schedule: SwapSchedule,
}

impl Bond {
    /// Constructs a bond with a regular coupon schedule generated from
    /// `(issue, maturity, freq)`.
    ///
    /// Validation:
    ///
    /// - `coupon` must be finite (negative coupons are accepted for
    ///   completeness; negative-yielding bonds with coupon-stripped
    ///   structures exist in EUR / CHF markets).
    /// - `notional > 0`.
    /// - `clean_price > 0`.
    /// - `accrued` finite and `>= 0`.
    /// - `issue < maturity`.
    /// - The regular schedule generator must succeed — see
    ///   [`SwapSchedule::from_regular`] for the regularity constraint.
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::InvalidInstrument`] if any validation step fails,
    ///   or if [`SwapSchedule::from_regular`] returns an error.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::Bond;
    /// use regit_curves::types::{Date, Daycount, Frequency};
    /// use regit_curves::BootstrapError;
    ///
    /// let issue    = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2029, 1, 2).unwrap();
    /// assert!(
    ///     Bond::new(
    ///         issue, maturity, 0.05, Frequency::Annual, Daycount::Act365F,
    ///         1.0, 1.0, 0.0,
    ///     )
    ///     .is_ok()
    /// );
    /// // Non-positive notional rejected:
    /// assert!(matches!(
    ///     Bond::new(
    ///         issue, maturity, 0.05, Frequency::Annual, Daycount::Act365F,
    ///         0.0, 1.0, 0.0,
    ///     )
    ///     .unwrap_err(),
    ///     BootstrapError::InvalidInstrument { .. },
    /// ));
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        issue: Date,
        maturity: Date,
        coupon: f64,
        freq: Frequency,
        daycount: Daycount,
        notional: f64,
        clean_price: f64,
        accrued: f64,
    ) -> Result<Self, BootstrapError> {
        Self::validate(coupon, notional, clean_price, accrued, issue, maturity)?;
        let schedule = SwapSchedule::from_regular(issue, maturity, freq)?;
        Ok(Self {
            issue,
            maturity,
            coupon,
            freq,
            daycount,
            notional,
            clean_price,
            accrued,
            schedule,
        })
    }

    /// Constructs a bond from an already-built schedule.
    ///
    /// Use this entry point when the coupon schedule is irregular (e.g. a
    /// stub first or last period). The supplied schedule must agree with
    /// `(issue, maturity)`: its first boundary date must equal `issue` and
    /// its last must equal `maturity`.
    ///
    /// Validation matches [`Bond::new`]; additionally the schedule's
    /// endpoints are checked against `(issue, maturity)`.
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::InvalidInstrument`] if any validation step
    ///   fails, or if the schedule's endpoints do not match
    ///   `(issue, maturity)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_curves::instruments::{Bond, SwapSchedule};
    /// use regit_curves::types::{Date, Daycount, Frequency};
    ///
    /// let issue    = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2026, 1, 2).unwrap();
    /// let sch = SwapSchedule::from_regular(issue, maturity, Frequency::Annual).unwrap();
    /// let bond = Bond::with_schedule(
    ///     issue, maturity, 0.05, Frequency::Annual, Daycount::Act365F,
    ///     1.0, 1.0, 0.0, sch,
    /// )
    /// .unwrap();
    /// assert_eq!(bond.schedule.len(), 2);
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn with_schedule(
        issue: Date,
        maturity: Date,
        coupon: f64,
        freq: Frequency,
        daycount: Daycount,
        notional: f64,
        clean_price: f64,
        accrued: f64,
        schedule: SwapSchedule,
    ) -> Result<Self, BootstrapError> {
        Self::validate(coupon, notional, clean_price, accrued, issue, maturity)?;
        if schedule.start() != issue || schedule.maturity() != maturity {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "schedule endpoints must match (issue, maturity)",
            });
        }
        Ok(Self {
            issue,
            maturity,
            coupon,
            freq,
            daycount,
            notional,
            clean_price,
            accrued,
            schedule,
        })
    }

    /// Shared validation for [`Bond::new`] and [`Bond::with_schedule`].
    fn validate(
        coupon: f64,
        notional: f64,
        clean_price: f64,
        accrued: f64,
        issue: Date,
        maturity: Date,
    ) -> Result<(), BootstrapError> {
        if !coupon.is_finite() {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "bond coupon must be finite",
            });
        }
        if !notional.is_finite() || notional <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "bond notional must be strictly positive",
            });
        }
        if !clean_price.is_finite() || clean_price <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "bond clean price must be strictly positive",
            });
        }
        if !accrued.is_finite() || accrued < 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "bond accrued interest must be finite and non-negative",
            });
        }
        if issue.days_between(maturity) <= 0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "bond issue must precede maturity",
            });
        }
        Ok(())
    }

    /// Present value of the coupon-leg cashflows at `reference_date`:
    ///
    /// ```text
    /// coupon_pv = coupon * SUM_i tau_i * D(t_i) * notional,
    /// ```
    ///
    /// with `tau_i` the period accruals under [`Bond::daycount`] and
    /// `D(t_i)` the curve's discount factor at each coupon'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::Bond;
    /// # use regit_curves::types::{Date, Daycount, Frequency};
    /// let issue    = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2025, 1, 2).unwrap();
    /// let bond = Bond::new(
    ///     issue, maturity, 0.05, Frequency::Annual, Daycount::Act365F,
    ///     1.0, 1.0, 0.0,
    /// ).unwrap();
    /// assert!((bond.coupon - 0.05).abs() < 1e-15);
    /// ```
    pub(crate) fn coupon_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.coupon * annuity * self.notional)
    }

    /// Present value of the principal repayment at `reference_date`:
    ///
    /// ```text
    /// principal_pv = notional * D(t_N).
    /// ```
    ///
    /// # Errors
    ///
    /// - [`BootstrapError::Type`] if the curve's day-count cannot produce a
    ///   year fraction for `maturity`.
    /// - [`BootstrapError::InvalidInstrument`] if the curve snapshot is
    ///   empty or returns a non-positive discount factor.
    ///
    /// # Examples
    ///
    /// ```
    /// # use regit_curves::instruments::Bond;
    /// # use regit_curves::types::{Date, Daycount, Frequency};
    /// let issue    = Date::from_ymd(2024, 1, 2).unwrap();
    /// let maturity = Date::from_ymd(2025, 1, 2).unwrap();
    /// let bond = Bond::new(
    ///     issue, maturity, 0.05, Frequency::Annual, Daycount::Act365F,
    ///     1.0, 1.0, 0.0,
    /// ).unwrap();
    /// assert_eq!(bond.maturity, maturity);
    /// ```
    pub(crate) fn principal_pv(
        &self,
        _reference_date: Date,
        curve: &CurveSnapshot<'_>,
    ) -> Result<f64, BootstrapError> {
        let t_maturity = curve
            .daycount
            .year_fraction(curve.reference_date, self.maturity)?;
        let d_maturity =
            curve
                .discount_at(t_maturity)
                .ok_or(BootstrapError::InvalidInstrument {
                    at_index: 0,
                    reason: "curve snapshot is empty",
                })?;
        if !d_maturity.is_finite() || d_maturity <= 0.0 {
            return Err(BootstrapError::InvalidInstrument {
                at_index: 0,
                reason: "non-positive discount factor in curve snapshot",
            });
        }
        Ok(self.notional * d_maturity)
    }
}

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

    fn residual(
        &self,
        reference_date: Date,
        curve: &CurveSnapshot<'_>,
    ) -> Result<f64, BootstrapError> {
        // residual = coupon_pv + principal_pv - dirty_price
        //          = coupon_pv + principal_pv - (clean_price + accrued).
        // Zero at the bootstrap solution.
        let coupon_pv = self.coupon_pv(reference_date, curve)?;
        let principal_pv = self.principal_pv(reference_date, curve)?;
        Ok(coupon_pv + principal_pv - (self.clean_price + self.accrued))
    }
}

#[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_5y_annual_bond() {
        let issue = d(2024, 1, 2);
        let maturity = d(2029, 1, 2);
        let bond = Bond::new(
            issue,
            maturity,
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        assert_eq!(bond.issue, issue);
        assert_eq!(bond.maturity, maturity);
        assert!((bond.coupon - 0.05).abs() < 1e-15);
        assert_eq!(bond.freq, Frequency::Annual);
        assert_eq!(bond.daycount, Daycount::Act365F);
        assert!((bond.notional - 1.0).abs() < 1e-15);
        assert!((bond.clean_price - 1.0).abs() < 1e-15);
        assert!((bond.accrued - 0.0).abs() < 1e-15);
        assert_eq!(bond.schedule.len(), 5);
        assert_eq!(bond.pillar(), maturity);
    }

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

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

    #[test]
    fn new_rejects_zero_notional() {
        let err = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            0.0,
            1.0,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_negative_notional() {
        let err = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            -1.0,
            1.0,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_zero_clean_price() {
        let err = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            0.0,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_negative_clean_price() {
        let err = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            -0.5,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_rejects_negative_accrued() {
        let err = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            -0.01,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

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

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

    #[test]
    fn new_rejects_equal_issue_and_maturity() {
        let s = d(2024, 1, 2);
        let err = Bond::new(
            s,
            s,
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn new_propagates_irregular_schedule_error() {
        let err = Bond::new(
            d(2024, 1, 2),
            d(2025, 2, 2),
            0.05,
            Frequency::SemiAnnual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

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

    #[test]
    fn with_schedule_accepts_matching_schedule() {
        let issue = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let sch = SwapSchedule::from_regular(issue, maturity, Frequency::Annual).unwrap();
        let bond = Bond::with_schedule(
            issue,
            maturity,
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
            sch,
        )
        .unwrap();
        assert_eq!(bond.schedule.len(), 2);
    }

    #[test]
    fn with_schedule_rejects_mismatched_endpoints() {
        let issue = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let other = d(2027, 1, 2);
        let sch = SwapSchedule::from_regular(issue, other, Frequency::Annual).unwrap();
        let err = Bond::with_schedule(
            issue,
            maturity,
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
            sch,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn with_schedule_rejects_invalid_inputs() {
        let issue = d(2024, 1, 2);
        let maturity = d(2026, 1, 2);
        let sch = SwapSchedule::from_regular(issue, maturity, Frequency::Annual).unwrap();
        let err = Bond::with_schedule(
            issue,
            maturity,
            f64::NAN,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
            sch,
        )
        .unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    // ─── Pricing identity on a flat continuously-compounded 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)
    }

    /// Closed-form par-bond coupon consistent with a flat continuously-
    /// compounded curve `D(t) = exp(-r_c * t)`. Solves
    /// `coupon * SUM_i tau_i * D(t_i) + D(t_N) = 1` (par-bond identity,
    /// unit notional, zero accrued).
    fn par_coupon_flat(bond: &Bond, reference: Date, r_c: f64) -> f64 {
        let dc_curve = bond.daycount; // same axis here
        let mut annuity = 0.0_f64;
        for i in 0..bond.schedule.len() {
            let s = bond.schedule.period_start(i);
            let e = bond.schedule.period_end(i);
            let tau = bond.daycount.year_fraction(s, e).unwrap();
            let t_pay = dc_curve.year_fraction(reference, e).unwrap();
            annuity += tau * (-r_c * t_pay).exp();
        }
        let t_n = dc_curve.year_fraction(reference, bond.maturity).unwrap();
        (1.0 - (-r_c * t_n).exp()) / annuity
    }

    #[test]
    fn par_bond_residual_is_zero_5y_annual_flat_5pct() {
        // Numerical target from the spec: 5y annual 5% bond on a flat 5%
        // continuously-compounded curve. Pick the par coupon analytically
        // and verify the residual is zero to 1e-12.
        let reference = d(2024, 1, 2);
        let dc = Daycount::Act365F;
        let r_c = 0.05_f64;
        let issue = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, dc, r_c);

        // Derive par coupon analytically.
        let placeholder = Bond::new(
            issue,
            maturity,
            0.0, // dummy coupon for schedule
            Frequency::Annual,
            dc,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let par_coupon = par_coupon_flat(&placeholder, reference, r_c);

        let bond = Bond::new(
            issue,
            maturity,
            par_coupon,
            Frequency::Annual,
            dc,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc,
            times: &times,
            discounts: &discounts,
        };
        let residual = bond.residual(reference, &snapshot).unwrap();
        assert!(
            residual.abs() < 1e-12,
            "par-bond residual on flat curve must be zero to 1e-12, got {residual}",
        );
    }

    #[test]
    fn off_par_bond_residual_matches_clean_price_shift() {
        // Same bond at par coupon, but quoted at clean_price = 0.95 instead
        // of 1.0 -> residual should be PV - 0.95 = 1.0 - 0.95 = +0.05.
        let reference = d(2024, 1, 2);
        let dc = Daycount::Act365F;
        let r_c = 0.05_f64;
        let issue = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, dc, r_c);

        let placeholder =
            Bond::new(issue, maturity, 0.0, Frequency::Annual, dc, 1.0, 1.0, 0.0).unwrap();
        let par_coupon = par_coupon_flat(&placeholder, reference, r_c);

        let bond = Bond::new(
            issue,
            maturity,
            par_coupon,
            Frequency::Annual,
            dc,
            1.0,
            0.95, // discount quote
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc,
            times: &times,
            discounts: &discounts,
        };
        let residual = bond.residual(reference, &snapshot).unwrap();
        // PV = 1.0 (par bond), clean_price = 0.95, accrued = 0 -> residual
        // = 1.0 - 0.95 = 0.05.
        assert!(
            (residual - 0.05).abs() < 1e-12,
            "off-par residual should be 0.05, got {residual}",
        );
    }

    #[test]
    fn coupon_pv_matches_manual_sum() {
        let reference = d(2024, 1, 2);
        let dc = Daycount::Act365F;
        let r_c = 0.04_f64;
        let issue = reference;
        let maturity = d(2027, 1, 2);
        let (times, discounts) = flat_curve(reference, dc, r_c);

        let bond = Bond::new(issue, maturity, 0.06, Frequency::Annual, dc, 1.0, 1.0, 0.0).unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc,
            times: &times,
            discounts: &discounts,
        };

        let mut expected = 0.0_f64;
        for i in 0..bond.schedule.len() {
            let p_start = bond.schedule.period_start(i);
            let p_end = bond.schedule.period_end(i);
            let tau = bond.daycount.year_fraction(p_start, p_end).unwrap();
            let t = dc.year_fraction(reference, p_end).unwrap();
            expected += tau * (-r_c * t).exp();
        }
        expected *= 0.06; // coupon * annuity * notional, notional = 1.0
        let got = bond.coupon_pv(reference, &snapshot).unwrap();
        assert!(
            (got - expected).abs() < 1e-12,
            "coupon_pv mismatch: got {got}, expected {expected}",
        );
    }

    #[test]
    fn principal_pv_equals_notional_times_discount() {
        let reference = d(2024, 1, 2);
        let dc = Daycount::Act365F;
        let r_c = 0.04_f64;
        let issue = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, dc, r_c);

        let bond = Bond::new(
            issue,
            maturity,
            0.05,
            Frequency::Annual,
            dc,
            100.0,
            100.0,
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc,
            times: &times,
            discounts: &discounts,
        };
        let t_n = dc.year_fraction(reference, maturity).unwrap();
        let expected = 100.0 * (-r_c * t_n).exp();
        let got = bond.principal_pv(reference, &snapshot).unwrap();
        assert!(
            (got - expected).abs() < 1e-10,
            "principal_pv mismatch: got {got}, expected {expected}",
        );
    }

    #[test]
    fn residual_errors_on_empty_snapshot() {
        let reference = d(2024, 1, 2);
        let bond = Bond::new(
            reference,
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: Daycount::Act365F,
            times: &[],
            discounts: &[],
        };
        let err = bond.residual(reference, &snapshot).unwrap_err();
        assert!(matches!(err, BootstrapError::InvalidInstrument { .. }));
    }

    #[test]
    fn bond_residual_propagates_business252_error() {
        // Coupon day-count is Business252; the accrual query inside
        // `coupon_pv` surfaces the day-count error rather than producing a
        // silent NaN.
        let reference = d(2024, 1, 2);
        let dc_curve = Daycount::Act365F;
        let (times, discounts) = flat_curve(reference, dc_curve, 0.04);
        let bond = Bond::new(
            reference,
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Business252,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc_curve,
            times: &times,
            discounts: &discounts,
        };
        let err = bond.residual(reference, &snapshot).unwrap_err();
        assert!(matches!(err, BootstrapError::Type(_)));
    }

    #[test]
    fn ten_year_semi_annual_par_bond_residual_zero() {
        // 10y semi-annual bond on a flat 4% curve.
        let reference = d(2024, 1, 2);
        let dc = Daycount::Act365F;
        let r_c = 0.04_f64;
        let issue = reference;
        let maturity = d(2034, 1, 2);
        let (times, discounts) = flat_curve(reference, dc, r_c);

        let placeholder = Bond::new(
            issue,
            maturity,
            0.0,
            Frequency::SemiAnnual,
            dc,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let par_coupon = par_coupon_flat(&placeholder, reference, r_c);

        let bond = Bond::new(
            issue,
            maturity,
            par_coupon,
            Frequency::SemiAnnual,
            dc,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc,
            times: &times,
            discounts: &discounts,
        };
        let residual = bond.residual(reference, &snapshot).unwrap();
        assert!(
            residual.abs() < 1e-10,
            "10y semi-annual residual at par should be < 1e-10, got {residual}",
        );
    }

    #[test]
    fn par_bond_invariant_under_mixed_daycounts() {
        // Coupons Thirty360BondBasis against Act/365F curve — the par-bond
        // identity still drives residual to zero with the right accruals.
        let reference = d(2024, 1, 2);
        let issue = reference;
        let maturity = d(2029, 1, 2);
        let dc_curve = Daycount::Act365F;
        let dc_coupon = Daycount::Thirty360BondBasis;
        let r_c = 0.04_f64;
        let (times, discounts) = flat_curve(reference, dc_curve, r_c);

        // Manual par-coupon under mixed day-counts.
        let schedule = SwapSchedule::from_regular(issue, maturity, Frequency::Annual).unwrap();
        let mut annuity = 0.0_f64;
        for i in 0..schedule.len() {
            let s = schedule.period_start(i);
            let e = schedule.period_end(i);
            let tau = dc_coupon.year_fraction(s, e).unwrap();
            let t_pay = dc_curve.year_fraction(reference, e).unwrap();
            annuity += tau * (-r_c * t_pay).exp();
        }
        let t_n = dc_curve.year_fraction(reference, maturity).unwrap();
        let par_coupon = (1.0 - (-r_c * t_n).exp()) / annuity;

        let bond = Bond::new(
            issue,
            maturity,
            par_coupon,
            Frequency::Annual,
            dc_coupon,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc_curve,
            times: &times,
            discounts: &discounts,
        };
        let residual = bond.residual(reference, &snapshot).unwrap();
        assert!(residual.abs() < 1e-10, "mixed-DC residual: {residual}");
    }

    #[test]
    fn debug_format_contains_struct_name() {
        let bond = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let s = format!("{bond:?}");
        assert!(s.contains("Bond"));
    }

    #[test]
    fn clone_and_eq_round_trip() {
        let bond = Bond::new(
            d(2024, 1, 2),
            d(2029, 1, 2),
            0.05,
            Frequency::Annual,
            Daycount::Act365F,
            1.0,
            1.0,
            0.0,
        )
        .unwrap();
        let cloned = bond.clone();
        assert_eq!(bond, cloned);
    }

    #[test]
    fn residual_with_accrued_subtracts_dirty_price() {
        // PV = 1.0 (par), clean = 0.98, accrued = 0.02 -> residual = 0.
        let reference = d(2024, 1, 2);
        let dc = Daycount::Act365F;
        let r_c = 0.05_f64;
        let issue = reference;
        let maturity = d(2029, 1, 2);
        let (times, discounts) = flat_curve(reference, dc, r_c);

        let placeholder =
            Bond::new(issue, maturity, 0.0, Frequency::Annual, dc, 1.0, 1.0, 0.0).unwrap();
        let par_coupon = par_coupon_flat(&placeholder, reference, r_c);

        let bond = Bond::new(
            issue,
            maturity,
            par_coupon,
            Frequency::Annual,
            dc,
            1.0,
            0.98,
            0.02,
        )
        .unwrap();
        let snapshot = CurveSnapshot {
            reference_date: reference,
            daycount: dc,
            times: &times,
            discounts: &discounts,
        };
        let residual = bond.residual(reference, &snapshot).unwrap();
        assert!(
            residual.abs() < 1e-12,
            "residual with accrued must be zero when PV = clean + accrued, got {residual}",
        );
    }
}