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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
use crate::compat::Instant;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::types::{with_metadata, ComputationOutput};
use crate::CorpFinanceResult;

// Re-use AssetAllocation from sibling module
use super::risk_parity::AssetAllocation;

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

/// Classification of asset class for shock sensitivity mapping.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum AssetClass {
    Equity,
    FixedIncome,
    Credit,
    Commodity,
    Currency,
    RealEstate,
    Alternative,
}

/// A single position in the portfolio being stress-tested.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortfolioPosition {
    pub name: String,
    pub weight: Decimal,
    pub asset_class: AssetClass,
    /// Equity beta (default 1.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub beta: Option<Decimal>,
    /// Fixed income / credit duration (default 5.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration: Option<Decimal>,
    /// Currency code of FX exposure (e.g. "EUR", "JPY")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fx_exposure: Option<String>,
}

/// Whether the scenario is based on a real historical event or hypothetical.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ScenarioType {
    Historical,
    Hypothetical,
}

/// A single market risk factor shock.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketShock {
    /// Factor name: "equity_market", "interest_rates", "credit_spreads",
    /// "fx_usd", "commodities", "volatility"
    pub factor: String,
    /// Shock magnitude as a decimal (e.g. -0.40 for a 40% equity decline)
    pub shock_pct: Decimal,
}

/// A complete stress scenario consisting of one or more market shocks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StressScenario {
    pub name: String,
    pub scenario_type: ScenarioType,
    pub shocks: Vec<MarketShock>,
}

/// Input for the stress-testing engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StressTestInput {
    /// Current portfolio positions
    pub portfolio: Vec<PortfolioPosition>,
    /// Scenarios to evaluate
    pub scenarios: Vec<StressScenario>,
    /// Multiply historical-scenario impacts by 1.2 to account for
    /// crisis correlation spikes (default true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub correlation_adjustments: Option<bool>,
}

/// Impact on a single position under one scenario.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionImpact {
    pub name: String,
    pub weight: Decimal,
    /// Percentage impact on the position
    pub impact_pct: Decimal,
    /// Contribution to portfolio P&L (weight * impact_pct)
    pub pnl_contribution: Decimal,
}

/// Result for a single stress scenario.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScenarioResult {
    pub scenario_name: String,
    /// Portfolio-level percentage change
    pub portfolio_impact: Decimal,
    /// Per-position breakdown
    pub position_impacts: Vec<PositionImpact>,
    /// Whether the scenario loss exceeds a simple 10% VaR threshold
    pub var_breach: bool,
}

/// High-level portfolio risk summary across all scenarios.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortfolioRiskSummary {
    pub current_weights: Vec<AssetAllocation>,
    pub max_drawdown_scenario: String,
    pub avg_scenario_loss: Decimal,
}

/// Output of the stress-testing engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StressTestOutput {
    pub scenario_results: Vec<ScenarioResult>,
    pub worst_case: ScenarioResult,
    pub portfolio_summary: PortfolioRiskSummary,
}

// ---------------------------------------------------------------------------
// Built-in historical scenarios
// ---------------------------------------------------------------------------

/// Return the five canonical historical stress scenarios.
pub fn get_historical_scenarios() -> Vec<StressScenario> {
    vec![
        // GFC 2008
        StressScenario {
            name: "GFC 2008".into(),
            scenario_type: ScenarioType::Historical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.38),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(-0.02), // -200 bps
                },
                MarketShock {
                    factor: "credit_spreads".into(),
                    shock_pct: dec!(0.04), // +400 bps
                },
                MarketShock {
                    factor: "commodities".into(),
                    shock_pct: dec!(-0.35),
                },
            ],
        },
        // COVID March 2020
        StressScenario {
            name: "COVID March 2020".into(),
            scenario_type: ScenarioType::Historical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.34),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(-0.01), // -100 bps
                },
                MarketShock {
                    factor: "credit_spreads".into(),
                    shock_pct: dec!(0.03), // +300 bps
                },
            ],
        },
        // Taper Tantrum 2013
        StressScenario {
            name: "Taper Tantrum 2013".into(),
            scenario_type: ScenarioType::Historical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.06),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(0.01), // +100 bps
                },
                MarketShock {
                    factor: "credit_spreads".into(),
                    shock_pct: dec!(0.005), // +50 bps
                },
            ],
        },
        // Dot-Com 2000
        StressScenario {
            name: "Dot-Com 2000".into(),
            scenario_type: ScenarioType::Historical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.49),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(-0.03), // -300 bps
                },
            ],
        },
        // Euro Crisis 2011
        StressScenario {
            name: "Euro Crisis 2011".into(),
            scenario_type: ScenarioType::Historical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.22),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(0.01), // +100 bps
                },
                MarketShock {
                    factor: "credit_spreads".into(),
                    shock_pct: dec!(0.02), // +200 bps
                },
            ],
        },
    ]
}

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

/// Run portfolio stress tests across one or more scenarios.
pub fn run_stress_test(
    input: &StressTestInput,
) -> CorpFinanceResult<ComputationOutput<StressTestOutput>> {
    let start = Instant::now();
    let warnings: Vec<String> = Vec::new();

    // -- Validation --
    if input.portfolio.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "Portfolio must contain at least one position".into(),
        ));
    }
    if input.scenarios.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one stress scenario required".into(),
        ));
    }
    for pos in &input.portfolio {
        if pos.weight < Decimal::ZERO || pos.weight > Decimal::ONE {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("portfolio.{}.weight", pos.name),
                reason: "Weight must be between 0 and 1".into(),
            });
        }
    }

    let use_corr_adj = input.correlation_adjustments.unwrap_or(true);

    // -- Evaluate each scenario --
    let mut scenario_results: Vec<ScenarioResult> = Vec::with_capacity(input.scenarios.len());

    for scenario in &input.scenarios {
        let result = evaluate_scenario(&input.portfolio, scenario, use_corr_adj);
        scenario_results.push(result);
    }

    // -- Find worst case (most negative portfolio_impact) --
    let worst_idx = scenario_results
        .iter()
        .enumerate()
        .min_by(|(_, a), (_, b)| {
            a.portfolio_impact
                .partial_cmp(&b.portfolio_impact)
                .unwrap_or(std::cmp::Ordering::Equal)
        })
        .map(|(i, _)| i)
        .unwrap_or(0);
    let worst_case = scenario_results[worst_idx].clone();

    // -- Portfolio summary --
    let current_weights: Vec<AssetAllocation> = input
        .portfolio
        .iter()
        .map(|p| AssetAllocation {
            name: p.name.clone(),
            weight: p.weight,
        })
        .collect();

    let total_loss: Decimal = scenario_results.iter().map(|r| r.portfolio_impact).sum();
    let avg_scenario_loss = total_loss / Decimal::from(scenario_results.len() as i64);

    let portfolio_summary = PortfolioRiskSummary {
        current_weights,
        max_drawdown_scenario: worst_case.scenario_name.clone(),
        avg_scenario_loss,
    };

    let output = StressTestOutput {
        scenario_results,
        worst_case,
        portfolio_summary,
    };

    let elapsed = start.elapsed().as_micros() as u64;
    Ok(with_metadata(
        "Portfolio Stress Testing",
        &serde_json::json!({
            "num_positions": input.portfolio.len(),
            "num_scenarios": input.scenarios.len(),
            "correlation_adjustments": use_corr_adj,
        }),
        warnings,
        elapsed,
        output,
    ))
}

// ---------------------------------------------------------------------------
// Internal logic
// ---------------------------------------------------------------------------

/// Evaluate a single scenario against the portfolio.
fn evaluate_scenario(
    portfolio: &[PortfolioPosition],
    scenario: &StressScenario,
    use_corr_adj: bool,
) -> ScenarioResult {
    let mut position_impacts: Vec<PositionImpact> = Vec::with_capacity(portfolio.len());
    let mut portfolio_impact = Decimal::ZERO;

    for pos in portfolio {
        let impact_pct = compute_position_impact(pos, &scenario.shocks);
        let pnl_contribution = pos.weight * impact_pct;
        portfolio_impact += pnl_contribution;

        position_impacts.push(PositionImpact {
            name: pos.name.clone(),
            weight: pos.weight,
            impact_pct,
            pnl_contribution,
        });
    }

    // Crisis correlation adjustment for historical scenarios
    if use_corr_adj && scenario.scenario_type == ScenarioType::Historical {
        portfolio_impact *= dec!(1.2);
    }

    // VaR breach: simple 10% threshold
    let var_breach = portfolio_impact < dec!(-0.10);

    ScenarioResult {
        scenario_name: scenario.name.clone(),
        portfolio_impact,
        position_impacts,
        var_breach,
    }
}

/// Compute the impact on a single position from the given shocks.
fn compute_position_impact(pos: &PortfolioPosition, shocks: &[MarketShock]) -> Decimal {
    let equity_shock = find_shock(shocks, "equity_market");
    let rate_shock = find_shock(shocks, "interest_rates");
    let credit_shock = find_shock(shocks, "credit_spreads");
    let commodity_shock = find_shock(shocks, "commodities");
    let fx_shock = find_shock(shocks, "fx_usd");
    let _vol_shock = find_shock(shocks, "volatility");

    match pos.asset_class {
        AssetClass::Equity => {
            let beta = pos.beta.unwrap_or(Decimal::ONE);
            equity_shock * beta
        }
        AssetClass::FixedIncome => {
            let duration = pos.duration.unwrap_or(dec!(5));
            -duration * rate_shock
        }
        AssetClass::Credit => {
            let duration = pos.duration.unwrap_or(dec!(5));
            -credit_shock * duration * dec!(0.5)
        }
        AssetClass::Commodity => commodity_shock,
        AssetClass::Currency => {
            if pos.fx_exposure.is_some() {
                fx_shock
            } else {
                Decimal::ZERO
            }
        }
        AssetClass::RealEstate => {
            let rate_duration = dec!(3);
            equity_shock * dec!(0.6) + rate_shock * (-rate_duration)
        }
        AssetClass::Alternative => equity_shock * dec!(0.4),
    }
}

/// Look up a shock factor by name, returning 0 if not present.
fn find_shock(shocks: &[MarketShock], factor: &str) -> Decimal {
    shocks
        .iter()
        .find(|s| s.factor == factor)
        .map(|s| s.shock_pct)
        .unwrap_or(Decimal::ZERO)
}

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

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

    fn single_equity_portfolio() -> Vec<PortfolioPosition> {
        vec![PortfolioPosition {
            name: "US Equities".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Equity,
            beta: Some(dec!(1.0)),
            duration: None,
            fx_exposure: None,
        }]
    }

    fn diversified_portfolio() -> Vec<PortfolioPosition> {
        vec![
            PortfolioPosition {
                name: "US Equities".into(),
                weight: dec!(0.40),
                asset_class: AssetClass::Equity,
                beta: Some(dec!(1.1)),
                duration: None,
                fx_exposure: None,
            },
            PortfolioPosition {
                name: "US Treasuries".into(),
                weight: dec!(0.30),
                asset_class: AssetClass::FixedIncome,
                beta: None,
                duration: Some(dec!(7)),
                fx_exposure: None,
            },
            PortfolioPosition {
                name: "IG Credit".into(),
                weight: dec!(0.15),
                asset_class: AssetClass::Credit,
                beta: None,
                duration: Some(dec!(5)),
                fx_exposure: None,
            },
            PortfolioPosition {
                name: "Commodities".into(),
                weight: dec!(0.10),
                asset_class: AssetClass::Commodity,
                beta: None,
                duration: None,
                fx_exposure: None,
            },
            PortfolioPosition {
                name: "EUR FX".into(),
                weight: dec!(0.05),
                asset_class: AssetClass::Currency,
                beta: None,
                duration: None,
                fx_exposure: Some("EUR".into()),
            },
        ]
    }

    fn simple_equity_crash() -> StressScenario {
        StressScenario {
            name: "Equity Crash".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "equity_market".into(),
                shock_pct: dec!(-0.30),
            }],
        }
    }

    // -- Single equity position --

    #[test]
    fn test_single_equity_crash() {
        let input = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        let sr = &result.result.scenario_results[0];
        // 100% equity * beta 1.0 * -30% = -30%
        assert_eq!(sr.portfolio_impact, dec!(-0.30));
        assert!(sr.var_breach);
    }

    #[test]
    fn test_single_equity_with_beta() {
        let portfolio = vec![PortfolioPosition {
            name: "High Beta".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Equity,
            beta: Some(dec!(1.5)),
            duration: None,
            fx_exposure: None,
        }];
        let input = StressTestInput {
            portfolio,
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // beta 1.5 * -30% = -45%
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.45)
        );
    }

    // -- Diversified portfolio --

    #[test]
    fn test_diversified_portfolio_gfc() {
        let scenarios = get_historical_scenarios();
        let gfc = scenarios
            .into_iter()
            .find(|s| s.name == "GFC 2008")
            .unwrap();
        let input = StressTestInput {
            portfolio: diversified_portfolio(),
            scenarios: vec![gfc],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        let sr = &result.result.scenario_results[0];
        assert!(sr.portfolio_impact < Decimal::ZERO);
        // Diversified should suffer less than pure equity
        assert!(sr.portfolio_impact > dec!(-0.38));
    }

    #[test]
    fn test_diversified_portfolio_all_historical() {
        let input = StressTestInput {
            portfolio: diversified_portfolio(),
            scenarios: get_historical_scenarios(),
            correlation_adjustments: Some(true),
        };
        let result = run_stress_test(&input).unwrap();
        assert_eq!(result.result.scenario_results.len(), 5);
        let worst = &result.result.worst_case;
        for sr in &result.result.scenario_results {
            assert!(worst.portfolio_impact <= sr.portfolio_impact);
        }
    }

    // -- Correlation adjustment --

    #[test]
    fn test_correlation_adjustment_multiplier() {
        let hist_scenario = StressScenario {
            name: "Historical Test".into(),
            scenario_type: ScenarioType::Historical,
            shocks: vec![MarketShock {
                factor: "equity_market".into(),
                shock_pct: dec!(-0.20),
            }],
        };
        let input_adj = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![hist_scenario.clone()],
            correlation_adjustments: Some(true),
        };
        let input_no_adj = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![hist_scenario],
            correlation_adjustments: Some(false),
        };
        let adj = run_stress_test(&input_adj).unwrap();
        let no_adj = run_stress_test(&input_no_adj).unwrap();
        let adj_impact = adj.result.scenario_results[0].portfolio_impact;
        let no_adj_impact = no_adj.result.scenario_results[0].portfolio_impact;
        let tolerance = dec!(0.0001);
        assert!(
            (adj_impact - no_adj_impact * dec!(1.2)).abs() < tolerance,
            "Adjusted {} should be 1.2x of unadjusted {}",
            adj_impact,
            no_adj_impact
        );
    }

    #[test]
    fn test_hypothetical_no_correlation_adjustment() {
        let input = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: Some(true),
        };
        let result = run_stress_test(&input).unwrap();
        // Hypothetical scenarios should not get the 1.2x multiplier
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.30)
        );
    }

    // -- Fixed income & duration --

    #[test]
    fn test_fixed_income_duration_sensitivity() {
        let short_dur = vec![PortfolioPosition {
            name: "Short Duration".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::FixedIncome,
            beta: None,
            duration: Some(dec!(2)),
            fx_exposure: None,
        }];
        let long_dur = vec![PortfolioPosition {
            name: "Long Duration".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::FixedIncome,
            beta: None,
            duration: Some(dec!(15)),
            fx_exposure: None,
        }];
        let rate_hike = StressScenario {
            name: "Rate Hike".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "interest_rates".into(),
                shock_pct: dec!(0.02), // +200bps
            }],
        };
        let short_input = StressTestInput {
            portfolio: short_dur,
            scenarios: vec![rate_hike.clone()],
            correlation_adjustments: Some(false),
        };
        let long_input = StressTestInput {
            portfolio: long_dur,
            scenarios: vec![rate_hike],
            correlation_adjustments: Some(false),
        };
        let short_result = run_stress_test(&short_input).unwrap();
        let long_result = run_stress_test(&long_input).unwrap();
        // Short duration: impact = -2 * 0.02 = -0.04
        assert_eq!(
            short_result.result.scenario_results[0].portfolio_impact,
            dec!(-0.04)
        );
        // Long duration: impact = -15 * 0.02 = -0.30
        assert_eq!(
            long_result.result.scenario_results[0].portfolio_impact,
            dec!(-0.30)
        );
        assert!(
            long_result.result.scenario_results[0].portfolio_impact
                < short_result.result.scenario_results[0].portfolio_impact
        );
    }

    // -- Credit --

    #[test]
    fn test_credit_spread_widening() {
        let portfolio = vec![PortfolioPosition {
            name: "IG Credit".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Credit,
            beta: None,
            duration: Some(dec!(5)),
            fx_exposure: None,
        }];
        let scenario = StressScenario {
            name: "Spread Widening".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "credit_spreads".into(),
                shock_pct: dec!(0.03), // +300bps
            }],
        };
        let input = StressTestInput {
            portfolio,
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // Impact = -0.03 * 5 * 0.5 = -0.075
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.075)
        );
    }

    // -- FX exposure --

    #[test]
    fn test_fx_exposure_impact() {
        let portfolio = vec![PortfolioPosition {
            name: "EUR Position".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Currency,
            beta: None,
            duration: None,
            fx_exposure: Some("EUR".into()),
        }];
        let scenario = StressScenario {
            name: "USD Strengthening".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "fx_usd".into(),
                shock_pct: dec!(-0.15),
            }],
        };
        let input = StressTestInput {
            portfolio,
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.15)
        );
    }

    #[test]
    fn test_no_fx_exposure_no_impact() {
        let portfolio = vec![PortfolioPosition {
            name: "Domestic".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Currency,
            beta: None,
            duration: None,
            fx_exposure: None,
        }];
        let scenario = StressScenario {
            name: "FX Shock".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "fx_usd".into(),
                shock_pct: dec!(-0.20),
            }],
        };
        let input = StressTestInput {
            portfolio,
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            Decimal::ZERO
        );
    }

    // -- Real estate and alternatives --

    #[test]
    fn test_real_estate_dual_sensitivity() {
        let portfolio = vec![PortfolioPosition {
            name: "REITs".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::RealEstate,
            beta: None,
            duration: None,
            fx_exposure: None,
        }];
        let scenario = StressScenario {
            name: "Combined Shock".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.20),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(0.01),
                },
            ],
        };
        let input = StressTestInput {
            portfolio,
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // RealEstate: equity*0.6 + rates*(-3) = -0.20*0.6 + 0.01*(-3) = -0.15
        let tolerance = dec!(0.0001);
        assert!(
            (result.result.scenario_results[0].portfolio_impact - dec!(-0.15)).abs() < tolerance
        );
    }

    #[test]
    fn test_alternative_partial_equity_sensitivity() {
        let portfolio = vec![PortfolioPosition {
            name: "Hedge Funds".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Alternative,
            beta: None,
            duration: None,
            fx_exposure: None,
        }];
        let input = StressTestInput {
            portfolio,
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // Alternative: equity * 0.4 = -0.30 * 0.4 = -0.12
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.12)
        );
    }

    // -- Custom hypothetical scenario --

    #[test]
    fn test_custom_hypothetical_scenario() {
        let scenario = StressScenario {
            name: "Stagflation".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![
                MarketShock {
                    factor: "equity_market".into(),
                    shock_pct: dec!(-0.25),
                },
                MarketShock {
                    factor: "interest_rates".into(),
                    shock_pct: dec!(0.03),
                },
                MarketShock {
                    factor: "commodities".into(),
                    shock_pct: dec!(0.20),
                },
            ],
        };
        let input = StressTestInput {
            portfolio: diversified_portfolio(),
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        assert_eq!(result.result.scenario_results.len(), 1);
        let impacts = &result.result.scenario_results[0].position_impacts;
        assert_eq!(impacts.len(), 5);
    }

    // -- Built-in historical scenarios --

    #[test]
    fn test_get_historical_scenarios_returns_five() {
        let scenarios = get_historical_scenarios();
        assert_eq!(scenarios.len(), 5);
        assert_eq!(scenarios[0].name, "GFC 2008");
        assert_eq!(scenarios[1].name, "COVID March 2020");
        assert_eq!(scenarios[2].name, "Taper Tantrum 2013");
        assert_eq!(scenarios[3].name, "Dot-Com 2000");
        assert_eq!(scenarios[4].name, "Euro Crisis 2011");
    }

    #[test]
    fn test_all_historical_scenarios_are_historical_type() {
        for s in get_historical_scenarios() {
            assert_eq!(s.scenario_type, ScenarioType::Historical);
        }
    }

    // -- VaR breach --

    #[test]
    fn test_var_breach_flag() {
        let input = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![
                StressScenario {
                    name: "Small Dip".into(),
                    scenario_type: ScenarioType::Hypothetical,
                    shocks: vec![MarketShock {
                        factor: "equity_market".into(),
                        shock_pct: dec!(-0.05),
                    }],
                },
                simple_equity_crash(), // -30%
            ],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        assert!(!result.result.scenario_results[0].var_breach);
        assert!(result.result.scenario_results[1].var_breach);
    }

    // -- Portfolio summary --

    #[test]
    fn test_portfolio_summary_avg_loss() {
        let input = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![
                StressScenario {
                    name: "Mild".into(),
                    scenario_type: ScenarioType::Hypothetical,
                    shocks: vec![MarketShock {
                        factor: "equity_market".into(),
                        shock_pct: dec!(-0.10),
                    }],
                },
                StressScenario {
                    name: "Severe".into(),
                    scenario_type: ScenarioType::Hypothetical,
                    shocks: vec![MarketShock {
                        factor: "equity_market".into(),
                        shock_pct: dec!(-0.30),
                    }],
                },
            ],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // Average = (-0.10 + -0.30) / 2 = -0.20
        assert_eq!(
            result.result.portfolio_summary.avg_scenario_loss,
            dec!(-0.20)
        );
        assert_eq!(
            result.result.portfolio_summary.max_drawdown_scenario,
            "Severe"
        );
    }

    // -- Validation errors --

    #[test]
    fn test_empty_portfolio_error() {
        let input = StressTestInput {
            portfolio: vec![],
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: None,
        };
        assert!(run_stress_test(&input).is_err());
    }

    #[test]
    fn test_empty_scenarios_error() {
        let input = StressTestInput {
            portfolio: single_equity_portfolio(),
            scenarios: vec![],
            correlation_adjustments: None,
        };
        assert!(run_stress_test(&input).is_err());
    }

    #[test]
    fn test_invalid_weight_error() {
        let portfolio = vec![PortfolioPosition {
            name: "Bad Weight".into(),
            weight: dec!(1.5),
            asset_class: AssetClass::Equity,
            beta: None,
            duration: None,
            fx_exposure: None,
        }];
        let input = StressTestInput {
            portfolio,
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: None,
        };
        assert!(run_stress_test(&input).is_err());
    }

    // -- Commodity position --

    #[test]
    fn test_commodity_direct_shock() {
        let portfolio = vec![PortfolioPosition {
            name: "Gold".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Commodity,
            beta: None,
            duration: None,
            fx_exposure: None,
        }];
        let scenario = StressScenario {
            name: "Commodity Crash".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "commodities".into(),
                shock_pct: dec!(-0.25),
            }],
        };
        let input = StressTestInput {
            portfolio,
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.25)
        );
    }

    // -- Default beta and duration --

    #[test]
    fn test_default_equity_beta() {
        let portfolio = vec![PortfolioPosition {
            name: "No Beta".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::Equity,
            beta: None, // Should default to 1.0
            duration: None,
            fx_exposure: None,
        }];
        let input = StressTestInput {
            portfolio,
            scenarios: vec![simple_equity_crash()],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // Default beta=1.0, so impact = -0.30
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.30)
        );
    }

    #[test]
    fn test_default_fi_duration() {
        let portfolio = vec![PortfolioPosition {
            name: "No Duration".into(),
            weight: Decimal::ONE,
            asset_class: AssetClass::FixedIncome,
            beta: None,
            duration: None, // Should default to 5
            fx_exposure: None,
        }];
        let scenario = StressScenario {
            name: "Rate Hike".into(),
            scenario_type: ScenarioType::Hypothetical,
            shocks: vec![MarketShock {
                factor: "interest_rates".into(),
                shock_pct: dec!(0.01),
            }],
        };
        let input = StressTestInput {
            portfolio,
            scenarios: vec![scenario],
            correlation_adjustments: Some(false),
        };
        let result = run_stress_test(&input).unwrap();
        // Default duration=5, impact = -5 * 0.01 = -0.05
        assert_eq!(
            result.result.scenario_results[0].portfolio_impact,
            dec!(-0.05)
        );
    }
}