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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//! CLO Tranche Analytics.
//!
//! Computes individual tranche-level metrics:
//! - Yield to Maturity / Yield to Call / Yield to Worst (Newton-Raphson)
//! - Weighted Average Life (WAL)
//! - Spread Duration (finite difference)
//! - Breakeven Default Rate (binary search)
//! - Equity IRR (Newton-Raphson)
//! - Cash-on-Cash Return
//!
//! All arithmetic uses `rust_decimal::Decimal`. No `f64`.

use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::CorpFinanceResult;

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

/// A single cash flow for a tranche.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrancheCashFlow {
    /// Period number (1-indexed).
    pub period: u32,
    /// Interest payment.
    pub interest: Decimal,
    /// Principal payment.
    pub principal: Decimal,
}

/// Input for tranche analytics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrancheAnalyticsInput {
    /// Tranche name/identifier.
    pub tranche_name: String,
    /// Projected cash flows.
    pub cash_flows: Vec<TrancheCashFlow>,
    /// Initial investment amount.
    pub initial_investment: Decimal,
    /// Current price as percentage of par (e.g. 99.5 = 99.5%).
    pub price: Decimal,
    /// Period at which the tranche can be called (optional, 0 = no call).
    pub call_date_period: u32,
    /// Reference rate for spread calculations.
    pub reference_rate: Decimal,
}

/// Output of tranche analytics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrancheAnalyticsOutput {
    /// Yield to maturity (annualized, decimal).
    pub yield_to_maturity: Decimal,
    /// Yield to call (annualized, decimal). None if no call date.
    pub yield_to_call: Option<Decimal>,
    /// Yield to worst (min of YTM and YTC).
    pub yield_to_worst: Decimal,
    /// Weighted average life (years, assuming 4 periods per year).
    pub wal: Decimal,
    /// Spread duration (sensitivity to 1bp spread change).
    pub spread_duration: Decimal,
    /// Breakeven CDR (annual) at which tranche begins taking losses.
    pub breakeven_cdr: Option<Decimal>,
    /// Equity IRR (if this is an equity tranche). None for rated tranches.
    pub equity_irr: Option<Decimal>,
    /// Cash-on-cash return (annualized). None for rated tranches.
    pub cash_on_cash: Option<Decimal>,
}

// ---------------------------------------------------------------------------
// Newton-Raphson yield solver
// ---------------------------------------------------------------------------

/// Solve for the periodic yield that equates PV of cash flows to price.
///
/// PV(y) = sum[ CF_t / (1+y)^t ] = price * par
/// We solve for y such that PV(y) - target = 0.
fn newton_yield(
    cash_flows: &[(u32, Decimal)],
    target_pv: Decimal,
    max_iter: u32,
) -> CorpFinanceResult<Decimal> {
    let mut y = dec!(0.02); // initial guess (periodic)

    for _iter in 0..max_iter {
        let mut pv = Decimal::ZERO;
        let mut dpv = Decimal::ZERO;

        for &(t, cf) in cash_flows {
            if cf.is_zero() {
                continue;
            }
            // discount factor = 1/(1+y)^t using iterative multiplication
            let mut df = Decimal::ONE;
            for _ in 0..t {
                let denom = Decimal::ONE + y;
                if denom.is_zero() {
                    return Err(CorpFinanceError::DivisionByZero {
                        context: "Yield solver: (1+y) is zero.".into(),
                    });
                }
                df /= denom;
            }
            pv += cf * df;
            // dpv/dy = -t * cf / (1+y)^(t+1)
            let denom_plus = Decimal::ONE + y;
            if denom_plus.is_zero() {
                return Err(CorpFinanceError::DivisionByZero {
                    context: "Yield solver derivative: (1+y) is zero.".into(),
                });
            }
            dpv -= Decimal::from(t) * cf * df / denom_plus;
        }

        let f_val = pv - target_pv;

        if f_val.abs() < dec!(0.0000001) {
            return Ok(y);
        }

        if dpv.is_zero() {
            break;
        }

        y -= f_val / dpv;

        // Clamp to avoid divergence
        if y < dec!(-0.5) {
            y = dec!(-0.5);
        }
        if y > dec!(2.0) {
            y = dec!(2.0);
        }
    }

    Ok(y)
}

/// Convert periodic yield to annualized (assuming 4 periods per year).
fn annualize_yield(periodic: Decimal, periods_per_year: u32) -> Decimal {
    periodic * Decimal::from(periods_per_year)
}

// ---------------------------------------------------------------------------
// Newton-Raphson IRR solver
// ---------------------------------------------------------------------------

/// Solve for IRR given cash flows (first is negative investment).
fn newton_irr(cash_flows: &[Decimal], max_iter: u32) -> CorpFinanceResult<Decimal> {
    if cash_flows.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "No cash flows for IRR.".into(),
        ));
    }

    let mut r = dec!(0.10); // initial guess (periodic)

    for _iter in 0..max_iter {
        let mut npv = Decimal::ZERO;
        let mut dnpv = Decimal::ZERO;

        for (t, cf) in cash_flows.iter().enumerate() {
            if cf.is_zero() {
                continue;
            }
            // df = 1/(1+r)^t
            let mut df = Decimal::ONE;
            for _ in 0..t {
                let denom = Decimal::ONE + r;
                if denom.is_zero() {
                    return Err(CorpFinanceError::DivisionByZero {
                        context: "IRR solver: (1+r) is zero.".into(),
                    });
                }
                df /= denom;
            }
            npv += *cf * df;
            // d(npv)/dr = -t * cf / (1+r)^(t+1)
            if t > 0 {
                let denom = Decimal::ONE + r;
                if denom.is_zero() {
                    return Err(CorpFinanceError::DivisionByZero {
                        context: "IRR solver derivative: (1+r) is zero.".into(),
                    });
                }
                dnpv -= Decimal::from(t as u32) * *cf * df / denom;
            }
        }

        if npv.abs() < dec!(0.0000001) {
            return Ok(r);
        }

        if dnpv.is_zero() {
            break;
        }

        r -= npv / dnpv;

        if r < dec!(-0.99) {
            r = dec!(-0.99);
        }
        if r > dec!(10.0) {
            r = dec!(10.0);
        }
    }

    Ok(r)
}

// ---------------------------------------------------------------------------
// Engine
// ---------------------------------------------------------------------------

/// Compute tranche analytics.
pub fn calculate_tranche_analytics(
    input: &TrancheAnalyticsInput,
) -> CorpFinanceResult<TrancheAnalyticsOutput> {
    validate_tranche_input(input)?;

    let periods_per_year: u32 = 4; // quarterly

    // Build total cash flow per period (interest + principal)
    let total_cfs: Vec<(u32, Decimal)> = input
        .cash_flows
        .iter()
        .map(|cf| (cf.period, cf.interest + cf.principal))
        .collect();

    // Target PV = price/100 * initial_investment
    let target_pv = input.price / dec!(100) * input.initial_investment;

    // --- Yield to Maturity ---
    let ytm_periodic = newton_yield(&total_cfs, target_pv, 30)?;
    let yield_to_maturity = annualize_yield(ytm_periodic, periods_per_year);

    // --- Yield to Call ---
    let yield_to_call = if input.call_date_period > 0 {
        let call_cfs: Vec<(u32, Decimal)> = input
            .cash_flows
            .iter()
            .filter(|cf| cf.period <= input.call_date_period)
            .map(|cf| (cf.period, cf.interest + cf.principal))
            .collect();

        // At call, remaining par is returned
        let prin_paid_to_call: Decimal = input
            .cash_flows
            .iter()
            .filter(|cf| cf.period <= input.call_date_period)
            .map(|cf| cf.principal)
            .sum();
        let remaining_par = input.initial_investment - prin_paid_to_call;

        let mut call_cfs_with_redemption = call_cfs;
        // Add remaining par to the last call period
        if let Some(last) = call_cfs_with_redemption.last_mut() {
            if last.0 == input.call_date_period {
                last.1 += remaining_par;
            }
        } else {
            call_cfs_with_redemption.push((input.call_date_period, remaining_par));
        }

        let ytc_periodic = newton_yield(&call_cfs_with_redemption, target_pv, 30)?;
        Some(annualize_yield(ytc_periodic, periods_per_year))
    } else {
        None
    };

    // --- Yield to Worst ---
    let yield_to_worst = match yield_to_call {
        Some(ytc) => {
            if ytc < yield_to_maturity {
                ytc
            } else {
                yield_to_maturity
            }
        }
        None => yield_to_maturity,
    };

    // --- WAL ---
    let total_principal: Decimal = input.cash_flows.iter().map(|cf| cf.principal).sum();
    let wal = if total_principal.is_zero() {
        Decimal::ZERO
    } else {
        let weighted_time: Decimal = input
            .cash_flows
            .iter()
            .map(|cf| cf.principal * Decimal::from(cf.period) / Decimal::from(periods_per_year))
            .sum();
        weighted_time / total_principal
    };

    // --- Spread Duration ---
    // Finite difference: dP/ds ~ [PV(s-1bp) - PV(s+1bp)] / (2 * 1bp * par)
    let one_bp = dec!(0.0001);
    let base_rate = ytm_periodic;

    let pv_down = {
        let r = base_rate - one_bp / Decimal::from(periods_per_year);
        let mut pv = Decimal::ZERO;
        for &(t, cf) in &total_cfs {
            let mut df = Decimal::ONE;
            for _ in 0..t {
                df /= Decimal::ONE + r;
            }
            pv += cf * df;
        }
        pv
    };

    let pv_up = {
        let r = base_rate + one_bp / Decimal::from(periods_per_year);
        let mut pv = Decimal::ZERO;
        for &(t, cf) in &total_cfs {
            let mut df = Decimal::ONE;
            for _ in 0..t {
                df /= Decimal::ONE + r;
            }
            pv += cf * df;
        }
        pv
    };

    let spread_duration = if target_pv.is_zero() {
        Decimal::ZERO
    } else {
        (pv_down - pv_up) / (dec!(2) * one_bp * target_pv)
    };

    // --- Breakeven CDR ---
    // Binary search: find annual CDR where cumulative losses exhaust subordination
    // For a rated tranche, subordination = initial_investment (simplified)
    // For equity, breakeven is not meaningful
    let is_equity = input.tranche_name.to_uppercase().contains("EQUITY");

    let breakeven_cdr = if !is_equity {
        // Simplified: total_principal represents tranche par
        // Breakeven when total losses >= subordination
        // Loss in each period ~= pool * CDR_periodic * (1 - recovery)
        // We approximate: loss ~ initial_investment fraction
        // Binary search from 0% to 100% CDR
        let mut lo = Decimal::ZERO;
        let mut hi = Decimal::ONE;
        let mut result = Decimal::ONE;

        for _ in 0..30 {
            let mid = (lo + hi) / dec!(2);
            // Approximate cumulative loss ratio
            // For simplicity: if CDR_annual = mid, then over n periods
            // cumulative_default_pct ~ 1 - (1 - mid * period_frac)^n
            let period_frac = Decimal::ONE / Decimal::from(periods_per_year);
            let periodic_survival = Decimal::ONE - mid * period_frac;
            let n = input.cash_flows.len() as u32;

            let mut survival = Decimal::ONE;
            for _ in 0..n {
                survival *= periodic_survival;
                if survival < Decimal::ZERO {
                    survival = Decimal::ZERO;
                    break;
                }
            }
            let cumulative_default = Decimal::ONE - survival;
            // Assume 40% recovery
            let cumulative_loss = cumulative_default * dec!(0.60);

            // Subordination fraction (price-based)
            let sub_frac = Decimal::ONE - input.price / dec!(100);
            let sub_frac = if sub_frac < Decimal::ZERO {
                Decimal::ZERO
            } else {
                sub_frac
            };

            // Extra subordination from structure (simplified to 30% of par)
            let structural_sub = dec!(0.30);
            let total_sub = sub_frac + structural_sub;

            if cumulative_loss > total_sub {
                hi = mid;
                result = mid;
            } else {
                lo = mid;
                result = mid;
            }
        }
        Some(result)
    } else {
        None
    };

    // --- Equity IRR ---
    let equity_irr = if is_equity {
        // Build equity cash flows: negative initial, then distributions
        let mut eq_cfs: Vec<Decimal> = vec![-input.initial_investment];
        for cf in &input.cash_flows {
            eq_cfs.push(cf.interest + cf.principal);
        }
        let irr_periodic = newton_irr(&eq_cfs, 30)?;
        Some(annualize_yield(irr_periodic, periods_per_year))
    } else {
        None
    };

    // --- Cash-on-Cash ---
    let cash_on_cash = if is_equity && !input.initial_investment.is_zero() {
        let total_dist: Decimal = input
            .cash_flows
            .iter()
            .map(|cf| cf.interest + cf.principal)
            .sum();
        let num_years =
            Decimal::from(input.cash_flows.len() as u32) / Decimal::from(periods_per_year);
        if num_years.is_zero() {
            Some(Decimal::ZERO)
        } else {
            let annual_dist = total_dist / num_years;
            Some(annual_dist / input.initial_investment)
        }
    } else {
        None
    };

    Ok(TrancheAnalyticsOutput {
        yield_to_maturity,
        yield_to_call,
        yield_to_worst,
        wal,
        spread_duration,
        breakeven_cdr,
        equity_irr,
        cash_on_cash,
    })
}

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

fn validate_tranche_input(input: &TrancheAnalyticsInput) -> CorpFinanceResult<()> {
    if input.cash_flows.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one cash flow is required.".into(),
        ));
    }
    if input.initial_investment <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "initial_investment".into(),
            reason: "Initial investment must be positive.".into(),
        });
    }
    if input.price <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "price".into(),
            reason: "Price must be positive.".into(),
        });
    }
    for cf in &input.cash_flows {
        if cf.interest < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "cash_flows.interest".into(),
                reason: "Interest cannot be negative.".into(),
            });
        }
        if cf.principal < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "cash_flows.principal".into(),
                reason: "Principal cannot be negative.".into(),
            });
        }
    }
    Ok(())
}

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

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

    fn approx_eq(a: Decimal, b: Decimal, eps: Decimal) -> bool {
        (a - b).abs() < eps
    }

    fn sample_rated_cfs() -> Vec<TrancheCashFlow> {
        // 20 quarterly periods, constant coupon, bullet principal at maturity
        let coupon = dec!(1_500_000); // quarterly coupon
        let par = dec!(100_000_000);
        let mut cfs = Vec::new();
        for p in 1..=20 {
            let principal = if p == 20 { par } else { Decimal::ZERO };
            cfs.push(TrancheCashFlow {
                period: p,
                interest: coupon,
                principal,
            });
        }
        cfs
    }

    fn sample_rated_input() -> TrancheAnalyticsInput {
        TrancheAnalyticsInput {
            tranche_name: "AAA".into(),
            cash_flows: sample_rated_cfs(),
            initial_investment: dec!(100_000_000),
            price: dec!(100),
            call_date_period: 8,
            reference_rate: dec!(0.05),
        }
    }

    fn sample_equity_cfs() -> Vec<TrancheCashFlow> {
        // 20 quarterly periods with distributions
        let mut cfs = Vec::new();
        for p in 1..=20 {
            cfs.push(TrancheCashFlow {
                period: p,
                interest: dec!(2_000_000), // residual interest
                principal: if p == 20 {
                    dec!(50_000_000)
                } else {
                    Decimal::ZERO
                },
            });
        }
        cfs
    }

    fn sample_equity_input() -> TrancheAnalyticsInput {
        TrancheAnalyticsInput {
            tranche_name: "Equity".into(),
            cash_flows: sample_equity_cfs(),
            initial_investment: dec!(50_000_000),
            price: dec!(100),
            call_date_period: 0,
            reference_rate: dec!(0.05),
        }
    }

    #[test]
    fn test_ytm_at_par_approximately_coupon_rate() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        // Coupon = 1.5M per quarter on 100M = 1.5% quarterly = 6% annual
        assert!(
            approx_eq(out.yield_to_maturity, dec!(0.06), dec!(0.005)),
            "YTM {} should be ~0.06",
            out.yield_to_maturity
        );
    }

    #[test]
    fn test_ytc_present_when_call_date_set() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.yield_to_call.is_some());
    }

    #[test]
    fn test_ytc_none_when_no_call() {
        let mut input = sample_rated_input();
        input.call_date_period = 0;
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.yield_to_call.is_none());
    }

    #[test]
    fn test_ytw_equals_min_of_ytm_ytc() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        if let Some(ytc) = out.yield_to_call {
            let expected = ytc.min(out.yield_to_maturity);
            assert_eq!(out.yield_to_worst, expected);
        }
    }

    #[test]
    fn test_ytw_equals_ytm_when_no_call() {
        let mut input = sample_rated_input();
        input.call_date_period = 0;
        let out = calculate_tranche_analytics(&input).unwrap();
        assert_eq!(out.yield_to_worst, out.yield_to_maturity);
    }

    #[test]
    fn test_wal_bullet_at_maturity() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        // All principal at period 20, WAL = 20/4 = 5.0 years
        assert!(
            approx_eq(out.wal, dec!(5.0), dec!(0.01)),
            "WAL {} should be ~5.0",
            out.wal
        );
    }

    #[test]
    fn test_wal_amortizing() {
        let mut input = sample_rated_input();
        // Equal principal in each period
        let per_period = dec!(5_000_000);
        input.cash_flows = (1..=20)
            .map(|p| TrancheCashFlow {
                period: p,
                interest: dec!(1_000_000),
                principal: per_period,
            })
            .collect();
        let out = calculate_tranche_analytics(&input).unwrap();
        // WAL should be less than 5.0 for amortizing
        assert!(
            out.wal < dec!(5.0),
            "Amortizing WAL {} should be < 5.0",
            out.wal
        );
    }

    #[test]
    fn test_spread_duration_positive() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(
            out.spread_duration > Decimal::ZERO,
            "Spread duration should be positive"
        );
    }

    #[test]
    fn test_breakeven_cdr_exists_for_rated() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.breakeven_cdr.is_some());
    }

    #[test]
    fn test_breakeven_cdr_none_for_equity() {
        let input = sample_equity_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.breakeven_cdr.is_none());
    }

    #[test]
    fn test_breakeven_cdr_in_valid_range() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        if let Some(cdr) = out.breakeven_cdr {
            assert!(cdr >= Decimal::ZERO && cdr <= Decimal::ONE);
        }
    }

    #[test]
    fn test_equity_irr_present() {
        let input = sample_equity_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.equity_irr.is_some());
    }

    #[test]
    fn test_equity_irr_positive() {
        let input = sample_equity_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(
            out.equity_irr.unwrap() > Decimal::ZERO,
            "Equity IRR should be positive"
        );
    }

    #[test]
    fn test_equity_irr_none_for_rated() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.equity_irr.is_none());
    }

    #[test]
    fn test_cash_on_cash_present_for_equity() {
        let input = sample_equity_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.cash_on_cash.is_some());
    }

    #[test]
    fn test_cash_on_cash_none_for_rated() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(out.cash_on_cash.is_none());
    }

    #[test]
    fn test_cash_on_cash_positive() {
        let input = sample_equity_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        assert!(
            out.cash_on_cash.unwrap() > Decimal::ZERO,
            "Cash-on-cash should be positive"
        );
    }

    #[test]
    fn test_discount_price_higher_yield() {
        let par_input = sample_rated_input();
        let par_out = calculate_tranche_analytics(&par_input).unwrap();

        let mut disc_input = sample_rated_input();
        disc_input.price = dec!(95); // discount
        let disc_out = calculate_tranche_analytics(&disc_input).unwrap();

        assert!(
            disc_out.yield_to_maturity > par_out.yield_to_maturity,
            "Discount price should give higher yield"
        );
    }

    #[test]
    fn test_reject_empty_cash_flows() {
        let mut input = sample_rated_input();
        input.cash_flows = vec![];
        assert!(calculate_tranche_analytics(&input).is_err());
    }

    #[test]
    fn test_reject_zero_investment() {
        let mut input = sample_rated_input();
        input.initial_investment = Decimal::ZERO;
        assert!(calculate_tranche_analytics(&input).is_err());
    }

    #[test]
    fn test_reject_zero_price() {
        let mut input = sample_rated_input();
        input.price = Decimal::ZERO;
        assert!(calculate_tranche_analytics(&input).is_err());
    }

    #[test]
    fn test_reject_negative_interest() {
        let mut input = sample_rated_input();
        input.cash_flows[0].interest = dec!(-100);
        assert!(calculate_tranche_analytics(&input).is_err());
    }

    #[test]
    fn test_reject_negative_principal() {
        let mut input = sample_rated_input();
        input.cash_flows[0].principal = dec!(-100);
        assert!(calculate_tranche_analytics(&input).is_err());
    }

    #[test]
    fn test_serialization_roundtrip() {
        let input = sample_rated_input();
        let out = calculate_tranche_analytics(&input).unwrap();
        let json = serde_json::to_string(&out).unwrap();
        let _: TrancheAnalyticsOutput = serde_json::from_str(&json).unwrap();
    }
}