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
//! Manager Selection and Due Diligence scoring model.
//!
//! Provides a quantitative + qualitative framework for evaluating
//! private equity fund managers:
//!
//! - **Performance scoring**: composite of TVPI, IRR, DPI quartile rankings
//! - **Persistence analysis**: correlation of consecutive fund performance
//! - **Alpha estimation**: fund IRR vs PME (excess return)
//! - **Dispersion analysis**: spread between top/bottom quartile
//! - **Qualitative scoring**: weighted factor assessment
//! - **Overall rating**: blended quant + qualitative score
//!
//! 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
// ---------------------------------------------------------------------------

/// A single fund in the manager's track record.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FundRecord {
    /// Fund name (e.g. "Fund III").
    pub name: String,
    /// Vintage year.
    pub vintage: u32,
    /// Net IRR to LPs.
    pub irr: Decimal,
    /// TVPI multiple.
    pub tvpi: Decimal,
    /// DPI multiple.
    pub dpi: Decimal,
    /// Public Market Equivalent ratio.
    pub pme: Decimal,
}

/// A qualitative assessment factor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualitativeFactor {
    /// Factor name (e.g. "Team", "Strategy", "Track Record").
    pub factor: String,
    /// Weight in the composite (decimal, should sum to 1.0 across all factors).
    pub weight: Decimal,
    /// Score from 1 to 5.
    pub score: Decimal,
}

/// Benchmark quartile boundaries for a given metric.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkQuartile {
    /// Metric name (e.g. "irr", "tvpi", "dpi").
    pub metric: String,
    /// First quartile (top 25%) threshold.
    pub q1: Decimal,
    /// Median threshold.
    pub median: Decimal,
    /// Third quartile (bottom 25%) threshold.
    pub q3: Decimal,
}

/// Input for manager selection analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManagerSelectionInput {
    /// Manager name.
    pub manager_name: String,
    /// Historical fund track record.
    pub funds: Vec<FundRecord>,
    /// Qualitative assessment factors.
    pub qualitative_scores: Vec<QualitativeFactor>,
    /// Benchmark quartile boundaries.
    pub benchmark_quartiles: Vec<BenchmarkQuartile>,
}

/// Quartile ranking for a specific fund and metric.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FundQuartileRanking {
    /// Fund name.
    pub fund: String,
    /// Metric name.
    pub metric: String,
    /// Metric value.
    pub value: Decimal,
    /// Quartile (1 = top, 4 = bottom).
    pub quartile: u32,
}

/// Recommendation rating.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Recommendation {
    StrongBuy,
    Buy,
    Hold,
    Pass,
}

impl std::fmt::Display for Recommendation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Recommendation::StrongBuy => write!(f, "Strong Buy"),
            Recommendation::Buy => write!(f, "Buy"),
            Recommendation::Hold => write!(f, "Hold"),
            Recommendation::Pass => write!(f, "Pass"),
        }
    }
}

/// Output of the manager selection analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManagerSelectionOutput {
    /// Quantitative score (0-100).
    pub quantitative_score: Decimal,
    /// Qualitative score (0-100).
    pub qualitative_score: Decimal,
    /// Overall blended score (0-100).
    pub overall_score: Decimal,
    /// Quartile rankings for each fund and metric.
    pub fund_quartiles: Vec<FundQuartileRanking>,
    /// Persistence correlation between consecutive funds.
    pub persistence_correlation: Decimal,
    /// Average alpha (fund IRR - PME).
    pub average_alpha: Decimal,
    /// Return consistency measure (1 - coefficient of variation of IRRs).
    pub return_consistency: Decimal,
    /// Overall recommendation.
    pub recommendation: Recommendation,
}

// ---------------------------------------------------------------------------
// Core computation
// ---------------------------------------------------------------------------

/// Perform manager selection analysis.
pub fn analyze_manager_selection(
    input: &ManagerSelectionInput,
) -> CorpFinanceResult<ManagerSelectionOutput> {
    validate_manager_input(input)?;

    // 1. Quartile rankings
    let fund_quartiles = compute_quartile_rankings(&input.funds, &input.benchmark_quartiles);

    // 2. Quantitative score based on average quartile rankings.
    //    Q1 = 100, Q2 = 75, Q3 = 50, Q4 = 25.
    let quantitative_score = compute_quantitative_score(&fund_quartiles);

    // 3. Qualitative score: weighted sum of scores, normalized to 0-100.
    let qualitative_score = compute_qualitative_score(&input.qualitative_scores);

    // 4. Persistence correlation
    let persistence_correlation = compute_persistence(&input.funds);

    // 5. Average alpha
    let average_alpha = compute_average_alpha(&input.funds);

    // 6. Return consistency
    let return_consistency = compute_return_consistency(&input.funds);

    // 7. Overall score: 60% quantitative + 40% qualitative.
    let overall_score = dec!(0.60) * quantitative_score + dec!(0.40) * qualitative_score;

    // 8. Recommendation based on overall score.
    let recommendation = if overall_score >= dec!(80) {
        Recommendation::StrongBuy
    } else if overall_score >= dec!(60) {
        Recommendation::Buy
    } else if overall_score >= dec!(40) {
        Recommendation::Hold
    } else {
        Recommendation::Pass
    };

    Ok(ManagerSelectionOutput {
        quantitative_score,
        qualitative_score,
        overall_score,
        fund_quartiles,
        persistence_correlation,
        average_alpha,
        return_consistency,
        recommendation,
    })
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn compute_quartile_rankings(
    funds: &[FundRecord],
    benchmarks: &[BenchmarkQuartile],
) -> Vec<FundQuartileRanking> {
    let mut rankings = Vec::new();
    for fund in funds {
        for bm in benchmarks {
            let value = match bm.metric.as_str() {
                "irr" => fund.irr,
                "tvpi" => fund.tvpi,
                "dpi" => fund.dpi,
                _ => continue,
            };
            let quartile = if value >= bm.q1 {
                1
            } else if value >= bm.median {
                2
            } else if value >= bm.q3 {
                3
            } else {
                4
            };
            rankings.push(FundQuartileRanking {
                fund: fund.name.clone(),
                metric: bm.metric.clone(),
                value,
                quartile,
            });
        }
    }
    rankings
}

fn compute_quantitative_score(rankings: &[FundQuartileRanking]) -> Decimal {
    if rankings.is_empty() {
        return Decimal::ZERO;
    }
    let total: Decimal = rankings
        .iter()
        .map(|r| match r.quartile {
            1 => dec!(100),
            2 => dec!(75),
            3 => dec!(50),
            _ => dec!(25),
        })
        .sum();
    total / Decimal::from(rankings.len() as u32)
}

fn compute_qualitative_score(factors: &[QualitativeFactor]) -> Decimal {
    if factors.is_empty() {
        return Decimal::ZERO;
    }
    let total_weight: Decimal = factors.iter().map(|f| f.weight).sum();
    if total_weight.is_zero() {
        return Decimal::ZERO;
    }
    // Weighted average score on 1-5 scale, converted to 0-100.
    let weighted_sum: Decimal = factors.iter().map(|f| f.weight * f.score).sum();
    let avg_score = weighted_sum / total_weight; // 1-5 scale
                                                 // Convert to 0-100: (score - 1) / 4 * 100
    (avg_score - Decimal::ONE) / dec!(4) * dec!(100)
}

/// Pearson correlation between consecutive fund IRRs.
fn compute_persistence(funds: &[FundRecord]) -> Decimal {
    if funds.len() < 3 {
        return Decimal::ZERO;
    }

    // Sort by vintage
    let mut sorted = funds.to_vec();
    sorted.sort_by_key(|f| f.vintage);

    // Pairs: (fund_n IRR, fund_n+1 IRR)
    let n = sorted.len() - 1;
    if n == 0 {
        return Decimal::ZERO;
    }

    let xs: Vec<Decimal> = sorted[..n].iter().map(|f| f.irr).collect();
    let ys: Vec<Decimal> = sorted[1..].iter().map(|f| f.irr).collect();

    pearson_correlation(&xs, &ys)
}

fn pearson_correlation(xs: &[Decimal], ys: &[Decimal]) -> Decimal {
    let n = xs.len();
    if n == 0 {
        return Decimal::ZERO;
    }
    let n_dec = Decimal::from(n as u32);
    let sum_x: Decimal = xs.iter().copied().sum();
    let sum_y: Decimal = ys.iter().copied().sum();
    let mean_x = sum_x / n_dec;
    let mean_y = sum_y / n_dec;

    let mut cov = Decimal::ZERO;
    let mut var_x = Decimal::ZERO;
    let mut var_y = Decimal::ZERO;

    for i in 0..n {
        let dx = xs[i] - mean_x;
        let dy = ys[i] - mean_y;
        cov += dx * dy;
        var_x += dx * dx;
        var_y += dy * dy;
    }

    let denom_sq = var_x * var_y;
    if denom_sq.is_zero() {
        return Decimal::ZERO;
    }
    // sqrt via Newton's method
    let denom = decimal_sqrt(denom_sq);
    if denom.is_zero() {
        return Decimal::ZERO;
    }

    cov / denom
}

/// Newton's method square root (20 iterations).
fn decimal_sqrt(x: Decimal) -> Decimal {
    if x <= Decimal::ZERO {
        return Decimal::ZERO;
    }
    let two = dec!(2);
    let mut guess = x / two;
    if guess.is_zero() {
        guess = Decimal::ONE;
    }
    for _ in 0..20 {
        let next = (guess + x / guess) / two;
        if (next - guess).abs() < dec!(0.0000000001) {
            return next;
        }
        guess = next;
    }
    guess
}

fn compute_average_alpha(funds: &[FundRecord]) -> Decimal {
    if funds.is_empty() {
        return Decimal::ZERO;
    }
    let total: Decimal = funds.iter().map(|f| f.irr - f.pme).sum();
    total / Decimal::from(funds.len() as u32)
}

fn compute_return_consistency(funds: &[FundRecord]) -> Decimal {
    if funds.len() < 2 {
        return Decimal::ONE;
    }
    let n = Decimal::from(funds.len() as u32);
    let mean: Decimal = funds.iter().map(|f| f.irr).sum::<Decimal>() / n;
    if mean.is_zero() {
        return Decimal::ZERO;
    }
    let variance: Decimal = funds
        .iter()
        .map(|f| {
            let diff = f.irr - mean;
            diff * diff
        })
        .sum::<Decimal>()
        / n;
    let std_dev = decimal_sqrt(variance);
    let cv = std_dev / mean.abs();
    // Consistency = 1 - CV, clamped to [0, 1]
    let consistency = Decimal::ONE - cv;
    if consistency < Decimal::ZERO {
        Decimal::ZERO
    } else if consistency > Decimal::ONE {
        Decimal::ONE
    } else {
        consistency
    }
}

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

fn validate_manager_input(input: &ManagerSelectionInput) -> CorpFinanceResult<()> {
    if input.funds.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one fund record is required.".into(),
        ));
    }
    for fund in &input.funds {
        if fund.tvpi < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "funds.tvpi".into(),
                reason: "TVPI cannot be negative.".into(),
            });
        }
        if fund.dpi < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "funds.dpi".into(),
                reason: "DPI cannot be negative.".into(),
            });
        }
    }
    for factor in &input.qualitative_scores {
        if factor.score < Decimal::ONE || factor.score > dec!(5) {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("qualitative_scores.{}", factor.factor),
                reason: "Qualitative scores must be in [1, 5].".into(),
            });
        }
        if factor.weight < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("qualitative_scores.{}.weight", factor.factor),
                reason: "Weights cannot be negative.".into(),
            });
        }
    }
    if input.benchmark_quartiles.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one benchmark quartile definition is required.".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 default_input() -> ManagerSelectionInput {
        ManagerSelectionInput {
            manager_name: "Test Capital Partners".into(),
            funds: vec![
                FundRecord {
                    name: "Fund I".into(),
                    vintage: 2012,
                    irr: dec!(0.18),
                    tvpi: dec!(2.1),
                    dpi: dec!(1.9),
                    pme: dec!(0.12),
                },
                FundRecord {
                    name: "Fund II".into(),
                    vintage: 2015,
                    irr: dec!(0.22),
                    tvpi: dec!(2.5),
                    dpi: dec!(2.0),
                    pme: dec!(0.14),
                },
                FundRecord {
                    name: "Fund III".into(),
                    vintage: 2018,
                    irr: dec!(0.15),
                    tvpi: dec!(1.8),
                    dpi: dec!(1.2),
                    pme: dec!(0.10),
                },
                FundRecord {
                    name: "Fund IV".into(),
                    vintage: 2021,
                    irr: dec!(0.20),
                    tvpi: dec!(1.6),
                    dpi: dec!(0.8),
                    pme: dec!(0.11),
                },
            ],
            qualitative_scores: vec![
                QualitativeFactor {
                    factor: "Team".into(),
                    weight: dec!(0.30),
                    score: dec!(4),
                },
                QualitativeFactor {
                    factor: "Strategy".into(),
                    weight: dec!(0.25),
                    score: dec!(4),
                },
                QualitativeFactor {
                    factor: "Track Record".into(),
                    weight: dec!(0.20),
                    score: dec!(5),
                },
                QualitativeFactor {
                    factor: "Operations".into(),
                    weight: dec!(0.15),
                    score: dec!(3),
                },
                QualitativeFactor {
                    factor: "Terms".into(),
                    weight: dec!(0.10),
                    score: dec!(3),
                },
            ],
            benchmark_quartiles: vec![
                BenchmarkQuartile {
                    metric: "irr".into(),
                    q1: dec!(0.20),
                    median: dec!(0.14),
                    q3: dec!(0.08),
                },
                BenchmarkQuartile {
                    metric: "tvpi".into(),
                    q1: dec!(2.0),
                    median: dec!(1.5),
                    q3: dec!(1.2),
                },
                BenchmarkQuartile {
                    metric: "dpi".into(),
                    q1: dec!(1.5),
                    median: dec!(1.0),
                    q3: dec!(0.5),
                },
            ],
        }
    }

    #[test]
    fn test_manager_basic_output() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        assert!(out.quantitative_score > Decimal::ZERO);
        assert!(out.qualitative_score > Decimal::ZERO);
        assert!(out.overall_score > Decimal::ZERO);
    }

    #[test]
    fn test_manager_quartile_rankings_count() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        // 4 funds * 3 metrics = 12 rankings
        assert_eq!(out.fund_quartiles.len(), 12);
    }

    #[test]
    fn test_manager_quartile_values_1_to_4() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        for r in &out.fund_quartiles {
            assert!(
                r.quartile >= 1 && r.quartile <= 4,
                "Invalid quartile {}",
                r.quartile
            );
        }
    }

    #[test]
    fn test_manager_top_quartile_for_strong_irr() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        // Fund II has IRR 0.22 > Q1 threshold 0.20 => quartile 1
        let fund2_irr = out
            .fund_quartiles
            .iter()
            .find(|r| r.fund == "Fund II" && r.metric == "irr")
            .unwrap();
        assert_eq!(fund2_irr.quartile, 1);
    }

    #[test]
    fn test_manager_quantitative_score_in_range() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        assert!(
            out.quantitative_score >= Decimal::ZERO && out.quantitative_score <= dec!(100),
            "Quant score {} out of range",
            out.quantitative_score
        );
    }

    #[test]
    fn test_manager_qualitative_score_in_range() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        assert!(
            out.qualitative_score >= Decimal::ZERO && out.qualitative_score <= dec!(100),
            "Qual score {} out of range",
            out.qualitative_score
        );
    }

    #[test]
    fn test_manager_overall_score_is_blend() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        let expected = dec!(0.60) * out.quantitative_score + dec!(0.40) * out.qualitative_score;
        assert!(
            approx_eq(out.overall_score, expected, dec!(0.01)),
            "Overall {} != 60/40 blend {}",
            out.overall_score,
            expected
        );
    }

    #[test]
    fn test_manager_average_alpha_positive() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        // All funds have IRR > PME, so average alpha should be positive
        assert!(out.average_alpha > Decimal::ZERO);
    }

    #[test]
    fn test_manager_average_alpha_calculation() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        // Manual: (0.18-0.12 + 0.22-0.14 + 0.15-0.10 + 0.20-0.11) / 4
        //       = (0.06 + 0.08 + 0.05 + 0.09) / 4 = 0.28/4 = 0.07
        assert!(
            approx_eq(out.average_alpha, dec!(0.07), dec!(0.001)),
            "Average alpha {} should be ~0.07",
            out.average_alpha
        );
    }

    #[test]
    fn test_manager_persistence_with_enough_funds() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        // With 4 funds, persistence should be computable (non-zero possible)
        // The actual value depends on the pattern
        assert!(
            out.persistence_correlation >= dec!(-1) && out.persistence_correlation <= Decimal::ONE,
            "Persistence {} out of [-1, 1]",
            out.persistence_correlation
        );
    }

    #[test]
    fn test_manager_return_consistency_in_range() {
        let input = default_input();
        let out = analyze_manager_selection(&input).unwrap();
        assert!(
            out.return_consistency >= Decimal::ZERO && out.return_consistency <= Decimal::ONE,
            "Consistency {} out of [0, 1]",
            out.return_consistency
        );
    }

    #[test]
    fn test_manager_recommendation_strong_buy() {
        // Create a strong manager: all top quartile + high qualitative
        let mut input = default_input();
        input.funds = vec![FundRecord {
            name: "Fund I".into(),
            vintage: 2018,
            irr: dec!(0.30),
            tvpi: dec!(3.0),
            dpi: dec!(2.5),
            pme: dec!(0.10),
        }];
        input.qualitative_scores = vec![QualitativeFactor {
            factor: "Team".into(),
            weight: dec!(1.0),
            score: dec!(5),
        }];
        let out = analyze_manager_selection(&input).unwrap();
        assert_eq!(out.recommendation, Recommendation::StrongBuy);
    }

    #[test]
    fn test_manager_recommendation_pass() {
        // Create a weak manager: all bottom quartile + low qualitative
        let mut input = default_input();
        input.funds = vec![FundRecord {
            name: "Fund I".into(),
            vintage: 2018,
            irr: dec!(0.02),
            tvpi: dec!(0.9),
            dpi: dec!(0.3),
            pme: dec!(0.10),
        }];
        input.qualitative_scores = vec![QualitativeFactor {
            factor: "Team".into(),
            weight: dec!(1.0),
            score: dec!(1),
        }];
        let out = analyze_manager_selection(&input).unwrap();
        assert_eq!(out.recommendation, Recommendation::Pass);
    }

    #[test]
    fn test_manager_single_fund_persistence_zero() {
        let mut input = default_input();
        input.funds = vec![input.funds[0].clone()];
        let out = analyze_manager_selection(&input).unwrap();
        // With only 1 fund, persistence correlation should be zero
        assert_eq!(out.persistence_correlation, Decimal::ZERO);
    }

    // -- Validation tests --

    #[test]
    fn test_reject_empty_funds() {
        let mut input = default_input();
        input.funds = vec![];
        assert!(analyze_manager_selection(&input).is_err());
    }

    #[test]
    fn test_reject_negative_tvpi() {
        let mut input = default_input();
        input.funds[0].tvpi = dec!(-1);
        assert!(analyze_manager_selection(&input).is_err());
    }

    #[test]
    fn test_reject_score_out_of_range() {
        let mut input = default_input();
        input.qualitative_scores[0].score = dec!(6);
        assert!(analyze_manager_selection(&input).is_err());
    }

    #[test]
    fn test_reject_score_below_one() {
        let mut input = default_input();
        input.qualitative_scores[0].score = Decimal::ZERO;
        assert!(analyze_manager_selection(&input).is_err());
    }

    #[test]
    fn test_reject_empty_benchmarks() {
        let mut input = default_input();
        input.benchmark_quartiles = vec![];
        assert!(analyze_manager_selection(&input).is_err());
    }

    #[test]
    fn test_reject_negative_weight() {
        let mut input = default_input();
        input.qualitative_scores[0].weight = dec!(-0.1);
        assert!(analyze_manager_selection(&input).is_err());
    }

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