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
//! Euler Risk Contribution (Capital Allocation).
//!
//! Covers:
//! 1. **Marginal Contribution** -- MC_i = dVaR/dw_i via finite difference
//! 2. **Euler Allocation** -- allocated_capital_i = w_i * MC_i
//! 3. **Diversification Benefit** -- sum(standalone) - portfolio capital
//! 4. **Concentration Index** -- HHI of capital allocations
//! 5. **Stand-alone Capital** -- VaR of each unit independently
//!
//! 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 business unit for Euler allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EulerUnit {
    /// Name of the business unit.
    pub name: String,
    /// Portfolio weight (must sum to 1 across all units, or be proportional).
    pub weight: Decimal,
    /// Stand-alone VaR for this unit.
    pub standalone_var: Decimal,
    /// Simulated returns or P&L series for this unit.
    pub returns: Vec<Decimal>,
}

/// Input for Euler allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EulerAllocationInput {
    /// Business units.
    pub units: Vec<EulerUnit>,
    /// Total portfolio VaR.
    pub portfolio_var: Decimal,
    /// Epsilon for finite difference (e.g. 0.01).
    pub epsilon: Decimal,
    /// Confidence level for VaR calculation (e.g. 0.99).
    pub confidence_level: Decimal,
}

/// A single unit's Euler allocation result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EulerAllocationDetail {
    /// Unit name.
    pub name: String,
    /// Portfolio weight.
    pub weight: Decimal,
    /// Stand-alone capital.
    pub standalone: Decimal,
    /// Marginal contribution = dVaR/dw.
    pub marginal_contribution: Decimal,
    /// Allocated capital = weight * marginal contribution.
    pub allocated_capital: Decimal,
    /// Percentage of total portfolio VaR.
    pub pct_of_total: Decimal,
}

/// Output of the Euler allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EulerAllocationOutput {
    /// Per-unit allocation details.
    pub allocations: Vec<EulerAllocationDetail>,
    /// Diversification benefit = sum(standalone) - portfolio_var.
    pub diversification_benefit: Decimal,
    /// Diversification ratio = sum(standalone) / portfolio_var.
    pub diversification_ratio: Decimal,
    /// HHI concentration index of allocated capital shares.
    pub hhi: Decimal,
}

/// Calculate Euler risk contribution allocation.
pub fn calculate_euler_allocation(
    input: &EulerAllocationInput,
) -> CorpFinanceResult<EulerAllocationOutput> {
    validate_euler_input(input)?;

    let n_units = input.units.len();

    // Determine number of scenarios from the first unit
    let n_scenarios = input.units[0].returns.len();

    // Calculate marginal contributions via finite difference on portfolio VaR
    let mut allocations = Vec::with_capacity(n_units);
    let mut total_standalone = Decimal::ZERO;
    let mut total_allocated = Decimal::ZERO;

    for unit in &input.units {
        total_standalone += unit.standalone_var;

        // Compute portfolio returns with perturbed weights
        // VaR(w_i + eps) and VaR(w_i - eps) using the returns data
        let var_up = compute_perturbed_var(
            &input.units,
            &unit.name,
            input.epsilon,
            input.confidence_level,
            n_scenarios,
        );
        let var_down = compute_perturbed_var(
            &input.units,
            &unit.name,
            -input.epsilon,
            input.confidence_level,
            n_scenarios,
        );

        let marginal_contribution = if input.epsilon.is_zero() {
            Decimal::ZERO
        } else {
            (var_up - var_down) / (dec!(2) * input.epsilon)
        };

        let allocated_capital = unit.weight * marginal_contribution;

        allocations.push(EulerAllocationDetail {
            name: unit.name.clone(),
            weight: unit.weight,
            standalone: unit.standalone_var,
            marginal_contribution,
            allocated_capital,
            pct_of_total: Decimal::ZERO, // filled below
        });

        total_allocated += allocated_capital;
    }

    // Compute pct_of_total
    for alloc in &mut allocations {
        alloc.pct_of_total = if input.portfolio_var.is_zero() {
            Decimal::ZERO
        } else {
            alloc.allocated_capital / input.portfolio_var
        };
    }

    // Diversification benefit
    let diversification_benefit = total_standalone - input.portfolio_var;

    // Diversification ratio
    let diversification_ratio = if input.portfolio_var.is_zero() {
        Decimal::ZERO
    } else {
        total_standalone / input.portfolio_var
    };

    // HHI of allocation shares
    let hhi = if total_allocated.is_zero() {
        Decimal::ZERO
    } else {
        allocations
            .iter()
            .map(|a| {
                let share = a.allocated_capital / total_allocated;
                share * share
            })
            .sum::<Decimal>()
    };

    Ok(EulerAllocationOutput {
        allocations,
        diversification_benefit,
        diversification_ratio,
        hhi,
    })
}

/// Compute VaR of the portfolio with one unit's weight perturbed by delta.
fn compute_perturbed_var(
    units: &[EulerUnit],
    perturb_name: &str,
    delta: Decimal,
    confidence_level: Decimal,
    n_scenarios: usize,
) -> Decimal {
    // Build perturbed portfolio returns for each scenario
    let mut portfolio_returns = vec![Decimal::ZERO; n_scenarios];

    for unit in units {
        let w = if unit.name == perturb_name {
            unit.weight + delta
        } else {
            unit.weight
        };
        for (i, &r) in unit.returns.iter().enumerate() {
            if i < n_scenarios {
                portfolio_returns[i] += w * r;
            }
        }
    }

    // VaR = quantile of losses (negative returns). Sort ascending.
    portfolio_returns.sort();

    let var_index = {
        // VaR at (1 - confidence) quantile of returns (loss perspective)
        let loss_quantile = Decimal::ONE - confidence_level;
        let idx_decimal = loss_quantile * Decimal::from(n_scenarios as u64);
        let idx_str = idx_decimal.to_string();
        let idx = idx_str
            .split('.')
            .next()
            .unwrap_or("0")
            .parse::<usize>()
            .unwrap_or(0);
        if idx >= n_scenarios {
            n_scenarios - 1
        } else if idx == 0 {
            0
        } else {
            idx
        }
    };

    // VaR = negative of the return at the quantile (loss is positive)
    -portfolio_returns[var_index]
}

fn validate_euler_input(input: &EulerAllocationInput) -> CorpFinanceResult<()> {
    if input.units.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one unit is required for Euler allocation.".into(),
        ));
    }
    if input.epsilon <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "epsilon".into(),
            reason: "Epsilon for finite difference must be positive.".into(),
        });
    }
    if input.confidence_level <= Decimal::ZERO || input.confidence_level >= Decimal::ONE {
        return Err(CorpFinanceError::InvalidInput {
            field: "confidence_level".into(),
            reason: "Confidence level must be in (0, 1).".into(),
        });
    }
    if input.portfolio_var < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "portfolio_var".into(),
            reason: "Portfolio VaR must be non-negative.".into(),
        });
    }

    let first_len = input.units[0].returns.len();
    if first_len == 0 {
        return Err(CorpFinanceError::InsufficientData(
            "Returns series must contain at least one observation.".into(),
        ));
    }
    for unit in &input.units {
        if unit.returns.len() != first_len {
            return Err(CorpFinanceError::InvalidInput {
                field: "returns".into(),
                reason: format!(
                    "All units must have same number of return observations. Expected {}, got {} for unit '{}'.",
                    first_len,
                    unit.returns.len(),
                    unit.name
                ),
            });
        }
        if unit.weight < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "weight".into(),
                reason: format!("Weight must be non-negative for unit '{}'.", unit.name),
            });
        }
        if unit.standalone_var < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "standalone_var".into(),
                reason: format!(
                    "Stand-alone VaR must be non-negative for unit '{}'.",
                    unit.name
                ),
            });
        }
    }
    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 make_returns_a() -> Vec<Decimal> {
        // 100 scenarios: some positive, some negative
        (0..100)
            .map(|i| {
                let val = Decimal::from(i as i64 - 50);
                val / dec!(100)
            })
            .collect()
    }

    fn make_returns_b() -> Vec<Decimal> {
        // 100 scenarios: offset pattern
        (0..100)
            .map(|i| {
                let val = Decimal::from(50 - i as i64);
                val / dec!(100)
            })
            .collect()
    }

    fn make_base_input() -> EulerAllocationInput {
        EulerAllocationInput {
            units: vec![
                EulerUnit {
                    name: "Unit_A".into(),
                    weight: dec!(0.6),
                    standalone_var: dec!(100),
                    returns: make_returns_a(),
                },
                EulerUnit {
                    name: "Unit_B".into(),
                    weight: dec!(0.4),
                    standalone_var: dec!(80),
                    returns: make_returns_b(),
                },
            ],
            portfolio_var: dec!(150),
            epsilon: dec!(0.01),
            confidence_level: dec!(0.99),
        }
    }

    #[test]
    fn test_allocations_count_matches_units() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        assert_eq!(out.allocations.len(), 2);
    }

    #[test]
    fn test_diversification_benefit_positive() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        // sum(standalone) = 100 + 80 = 180 > 150 = portfolio_var
        assert_eq!(out.diversification_benefit, dec!(30));
    }

    #[test]
    fn test_diversification_ratio_gt_one() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        assert!(
            out.diversification_ratio > Decimal::ONE,
            "Diversification ratio should be > 1"
        );
    }

    #[test]
    fn test_diversification_ratio_value() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        // 180 / 150 = 1.2
        assert_eq!(out.diversification_ratio, dec!(180) / dec!(150));
    }

    #[test]
    fn test_hhi_non_negative() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        // HHI is sum of squared shares, always non-negative
        assert!(
            out.hhi >= Decimal::ZERO,
            "HHI should be non-negative, got {}",
            out.hhi
        );
    }

    #[test]
    fn test_allocation_names_preserved() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        assert_eq!(out.allocations[0].name, "Unit_A");
        assert_eq!(out.allocations[1].name, "Unit_B");
    }

    #[test]
    fn test_allocation_weights_preserved() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        assert_eq!(out.allocations[0].weight, dec!(0.6));
        assert_eq!(out.allocations[1].weight, dec!(0.4));
    }

    #[test]
    fn test_standalone_preserved() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        assert_eq!(out.allocations[0].standalone, dec!(100));
        assert_eq!(out.allocations[1].standalone, dec!(80));
    }

    #[test]
    fn test_single_unit_marginal_equals_var() {
        let input = EulerAllocationInput {
            units: vec![EulerUnit {
                name: "Only".into(),
                weight: dec!(1.0),
                standalone_var: dec!(50),
                returns: make_returns_a(),
            }],
            portfolio_var: dec!(50),
            epsilon: dec!(0.01),
            confidence_level: dec!(0.99),
        };
        let out = calculate_euler_allocation(&input).unwrap();
        // For a single unit, diversification benefit should be zero
        assert_eq!(out.diversification_benefit, Decimal::ZERO);
    }

    #[test]
    fn test_single_unit_hhi_is_one() {
        let input = EulerAllocationInput {
            units: vec![EulerUnit {
                name: "Only".into(),
                weight: dec!(1.0),
                standalone_var: dec!(50),
                returns: make_returns_a(),
            }],
            portfolio_var: dec!(50),
            epsilon: dec!(0.01),
            confidence_level: dec!(0.99),
        };
        let out = calculate_euler_allocation(&input).unwrap();
        // Single unit: HHI = 1.0^2 = 1.0
        assert!(
            approx_eq(out.hhi, Decimal::ONE, dec!(0.01)),
            "Single unit HHI should be ~1.0, got {}",
            out.hhi
        );
    }

    #[test]
    fn test_marginal_contribution_finite() {
        let input = make_base_input();
        let out = calculate_euler_allocation(&input).unwrap();
        for alloc in &out.allocations {
            assert!(
                alloc.marginal_contribution.abs() < dec!(10000),
                "Marginal contribution should be finite"
            );
        }
    }

    #[test]
    fn test_zero_portfolio_var_zero_pct() {
        let mut input = make_base_input();
        input.portfolio_var = Decimal::ZERO;
        let out = calculate_euler_allocation(&input).unwrap();
        for alloc in &out.allocations {
            assert_eq!(alloc.pct_of_total, Decimal::ZERO);
        }
    }

    #[test]
    fn test_three_units() {
        let returns_c: Vec<Decimal> = (0..100)
            .map(|i| Decimal::from((i % 7) as i64 - 3) / dec!(100))
            .collect();
        let input = EulerAllocationInput {
            units: vec![
                EulerUnit {
                    name: "A".into(),
                    weight: dec!(0.4),
                    standalone_var: dec!(60),
                    returns: make_returns_a(),
                },
                EulerUnit {
                    name: "B".into(),
                    weight: dec!(0.35),
                    standalone_var: dec!(50),
                    returns: make_returns_b(),
                },
                EulerUnit {
                    name: "C".into(),
                    weight: dec!(0.25),
                    standalone_var: dec!(40),
                    returns: returns_c,
                },
            ],
            portfolio_var: dec!(120),
            epsilon: dec!(0.01),
            confidence_level: dec!(0.95),
        };
        let out = calculate_euler_allocation(&input).unwrap();
        assert_eq!(out.allocations.len(), 3);
        assert_eq!(out.diversification_benefit, dec!(30)); // 150 - 120
    }

    #[test]
    fn test_equal_weight_units() {
        let input = EulerAllocationInput {
            units: vec![
                EulerUnit {
                    name: "X".into(),
                    weight: dec!(0.5),
                    standalone_var: dec!(100),
                    returns: make_returns_a(),
                },
                EulerUnit {
                    name: "Y".into(),
                    weight: dec!(0.5),
                    standalone_var: dec!(100),
                    returns: make_returns_a(),
                },
            ],
            portfolio_var: dec!(100),
            epsilon: dec!(0.01),
            confidence_level: dec!(0.99),
        };
        let out = calculate_euler_allocation(&input).unwrap();
        // Identical units => identical allocations
        assert_eq!(
            out.allocations[0].allocated_capital,
            out.allocations[1].allocated_capital
        );
    }

    // -- Validation tests --

    #[test]
    fn test_reject_empty_units() {
        let input = EulerAllocationInput {
            units: vec![],
            portfolio_var: dec!(100),
            epsilon: dec!(0.01),
            confidence_level: dec!(0.99),
        };
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_zero_epsilon() {
        let mut input = make_base_input();
        input.epsilon = Decimal::ZERO;
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_negative_epsilon() {
        let mut input = make_base_input();
        input.epsilon = dec!(-0.01);
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_confidence_zero() {
        let mut input = make_base_input();
        input.confidence_level = Decimal::ZERO;
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_confidence_one() {
        let mut input = make_base_input();
        input.confidence_level = Decimal::ONE;
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_negative_portfolio_var() {
        let mut input = make_base_input();
        input.portfolio_var = dec!(-1);
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_mismatched_returns_length() {
        let mut input = make_base_input();
        input.units[1].returns = vec![dec!(0.01); 50]; // 50 vs 100
        assert!(calculate_euler_allocation(&input).is_err());
    }

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

    #[test]
    fn test_reject_negative_standalone_var() {
        let mut input = make_base_input();
        input.units[0].standalone_var = dec!(-10);
        assert!(calculate_euler_allocation(&input).is_err());
    }

    #[test]
    fn test_reject_empty_returns() {
        let mut input = make_base_input();
        input.units[0].returns = vec![];
        input.units[1].returns = vec![];
        assert!(calculate_euler_allocation(&input).is_err());
    }

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