corp-finance-core 1.1.0

Institutional-grade corporate finance calculations with 128-bit decimal precision — DCF, WACC, comps, LBO, credit metrics, derivatives, fixed income, options, and 60+ specialty modules. No f64 in financials. WASM-compatible.
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
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::types::*;
use crate::{CorpFinanceError, CorpFinanceResult};

// ---------------------------------------------------------------------------
// Input / Output types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CdsInput {
    pub reference_entity: String,
    pub notional: Money,
    /// CDS spread in basis points (market quoted)
    pub spread_bps: Decimal,
    /// Expected recovery rate (e.g. 0.40)
    pub recovery_rate: Rate,
    /// Risk-free discount rate
    pub risk_free_rate: Rate,
    /// CDS tenor in years (1-30)
    pub maturity_years: u32,
    /// Premium payments per year (1, 2, or 4)
    pub payment_frequency: u32,
    /// Annual hazard rate / default probability. If None, implied from spread.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_probability: Option<Rate>,
    /// Market spread for MTM calculation. If different from spread_bps, calculate mark-to-market.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub market_spread_bps: Option<Decimal>,
    /// Counterparty credit rating for risk assessment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub counterparty_rating: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SurvivalPoint {
    pub year: u32,
    pub survival_probability: Rate,
    pub cumulative_default_probability: Rate,
    pub discount_factor: Decimal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreditTriangle {
    pub spread_bps: Decimal,
    pub default_probability: Rate,
    pub recovery_rate: Rate,
    pub loss_given_default: Rate,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CdsOutput {
    pub reference_entity: String,
    pub notional: Money,
    pub spread_bps: Decimal,
    /// Annual premium payment (notional * spread / 10000)
    pub annual_premium: Money,
    /// Implied from spread if not provided: spread_bps / (10000 * (1 - recovery_rate))
    pub implied_default_probability: Rate,
    /// Year-by-year survival probabilities
    pub survival_probabilities: Vec<SurvivalPoint>,
    /// Present value of 1bp of premium (risky annuity)
    pub risky_pv01: Decimal,
    /// PV of protection payments
    pub protection_leg_pv: Money,
    /// PV of premium payments at the quoted spread
    pub premium_leg_pv: Money,
    /// MTM / upfront = (market_spread - contract_spread) * risky_pv01 * notional / 10000
    pub upfront_payment: Money,
    /// Same as upfront if market_spread given, else zero
    pub mark_to_market: Money,
    /// Fair spread = protection_leg_pv / risky_pv01 * 10000
    pub breakeven_spread_bps: Decimal,
    /// Dollar value of 1bp spread change
    pub dv01: Money,
    /// Loss given immediate default = notional * (1 - recovery_rate) - accrued
    pub jump_to_default: Money,
    /// Implied credit metrics
    pub credit_triangle: CreditTriangle,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Price a single-name credit default swap.
///
/// Calculates premium and protection leg PVs using a discrete hazard-rate
/// model, producing survival curves, risky PV01, mark-to-market, DV01,
/// jump-to-default exposure, and breakeven spread.
pub fn price_cds(input: &CdsInput) -> CorpFinanceResult<CdsOutput> {
    validate_cds_input(input)?;

    let lgd = Decimal::ONE - input.recovery_rate;

    // Implied default probability (hazard rate)
    let lambda = match input.default_probability {
        Some(pd) => pd,
        None => {
            // Credit triangle: spread = PD * LGD * 10000
            // => PD = spread / (10000 * LGD)
            if lgd.is_zero() {
                return Err(CorpFinanceError::DivisionByZero {
                    context: "Cannot imply default probability with zero LGD".to_string(),
                });
            }
            input.spread_bps / (dec!(10000) * lgd)
        }
    };

    // Build survival curve, discount factors, risky PV01, and protection leg PV
    let periods_per_year = input.payment_frequency;
    let total_periods = input.maturity_years * periods_per_year;
    let dt = Decimal::ONE / Decimal::from(periods_per_year);

    // Survival factor per period: (1 - lambda)^dt
    // For discrete model with sub-annual periods, use iterative approach.
    // Per-period survival = (1 - lambda * dt) clamped to [0, 1]
    // This is a first-order approximation suitable for small dt * lambda.
    let per_period_survival = (Decimal::ONE - lambda * dt).max(Decimal::ZERO);

    let mut survival_prob = Decimal::ONE;
    let mut discount_factor = Decimal::ONE;
    let one_plus_r = Decimal::ONE + input.risk_free_rate;

    // Per-period discount factor: 1 / (1+r)^dt
    // Compute iteratively: each period multiply by 1/(1 + r*dt) for simple,
    // or use per_period_discount = 1/(1+r)^(1/freq).
    // We use iterative multiplication to avoid powd.
    // For fractional powers, we approximate: (1+r)^(1/n) via Newton's method.
    let per_period_discount = nth_root(one_plus_r, periods_per_year);
    let per_period_discount_inv = if per_period_discount.is_zero() {
        Decimal::ONE
    } else {
        Decimal::ONE / per_period_discount
    };

    let mut risky_pv01 = Decimal::ZERO;
    let mut protection_leg = Decimal::ZERO;

    // Collect annual survival points
    let mut survival_points: Vec<SurvivalPoint> = Vec::new();

    for period in 1..=total_periods {
        let prev_s = survival_prob;
        survival_prob *= per_period_survival;
        discount_factor *= per_period_discount_inv;

        // Risky PV01: sum of dt * S(t) * D(t)
        risky_pv01 += dt * survival_prob * discount_factor;

        // Protection leg: LGD * (S(t-1) - S(t)) * D(t)
        let marginal_default = prev_s - survival_prob;
        protection_leg += lgd * marginal_default * discount_factor;

        // Record annual survival points
        if period % periods_per_year == 0 {
            let year = period / periods_per_year;
            let cum_default = Decimal::ONE - survival_prob;
            survival_points.push(SurvivalPoint {
                year,
                survival_probability: survival_prob,
                cumulative_default_probability: cum_default,
                discount_factor,
            });
        }
    }

    // Handle case where last period doesn't align with a year boundary
    if !total_periods.is_multiple_of(periods_per_year) {
        let year = input.maturity_years;
        let cum_default = Decimal::ONE - survival_prob;
        survival_points.push(SurvivalPoint {
            year,
            survival_probability: survival_prob,
            cumulative_default_probability: cum_default,
            discount_factor,
        });
    }

    // Protection leg PV (notional-scaled)
    let protection_leg_pv = protection_leg * input.notional;

    // Premium leg PV at quoted spread
    let spread_decimal = input.spread_bps / dec!(10000);
    let premium_leg_pv = spread_decimal * risky_pv01 * input.notional;

    // Annual premium
    let annual_premium = input.notional * spread_decimal;

    // Breakeven spread
    let breakeven_spread_bps = if risky_pv01.is_zero() {
        Decimal::ZERO
    } else {
        protection_leg / risky_pv01 * dec!(10000)
    };

    // DV01: risky_pv01 * notional / 10000
    let dv01 = risky_pv01 * input.notional / dec!(10000);

    // MTM / upfront payment
    let (upfront_payment, mark_to_market) = match input.market_spread_bps {
        Some(mkt_spread) => {
            let upfront =
                (mkt_spread - input.spread_bps) * risky_pv01 * input.notional / dec!(10000);
            (upfront, upfront)
        }
        None => (Decimal::ZERO, Decimal::ZERO),
    };

    // Jump to default: notional * LGD minus half a period of accrued premium
    // Accrued approximation: half a coupon period
    let accrued = spread_decimal * input.notional * dt / dec!(2);
    let jump_to_default = input.notional * lgd - accrued;

    let credit_triangle = CreditTriangle {
        spread_bps: input.spread_bps,
        default_probability: lambda,
        recovery_rate: input.recovery_rate,
        loss_given_default: lgd,
    };

    Ok(CdsOutput {
        reference_entity: input.reference_entity.clone(),
        notional: input.notional,
        spread_bps: input.spread_bps,
        annual_premium,
        implied_default_probability: lambda,
        survival_probabilities: survival_points,
        risky_pv01,
        protection_leg_pv,
        premium_leg_pv,
        upfront_payment,
        mark_to_market,
        breakeven_spread_bps,
        dv01,
        jump_to_default,
        credit_triangle,
    })
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

fn validate_cds_input(input: &CdsInput) -> CorpFinanceResult<()> {
    if input.notional <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "notional".into(),
            reason: "Notional must be positive.".into(),
        });
    }
    if input.recovery_rate < Decimal::ZERO || input.recovery_rate >= Decimal::ONE {
        return Err(CorpFinanceError::InvalidInput {
            field: "recovery_rate".into(),
            reason: "Recovery rate must be in [0, 1).".into(),
        });
    }
    if input.risk_free_rate < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "risk_free_rate".into(),
            reason: "Risk-free rate must be non-negative.".into(),
        });
    }
    if input.maturity_years < 1 || input.maturity_years > 30 {
        return Err(CorpFinanceError::InvalidInput {
            field: "maturity_years".into(),
            reason: "Maturity must be between 1 and 30 years.".into(),
        });
    }
    if input.payment_frequency != 1 && input.payment_frequency != 2 && input.payment_frequency != 4
    {
        return Err(CorpFinanceError::InvalidInput {
            field: "payment_frequency".into(),
            reason: "Payment frequency must be 1, 2, or 4.".into(),
        });
    }
    if input.spread_bps < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "spread_bps".into(),
            reason: "Spread must be non-negative.".into(),
        });
    }
    if let Some(pd) = input.default_probability {
        if pd < Decimal::ZERO || pd >= Decimal::ONE {
            return Err(CorpFinanceError::InvalidInput {
                field: "default_probability".into(),
                reason: "Default probability must be in [0, 1).".into(),
            });
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Math helpers
// ---------------------------------------------------------------------------

/// Compute the nth root of `x` via Newton's method: x^(1/n).
/// Uses 30 iterations for convergence.
fn nth_root(x: Decimal, n: u32) -> Decimal {
    if n == 0 || x.is_zero() {
        return x;
    }
    if n == 1 {
        return x;
    }

    let n_dec = Decimal::from(n);
    let n_minus_1 = n_dec - Decimal::ONE;

    // Initial guess: 1 + (x - 1) / n  (first-order Taylor)
    let mut guess = Decimal::ONE + (x - Decimal::ONE) / n_dec;

    for _ in 0..30 {
        // guess^(n-1) via iterative multiplication
        let mut power = Decimal::ONE;
        for _ in 0..(n - 1) {
            power *= guess;
        }
        if power.is_zero() {
            break;
        }
        // Newton step: new_guess = ((n-1)*guess + x / guess^(n-1)) / n
        guess = (n_minus_1 * guess + x / power) / n_dec;
    }

    guess
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    fn basic_cds_input() -> CdsInput {
        CdsInput {
            reference_entity: "Acme Corp".to_string(),
            notional: dec!(10_000_000),
            spread_bps: dec!(100), // 100 bps = 1%
            recovery_rate: dec!(0.40),
            risk_free_rate: dec!(0.05),
            maturity_years: 5,
            payment_frequency: 4,
            default_probability: None,
            market_spread_bps: None,
            counterparty_rating: None,
        }
    }

    #[test]
    fn test_basic_cds_pricing() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        assert_eq!(result.reference_entity, "Acme Corp");
        assert_eq!(result.notional, dec!(10_000_000));
        assert_eq!(result.spread_bps, dec!(100));
        assert!(result.annual_premium > Decimal::ZERO);
        assert!(result.risky_pv01 > Decimal::ZERO);
        assert!(result.protection_leg_pv > Decimal::ZERO);
        assert!(result.premium_leg_pv > Decimal::ZERO);
    }

    #[test]
    fn test_annual_premium_calculation() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();
        // annual_premium = notional * spread / 10000 = 10M * 100/10000 = 100,000
        assert_eq!(result.annual_premium, dec!(100_000));
    }

    #[test]
    fn test_implied_default_probability() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        // lambda = spread / (10000 * LGD) = 100 / (10000 * 0.6) = 100/6000
        let expected_lambda = dec!(100) / dec!(6000);
        assert_eq!(result.implied_default_probability, expected_lambda);
    }

    #[test]
    fn test_explicit_default_probability() {
        let mut input = basic_cds_input();
        input.default_probability = Some(dec!(0.02));
        let result = price_cds(&input).unwrap();

        assert_eq!(result.implied_default_probability, dec!(0.02));
    }

    #[test]
    fn test_survival_curve_decreasing() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        assert_eq!(result.survival_probabilities.len(), 5);
        let mut prev = Decimal::ONE;
        for sp in &result.survival_probabilities {
            assert!(
                sp.survival_probability < prev,
                "Survival probability should decrease: {} >= {}",
                sp.survival_probability,
                prev
            );
            assert!(
                sp.survival_probability > Decimal::ZERO,
                "Survival probability should be positive"
            );
            prev = sp.survival_probability;
        }
    }

    #[test]
    fn test_cumulative_default_probability() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        for sp in &result.survival_probabilities {
            let sum = sp.survival_probability + sp.cumulative_default_probability;
            // Should sum to 1
            let diff = (sum - Decimal::ONE).abs();
            assert!(
                diff < dec!(0.0001),
                "S(t) + CumPD(t) should equal 1, got {}",
                sum
            );
        }
    }

    #[test]
    fn test_discount_factors_decreasing() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        let mut prev = Decimal::ONE;
        for sp in &result.survival_probabilities {
            assert!(
                sp.discount_factor < prev,
                "Discount factors should decrease"
            );
            assert!(sp.discount_factor > Decimal::ZERO);
            prev = sp.discount_factor;
        }
    }

    #[test]
    fn test_dv01_positive() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        // DV01 = risky_pv01 * notional / 10000
        let expected_dv01 = result.risky_pv01 * dec!(10_000_000) / dec!(10000);
        let diff = (result.dv01 - expected_dv01).abs();
        assert!(
            diff < dec!(0.01),
            "DV01 mismatch: {} vs {}",
            result.dv01,
            expected_dv01
        );
        assert!(result.dv01 > Decimal::ZERO);
    }

    #[test]
    fn test_breakeven_spread() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        // Breakeven should be close to input spread for a fairly priced CDS
        // (slight difference due to discrete model vs. continuous)
        assert!(
            result.breakeven_spread_bps > Decimal::ZERO,
            "Breakeven spread should be positive"
        );
    }

    #[test]
    fn test_mtm_zero_when_no_market_spread() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        assert_eq!(result.upfront_payment, Decimal::ZERO);
        assert_eq!(result.mark_to_market, Decimal::ZERO);
    }

    #[test]
    fn test_mtm_with_market_spread() {
        let mut input = basic_cds_input();
        input.market_spread_bps = Some(dec!(150)); // 150 bps vs 100 bps contract
        let result = price_cds(&input).unwrap();

        // Market wider than contract => protection buyer gains => positive upfront
        assert!(
            result.upfront_payment > Decimal::ZERO,
            "Upfront should be positive when market spread > contract spread"
        );
        assert_eq!(result.mark_to_market, result.upfront_payment);
    }

    #[test]
    fn test_mtm_negative_when_spread_tightens() {
        let mut input = basic_cds_input();
        input.market_spread_bps = Some(dec!(50)); // 50 bps vs 100 bps contract
        let result = price_cds(&input).unwrap();

        // Market tighter than contract => protection buyer loses
        assert!(
            result.upfront_payment < Decimal::ZERO,
            "Upfront should be negative when market spread < contract spread"
        );
    }

    #[test]
    fn test_jump_to_default() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        // LGD exposure minus accrued
        let lgd_exposure = dec!(10_000_000) * dec!(0.60);
        assert!(
            result.jump_to_default < lgd_exposure,
            "JTD should be less than full LGD exposure (minus accrued)"
        );
        assert!(
            result.jump_to_default > Decimal::ZERO,
            "JTD should be positive"
        );
    }

    #[test]
    fn test_credit_triangle() {
        let input = basic_cds_input();
        let result = price_cds(&input).unwrap();

        assert_eq!(result.credit_triangle.spread_bps, dec!(100));
        assert_eq!(result.credit_triangle.recovery_rate, dec!(0.40));
        assert_eq!(result.credit_triangle.loss_given_default, dec!(0.60));
        // PD * LGD * 10000 should approximate spread
        let reconstructed =
            result.credit_triangle.default_probability * result.credit_triangle.loss_given_default;
        let diff = (reconstructed - dec!(0.01)).abs(); // spread = 100bps = 0.01
        assert!(
            diff < dec!(0.0001),
            "Credit triangle consistency: PD*LGD={} vs spread=0.01",
            reconstructed
        );
    }

    #[test]
    fn test_higher_spread_higher_protection_pv() {
        let input_low = basic_cds_input(); // 100 bps
        let mut input_high = basic_cds_input();
        input_high.spread_bps = dec!(500); // 500 bps

        let result_low = price_cds(&input_low).unwrap();
        let result_high = price_cds(&input_high).unwrap();

        // Higher spread implies higher default probability, hence higher protection leg PV
        assert!(
            result_high.protection_leg_pv > result_low.protection_leg_pv,
            "Higher spread should yield higher protection leg PV"
        );
    }

    #[test]
    fn test_quarterly_vs_annual_payments() {
        let mut input_q = basic_cds_input();
        input_q.payment_frequency = 4;
        let mut input_a = basic_cds_input();
        input_a.payment_frequency = 1;

        let result_q = price_cds(&input_q).unwrap();
        let result_a = price_cds(&input_a).unwrap();

        // More frequent payments generally yield a slightly different risky PV01
        assert_ne!(
            result_q.risky_pv01, result_a.risky_pv01,
            "Different frequencies should yield different risky PV01"
        );
    }

    #[test]
    fn test_zero_spread() {
        let mut input = basic_cds_input();
        input.spread_bps = Decimal::ZERO;
        let result = price_cds(&input).unwrap();

        assert_eq!(result.annual_premium, Decimal::ZERO);
        assert_eq!(result.premium_leg_pv, Decimal::ZERO);
        assert_eq!(result.implied_default_probability, Decimal::ZERO);
    }

    #[test]
    fn test_high_recovery_rate() {
        let mut input = basic_cds_input();
        input.recovery_rate = dec!(0.95); // very high recovery
        let result = price_cds(&input).unwrap();

        // Higher recovery => lower LGD => higher implied PD for same spread
        assert!(
            result.implied_default_probability > dec!(0.10),
            "High recovery should imply high PD for same spread"
        );
    }

    #[test]
    fn test_1yr_maturity() {
        let mut input = basic_cds_input();
        input.maturity_years = 1;
        let result = price_cds(&input).unwrap();

        assert_eq!(result.survival_probabilities.len(), 1);
        assert!(result.risky_pv01 > Decimal::ZERO);
        assert!(result.risky_pv01 < dec!(1)); // PV01 for 1yr should be small
    }

    #[test]
    fn test_long_maturity() {
        let mut input = basic_cds_input();
        input.maturity_years = 10;
        let result = price_cds(&input).unwrap();

        assert_eq!(result.survival_probabilities.len(), 10);
        // Longer maturity => higher risky PV01
        let result_5y = price_cds(&basic_cds_input()).unwrap();
        assert!(
            result.risky_pv01 > result_5y.risky_pv01,
            "10Y risky PV01 should exceed 5Y"
        );
    }

    // -- Validation tests --

    #[test]
    fn test_invalid_notional() {
        let mut input = basic_cds_input();
        input.notional = Decimal::ZERO;
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => assert_eq!(field, "notional"),
            other => panic!("Expected InvalidInput for notional, got {other:?}"),
        }
    }

    #[test]
    fn test_invalid_recovery_rate_too_high() {
        let mut input = basic_cds_input();
        input.recovery_rate = Decimal::ONE;
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => assert_eq!(field, "recovery_rate"),
            other => panic!("Expected InvalidInput for recovery_rate, got {other:?}"),
        }
    }

    #[test]
    fn test_invalid_recovery_rate_negative() {
        let mut input = basic_cds_input();
        input.recovery_rate = dec!(-0.1);
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => assert_eq!(field, "recovery_rate"),
            other => panic!("Expected InvalidInput for recovery_rate, got {other:?}"),
        }
    }

    #[test]
    fn test_invalid_maturity() {
        let mut input = basic_cds_input();
        input.maturity_years = 0;
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => assert_eq!(field, "maturity_years"),
            other => panic!("Expected InvalidInput for maturity_years, got {other:?}"),
        }
    }

    #[test]
    fn test_invalid_payment_frequency() {
        let mut input = basic_cds_input();
        input.payment_frequency = 3;
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert_eq!(field, "payment_frequency")
            }
            other => panic!("Expected InvalidInput for payment_frequency, got {other:?}"),
        }
    }

    #[test]
    fn test_invalid_default_probability() {
        let mut input = basic_cds_input();
        input.default_probability = Some(dec!(1.0));
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert_eq!(field, "default_probability")
            }
            other => panic!("Expected InvalidInput for default_probability, got {other:?}"),
        }
    }

    #[test]
    fn test_semiannual_payments() {
        let mut input = basic_cds_input();
        input.payment_frequency = 2;
        let result = price_cds(&input).unwrap();

        assert_eq!(result.survival_probabilities.len(), 5);
        assert!(result.risky_pv01 > Decimal::ZERO);
    }

    #[test]
    fn test_negative_spread_rejected() {
        let mut input = basic_cds_input();
        input.spread_bps = dec!(-10);
        let err = price_cds(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => assert_eq!(field, "spread_bps"),
            other => panic!("Expected InvalidInput for spread_bps, got {other:?}"),
        }
    }

    #[test]
    fn test_nth_root_helper() {
        // 4th root of 16 = 2
        let result = nth_root(dec!(16), 4);
        let diff = (result - dec!(2)).abs();
        assert!(
            diff < dec!(0.0001),
            "4th root of 16 should be ~2, got {}",
            result
        );

        // square root of 4 = 2
        let result2 = nth_root(dec!(4), 2);
        let diff2 = (result2 - dec!(2)).abs();
        assert!(
            diff2 < dec!(0.0001),
            "sqrt(4) should be ~2, got {}",
            result2
        );
    }
}