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
//! DuPont decomposition analysis (3-Way and 5-Way).
//!
//! Decomposes Return on Equity into its component drivers:
//! - 3-Way: Net Profit Margin x Asset Turnover x Equity Multiplier
//! - 5-Way: Tax Burden x Interest Burden x Operating Margin x Asset Turnover x Equity Multiplier
//!
//! 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;

type TrendResult = (
    Option<Decimal>,
    Option<Decimal>,
    Option<Decimal>,
    Option<Decimal>,
    Option<String>,
);

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

/// Input for DuPont decomposition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DupontInput {
    pub net_income: Decimal,
    pub revenue: Decimal,
    pub total_assets: Decimal,
    pub shareholders_equity: Decimal,
    pub ebt: Decimal,
    pub ebit: Decimal,
    pub interest_expense: Decimal,
    pub tax_expense: Decimal,
    /// Prior period for trend analysis (all optional).
    pub prior_net_income: Option<Decimal>,
    pub prior_revenue: Option<Decimal>,
    pub prior_total_assets: Option<Decimal>,
    pub prior_equity: Option<Decimal>,
}

/// Output of DuPont decomposition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DupontOutput {
    // 3-Way decomposition
    pub roe: Decimal,
    pub net_profit_margin: Decimal,
    pub asset_turnover: Decimal,
    pub equity_multiplier: Decimal,
    pub roe_check: Decimal,
    // 5-Way decomposition
    pub tax_burden: Decimal,
    pub interest_burden: Decimal,
    pub operating_margin: Decimal,
    pub asset_turnover_5: Decimal,
    pub equity_multiplier_5: Decimal,
    pub roe_check_5: Decimal,
    // Trend (if prior period provided)
    pub roe_change: Option<Decimal>,
    pub margin_change: Option<Decimal>,
    pub turnover_change: Option<Decimal>,
    pub leverage_change: Option<Decimal>,
    pub primary_driver: Option<String>,
    // Qualitative assessment
    pub diagnosis: String,
}

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

fn safe_div(num: Decimal, den: Decimal, ctx: &str) -> CorpFinanceResult<Decimal> {
    if den == Decimal::ZERO {
        return Err(CorpFinanceError::DivisionByZero {
            context: ctx.to_string(),
        });
    }
    Ok(num / den)
}

fn diagnose(
    margin: Decimal,
    turnover: Decimal,
    multiplier: Decimal,
    tax_burden: Decimal,
    interest_burden: Decimal,
) -> String {
    let mut parts = Vec::new();

    if margin < dec!(0.05) {
        parts.push("Low profit margins indicate pricing pressure or cost issues");
    } else if margin > dec!(0.20) {
        parts.push("Strong profit margins suggest competitive advantage");
    }

    if turnover < dec!(0.5) {
        parts.push(
            "Low asset turnover suggests capital-intensive operations or underutilized assets",
        );
    } else if turnover > dec!(2.0) {
        parts.push("High asset turnover indicates efficient asset utilization");
    }

    if multiplier > dec!(5.0) {
        parts.push("Very high financial leverage increases financial risk significantly");
    } else if multiplier > dec!(3.0) {
        parts.push("Elevated financial leverage increases risk");
    } else if multiplier < dec!(1.5) {
        parts.push("Conservative leverage with low financial risk");
    }

    if tax_burden < dec!(0.6) {
        parts.push("High effective tax rate reduces returns");
    }

    if interest_burden < dec!(0.7) {
        parts.push("Interest expense significantly erodes pre-tax profits");
    }

    if parts.is_empty() {
        "Balanced DuPont profile with no notable concerns".to_string()
    } else {
        parts.join(". ") + "."
    }
}

// ---------------------------------------------------------------------------
// Core function
// ---------------------------------------------------------------------------

/// Perform DuPont decomposition (3-way and 5-way).
pub fn calculate_dupont(input: &DupontInput) -> CorpFinanceResult<DupontOutput> {
    // Validation
    if input.revenue == Decimal::ZERO {
        return Err(CorpFinanceError::DivisionByZero {
            context: "Revenue cannot be zero for DuPont analysis.".into(),
        });
    }
    if input.total_assets == Decimal::ZERO {
        return Err(CorpFinanceError::DivisionByZero {
            context: "Total assets cannot be zero for DuPont analysis.".into(),
        });
    }
    if input.shareholders_equity == Decimal::ZERO {
        return Err(CorpFinanceError::DivisionByZero {
            context: "Shareholders equity cannot be zero for DuPont analysis.".into(),
        });
    }
    if input.ebt == Decimal::ZERO && input.net_income != Decimal::ZERO {
        // Allow EBT=0 only if NI is also 0
        return Err(CorpFinanceError::DivisionByZero {
            context: "EBT cannot be zero when net income is non-zero.".into(),
        });
    }
    if input.ebit == Decimal::ZERO && input.ebt != Decimal::ZERO {
        return Err(CorpFinanceError::DivisionByZero {
            context: "EBIT cannot be zero when EBT is non-zero.".into(),
        });
    }

    // 3-Way decomposition
    let roe = safe_div(input.net_income, input.shareholders_equity, "ROE")?;
    let net_profit_margin = safe_div(input.net_income, input.revenue, "Net profit margin")?;
    let asset_turnover = safe_div(input.revenue, input.total_assets, "Asset turnover")?;
    let equity_multiplier = safe_div(
        input.total_assets,
        input.shareholders_equity,
        "Equity multiplier",
    )?;
    let roe_check = net_profit_margin * asset_turnover * equity_multiplier;

    // 5-Way decomposition
    let tax_burden = if input.ebt == Decimal::ZERO {
        Decimal::ZERO
    } else {
        safe_div(input.net_income, input.ebt, "Tax burden")?
    };
    let interest_burden = if input.ebit == Decimal::ZERO {
        Decimal::ZERO
    } else {
        safe_div(input.ebt, input.ebit, "Interest burden")?
    };
    let operating_margin = safe_div(input.ebit, input.revenue, "Operating margin")?;
    let asset_turnover_5 = asset_turnover;
    let equity_multiplier_5 = equity_multiplier;
    let roe_check_5 =
        tax_burden * interest_burden * operating_margin * asset_turnover_5 * equity_multiplier_5;

    // Trend analysis
    let (roe_change, margin_change, turnover_change, leverage_change, primary_driver) =
        compute_trend(input, net_profit_margin, asset_turnover, equity_multiplier)?;

    let diagnosis = diagnose(
        net_profit_margin,
        asset_turnover,
        equity_multiplier,
        tax_burden,
        interest_burden,
    );

    Ok(DupontOutput {
        roe,
        net_profit_margin,
        asset_turnover,
        equity_multiplier,
        roe_check,
        tax_burden,
        interest_burden,
        operating_margin,
        asset_turnover_5,
        equity_multiplier_5,
        roe_check_5,
        roe_change,
        margin_change,
        turnover_change,
        leverage_change,
        primary_driver,
        diagnosis,
    })
}

fn compute_trend(
    input: &DupontInput,
    cur_margin: Decimal,
    cur_turnover: Decimal,
    cur_multiplier: Decimal,
) -> CorpFinanceResult<TrendResult> {
    let (prior_ni, prior_rev, prior_ta, prior_eq) = match (
        input.prior_net_income,
        input.prior_revenue,
        input.prior_total_assets,
        input.prior_equity,
    ) {
        (Some(ni), Some(rev), Some(ta), Some(eq)) => (ni, rev, ta, eq),
        _ => return Ok((None, None, None, None, None)),
    };

    if prior_rev == Decimal::ZERO || prior_ta == Decimal::ZERO || prior_eq == Decimal::ZERO {
        return Ok((None, None, None, None, None));
    }

    let prior_roe = prior_ni / prior_eq;
    let prior_margin = prior_ni / prior_rev;
    let prior_turnover = prior_rev / prior_ta;
    let prior_multiplier = prior_ta / prior_eq;

    let roe_chg = cur_margin * cur_turnover * cur_multiplier - prior_roe;
    let margin_chg = cur_margin - prior_margin;
    let turnover_chg = cur_turnover - prior_turnover;
    let leverage_chg = cur_multiplier - prior_multiplier;

    // Primary driver = component with largest absolute change
    let changes = [
        ("Profit margin".to_string(), margin_chg.abs()),
        ("Asset turnover".to_string(), turnover_chg.abs()),
        ("Financial leverage".to_string(), leverage_chg.abs()),
    ];
    let driver = changes
        .iter()
        .max_by(|a, b| a.1.cmp(&b.1))
        .map(|(name, _)| name.clone());

    Ok((
        Some(roe_chg),
        Some(margin_chg),
        Some(turnover_chg),
        Some(leverage_chg),
        driver,
    ))
}

// ---------------------------------------------------------------------------
// 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 base_input() -> DupontInput {
        DupontInput {
            net_income: dec!(100),
            revenue: dec!(1000),
            total_assets: dec!(2000),
            shareholders_equity: dec!(800),
            ebt: dec!(130),
            ebit: dec!(150),
            interest_expense: dec!(20),
            tax_expense: dec!(30),
            prior_net_income: None,
            prior_revenue: None,
            prior_total_assets: None,
            prior_equity: None,
        }
    }

    #[test]
    fn test_roe_calculation() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.roe, dec!(100) / dec!(800));
    }

    #[test]
    fn test_net_profit_margin() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.net_profit_margin, dec!(100) / dec!(1000));
    }

    #[test]
    fn test_asset_turnover() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.asset_turnover, dec!(1000) / dec!(2000));
    }

    #[test]
    fn test_equity_multiplier() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.equity_multiplier, dec!(2000) / dec!(800));
    }

    #[test]
    fn test_three_way_identity() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert!(approx_eq(out.roe, out.roe_check, dec!(0.0001)));
    }

    #[test]
    fn test_tax_burden() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.tax_burden, dec!(100) / dec!(130));
    }

    #[test]
    fn test_interest_burden() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.interest_burden, dec!(130) / dec!(150));
    }

    #[test]
    fn test_operating_margin() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert_eq!(out.operating_margin, dec!(150) / dec!(1000));
    }

    #[test]
    fn test_five_way_identity() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert!(
            approx_eq(out.roe, out.roe_check_5, dec!(0.0001)),
            "ROE={} vs 5-way check={}",
            out.roe,
            out.roe_check_5
        );
    }

    #[test]
    fn test_high_margin_low_turnover() {
        let mut input = base_input();
        input.net_income = dec!(300);
        input.revenue = dec!(1000);
        input.total_assets = dec!(5000);
        input.shareholders_equity = dec!(3000);
        input.ebt = dec!(400);
        input.ebit = dec!(450);
        let out = calculate_dupont(&input).unwrap();
        assert!(out.net_profit_margin > dec!(0.2));
        assert!(out.asset_turnover < dec!(0.5));
    }

    #[test]
    fn test_high_leverage() {
        let mut input = base_input();
        input.shareholders_equity = dec!(200);
        let out = calculate_dupont(&input).unwrap();
        assert!(out.equity_multiplier > dec!(5.0));
    }

    #[test]
    fn test_trend_analysis_with_prior() {
        let mut input = base_input();
        input.prior_net_income = Some(dec!(80));
        input.prior_revenue = Some(dec!(900));
        input.prior_total_assets = Some(dec!(1800));
        input.prior_equity = Some(dec!(750));
        let out = calculate_dupont(&input).unwrap();
        assert!(out.roe_change.is_some());
        assert!(out.margin_change.is_some());
        assert!(out.turnover_change.is_some());
        assert!(out.leverage_change.is_some());
        assert!(out.primary_driver.is_some());
    }

    #[test]
    fn test_no_trend_without_prior() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert!(out.roe_change.is_none());
        assert!(out.primary_driver.is_none());
    }

    #[test]
    fn test_primary_driver_margin() {
        let mut input = base_input();
        // Big margin change, small turnover and leverage change
        input.prior_net_income = Some(dec!(20));
        input.prior_revenue = Some(dec!(1000));
        input.prior_total_assets = Some(dec!(2000));
        input.prior_equity = Some(dec!(800));
        let out = calculate_dupont(&input).unwrap();
        assert_eq!(out.primary_driver.as_deref(), Some("Profit margin"));
    }

    #[test]
    fn test_zero_revenue_error() {
        let mut input = base_input();
        input.revenue = Decimal::ZERO;
        assert!(calculate_dupont(&input).is_err());
    }

    #[test]
    fn test_zero_total_assets_error() {
        let mut input = base_input();
        input.total_assets = Decimal::ZERO;
        assert!(calculate_dupont(&input).is_err());
    }

    #[test]
    fn test_zero_equity_error() {
        let mut input = base_input();
        input.shareholders_equity = Decimal::ZERO;
        assert!(calculate_dupont(&input).is_err());
    }

    #[test]
    fn test_diagnosis_contains_text() {
        let out = calculate_dupont(&base_input()).unwrap();
        assert!(!out.diagnosis.is_empty());
    }

    #[test]
    fn test_diagnosis_high_leverage() {
        let mut input = base_input();
        input.shareholders_equity = dec!(100);
        let out = calculate_dupont(&input).unwrap();
        assert!(out.diagnosis.contains("leverage"));
    }

    #[test]
    fn test_zero_ebt_zero_ni_ok() {
        let mut input = base_input();
        input.net_income = Decimal::ZERO;
        input.ebt = Decimal::ZERO;
        input.ebit = Decimal::ZERO;
        let out = calculate_dupont(&input).unwrap();
        assert_eq!(out.tax_burden, Decimal::ZERO);
        assert_eq!(out.interest_burden, Decimal::ZERO);
    }

    #[test]
    fn test_serialization_roundtrip() {
        let out = calculate_dupont(&base_input()).unwrap();
        let json = serde_json::to_string(&out).unwrap();
        let _deser: DupontOutput = serde_json::from_str(&json).unwrap();
    }
}