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
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
use crate::compat::Instant;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::types::*;
use crate::CorpFinanceResult;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptionType {
    Expand,
    Abandon,
    Defer,
    Switch,
    Contract,
    Compound,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RealOptionInput {
    pub option_type: OptionType,
    /// Current project NPV or asset value
    pub underlying_value: Decimal,
    /// Cost to exercise (investment cost, salvage value, etc.)
    pub exercise_price: Decimal,
    /// Project value volatility
    pub volatility: Decimal,
    /// Risk-free discount rate
    pub risk_free_rate: Decimal,
    /// Years until option expires
    pub time_to_expiry: Decimal,
    /// Number of binomial tree steps (default 100)
    #[serde(default = "default_steps")]
    pub steps: u32,
    /// Continuous leakage / cost of waiting
    pub dividend_yield: Option<Decimal>,
    /// For expand option: scale multiplier (e.g. 1.5 = 50% expansion)
    pub expansion_factor: Option<Decimal>,
    /// For contract option: scale reduction factor
    pub contraction_factor: Option<Decimal>,
    /// For switch option: cost to switch between modes
    pub switch_cost: Option<Decimal>,
    /// For switch option: value ratio in alternate mode
    pub switch_value_ratio: Option<Decimal>,
}

fn default_steps() -> u32 {
    100
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExerciseBoundary {
    pub time_step: u32,
    pub threshold_value: Decimal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RealOptionOutput {
    /// Value of the real option
    pub option_value: Decimal,
    /// Static NPV + option value
    pub expanded_npv: Decimal,
    /// Underlying value minus exercise price (for defer/expand)
    pub static_npv: Decimal,
    /// option_value - max(static_npv, 0)
    pub option_premium: Decimal,
    /// Exercise threshold at each time step
    pub optimal_exercise_boundary: Vec<ExerciseBoundary>,
    /// Sensitivity to underlying value change
    pub delta: Decimal,
    /// Second-order sensitivity
    pub gamma: Decimal,
    /// Time decay per year
    pub theta: Decimal,
    /// Sensitivity to volatility
    pub vega: Decimal,
    /// Whether early exercise is recommended now
    pub early_exercise_optimal: bool,
    /// Volatility at which option_value = 0 premium
    pub breakeven_volatility: Decimal,
}

// ---------------------------------------------------------------------------
// Decimal math helpers (no f64, no MathematicalOps)
// ---------------------------------------------------------------------------

/// Taylor series exp(x) with range reduction for |x| > 2.
fn exp_decimal(x: Decimal) -> Decimal {
    let two = dec!(2);
    if x > two || x < -two {
        let half = exp_decimal(x / two);
        return half * half;
    }
    let mut sum = Decimal::ONE;
    let mut term = Decimal::ONE;
    for n in 1u32..=30 {
        term = term * x / Decimal::from(n);
        sum += term;
    }
    sum
}

/// Newton's method sqrt: 20 iterations.
fn sqrt_decimal(x: Decimal) -> Decimal {
    if x <= Decimal::ZERO {
        return Decimal::ZERO;
    }
    if x == Decimal::ONE {
        return Decimal::ONE;
    }
    let two = dec!(2);
    let mut guess = x / two;
    if x > dec!(100) {
        guess = dec!(10);
    } else if x < dec!(0.01) {
        guess = dec!(0.1);
    }
    for _ in 0..20 {
        guess = (guess + x / guess) / two;
    }
    guess
}

/// Natural log via Newton's method: find y such that exp(y) = x. 20 iterations.
#[allow(dead_code)]
fn ln_decimal(x: Decimal) -> Decimal {
    if x <= Decimal::ZERO {
        return dec!(-999);
    }
    if x == Decimal::ONE {
        return Decimal::ZERO;
    }
    let mut y = if x > dec!(0.5) && x < dec!(2) {
        x - Decimal::ONE
    } else {
        let mut approx = Decimal::ZERO;
        let mut v = x;
        let e_approx = dec!(2.718281828459045);
        if x > Decimal::ONE {
            while v > e_approx {
                v /= e_approx;
                approx += Decimal::ONE;
            }
            approx + (v - Decimal::ONE)
        } else {
            while v < Decimal::ONE / e_approx {
                v *= e_approx;
                approx -= Decimal::ONE;
            }
            approx + (v - Decimal::ONE)
        }
    };
    for _ in 0..20 {
        let ey = exp_decimal(y);
        if ey == Decimal::ZERO {
            break;
        }
        y = y - Decimal::ONE + x / ey;
    }
    y
}

/// Integer power via exponentiation by squaring (iterative multiplication).
#[allow(dead_code)]
fn pow_decimal(base: Decimal, exp: u32) -> Decimal {
    if exp == 0 {
        return Decimal::ONE;
    }
    let mut result = Decimal::ONE;
    let mut b = base;
    let mut e = exp;
    while e > 0 {
        if e & 1 == 1 {
            result *= b;
        }
        b *= b;
        e >>= 1;
    }
    result
}

/// Absolute value of a Decimal.
#[allow(dead_code)]
fn abs_decimal(x: Decimal) -> Decimal {
    if x < Decimal::ZERO {
        -x
    } else {
        x
    }
}

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

fn validate_input(input: &RealOptionInput) -> CorpFinanceResult<()> {
    if input.underlying_value <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "underlying_value".into(),
            reason: "must be positive".into(),
        });
    }
    if input.exercise_price <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "exercise_price".into(),
            reason: "must be positive".into(),
        });
    }
    if input.volatility < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "volatility".into(),
            reason: "must be non-negative".into(),
        });
    }
    if input.time_to_expiry <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "time_to_expiry".into(),
            reason: "must be positive".into(),
        });
    }
    if input.steps == 0 {
        return Err(CorpFinanceError::InvalidInput {
            field: "steps".into(),
            reason: "must be at least 1".into(),
        });
    }
    if let Some(q) = input.dividend_yield {
        if q < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "dividend_yield".into(),
                reason: "must be non-negative".into(),
            });
        }
    }
    // Option-type-specific validation
    match input.option_type {
        OptionType::Expand => {
            if input.expansion_factor.is_none() {
                return Err(CorpFinanceError::InvalidInput {
                    field: "expansion_factor".into(),
                    reason: "required for Expand option type".into(),
                });
            }
            let ef = input.expansion_factor.unwrap();
            if ef <= Decimal::ONE {
                return Err(CorpFinanceError::InvalidInput {
                    field: "expansion_factor".into(),
                    reason: "must be greater than 1.0".into(),
                });
            }
        }
        OptionType::Contract => {
            if input.contraction_factor.is_none() {
                return Err(CorpFinanceError::InvalidInput {
                    field: "contraction_factor".into(),
                    reason: "required for Contract option type".into(),
                });
            }
            let cf = input.contraction_factor.unwrap();
            if cf <= Decimal::ZERO || cf >= Decimal::ONE {
                return Err(CorpFinanceError::InvalidInput {
                    field: "contraction_factor".into(),
                    reason: "must be between 0 and 1 exclusive".into(),
                });
            }
        }
        OptionType::Switch => {
            if input.switch_cost.is_none() {
                return Err(CorpFinanceError::InvalidInput {
                    field: "switch_cost".into(),
                    reason: "required for Switch option type".into(),
                });
            }
            if input.switch_value_ratio.is_none() {
                return Err(CorpFinanceError::InvalidInput {
                    field: "switch_value_ratio".into(),
                    reason: "required for Switch option type".into(),
                });
            }
        }
        _ => {}
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Core binomial tree engine
// ---------------------------------------------------------------------------

/// Result from the binomial tree computation, including full value information.
#[allow(dead_code)]
struct BinomialResult {
    option_value: Decimal,
    /// Price tree at time step 1: [down_node, up_node]
    price_step1: (Decimal, Decimal),
    /// Option values at time step 1: [down_node, up_node]
    value_step1: (Decimal, Decimal),
    /// Price tree at time step 2 center node
    price_step2_mid: Decimal,
    /// Option value at time step 2 center node
    value_step2_mid: Decimal,
    /// Exercise boundaries per time step
    exercise_boundaries: Vec<ExerciseBoundary>,
    /// Whether the root node exercises immediately
    early_exercise_at_root: bool,
}

/// Maximum representable Decimal value used as a cap to avoid overflow panics.
const DECIMAL_CAP: Decimal = Decimal::from_parts(u32::MAX, u32::MAX, u32::MAX, false, 0);

/// Checked multiplication that returns DECIMAL_CAP on overflow instead of panicking.
fn safe_mul(a: Decimal, b: Decimal) -> Decimal {
    a.checked_mul(b).unwrap_or(DECIMAL_CAP)
}

/// Compute node price using net exponent to avoid overflow.
/// Since d = 1/u, we have: S * u^ups * d^downs = S * u^(ups - downs).
/// For net_up >= 0: S * u^net_up. For net_up < 0: S / u^|net_up|.
fn node_price(s: Decimal, u: Decimal, ups: u32, downs: u32) -> Decimal {
    if ups >= downs {
        safe_mul(s, pow_decimal_safe(u, ups - downs))
    } else {
        let denom = pow_decimal_safe(u, downs - ups);
        if denom == Decimal::ZERO {
            return Decimal::ZERO;
        }
        s / denom
    }
}

/// Safe integer power that caps on overflow.
fn pow_decimal_safe(base: Decimal, exp: u32) -> Decimal {
    if exp == 0 {
        return Decimal::ONE;
    }
    let mut result = Decimal::ONE;
    let mut b = base;
    let mut e = exp;
    while e > 0 {
        if e & 1 == 1 {
            result = match result.checked_mul(b) {
                Some(v) => v,
                None => return DECIMAL_CAP,
            };
        }
        b = match b.checked_mul(b) {
            Some(v) => v,
            None => {
                // If we still have bits to process, result is already capped
                if e > 1 {
                    return DECIMAL_CAP;
                }
                break;
            }
        };
        e >>= 1;
    }
    result
}

/// Build and solve the binomial tree for the given option type.
#[allow(clippy::too_many_arguments)]
fn binomial_tree(
    s: Decimal,
    k: Decimal,
    sigma: Decimal,
    r: Decimal,
    q: Decimal,
    t: Decimal,
    n: u32,
    option_type: OptionType,
    expansion_factor: Decimal,
    contraction_factor: Decimal,
    switch_cost: Decimal,
    switch_value_ratio: Decimal,
) -> BinomialResult {
    let dt = t / Decimal::from(n);
    let u = exp_decimal(sigma * sqrt_decimal(dt));
    let d = Decimal::ONE / u;
    let exp_rq_dt = exp_decimal((r - q) * dt);
    let disc = exp_decimal(-r * dt);
    let p_up = if u == d {
        dec!(0.5)
    } else {
        (exp_rq_dt - d) / (u - d)
    };
    let p_down = Decimal::ONE - p_up;

    let size = (n + 1) as usize;

    // Build terminal asset prices using net exponent to avoid overflow
    let mut prices = Vec::with_capacity(size);
    for i in 0..size {
        let ups = i as u32;
        let downs = n - ups;
        prices.push(node_price(s, u, ups, downs));
    }

    // Compute terminal payoffs
    let mut values: Vec<Decimal> = prices
        .iter()
        .map(|&price| {
            terminal_payoff(
                price,
                k,
                option_type,
                expansion_factor,
                contraction_factor,
                switch_cost,
                switch_value_ratio,
            )
        })
        .collect();

    // Track exercise boundaries and capture step-1/step-2 values for Greeks
    let mut exercise_boundaries = Vec::new();
    let mut step1_values: Option<(Decimal, Decimal)> = None;
    let mut step2_mid_value: Option<Decimal> = None;

    // Backward induction
    for step in (0..n).rev() {
        let step_size = (step + 1) as usize;
        let mut boundary_found = false;
        let mut boundary_value = Decimal::ZERO;

        for i in 0..step_size {
            let continuation = safe_mul(
                disc,
                safe_mul(p_up, values[i + 1]) + safe_mul(p_down, values[i]),
            );
            let ups = i as u32;
            let downs = step - ups;
            let np = node_price(s, u, ups, downs);
            let exercise = exercise_payoff(
                np,
                k,
                option_type,
                expansion_factor,
                contraction_factor,
                switch_cost,
                switch_value_ratio,
            );

            if exercise > continuation {
                values[i] = exercise;
                if !boundary_found {
                    boundary_found = true;
                    boundary_value = np;
                }
            } else {
                values[i] = continuation;
            }
        }

        // Capture step 2 middle value (node index 1 at step 2 = 1 up, 1 down)
        if step == 2 && step_size >= 2 {
            step2_mid_value = Some(values[1]);
        }

        // Capture step 1 values after backward induction reaches step 1
        if step == 1 {
            step1_values = Some((values[0], values[1]));
        }

        if boundary_found {
            exercise_boundaries.push(ExerciseBoundary {
                time_step: step,
                threshold_value: boundary_value,
            });
        }
    }

    exercise_boundaries.reverse();

    // For n=1, step1 values are terminal payoffs before final backward step
    let (val_down, val_up) = step1_values.unwrap_or((values[0], values[0]));
    let price_up = s * u;
    let price_down = s * d;
    let price_mid2 = s; // s * u * d = s since d = 1/u
    let val_mid2 = step2_mid_value
        .unwrap_or_else(|| safe_mul(disc, safe_mul(p_up, val_up) + safe_mul(p_down, val_down)));

    let option_value = values[0];

    // Check if immediate exercise is optimal at root
    let immediate = exercise_payoff(
        s,
        k,
        option_type,
        expansion_factor,
        contraction_factor,
        switch_cost,
        switch_value_ratio,
    );
    let early_exercise_at_root = immediate >= option_value && immediate > Decimal::ZERO;

    BinomialResult {
        option_value,
        price_step1: (price_down, price_up),
        value_step1: (val_down, val_up),
        price_step2_mid: price_mid2,
        value_step2_mid: val_mid2,
        exercise_boundaries,
        early_exercise_at_root,
    }
}

/// Terminal (and exercise) payoff for each option type.
fn terminal_payoff(
    price: Decimal,
    k: Decimal,
    option_type: OptionType,
    expansion_factor: Decimal,
    contraction_factor: Decimal,
    switch_cost: Decimal,
    switch_value_ratio: Decimal,
) -> Decimal {
    exercise_payoff(
        price,
        k,
        option_type,
        expansion_factor,
        contraction_factor,
        switch_cost,
        switch_value_ratio,
    )
}

/// Exercise value at any node. Returns the payoff from exercising the option.
/// Uses safe multiplication to avoid overflow panics.
fn exercise_payoff(
    price: Decimal,
    k: Decimal,
    option_type: OptionType,
    expansion_factor: Decimal,
    contraction_factor: Decimal,
    switch_cost: Decimal,
    switch_value_ratio: Decimal,
) -> Decimal {
    match option_type {
        OptionType::Defer => {
            // American call: max(S - K, 0)
            (price - k).max(Decimal::ZERO)
        }
        OptionType::Expand => {
            // Exercise: get expanded project value minus cost
            // Payoff = max(S * expansion_factor - K, S)
            let expanded = safe_mul(price, expansion_factor) - k;
            expanded.max(price)
        }
        OptionType::Abandon => {
            // American put: max(K, S) where K = salvage value
            k.max(price)
        }
        OptionType::Contract => {
            // Contract: reduce scale, save some cost
            // Payoff = max(S * cf + K, S) where K = savings from contracting
            let contracted = safe_mul(price, contraction_factor) + k;
            contracted.max(price)
        }
        OptionType::Switch => {
            // Switch between modes
            // Payoff = max(S * switch_ratio - switch_cost, S)
            let switched = safe_mul(price, switch_value_ratio) - switch_cost;
            switched.max(price)
        }
        OptionType::Compound => {
            // Simplified compound option: treated as defer (option on option)
            (price - k).max(Decimal::ZERO)
        }
    }
}

// ---------------------------------------------------------------------------
// Greeks via finite differences
// ---------------------------------------------------------------------------

struct Greeks {
    delta: Decimal,
    gamma: Decimal,
    theta: Decimal,
    vega: Decimal,
}

fn compute_greeks(
    input: &RealOptionInput,
    _base_result: &BinomialResult,
    base_value: Decimal,
) -> Greeks {
    let s = input.underlying_value;

    // All Greeks via central finite differences (reprice the full tree)
    let ds = s * dec!(0.01); // 1% bump

    // Delta: dV/dS
    let mut up_input = input.clone();
    up_input.underlying_value = s + ds;
    let v_up = run_binomial(&up_input);

    let mut down_input = input.clone();
    down_input.underlying_value = s - ds;
    let v_down = run_binomial(&down_input);

    let delta = if ds != Decimal::ZERO {
        (v_up - v_down) / (dec!(2) * ds)
    } else {
        Decimal::ZERO
    };

    // Gamma: d2V/dS2
    let gamma = if ds != Decimal::ZERO {
        (v_up - dec!(2) * base_value + v_down) / (ds * ds)
    } else {
        Decimal::ZERO
    };

    // Theta: dV/dt (time decay)
    let dt_shift = dec!(0.01);
    let theta = if input.time_to_expiry > dt_shift {
        let mut shifted_input = input.clone();
        shifted_input.time_to_expiry = input.time_to_expiry - dt_shift;
        let shifted = run_binomial(&shifted_input);
        (shifted - base_value) / dt_shift
    } else {
        Decimal::ZERO
    };

    // Vega: dV/dsigma
    let vol_shift = dec!(0.01);
    let vega = if input.volatility > Decimal::ZERO {
        let mut vol_up_input = input.clone();
        vol_up_input.volatility = input.volatility + vol_shift;
        let v_up_vol = run_binomial(&vol_up_input);

        let mut vol_down_input = input.clone();
        vol_down_input.volatility = (input.volatility - vol_shift).max(dec!(0.001));
        let v_down_vol = run_binomial(&vol_down_input);

        let actual_shift = vol_up_input.volatility - vol_down_input.volatility;
        if actual_shift != Decimal::ZERO {
            (v_up_vol - v_down_vol) / actual_shift
        } else {
            Decimal::ZERO
        }
    } else {
        Decimal::ZERO
    };

    Greeks {
        delta,
        gamma,
        theta,
        vega,
    }
}

/// Run the binomial tree and return just the option value (for Greek finite differences).
fn run_binomial(input: &RealOptionInput) -> Decimal {
    let s = input.underlying_value;
    let k = input.exercise_price;
    let sigma = input.volatility;
    let r = input.risk_free_rate;
    let q = input.dividend_yield.unwrap_or(Decimal::ZERO);
    let t = input.time_to_expiry;
    let n = input.steps;
    let ef = input.expansion_factor.unwrap_or(dec!(1.5));
    let cf = input.contraction_factor.unwrap_or(dec!(0.5));
    let sc = input.switch_cost.unwrap_or(Decimal::ZERO);
    let sr = input.switch_value_ratio.unwrap_or(Decimal::ONE);

    if sigma <= Decimal::ZERO || t <= Decimal::ZERO || n == 0 {
        return Decimal::ZERO;
    }

    let result = binomial_tree(s, k, sigma, r, q, t, n, input.option_type, ef, cf, sc, sr);
    result.option_value
}

// ---------------------------------------------------------------------------
// Breakeven volatility via bisection
// ---------------------------------------------------------------------------

fn find_breakeven_volatility(input: &RealOptionInput) -> Decimal {
    // Find the volatility where option premium = 0
    // i.e., option_value = max(static_npv, 0)
    let s = input.underlying_value;
    let k = input.exercise_price;

    let static_npv = compute_static_npv(s, k, input.option_type, input.expansion_factor);
    let target = static_npv.max(Decimal::ZERO);

    let mut lo = dec!(0.001);
    let mut hi = dec!(2.0);

    // Use fewer steps for the breakeven search to avoid overflow with high vol
    let search_steps = input.steps.min(30);

    // Bisection search: 25 iterations
    for _ in 0..25 {
        let mid = (lo + hi) / dec!(2);
        let mut test_input = input.clone();
        test_input.volatility = mid;
        test_input.steps = search_steps;
        let val = run_binomial(&test_input);

        if val > target {
            hi = mid;
        } else {
            lo = mid;
        }
    }
    (lo + hi) / dec!(2)
}

/// Compute static NPV depending on option type.
fn compute_static_npv(
    s: Decimal,
    k: Decimal,
    option_type: OptionType,
    expansion_factor: Option<Decimal>,
) -> Decimal {
    match option_type {
        OptionType::Defer | OptionType::Compound => s - k,
        OptionType::Expand => {
            let ef = expansion_factor.unwrap_or(dec!(1.5));
            s * ef - k - s // net gain from expanding minus what you already have
        }
        OptionType::Abandon => k - s, // salvage value minus current value
        OptionType::Contract => {
            // Savings from contracting (exercise_price) minus lost value
            k - s * (Decimal::ONE - dec!(0.5)) // approximate
        }
        OptionType::Switch => s - k, // simplified
    }
}

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

pub fn value_real_option(
    input: &RealOptionInput,
) -> CorpFinanceResult<ComputationOutput<RealOptionOutput>> {
    let start = Instant::now();
    validate_input(input)?;

    let s = input.underlying_value;
    let k = input.exercise_price;
    let sigma = input.volatility;
    let r = input.risk_free_rate;
    let q = input.dividend_yield.unwrap_or(Decimal::ZERO);
    let t = input.time_to_expiry;
    let n = input.steps;
    let ef = input.expansion_factor.unwrap_or(dec!(1.5));
    let cf = input.contraction_factor.unwrap_or(dec!(0.5));
    let sc = input.switch_cost.unwrap_or(Decimal::ZERO);
    let sr = input.switch_value_ratio.unwrap_or(Decimal::ONE);

    // Handle zero volatility edge case
    if sigma == Decimal::ZERO {
        let immediate = exercise_payoff(s, k, input.option_type, ef, cf, sc, sr);
        let static_npv = compute_static_npv(s, k, input.option_type, input.expansion_factor);
        let option_premium = immediate - static_npv.max(Decimal::ZERO);
        let output = RealOptionOutput {
            option_value: immediate,
            expanded_npv: static_npv + immediate.max(Decimal::ZERO),
            static_npv,
            option_premium: option_premium.max(Decimal::ZERO),
            optimal_exercise_boundary: vec![],
            delta: Decimal::ZERO,
            gamma: Decimal::ZERO,
            theta: Decimal::ZERO,
            vega: Decimal::ZERO,
            early_exercise_optimal: immediate > Decimal::ZERO,
            breakeven_volatility: Decimal::ZERO,
        };
        let elapsed = start.elapsed().as_micros() as u64;
        return Ok(with_metadata(
            "Real option valuation (zero volatility)",
            &serde_json::json!({"volatility": "0", "option_type": format!("{:?}", input.option_type)}),
            vec!["Zero volatility: option value equals deterministic exercise value".into()],
            elapsed,
            output,
        ));
    }

    let binom = binomial_tree(s, k, sigma, r, q, t, n, input.option_type, ef, cf, sc, sr);

    let option_value = binom.option_value;
    let static_npv = compute_static_npv(s, k, input.option_type, input.expansion_factor);
    let expanded_npv = static_npv + option_value.max(Decimal::ZERO);
    let option_premium = option_value - static_npv.max(Decimal::ZERO);

    // Compute Greeks
    let greeks = compute_greeks(input, &binom, option_value);

    // Breakeven volatility
    let breakeven_vol = find_breakeven_volatility(input);

    let output = RealOptionOutput {
        option_value,
        expanded_npv,
        static_npv,
        option_premium: option_premium.max(Decimal::ZERO),
        optimal_exercise_boundary: binom.exercise_boundaries,
        delta: greeks.delta,
        gamma: greeks.gamma,
        theta: greeks.theta,
        vega: greeks.vega,
        early_exercise_optimal: binom.early_exercise_at_root,
        breakeven_volatility: breakeven_vol,
    };

    let mut warnings = Vec::new();
    if option_value < Decimal::ZERO {
        warnings.push("Option value is negative; check inputs".into());
    }
    if binom.early_exercise_at_root {
        warnings.push("Immediate exercise appears optimal".into());
    }

    let assumptions = serde_json::json!({
        "model": "CRR Binomial Tree",
        "option_type": format!("{:?}", input.option_type),
        "steps": n,
        "risk_free_rate": r.to_string(),
        "volatility": sigma.to_string(),
        "dividend_yield": q.to_string(),
        "time_to_expiry": t.to_string(),
    });

    let elapsed = start.elapsed().as_micros() as u64;
    Ok(with_metadata(
        "CRR Binomial Tree — Real Option Valuation",
        &assumptions,
        warnings,
        elapsed,
        output,
    ))
}

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

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

    fn approx_eq(a: Decimal, b: Decimal, tol: Decimal) -> bool {
        abs_decimal(a - b) < tol
    }

    fn default_defer_input() -> RealOptionInput {
        RealOptionInput {
            option_type: OptionType::Defer,
            underlying_value: dec!(100),
            exercise_price: dec!(105),
            volatility: dec!(0.30),
            risk_free_rate: dec!(0.05),
            time_to_expiry: dec!(1),
            steps: 100,
            dividend_yield: None,
            expansion_factor: None,
            contraction_factor: None,
            switch_cost: None,
            switch_value_ratio: None,
        }
    }

    // -----------------------------------------------------------------------
    // Defer option tests (American call)
    // -----------------------------------------------------------------------

    #[test]
    fn test_defer_option_positive_value() {
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value > Decimal::ZERO,
            "Defer option should have positive value, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_defer_option_exceeds_intrinsic() {
        // ITM defer option: value should exceed intrinsic
        let input = RealOptionInput {
            underlying_value: dec!(120),
            exercise_price: dec!(100),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        let intrinsic = dec!(120) - dec!(100); // 20
        assert!(
            result.result.option_value >= intrinsic - dec!(0.1),
            "Defer option {} should be >= intrinsic {}",
            result.result.option_value,
            intrinsic
        );
    }

    #[test]
    fn test_defer_deep_otm() {
        // Deep OTM: underlying << exercise price
        let input = RealOptionInput {
            underlying_value: dec!(50),
            exercise_price: dec!(200),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value < dec!(5),
            "Deep OTM defer option should be near zero, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_defer_deep_itm() {
        // Deep ITM: underlying >> exercise price
        let input = RealOptionInput {
            underlying_value: dec!(200),
            exercise_price: dec!(50),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        let intrinsic = dec!(200) - dec!(50);
        assert!(
            result.result.option_value >= intrinsic - dec!(1),
            "Deep ITM defer option {} should be close to intrinsic {}",
            result.result.option_value,
            intrinsic
        );
    }

    #[test]
    fn test_defer_higher_vol_higher_value() {
        let low_vol = RealOptionInput {
            volatility: dec!(0.15),
            ..default_defer_input()
        };
        let high_vol = RealOptionInput {
            volatility: dec!(0.50),
            ..default_defer_input()
        };
        let low = value_real_option(&low_vol).unwrap().result.option_value;
        let high = value_real_option(&high_vol).unwrap().result.option_value;
        assert!(
            high > low,
            "Higher vol {} should give higher option value than lower vol {}",
            high,
            low
        );
    }

    #[test]
    fn test_defer_longer_expiry_higher_value() {
        let short = RealOptionInput {
            time_to_expiry: dec!(0.5),
            ..default_defer_input()
        };
        let long = RealOptionInput {
            time_to_expiry: dec!(3),
            ..default_defer_input()
        };
        let v_short = value_real_option(&short).unwrap().result.option_value;
        let v_long = value_real_option(&long).unwrap().result.option_value;
        assert!(
            v_long > v_short,
            "Longer expiry {} should give higher value than shorter {}",
            v_long,
            v_short
        );
    }

    #[test]
    fn test_defer_with_dividend_yield() {
        // Dividend yield reduces option value (cost of waiting)
        let no_div = default_defer_input();
        let with_div = RealOptionInput {
            dividend_yield: Some(dec!(0.05)),
            ..default_defer_input()
        };
        let v_no = value_real_option(&no_div).unwrap().result.option_value;
        let v_div = value_real_option(&with_div).unwrap().result.option_value;
        assert!(
            v_no > v_div,
            "No-dividend value {} should exceed dividend value {}",
            v_no,
            v_div
        );
    }

    #[test]
    fn test_defer_expanded_npv() {
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        let r = &result.result;
        // expanded_npv = static_npv + option_value
        let expected = r.static_npv + r.option_value.max(Decimal::ZERO);
        assert!(
            approx_eq(r.expanded_npv, expected, dec!(0.01)),
            "expanded_npv {} should equal static_npv + option_value = {}",
            r.expanded_npv,
            expected
        );
    }

    // -----------------------------------------------------------------------
    // Abandon option tests (American put)
    // -----------------------------------------------------------------------

    #[test]
    fn test_abandon_option_positive() {
        let input = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(80),
            exercise_price: dec!(100), // salvage value
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // Abandon option: max(K, continuation) where K=salvage
        // When S < K, the abandon option has intrinsic value
        assert!(
            result.result.option_value > Decimal::ZERO,
            "Abandon option should have positive value, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_abandon_deep_itm() {
        // Asset value well below salvage: option should be close to salvage
        let input = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(30),
            exercise_price: dec!(100), // salvage value
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // With S=30, K=100, the abandon option value >= 100 (salvage)
        assert!(
            result.result.option_value >= dec!(99),
            "Deep ITM abandon value {} should be near salvage 100",
            result.result.option_value
        );
    }

    #[test]
    fn test_abandon_otm() {
        // Asset value well above salvage: option is OTM but still has time value
        let input = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(200),
            exercise_price: dec!(100), // salvage
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // Option value = max(K, continuation). When S >> K, value ~ S (hold project)
        assert!(
            result.result.option_value >= dec!(190),
            "OTM abandon option value {} should be near underlying 200",
            result.result.option_value
        );
    }

    #[test]
    fn test_abandon_higher_salvage_higher_value() {
        let low_salvage = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(100),
            exercise_price: dec!(80),
            ..default_defer_input()
        };
        let high_salvage = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(100),
            exercise_price: dec!(120),
            ..default_defer_input()
        };
        let v_low = value_real_option(&low_salvage).unwrap().result.option_value;
        let v_high = value_real_option(&high_salvage)
            .unwrap()
            .result
            .option_value;
        assert!(
            v_high > v_low,
            "Higher salvage value {} should give higher option value {}",
            v_high,
            v_low
        );
    }

    // -----------------------------------------------------------------------
    // Expand option tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_expand_option_positive() {
        let input = RealOptionInput {
            option_type: OptionType::Expand,
            underlying_value: dec!(100),
            exercise_price: dec!(40), // cost to expand
            expansion_factor: Some(dec!(1.5)),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value > Decimal::ZERO,
            "Expand option should have positive value, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_expand_higher_factor_higher_value() {
        let small = RealOptionInput {
            option_type: OptionType::Expand,
            exercise_price: dec!(40),
            expansion_factor: Some(dec!(1.2)),
            ..default_defer_input()
        };
        let big = RealOptionInput {
            option_type: OptionType::Expand,
            exercise_price: dec!(40),
            expansion_factor: Some(dec!(2.0)),
            ..default_defer_input()
        };
        let v_small = value_real_option(&small).unwrap().result.option_value;
        let v_big = value_real_option(&big).unwrap().result.option_value;
        assert!(
            v_big > v_small,
            "Bigger expansion {} should have higher value than smaller {}",
            v_big,
            v_small
        );
    }

    #[test]
    fn test_expand_expensive_exercise() {
        // Very expensive expansion: less valuable
        let cheap = RealOptionInput {
            option_type: OptionType::Expand,
            exercise_price: dec!(10),
            expansion_factor: Some(dec!(1.5)),
            ..default_defer_input()
        };
        let expensive = RealOptionInput {
            option_type: OptionType::Expand,
            exercise_price: dec!(200),
            expansion_factor: Some(dec!(1.5)),
            ..default_defer_input()
        };
        let v_cheap = value_real_option(&cheap).unwrap().result.option_value;
        let v_expensive = value_real_option(&expensive).unwrap().result.option_value;
        assert!(
            v_cheap > v_expensive,
            "Cheap expansion {} should be more valuable than expensive {}",
            v_cheap,
            v_expensive
        );
    }

    // -----------------------------------------------------------------------
    // Contract option tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_contract_option_positive() {
        let input = RealOptionInput {
            option_type: OptionType::Contract,
            underlying_value: dec!(100),
            exercise_price: dec!(30), // savings from contracting
            contraction_factor: Some(dec!(0.5)),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value > Decimal::ZERO,
            "Contract option should have positive value, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_contract_lower_factor_different_value() {
        // A lower contraction factor means more contraction (keeping less of project)
        let mild = RealOptionInput {
            option_type: OptionType::Contract,
            exercise_price: dec!(20),
            contraction_factor: Some(dec!(0.8)),
            ..default_defer_input()
        };
        let severe = RealOptionInput {
            option_type: OptionType::Contract,
            exercise_price: dec!(20),
            contraction_factor: Some(dec!(0.3)),
            ..default_defer_input()
        };
        let v_mild = value_real_option(&mild).unwrap().result.option_value;
        let v_severe = value_real_option(&severe).unwrap().result.option_value;
        // Both should be valid, values depend on savings vs lost capacity
        assert!(
            v_mild > Decimal::ZERO,
            "Mild contraction should be positive"
        );
        assert!(
            v_severe > Decimal::ZERO,
            "Severe contraction should be positive"
        );
    }

    // -----------------------------------------------------------------------
    // Switch option tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_switch_option_positive() {
        let input = RealOptionInput {
            option_type: OptionType::Switch,
            underlying_value: dec!(100),
            exercise_price: dec!(100), // not used directly for switch payoff
            switch_cost: Some(dec!(10)),
            switch_value_ratio: Some(dec!(1.3)),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value > Decimal::ZERO,
            "Switch option should have positive value, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_switch_higher_ratio_higher_value() {
        let low = RealOptionInput {
            option_type: OptionType::Switch,
            exercise_price: dec!(100),
            switch_cost: Some(dec!(10)),
            switch_value_ratio: Some(dec!(1.1)),
            ..default_defer_input()
        };
        let high = RealOptionInput {
            option_type: OptionType::Switch,
            exercise_price: dec!(100),
            switch_cost: Some(dec!(10)),
            switch_value_ratio: Some(dec!(1.8)),
            ..default_defer_input()
        };
        let v_low = value_real_option(&low).unwrap().result.option_value;
        let v_high = value_real_option(&high).unwrap().result.option_value;
        assert!(
            v_high > v_low,
            "Higher switch ratio {} should beat lower {}",
            v_high,
            v_low
        );
    }

    #[test]
    fn test_switch_higher_cost_lower_value() {
        let cheap = RealOptionInput {
            option_type: OptionType::Switch,
            exercise_price: dec!(100),
            switch_cost: Some(dec!(5)),
            switch_value_ratio: Some(dec!(1.3)),
            ..default_defer_input()
        };
        let expensive = RealOptionInput {
            option_type: OptionType::Switch,
            exercise_price: dec!(100),
            switch_cost: Some(dec!(50)),
            switch_value_ratio: Some(dec!(1.3)),
            ..default_defer_input()
        };
        let v_cheap = value_real_option(&cheap).unwrap().result.option_value;
        let v_expensive = value_real_option(&expensive).unwrap().result.option_value;
        assert!(
            v_cheap > v_expensive,
            "Cheaper switching {} should be more valuable than expensive {}",
            v_cheap,
            v_expensive
        );
    }

    // -----------------------------------------------------------------------
    // Compound option tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_compound_option_positive() {
        let input = RealOptionInput {
            option_type: OptionType::Compound,
            underlying_value: dec!(100),
            exercise_price: dec!(90),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value > Decimal::ZERO,
            "Compound option should have positive value, got {}",
            result.result.option_value
        );
    }

    // -----------------------------------------------------------------------
    // Greeks tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_delta_positive_for_defer() {
        // Defer (call-like) should have positive delta
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.delta > Decimal::ZERO,
            "Defer delta should be positive, got {}",
            result.result.delta
        );
    }

    #[test]
    fn test_delta_bounded() {
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.delta <= Decimal::ONE + dec!(0.01),
            "Delta {} should be <= 1",
            result.result.delta
        );
    }

    #[test]
    fn test_vega_positive() {
        // Vega should be positive (more vol = more option value)
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.vega > Decimal::ZERO,
            "Vega should be positive, got {}",
            result.result.vega
        );
    }

    #[test]
    fn test_theta_negative_for_defer() {
        // Time decay: theta should be negative for typical defer option
        let input = RealOptionInput {
            underlying_value: dec!(100),
            exercise_price: dec!(100),
            volatility: dec!(0.30),
            time_to_expiry: dec!(2),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.theta < Decimal::ZERO,
            "Theta should be negative (time decay), got {}",
            result.result.theta
        );
    }

    #[test]
    fn test_greeks_approximate_bs_for_european_defer() {
        // With many steps and short expiry, defer option Greeks should
        // approximate Black-Scholes call Greeks
        let input = RealOptionInput {
            underlying_value: dec!(100),
            exercise_price: dec!(100),
            volatility: dec!(0.20),
            risk_free_rate: dec!(0.05),
            time_to_expiry: dec!(1),
            steps: 200,
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        let delta = result.result.delta;
        // BS ATM call delta ~ 0.57-0.64 (with r=5%, vol=20%, T=1)
        assert!(
            delta > dec!(0.45) && delta < dec!(0.80),
            "Delta {} should approximate BS call delta ~0.60",
            delta
        );
    }

    // -----------------------------------------------------------------------
    // Exercise boundary tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_exercise_boundary_exists_for_abandon() {
        let input = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(80),
            exercise_price: dec!(100),
            steps: 50,
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // For an American put (abandon), there should be exercise boundaries
        // when S is low enough
        assert!(
            !result.result.optimal_exercise_boundary.is_empty(),
            "Abandon option should have exercise boundaries"
        );
    }

    #[test]
    fn test_exercise_boundary_time_steps_valid() {
        let input = RealOptionInput {
            option_type: OptionType::Abandon,
            underlying_value: dec!(80),
            exercise_price: dec!(100),
            steps: 50,
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        for boundary in &result.result.optimal_exercise_boundary {
            assert!(
                boundary.time_step < 50,
                "Boundary time_step {} should be < steps 50",
                boundary.time_step
            );
            assert!(
                boundary.threshold_value > Decimal::ZERO,
                "Boundary threshold should be positive"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Breakeven volatility tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_breakeven_volatility_positive() {
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.breakeven_volatility > Decimal::ZERO,
            "Breakeven vol should be positive, got {}",
            result.result.breakeven_volatility
        );
    }

    #[test]
    fn test_breakeven_volatility_below_input_vol() {
        // For an ATM option with vol=30%, breakeven should be well below 30%
        let input = RealOptionInput {
            underlying_value: dec!(100),
            exercise_price: dec!(100),
            volatility: dec!(0.30),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // Breakeven vol is the vol where premium = 0
        // For ATM, this is very low (essentially zero premium at zero vol)
        assert!(
            result.result.breakeven_volatility < dec!(0.30),
            "Breakeven vol {} should be less than input vol 0.30",
            result.result.breakeven_volatility
        );
    }

    // -----------------------------------------------------------------------
    // Zero volatility edge case
    // -----------------------------------------------------------------------

    #[test]
    fn test_zero_volatility_defer() {
        let input = RealOptionInput {
            volatility: Decimal::ZERO,
            underlying_value: dec!(110),
            exercise_price: dec!(100),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // With zero vol, option value = max(S-K, 0) = 10
        assert!(
            approx_eq(result.result.option_value, dec!(10), dec!(1)),
            "Zero vol defer option {} should be near intrinsic 10",
            result.result.option_value
        );
    }

    #[test]
    fn test_zero_volatility_abandon() {
        let input = RealOptionInput {
            option_type: OptionType::Abandon,
            volatility: Decimal::ZERO,
            underlying_value: dec!(80),
            exercise_price: dec!(100),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // With zero vol, abandon value = max(K, S) = 100
        assert!(
            approx_eq(result.result.option_value, dec!(100), dec!(1)),
            "Zero vol abandon option {} should be salvage 100",
            result.result.option_value
        );
    }

    // -----------------------------------------------------------------------
    // Validation error tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_invalid_negative_underlying() {
        let input = RealOptionInput {
            underlying_value: dec!(-100),
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_invalid_zero_exercise() {
        let input = RealOptionInput {
            exercise_price: Decimal::ZERO,
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_invalid_negative_vol() {
        let input = RealOptionInput {
            volatility: dec!(-0.20),
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_invalid_zero_expiry() {
        let input = RealOptionInput {
            time_to_expiry: Decimal::ZERO,
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_invalid_zero_steps() {
        let input = RealOptionInput {
            steps: 0,
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_expand_missing_factor() {
        let input = RealOptionInput {
            option_type: OptionType::Expand,
            expansion_factor: None,
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_expand_factor_must_exceed_one() {
        let input = RealOptionInput {
            option_type: OptionType::Expand,
            expansion_factor: Some(dec!(0.8)),
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_contract_missing_factor() {
        let input = RealOptionInput {
            option_type: OptionType::Contract,
            contraction_factor: None,
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_contract_factor_out_of_range() {
        let input = RealOptionInput {
            option_type: OptionType::Contract,
            contraction_factor: Some(dec!(1.5)),
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_switch_missing_cost() {
        let input = RealOptionInput {
            option_type: OptionType::Switch,
            switch_cost: None,
            switch_value_ratio: Some(dec!(1.3)),
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    #[test]
    fn test_switch_missing_ratio() {
        let input = RealOptionInput {
            option_type: OptionType::Switch,
            switch_cost: Some(dec!(10)),
            switch_value_ratio: None,
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    // -----------------------------------------------------------------------
    // Metadata and output format tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_metadata_populated() {
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(!result.methodology.is_empty());
        assert!(!result.metadata.version.is_empty());
        assert_eq!(result.metadata.precision, "rust_decimal_128bit");
    }

    #[test]
    fn test_static_npv_calculation() {
        let input = RealOptionInput {
            underlying_value: dec!(120),
            exercise_price: dec!(100),
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        // For defer: static_npv = S - K = 120 - 100 = 20
        assert!(
            approx_eq(result.result.static_npv, dec!(20), dec!(1)),
            "Static NPV {} should be ~20",
            result.result.static_npv
        );
    }

    #[test]
    fn test_option_premium_non_negative() {
        let input = default_defer_input();
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_premium >= Decimal::ZERO,
            "Option premium should be non-negative, got {}",
            result.result.option_premium
        );
    }

    #[test]
    fn test_very_high_volatility() {
        let input = RealOptionInput {
            volatility: dec!(2.0),
            steps: 50, // fewer steps for speed with high vol
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value > Decimal::ZERO,
            "High vol option should have positive value, got {}",
            result.result.option_value
        );
    }

    #[test]
    fn test_single_step() {
        // Minimum viable tree: 1 step
        let input = RealOptionInput {
            steps: 1,
            ..default_defer_input()
        };
        let result = value_real_option(&input).unwrap();
        assert!(
            result.result.option_value >= Decimal::ZERO,
            "Single-step tree should produce non-negative value"
        );
    }

    #[test]
    fn test_convergence_with_more_steps() {
        // Value should stabilize with more steps
        let v50 = value_real_option(&RealOptionInput {
            steps: 50,
            ..default_defer_input()
        })
        .unwrap()
        .result
        .option_value;

        let v200 = value_real_option(&RealOptionInput {
            steps: 200,
            ..default_defer_input()
        })
        .unwrap()
        .result
        .option_value;

        // The values should be reasonably close (converging)
        assert!(
            approx_eq(v50, v200, dec!(2)),
            "50 steps ({}) and 200 steps ({}) should be within 2 of each other",
            v50,
            v200
        );
    }

    #[test]
    fn test_negative_dividend_yield_rejected() {
        let input = RealOptionInput {
            dividend_yield: Some(dec!(-0.05)),
            ..default_defer_input()
        };
        assert!(value_real_option(&input).is_err());
    }

    // -----------------------------------------------------------------------
    // Math helper tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_exp_decimal_basic() {
        assert!(approx_eq(exp_decimal(dec!(0)), dec!(1), dec!(0.0001)));
        assert!(approx_eq(exp_decimal(dec!(1)), dec!(2.71828), dec!(0.001)));
    }

    #[test]
    fn test_sqrt_decimal_basic() {
        assert!(approx_eq(sqrt_decimal(dec!(4)), dec!(2), dec!(0.0001)));
        assert!(approx_eq(sqrt_decimal(dec!(9)), dec!(3), dec!(0.0001)));
        assert!(approx_eq(sqrt_decimal(dec!(0.25)), dec!(0.5), dec!(0.001)));
    }

    #[test]
    fn test_ln_decimal_basic() {
        assert!(approx_eq(ln_decimal(dec!(1)), dec!(0), dec!(0.0001)));
        assert!(approx_eq(
            ln_decimal(dec!(2.71828182845)),
            dec!(1),
            dec!(0.001)
        ));
    }

    #[test]
    fn test_pow_decimal_basic() {
        assert_eq!(pow_decimal(dec!(2), 0), dec!(1));
        assert_eq!(pow_decimal(dec!(2), 3), dec!(8));
        assert_eq!(pow_decimal(dec!(3), 4), dec!(81));
    }
}