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
use crate::compat::Instant;
use rust_decimal::Decimal;
use rust_decimal::MathematicalOps;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::types::{with_metadata, ComputationOutput, Currency, Money, Multiple, Rate};
use crate::CorpFinanceResult;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// Financial metrics for a company (target or comparable).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompanyMetrics {
    /// Enterprise value
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enterprise_value: Option<Money>,
    /// Market capitalisation (equity value)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub market_cap: Option<Money>,
    /// Total revenue / sales
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revenue: Option<Money>,
    /// EBITDA
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ebitda: Option<Money>,
    /// EBIT / operating income
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ebit: Option<Money>,
    /// Net income
    #[serde(skip_serializing_if = "Option::is_none")]
    pub net_income: Option<Money>,
    /// Book value of equity
    #[serde(skip_serializing_if = "Option::is_none")]
    pub book_value: Option<Money>,
    /// Earnings per share
    #[serde(skip_serializing_if = "Option::is_none")]
    pub eps: Option<Decimal>,
    /// Expected EPS growth rate (for PEG ratio)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub eps_growth_rate: Option<Rate>,
    /// Share price
    #[serde(skip_serializing_if = "Option::is_none")]
    pub share_price: Option<Money>,
}

/// A comparable company with its financial metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComparableCompany {
    /// Company name or ticker
    pub name: String,
    /// Financial metrics
    pub metrics: CompanyMetrics,
    /// Include in the analysis (allows easy toggling)
    pub include: bool,
}

/// Types of valuation multiples to compute.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MultipleType {
    EvEbitda,
    EvRevenue,
    EvEbit,
    PriceEarnings,
    PriceBook,
    Peg,
}

impl std::fmt::Display for MultipleType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MultipleType::EvEbitda => write!(f, "EV/EBITDA"),
            MultipleType::EvRevenue => write!(f, "EV/Revenue"),
            MultipleType::EvEbit => write!(f, "EV/EBIT"),
            MultipleType::PriceEarnings => write!(f, "P/E"),
            MultipleType::PriceBook => write!(f, "P/B"),
            MultipleType::Peg => write!(f, "PEG"),
        }
    }
}

/// Input for a trading comparables analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompsInput {
    /// Target company name
    pub target_name: String,
    /// Target company metrics
    pub target_metrics: CompanyMetrics,
    /// List of comparable companies
    pub comparables: Vec<ComparableCompany>,
    /// Which multiples to compute
    pub multiples: Vec<MultipleType>,
    /// Reporting currency
    pub currency: Currency,
}

/// Descriptive statistics for a single multiple across the comp set.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultipleStatistics {
    pub multiple_type: MultipleType,
    pub values: Vec<(String, Multiple)>,
    pub mean: Multiple,
    pub median: Multiple,
    pub high: Multiple,
    pub low: Multiple,
    pub std_dev: Multiple,
    pub count: usize,
}

/// An implied valuation for the target from one multiple.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImpliedValuation {
    pub multiple_type: MultipleType,
    /// Implied value using median multiple
    pub implied_at_median: Money,
    /// Implied value using mean multiple
    pub implied_at_mean: Money,
    /// Implied value using low multiple
    pub implied_at_low: Money,
    /// Implied value using high multiple
    pub implied_at_high: Money,
    /// The target metric used as the base
    pub target_metric_value: Money,
}

/// Output of a trading comparables analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompsOutput {
    /// Statistics for each requested multiple
    pub multiple_statistics: Vec<MultipleStatistics>,
    /// Implied valuations of the target company
    pub implied_valuations: Vec<ImpliedValuation>,
    /// Number of comparable companies included
    pub companies_included: usize,
    /// Number of comparable companies excluded
    pub companies_excluded: usize,
}

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

/// Run a trading comparables analysis.
pub fn calculate_comps(input: &CompsInput) -> CorpFinanceResult<ComputationOutput<CompsOutput>> {
    let start = Instant::now();
    let mut warnings: Vec<String> = Vec::new();

    // --- Validate ---
    let included: Vec<&ComparableCompany> =
        input.comparables.iter().filter(|c| c.include).collect();
    let excluded_count = input.comparables.len() - included.len();

    if included.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "No comparable companies included in the analysis".into(),
        ));
    }
    if input.multiples.is_empty() {
        return Err(CorpFinanceError::InvalidInput {
            field: "multiples".into(),
            reason: "At least one multiple type must be specified".into(),
        });
    }
    if included.len() < 3 {
        warnings.push(format!(
            "Only {} comparables included; consider adding more for statistical significance",
            included.len()
        ));
    }

    // --- Compute multiples and statistics ---
    let mut multiple_statistics: Vec<MultipleStatistics> = Vec::new();
    let mut implied_valuations: Vec<ImpliedValuation> = Vec::new();

    for mult_type in &input.multiples {
        let values = compute_multiples_for_type(mult_type, &included, &mut warnings);

        if values.is_empty() {
            warnings.push(format!(
                "No comparable companies had sufficient data for {mult_type}"
            ));
            continue;
        }

        let stats = compute_statistics(mult_type.clone(), values);

        // Compute implied valuation for target
        if let Some(implied) =
            compute_implied_valuation(mult_type, &stats, &input.target_metrics, &mut warnings)
        {
            implied_valuations.push(implied);
        }

        multiple_statistics.push(stats);
    }

    if multiple_statistics.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "Could not compute any multiples from the comparable set".into(),
        ));
    }

    let output = CompsOutput {
        multiple_statistics,
        implied_valuations,
        companies_included: included.len(),
        companies_excluded: excluded_count,
    };

    let elapsed = start.elapsed().as_micros() as u64;

    Ok(with_metadata(
        "Trading Comparables Analysis",
        input,
        warnings,
        elapsed,
        output,
    ))
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Extract the multiple value for each comparable that has the required data.
fn compute_multiples_for_type(
    mult_type: &MultipleType,
    companies: &[&ComparableCompany],
    warnings: &mut Vec<String>,
) -> Vec<(String, Multiple)> {
    let mut values = Vec::new();

    for comp in companies {
        let m = &comp.metrics;
        let result = match mult_type {
            MultipleType::EvEbitda => match (m.enterprise_value, m.ebitda) {
                (Some(ev), Some(ebitda)) if ebitda > Decimal::ZERO => Some(ev / ebitda),
                _ => None,
            },
            MultipleType::EvRevenue => match (m.enterprise_value, m.revenue) {
                (Some(ev), Some(rev)) if rev > Decimal::ZERO => Some(ev / rev),
                _ => None,
            },
            MultipleType::EvEbit => match (m.enterprise_value, m.ebit) {
                (Some(ev), Some(ebit)) if ebit > Decimal::ZERO => Some(ev / ebit),
                _ => None,
            },
            MultipleType::PriceEarnings => match (m.market_cap, m.net_income) {
                (Some(mc), Some(ni)) if ni > Decimal::ZERO => Some(mc / ni),
                _ => None,
            },
            MultipleType::PriceBook => match (m.market_cap, m.book_value) {
                (Some(mc), Some(bv)) if bv > Decimal::ZERO => Some(mc / bv),
                _ => None,
            },
            MultipleType::Peg => {
                // PEG = (P/E) / (EPS growth * 100)
                match (m.market_cap, m.net_income, m.eps_growth_rate) {
                    (Some(mc), Some(ni), Some(g)) if ni > Decimal::ZERO && g > Decimal::ZERO => {
                        let pe = mc / ni;
                        let growth_pct = g * dec!(100);
                        if growth_pct > Decimal::ZERO {
                            Some(pe / growth_pct)
                        } else {
                            None
                        }
                    }
                    _ => None,
                }
            }
        };

        match result {
            Some(v) => values.push((comp.name.clone(), v)),
            None => {
                warnings.push(format!("{}: insufficient data for {mult_type}", comp.name));
            }
        }
    }

    values
}

fn compute_statistics(
    multiple_type: MultipleType,
    values: Vec<(String, Multiple)>,
) -> MultipleStatistics {
    let count = values.len();
    let mut sorted_vals: Vec<Multiple> = values.iter().map(|(_, v)| *v).collect();
    sorted_vals.sort();

    let sum: Decimal = sorted_vals.iter().copied().sum();
    let mean = sum / Decimal::from(count as i64);

    let median = if count.is_multiple_of(2) {
        let mid = count / 2;
        (sorted_vals[mid - 1] + sorted_vals[mid]) / dec!(2)
    } else {
        sorted_vals[count / 2]
    };

    let high = sorted_vals[count - 1];
    let low = sorted_vals[0];

    // Standard deviation
    let std_dev = if count > 1 {
        let variance: Decimal = sorted_vals
            .iter()
            .map(|v| {
                let diff = *v - mean;
                diff * diff
            })
            .sum::<Decimal>()
            / Decimal::from((count - 1) as i64); // sample std dev
        sqrt_decimal(variance)
    } else {
        Decimal::ZERO
    };

    MultipleStatistics {
        multiple_type,
        values,
        mean,
        median,
        high,
        low,
        std_dev,
        count,
    }
}

/// Compute implied valuation for the target using the given statistics.
fn compute_implied_valuation(
    mult_type: &MultipleType,
    stats: &MultipleStatistics,
    target: &CompanyMetrics,
    warnings: &mut Vec<String>,
) -> Option<ImpliedValuation> {
    // Determine which target metric to multiply
    let base_value = match mult_type {
        MultipleType::EvEbitda => target.ebitda,
        MultipleType::EvRevenue => target.revenue,
        MultipleType::EvEbit => target.ebit,
        MultipleType::PriceEarnings => target.net_income,
        MultipleType::PriceBook => target.book_value,
        MultipleType::Peg => {
            // For PEG, implied value = PEG * growth * earnings
            match (target.net_income, target.eps_growth_rate) {
                (Some(ni), Some(g)) if g > Decimal::ZERO => {
                    let growth_pct = g * dec!(100);
                    return Some(ImpliedValuation {
                        multiple_type: mult_type.clone(),
                        implied_at_median: ni * stats.median * growth_pct,
                        implied_at_mean: ni * stats.mean * growth_pct,
                        implied_at_low: ni * stats.low * growth_pct,
                        implied_at_high: ni * stats.high * growth_pct,
                        target_metric_value: ni,
                    });
                }
                _ => {
                    warnings.push(
                        "Target missing net_income or eps_growth_rate for PEG implied valuation"
                            .to_string(),
                    );
                    return None;
                }
            }
        }
    };

    match base_value {
        Some(val) if val > Decimal::ZERO => Some(ImpliedValuation {
            multiple_type: mult_type.clone(),
            implied_at_median: val * stats.median,
            implied_at_mean: val * stats.mean,
            implied_at_low: val * stats.low,
            implied_at_high: val * stats.high,
            target_metric_value: val,
        }),
        _ => {
            warnings.push(format!(
                "Target missing required metric for {mult_type} implied valuation"
            ));
            None
        }
    }
}

/// Integer square root approximation for Decimal via Newton's method.
fn sqrt_decimal(value: Decimal) -> Decimal {
    if value <= Decimal::ZERO {
        return Decimal::ZERO;
    }
    // Use rust_decimal's built-in sqrt
    value.sqrt().unwrap_or(Decimal::ZERO)
}

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

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

    fn sample_comps_input() -> CompsInput {
        CompsInput {
            target_name: "TargetCo".into(),
            target_metrics: CompanyMetrics {
                enterprise_value: None,
                market_cap: None,
                revenue: Some(dec!(500)),
                ebitda: Some(dec!(125)),
                ebit: Some(dec!(100)),
                net_income: Some(dec!(75)),
                book_value: Some(dec!(300)),
                eps: Some(dec!(2.50)),
                eps_growth_rate: Some(dec!(0.15)),
                share_price: Some(dec!(40)),
            },
            comparables: vec![
                ComparableCompany {
                    name: "CompA".into(),
                    metrics: CompanyMetrics {
                        enterprise_value: Some(dec!(2000)),
                        market_cap: Some(dec!(1600)),
                        revenue: Some(dec!(800)),
                        ebitda: Some(dec!(200)),
                        ebit: Some(dec!(160)),
                        net_income: Some(dec!(120)),
                        book_value: Some(dec!(500)),
                        eps: Some(dec!(3.00)),
                        eps_growth_rate: Some(dec!(0.12)),
                        share_price: Some(dec!(50)),
                    },
                    include: true,
                },
                ComparableCompany {
                    name: "CompB".into(),
                    metrics: CompanyMetrics {
                        enterprise_value: Some(dec!(3000)),
                        market_cap: Some(dec!(2500)),
                        revenue: Some(dec!(1200)),
                        ebitda: Some(dec!(360)),
                        ebit: Some(dec!(300)),
                        net_income: Some(dec!(200)),
                        book_value: Some(dec!(800)),
                        eps: Some(dec!(4.00)),
                        eps_growth_rate: Some(dec!(0.20)),
                        share_price: Some(dec!(60)),
                    },
                    include: true,
                },
                ComparableCompany {
                    name: "CompC".into(),
                    metrics: CompanyMetrics {
                        enterprise_value: Some(dec!(1500)),
                        market_cap: Some(dec!(1200)),
                        revenue: Some(dec!(600)),
                        ebitda: Some(dec!(150)),
                        ebit: Some(dec!(120)),
                        net_income: Some(dec!(90)),
                        book_value: Some(dec!(400)),
                        eps: Some(dec!(2.00)),
                        eps_growth_rate: Some(dec!(0.10)),
                        share_price: Some(dec!(35)),
                    },
                    include: true,
                },
                ComparableCompany {
                    name: "CompD_excluded".into(),
                    metrics: CompanyMetrics {
                        enterprise_value: Some(dec!(5000)),
                        market_cap: Some(dec!(4000)),
                        revenue: Some(dec!(2000)),
                        ebitda: Some(dec!(400)),
                        ebit: Some(dec!(350)),
                        net_income: Some(dec!(250)),
                        book_value: Some(dec!(1000)),
                        eps: Some(dec!(5.00)),
                        eps_growth_rate: Some(dec!(0.08)),
                        share_price: Some(dec!(80)),
                    },
                    include: false,
                },
            ],
            multiples: vec![
                MultipleType::EvEbitda,
                MultipleType::EvRevenue,
                MultipleType::PriceEarnings,
                MultipleType::PriceBook,
            ],
            currency: Currency::USD,
        }
    }

    #[test]
    fn test_basic_comps() {
        let input = sample_comps_input();
        let result = calculate_comps(&input).unwrap();
        let out = &result.result;

        assert_eq!(out.companies_included, 3);
        assert_eq!(out.companies_excluded, 1);
        assert_eq!(out.multiple_statistics.len(), 4);
    }

    #[test]
    fn test_ev_ebitda_multiples() {
        let input = sample_comps_input();
        let result = calculate_comps(&input).unwrap();
        let out = &result.result;

        let ev_ebitda = out
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::EvEbitda)
            .unwrap();

        // CompA: 2000/200 = 10x, CompB: 3000/360 = 8.33x, CompC: 1500/150 = 10x
        assert_eq!(ev_ebitda.count, 3);

        // Sorted: ~8.33, 10, 10
        assert!(
            (ev_ebitda.low - dec!(8.33)).abs() < dec!(0.01),
            "Low: expected ~8.33, got {}",
            ev_ebitda.low
        );

        // Median should be 10
        assert!(
            (ev_ebitda.median - dec!(10)).abs() < dec!(0.01),
            "Median: expected ~10, got {}",
            ev_ebitda.median
        );
    }

    #[test]
    fn test_implied_valuation_ev_ebitda() {
        let input = sample_comps_input();
        let result = calculate_comps(&input).unwrap();
        let out = &result.result;

        let implied = out
            .implied_valuations
            .iter()
            .find(|v| v.multiple_type == MultipleType::EvEbitda)
            .unwrap();

        // Target EBITDA = 125
        assert_eq!(implied.target_metric_value, dec!(125));

        // At median 10x: 125 * 10 = 1250
        let ev_ebitda_stats = out
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::EvEbitda)
            .unwrap();
        let expected_median = dec!(125) * ev_ebitda_stats.median;
        assert_eq!(implied.implied_at_median, expected_median);
    }

    #[test]
    fn test_excluded_company_not_in_stats() {
        let input = sample_comps_input();
        let result = calculate_comps(&input).unwrap();
        let out = &result.result;

        for stats in &out.multiple_statistics {
            assert!(
                !stats
                    .values
                    .iter()
                    .any(|(name, _)| name == "CompD_excluded"),
                "Excluded company should not appear in {}: {:?}",
                stats.multiple_type,
                stats.values
            );
        }
    }

    #[test]
    fn test_missing_data_handled() {
        let mut input = sample_comps_input();
        // Remove EBITDA from CompA
        input.comparables[0].metrics.ebitda = None;

        let result = calculate_comps(&input).unwrap();
        let ev_ebitda = result
            .result
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::EvEbitda)
            .unwrap();

        // Only CompB and CompC should contribute
        assert_eq!(ev_ebitda.count, 2);
        assert!(result
            .warnings
            .iter()
            .any(|w| w.contains("CompA") && w.contains("EV/EBITDA")));
    }

    #[test]
    fn test_no_comparables_error() {
        let mut input = sample_comps_input();
        for comp in &mut input.comparables {
            comp.include = false;
        }

        let result = calculate_comps(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_no_multiples_error() {
        let mut input = sample_comps_input();
        input.multiples.clear();

        let result = calculate_comps(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_negative_ebitda_excluded() {
        let mut input = sample_comps_input();
        input.comparables[0].metrics.ebitda = Some(dec!(-50));

        let result = calculate_comps(&input).unwrap();
        let ev_ebitda = result
            .result
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::EvEbitda)
            .unwrap();

        // CompA should be excluded due to negative EBITDA
        assert_eq!(ev_ebitda.count, 2);
    }

    #[test]
    fn test_statistics_single_company() {
        let mut input = sample_comps_input();
        input.comparables[1].include = false;
        input.comparables[2].include = false;

        let result = calculate_comps(&input).unwrap();
        let ev_ebitda = result
            .result
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::EvEbitda)
            .unwrap();

        assert_eq!(ev_ebitda.count, 1);
        assert_eq!(ev_ebitda.mean, ev_ebitda.median);
        assert_eq!(ev_ebitda.std_dev, Decimal::ZERO);
    }

    #[test]
    fn test_peg_multiple() {
        let mut input = sample_comps_input();
        input.multiples = vec![MultipleType::Peg];

        let result = calculate_comps(&input).unwrap();
        let out = &result.result;

        // Should have PEG statistics
        let peg = out
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::Peg)
            .unwrap();

        // CompA: P/E = 1600/120 = 13.33, growth = 12% => PEG = 13.33/12 = 1.11
        // CompB: P/E = 2500/200 = 12.5, growth = 20% => PEG = 12.5/20 = 0.625
        // CompC: P/E = 1200/90 = 13.33, growth = 10% => PEG = 13.33/10 = 1.33
        assert_eq!(peg.count, 3);
        assert!(peg.low < peg.median);
        assert!(peg.median <= peg.high);
    }

    #[test]
    fn test_price_book_multiple() {
        let mut input = sample_comps_input();
        input.multiples = vec![MultipleType::PriceBook];

        let result = calculate_comps(&input).unwrap();
        let pb = result
            .result
            .multiple_statistics
            .iter()
            .find(|s| s.multiple_type == MultipleType::PriceBook)
            .unwrap();

        // CompA: 1600/500=3.2, CompB: 2500/800=3.125, CompC: 1200/400=3.0
        assert_eq!(pb.count, 3);
        assert!(
            (pb.low - dec!(3.0)).abs() < dec!(0.01),
            "P/B low: expected 3.0, got {}",
            pb.low
        );
    }

    #[test]
    fn test_methodology_string() {
        let input = sample_comps_input();
        let result = calculate_comps(&input).unwrap();
        assert_eq!(result.methodology, "Trading Comparables Analysis");
    }

    #[test]
    fn test_few_comparables_warning() {
        let mut input = sample_comps_input();
        input.comparables[1].include = false;
        input.comparables[2].include = false;
        // Only CompA included (1 company)

        let result = calculate_comps(&input).unwrap();
        assert!(result
            .warnings
            .iter()
            .any(|w| w.contains("comparables included")));
    }
}