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
use crate::compat::Instant;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

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

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

/// Method for loss reserve estimation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReservingMethod {
    ChainLadder,
    BornhuetterFerguson,
    Both,
}

/// Cumulative claims triangle.
///
/// Each row corresponds to an accident year.  Each column is a development
/// period.  Values are cumulative paid (or incurred) claims.  `None` marks
/// cells that have not yet developed (the lower-right portion of the
/// triangle).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaimsTriangle {
    /// Accident years, e.g. [2018, 2019, 2020, 2021, 2022].
    pub accident_years: Vec<u32>,
    /// Development periods in years, e.g. [1, 2, 3, 4, 5].
    pub development_periods: Vec<u32>,
    /// Cumulative claim amounts.  `values[i][j]` is the cumulative claims
    /// for accident year `i` at development period `j`.  `None` for cells
    /// that have not yet emerged.
    pub values: Vec<Vec<Option<Decimal>>>,
}

/// Top-level input for loss reserve estimation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReservingInput {
    /// Line of business, e.g. "Auto Liability".
    pub line_of_business: String,
    /// Cumulative claims triangle.
    pub triangle: ClaimsTriangle,
    /// Reserving method to apply.
    pub method: ReservingMethod,
    /// Earned premium by accident year (required for Bornhuetter-Ferguson).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub earned_premium: Option<Vec<Decimal>>,
    /// A priori expected loss ratio (required for BF, e.g. 0.65 = 65%).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expected_loss_ratio: Option<Decimal>,
    /// Tail factor for development beyond the last observed column.
    /// Defaults to 1.0 (no tail) when `None`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tail_factor: Option<Decimal>,
    /// Discount rate for present-valuing reserves.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub discount_rate: Option<Decimal>,
}

// ---------------------------------------------------------------------------
// Result types
// ---------------------------------------------------------------------------

/// A single age-to-age development factor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DevelopmentFactor {
    pub from_period: u32,
    pub to_period: u32,
    /// Weighted average link ratio.
    pub factor: Decimal,
    /// Volume-weighted factor: sum(C_{i,j+1}) / sum(C_{i,j}).
    pub volume_weighted: Decimal,
}

/// Ultimate and IBNR for one accident year.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccidentYearUltimate {
    pub accident_year: u32,
    /// Latest diagonal value (paid to date).
    pub paid_to_date: Decimal,
    /// Developed to ultimate.
    pub ultimate: Decimal,
    /// IBNR = ultimate - paid_to_date.
    pub ibnr: Decimal,
}

/// IBNR for one accident year.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccidentYearIbnr {
    pub accident_year: u32,
    pub ibnr: Decimal,
}

/// Chain-ladder specific results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChainLadderResult {
    pub development_factors: Vec<DevelopmentFactor>,
    /// Cumulative development factors to ultimate.
    pub cumulative_factors: Vec<Decimal>,
    /// Fully developed triangle (all cells filled).
    pub completed_triangle: Vec<Vec<Decimal>>,
    pub ultimates: Vec<AccidentYearUltimate>,
    pub ibnr_by_year: Vec<AccidentYearIbnr>,
}

/// Bornhuetter-Ferguson specific results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BfResult {
    pub development_factors: Vec<DevelopmentFactor>,
    pub cumulative_factors: Vec<Decimal>,
    /// Expected ultimates = earned_premium * ELR.
    pub expected_ultimates: Vec<Decimal>,
    pub bf_ultimates: Vec<AccidentYearUltimate>,
    pub bf_ibnr_by_year: Vec<AccidentYearIbnr>,
}

/// Selected reserve for one accident year (possibly present-valued).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccidentYearReserve {
    pub accident_year: u32,
    pub method: String,
    pub reserve: Decimal,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub present_value_reserve: Option<Decimal>,
}

/// High-level summary statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReserveSummary {
    pub total_ibnr: Decimal,
    pub total_paid: Decimal,
    pub total_ultimate: Decimal,
    /// total_ultimate / total_premium (if premium provided).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub overall_loss_ratio: Option<Decimal>,
    /// actual_ultimate / expected_ultimate (>1 means under-reserved a priori).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub adequacy_ratio: Option<Decimal>,
    /// Present value of total reserves.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub present_value_total: Option<Decimal>,
}

/// Top-level reserving output.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReservingOutput {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chain_ladder: Option<ChainLadderResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bornhuetter_ferguson: Option<BfResult>,
    pub selected_reserves: Vec<AccidentYearReserve>,
    pub summary: ReserveSummary,
}

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

/// Estimate loss reserves using chain-ladder and/or Bornhuetter-Ferguson
/// methods.
///
/// The function validates the claims triangle, computes age-to-age
/// development factors, projects ultimate losses, and derives IBNR
/// reserves.  When `method == Both`, immature accident years (<=50%
/// developed) use BF and mature years use chain-ladder.
pub fn estimate_reserves(
    input: &ReservingInput,
) -> CorpFinanceResult<ComputationOutput<ReservingOutput>> {
    let start = Instant::now();
    let mut warnings: Vec<String> = Vec::new();

    // ------------------------------------------------------------------
    // 1. Validate
    // ------------------------------------------------------------------
    validate_input(input)?;

    let tri = &input.triangle;
    let n_years = tri.accident_years.len();
    let n_periods = tri.development_periods.len();
    let tail = input.tail_factor.unwrap_or(Decimal::ONE);

    // ------------------------------------------------------------------
    // 2. Compute development factors (shared by CL and BF)
    // ------------------------------------------------------------------
    let dev_factors = compute_development_factors(tri, n_years, n_periods)?;
    let cum_factors = compute_cumulative_factors(&dev_factors, n_periods, tail);

    // ------------------------------------------------------------------
    // 3. Determine latest diagonal (paid-to-date per accident year)
    // ------------------------------------------------------------------
    let latest_diag = latest_diagonal(tri, n_years, n_periods);

    // ------------------------------------------------------------------
    // 4. Chain-ladder
    // ------------------------------------------------------------------
    let run_cl = matches!(
        input.method,
        ReservingMethod::ChainLadder | ReservingMethod::Both
    );
    let cl_result = if run_cl {
        Some(run_chain_ladder(
            tri,
            &dev_factors,
            &cum_factors,
            &latest_diag,
            n_years,
            n_periods,
        ))
    } else {
        None
    };

    // ------------------------------------------------------------------
    // 5. Bornhuetter-Ferguson
    // ------------------------------------------------------------------
    let run_bf = matches!(
        input.method,
        ReservingMethod::BornhuetterFerguson | ReservingMethod::Both
    );
    let bf_result = if run_bf {
        let premium =
            input
                .earned_premium
                .as_ref()
                .ok_or_else(|| CorpFinanceError::InvalidInput {
                    field: "earned_premium".into(),
                    reason: "Earned premium is required for Bornhuetter-Ferguson".into(),
                })?;
        let elr = input
            .expected_loss_ratio
            .ok_or_else(|| CorpFinanceError::InvalidInput {
                field: "expected_loss_ratio".into(),
                reason: "Expected loss ratio is required for Bornhuetter-Ferguson".into(),
            })?;
        Some(run_bornhuetter_ferguson(
            tri,
            &dev_factors,
            &cum_factors,
            &latest_diag,
            premium,
            elr,
            n_years,
            n_periods,
        ))
    } else {
        None
    };

    // ------------------------------------------------------------------
    // 6. Select reserves per accident year
    // ------------------------------------------------------------------
    let selected = select_reserves(
        &input.method,
        &cl_result,
        &bf_result,
        tri,
        input.discount_rate,
        n_years,
        n_periods,
        &mut warnings,
    );

    // ------------------------------------------------------------------
    // 7. Summary
    // ------------------------------------------------------------------
    let total_ibnr = selected.iter().map(|r| r.reserve).sum::<Decimal>();
    let total_paid = latest_diag.iter().map(|(_, v)| *v).sum::<Decimal>();
    let total_ultimate = total_paid + total_ibnr;

    let total_premium: Option<Decimal> = input
        .earned_premium
        .as_ref()
        .map(|p| p.iter().copied().sum());
    let overall_loss_ratio = total_premium
        .filter(|p| *p > Decimal::ZERO)
        .map(|p| total_ultimate / p);

    let adequacy_ratio = if run_bf {
        let expected_total: Decimal = input
            .earned_premium
            .as_ref()
            .map(|p| {
                p.iter().copied().sum::<Decimal>()
                    * input.expected_loss_ratio.unwrap_or(Decimal::ZERO)
            })
            .unwrap_or(Decimal::ZERO);
        if expected_total > Decimal::ZERO {
            Some(total_ultimate / expected_total)
        } else {
            None
        }
    } else {
        None
    };

    let present_value_total = if input.discount_rate.is_some() {
        Some(
            selected
                .iter()
                .filter_map(|r| r.present_value_reserve)
                .sum(),
        )
    } else {
        None
    };

    let summary = ReserveSummary {
        total_ibnr,
        total_paid,
        total_ultimate,
        overall_loss_ratio,
        adequacy_ratio,
        present_value_total,
    };

    let output = ReservingOutput {
        chain_ladder: cl_result,
        bornhuetter_ferguson: bf_result,
        selected_reserves: selected,
        summary,
    };

    let elapsed = start.elapsed().as_micros() as u64;
    let assumptions = serde_json::json!({
        "line_of_business": input.line_of_business,
        "method": format!("{:?}", input.method),
        "tail_factor": tail.to_string(),
        "discount_rate": input.discount_rate.map(|d| d.to_string()),
        "accident_years": tri.accident_years,
        "development_periods": tri.development_periods,
    });

    Ok(with_metadata(
        "Loss Reserving: Chain-Ladder / Bornhuetter-Ferguson",
        &assumptions,
        warnings,
        elapsed,
        output,
    ))
}

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

fn validate_input(input: &ReservingInput) -> CorpFinanceResult<()> {
    let tri = &input.triangle;

    if tri.accident_years.is_empty() {
        return Err(CorpFinanceError::InvalidInput {
            field: "accident_years".into(),
            reason: "At least one accident year is required".into(),
        });
    }
    if tri.development_periods.is_empty() {
        return Err(CorpFinanceError::InvalidInput {
            field: "development_periods".into(),
            reason: "At least one development period is required".into(),
        });
    }
    if tri.values.len() != tri.accident_years.len() {
        return Err(CorpFinanceError::InvalidInput {
            field: "triangle.values".into(),
            reason: format!(
                "Number of rows ({}) must equal number of accident years ({})",
                tri.values.len(),
                tri.accident_years.len()
            ),
        });
    }
    for (i, row) in tri.values.iter().enumerate() {
        if row.len() != tri.development_periods.len() {
            return Err(CorpFinanceError::InvalidInput {
                field: "triangle.values".into(),
                reason: format!(
                    "Row {} has {} columns but {} development periods declared",
                    i,
                    row.len(),
                    tri.development_periods.len()
                ),
            });
        }
    }

    // Every row must have at least one observed (Some) value
    for (i, row) in tri.values.iter().enumerate() {
        if row.iter().all(|v| v.is_none()) {
            return Err(CorpFinanceError::InvalidInput {
                field: "triangle.values".into(),
                reason: format!(
                    "Accident year {} (row {}) has no observed values",
                    tri.accident_years[i], i
                ),
            });
        }
    }

    // The first row (oldest accident year) must have all values present
    // (it defines the fully developed baseline).
    // Relaxed: at least the first value must be present for every row.
    if tri.values[0][0].is_none() {
        return Err(CorpFinanceError::InvalidInput {
            field: "triangle.values".into(),
            reason: "First cell of the triangle (oldest year, first period) must be present".into(),
        });
    }

    // BF-specific: premium count must match accident years
    if matches!(
        input.method,
        ReservingMethod::BornhuetterFerguson | ReservingMethod::Both
    ) {
        if let Some(ref premium) = input.earned_premium {
            if premium.len() != tri.accident_years.len() {
                return Err(CorpFinanceError::InvalidInput {
                    field: "earned_premium".into(),
                    reason: format!(
                        "Earned premium count ({}) must match accident year count ({})",
                        premium.len(),
                        tri.accident_years.len()
                    ),
                });
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Development factors
// ---------------------------------------------------------------------------

/// Compute age-to-age (link) development factors using volume-weighted
/// averages.
fn compute_development_factors(
    tri: &ClaimsTriangle,
    n_years: usize,
    n_periods: usize,
) -> CorpFinanceResult<Vec<DevelopmentFactor>> {
    let mut factors = Vec::with_capacity(n_periods.saturating_sub(1));

    for j in 0..n_periods.saturating_sub(1) {
        let mut sum_next = Decimal::ZERO;
        let mut sum_curr = Decimal::ZERO;

        for i in 0..n_years {
            if let (Some(curr), Some(next)) = (tri.values[i][j], tri.values[i][j + 1]) {
                sum_curr += curr;
                sum_next += next;
            }
        }

        let volume_weighted = if sum_curr > Decimal::ZERO {
            sum_next / sum_curr
        } else {
            Decimal::ONE
        };

        factors.push(DevelopmentFactor {
            from_period: tri.development_periods[j],
            to_period: tri.development_periods[j + 1],
            factor: volume_weighted,
            volume_weighted,
        });
    }

    Ok(factors)
}

/// Compute cumulative development factors to ultimate.
///
/// CDF_j = product(f_k for k = j..last) * tail_factor
fn compute_cumulative_factors(
    dev_factors: &[DevelopmentFactor],
    n_periods: usize,
    tail: Decimal,
) -> Vec<Decimal> {
    // cum_factors[j] is the CDF from development period j to ultimate.
    // The last period's CDF is just the tail factor.
    let mut cum = vec![Decimal::ZERO; n_periods];

    // CDF for the last column = tail_factor
    cum[n_periods - 1] = tail;

    // Work backwards
    for j in (0..n_periods - 1).rev() {
        cum[j] = dev_factors[j].volume_weighted * cum[j + 1];
    }

    cum
}

/// For each accident year, find the latest observed value (the diagonal).
fn latest_diagonal(tri: &ClaimsTriangle, n_years: usize, n_periods: usize) -> Vec<(u32, Decimal)> {
    let mut diag = Vec::with_capacity(n_years);
    for i in 0..n_years {
        let mut latest_val = Decimal::ZERO;
        for j in (0..n_periods).rev() {
            if let Some(v) = tri.values[i][j] {
                latest_val = v;
                break;
            }
        }
        diag.push((tri.accident_years[i], latest_val));
    }
    diag
}

/// Find the column index of the latest observed value for a given row.
fn latest_col_index(tri: &ClaimsTriangle, row: usize, n_periods: usize) -> usize {
    for j in (0..n_periods).rev() {
        if tri.values[row][j].is_some() {
            return j;
        }
    }
    0
}

// ---------------------------------------------------------------------------
// Chain-Ladder
// ---------------------------------------------------------------------------

fn run_chain_ladder(
    tri: &ClaimsTriangle,
    dev_factors: &[DevelopmentFactor],
    cum_factors: &[Decimal],
    latest_diag: &[(u32, Decimal)],
    n_years: usize,
    n_periods: usize,
) -> ChainLadderResult {
    // Build the completed triangle
    let mut completed: Vec<Vec<Decimal>> = Vec::with_capacity(n_years);
    for i in 0..n_years {
        let mut row = Vec::with_capacity(n_periods);
        // Copy observed values
        for j in 0..n_periods {
            if let Some(v) = tri.values[i][j] {
                row.push(v);
            } else {
                // Project: take the previous column value and multiply by
                // the link ratio
                let prev = row[j - 1];
                let factor = dev_factors[j - 1].volume_weighted;
                row.push(prev * factor);
            }
        }
        completed.push(row);
    }

    // Ultimates
    let mut ultimates = Vec::with_capacity(n_years);
    let mut ibnr_by_year = Vec::with_capacity(n_years);

    #[allow(clippy::needless_range_loop)]
    for i in 0..n_years {
        let last_col = latest_col_index(tri, i, n_periods);
        let paid = latest_diag[i].1;
        let ultimate = paid * cum_factors[last_col];
        let ibnr = ultimate - paid;

        ultimates.push(AccidentYearUltimate {
            accident_year: tri.accident_years[i],
            paid_to_date: paid,
            ultimate,
            ibnr,
        });
        ibnr_by_year.push(AccidentYearIbnr {
            accident_year: tri.accident_years[i],
            ibnr,
        });
    }

    ChainLadderResult {
        development_factors: dev_factors.to_vec(),
        cumulative_factors: cum_factors.to_vec(),
        completed_triangle: completed,
        ultimates,
        ibnr_by_year,
    }
}

// ---------------------------------------------------------------------------
// Bornhuetter-Ferguson
// ---------------------------------------------------------------------------

fn run_bornhuetter_ferguson(
    tri: &ClaimsTriangle,
    dev_factors: &[DevelopmentFactor],
    cum_factors: &[Decimal],
    latest_diag: &[(u32, Decimal)],
    premium: &[Decimal],
    elr: Decimal,
    n_years: usize,
    n_periods: usize,
) -> BfResult {
    let expected_ultimates: Vec<Decimal> = premium.iter().map(|p| *p * elr).collect();

    let mut bf_ultimates = Vec::with_capacity(n_years);
    let mut bf_ibnr_by_year = Vec::with_capacity(n_years);

    for i in 0..n_years {
        let last_col = latest_col_index(tri, i, n_periods);
        let cdf = cum_factors[last_col];
        let paid = latest_diag[i].1;

        // % unreported = 1 - 1/CDF
        let pct_unreported = if cdf > Decimal::ZERO {
            Decimal::ONE - Decimal::ONE / cdf
        } else {
            Decimal::ZERO
        };

        let bf_ibnr = expected_ultimates[i] * pct_unreported;
        let bf_ult = paid + bf_ibnr;

        bf_ultimates.push(AccidentYearUltimate {
            accident_year: tri.accident_years[i],
            paid_to_date: paid,
            ultimate: bf_ult,
            ibnr: bf_ibnr,
        });
        bf_ibnr_by_year.push(AccidentYearIbnr {
            accident_year: tri.accident_years[i],
            ibnr: bf_ibnr,
        });
    }

    BfResult {
        development_factors: dev_factors.to_vec(),
        cumulative_factors: cum_factors.to_vec(),
        expected_ultimates,
        bf_ultimates,
        bf_ibnr_by_year,
    }
}

// ---------------------------------------------------------------------------
// Reserve selection & present-valuing
// ---------------------------------------------------------------------------

fn select_reserves(
    method: &ReservingMethod,
    cl: &Option<ChainLadderResult>,
    bf: &Option<BfResult>,
    tri: &ClaimsTriangle,
    discount_rate: Option<Decimal>,
    n_years: usize,
    n_periods: usize,
    warnings: &mut Vec<String>,
) -> Vec<AccidentYearReserve> {
    let mut reserves = Vec::with_capacity(n_years);

    for i in 0..n_years {
        let last_col = latest_col_index(tri, i, n_periods);

        // Maturity measured as the fraction of development periods
        // completed.  This is the standard actuarial heuristic for
        // choosing between CL and BF: years with fewer than half their
        // development periods observed are considered immature.
        let pct_periods_complete = if n_periods > 0 {
            Decimal::from((last_col + 1) as u32) / Decimal::from(n_periods as u32)
        } else {
            Decimal::ONE
        };

        let (reserve, method_name) = match method {
            ReservingMethod::ChainLadder => {
                let cl_ref = cl.as_ref().unwrap();
                (cl_ref.ibnr_by_year[i].ibnr, "ChainLadder")
            }
            ReservingMethod::BornhuetterFerguson => {
                let bf_ref = bf.as_ref().unwrap();
                (bf_ref.bf_ibnr_by_year[i].ibnr, "BornhuetterFerguson")
            }
            ReservingMethod::Both => {
                let cl_ref = cl.as_ref().unwrap();
                let bf_ref = bf.as_ref().unwrap();
                // Use BF for immature years (<=50% developed), CL for mature
                if pct_periods_complete <= dec!(0.50) {
                    (bf_ref.bf_ibnr_by_year[i].ibnr, "BornhuetterFerguson")
                } else {
                    (cl_ref.ibnr_by_year[i].ibnr, "ChainLadder")
                }
            }
        };

        // Present value discount
        let pv_reserve = discount_rate.map(|rate| {
            let remaining_periods = n_periods.saturating_sub(last_col + 1);
            let avg_years = Decimal::from(remaining_periods as u32) / dec!(2);
            discount_value(reserve, rate, avg_years)
        });

        if reserve < Decimal::ZERO {
            warnings.push(format!(
                "Negative IBNR ({}) for accident year {} — this may indicate \
                 over-reserving or data issues",
                reserve, tri.accident_years[i]
            ));
        }

        reserves.push(AccidentYearReserve {
            accident_year: tri.accident_years[i],
            method: method_name.to_string(),
            reserve,
            present_value_reserve: pv_reserve,
        });
    }

    reserves
}

/// Discount a value by (1 + rate)^years using iterative multiplication.
fn discount_value(value: Decimal, rate: Decimal, years: Decimal) -> Decimal {
    if years <= Decimal::ZERO || rate <= Decimal::ZERO {
        return value;
    }

    // For fractional years we split into integer + fractional parts.
    // Integer part: iterative multiplication. Fractional part: linear
    // interpolation between floor and ceil discount factors.
    let whole = years.trunc();
    let frac = years - whole;
    let whole_u32 = whole.to_string().parse::<u32>().unwrap_or(0);

    let base = Decimal::ONE + rate;

    // Compute base^whole iteratively
    let mut factor_whole = Decimal::ONE;
    for _ in 0..whole_u32 {
        factor_whole *= base;
    }

    // Linear interpolation for fractional year
    let factor = if frac > Decimal::ZERO {
        let factor_next = factor_whole * base;
        factor_whole + (factor_next - factor_whole) * frac
    } else {
        factor_whole
    };

    if factor > Decimal::ZERO {
        value / factor
    } else {
        value
    }
}

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

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

    // -----------------------------------------------------------------------
    // Test helpers
    // -----------------------------------------------------------------------

    /// Standard 5x5 cumulative paid claims triangle.
    ///
    /// Accident years: 2018-2022
    /// Development periods: 1-5
    ///
    /// ```text
    ///        Dev1      Dev2      Dev3      Dev4      Dev5
    /// 2018   1000      1500      1700      1750      1760
    /// 2019   1100      1650      1870      1920      None
    /// 2020   1200      1800      2040      None      None
    /// 2021   1300      1950      None      None      None
    /// 2022   1400      None      None      None      None
    /// ```
    fn triangle_5x5() -> ClaimsTriangle {
        ClaimsTriangle {
            accident_years: vec![2018, 2019, 2020, 2021, 2022],
            development_periods: vec![1, 2, 3, 4, 5],
            values: vec![
                vec![
                    Some(dec!(1000)),
                    Some(dec!(1500)),
                    Some(dec!(1700)),
                    Some(dec!(1750)),
                    Some(dec!(1760)),
                ],
                vec![
                    Some(dec!(1100)),
                    Some(dec!(1650)),
                    Some(dec!(1870)),
                    Some(dec!(1920)),
                    None,
                ],
                vec![
                    Some(dec!(1200)),
                    Some(dec!(1800)),
                    Some(dec!(2040)),
                    None,
                    None,
                ],
                vec![Some(dec!(1300)), Some(dec!(1950)), None, None, None],
                vec![Some(dec!(1400)), None, None, None, None],
            ],
        }
    }

    /// 3x3 minimal triangle.
    fn triangle_3x3() -> ClaimsTriangle {
        ClaimsTriangle {
            accident_years: vec![2020, 2021, 2022],
            development_periods: vec![1, 2, 3],
            values: vec![
                vec![Some(dec!(500)), Some(dec!(750)), Some(dec!(800))],
                vec![Some(dec!(600)), Some(dec!(900)), None],
                vec![Some(dec!(700)), None, None],
            ],
        }
    }

    /// 4x4 triangle.
    fn triangle_4x4() -> ClaimsTriangle {
        ClaimsTriangle {
            accident_years: vec![2019, 2020, 2021, 2022],
            development_periods: vec![1, 2, 3, 4],
            values: vec![
                vec![
                    Some(dec!(800)),
                    Some(dec!(1200)),
                    Some(dec!(1350)),
                    Some(dec!(1380)),
                ],
                vec![Some(dec!(900)), Some(dec!(1350)), Some(dec!(1520)), None],
                vec![Some(dec!(1000)), Some(dec!(1500)), None, None],
                vec![Some(dec!(1100)), None, None, None],
            ],
        }
    }

    fn standard_cl_input() -> ReservingInput {
        ReservingInput {
            line_of_business: "Auto Liability".to_string(),
            triangle: triangle_5x5(),
            method: ReservingMethod::ChainLadder,
            earned_premium: None,
            expected_loss_ratio: None,
            tail_factor: None,
            discount_rate: None,
        }
    }

    fn standard_bf_input() -> ReservingInput {
        ReservingInput {
            line_of_business: "Workers Comp".to_string(),
            triangle: triangle_5x5(),
            method: ReservingMethod::BornhuetterFerguson,
            earned_premium: Some(vec![
                dec!(2700),
                dec!(2900),
                dec!(3100),
                dec!(3300),
                dec!(3500),
            ]),
            expected_loss_ratio: Some(dec!(0.65)),
            tail_factor: None,
            discount_rate: None,
        }
    }

    fn standard_both_input() -> ReservingInput {
        ReservingInput {
            line_of_business: "General Liability".to_string(),
            triangle: triangle_5x5(),
            method: ReservingMethod::Both,
            earned_premium: Some(vec![
                dec!(2700),
                dec!(2900),
                dec!(3100),
                dec!(3300),
                dec!(3500),
            ]),
            expected_loss_ratio: Some(dec!(0.65)),
            tail_factor: None,
            discount_rate: None,
        }
    }

    // -----------------------------------------------------------------------
    // Test 1: Chain-ladder development factors on 5x5 triangle
    // -----------------------------------------------------------------------
    #[test]
    fn test_cl_development_factors_5x5() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        // There should be 4 development factors (periods 1->2, 2->3, 3->4, 4->5)
        assert_eq!(cl.development_factors.len(), 4);

        // Factor 1->2: sum of Dev2 values / sum of Dev1 values
        // (1500+1650+1800+1950) / (1000+1100+1200+1300) = 6900/4600
        let expected_f1 = dec!(6900) / dec!(4600);
        assert_eq!(
            cl.development_factors[0].volume_weighted, expected_f1,
            "Factor 1->2 should be 6900/4600 = {}",
            expected_f1
        );

        // Factor 2->3: (1700+1870+2040) / (1500+1650+1800) = 5610/4950
        let expected_f2 = dec!(5610) / dec!(4950);
        assert_eq!(
            cl.development_factors[1].volume_weighted, expected_f2,
            "Factor 2->3 should be 5610/4950 = {}",
            expected_f2
        );

        // Factor 3->4: (1750+1920) / (1700+1870) = 3670/3570
        let expected_f3 = dec!(3670) / dec!(3570);
        assert_eq!(
            cl.development_factors[2].volume_weighted, expected_f3,
            "Factor 3->4 should be 3670/3570 = {}",
            expected_f3
        );

        // Factor 4->5: 1760/1750
        let expected_f4 = dec!(1760) / dec!(1750);
        assert_eq!(
            cl.development_factors[3].volume_weighted, expected_f4,
            "Factor 4->5 should be 1760/1750 = {}",
            expected_f4
        );
    }

    // -----------------------------------------------------------------------
    // Test 2: Cumulative factors from link ratios
    // -----------------------------------------------------------------------
    #[test]
    fn test_cl_cumulative_factors() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        // CDF for the last column (period 5) = tail = 1.0 (no tail specified)
        assert_eq!(cl.cumulative_factors[4], Decimal::ONE);

        // CDF for period 4 = f(4->5) * tail = f4 * 1.0
        let f4 = cl.development_factors[3].volume_weighted;
        assert_eq!(cl.cumulative_factors[3], f4);

        // CDF for period 3 = f(3->4) * CDF(4)
        let f3 = cl.development_factors[2].volume_weighted;
        assert_eq!(cl.cumulative_factors[2], f3 * cl.cumulative_factors[3]);

        // CDF for period 1 = product of all link ratios * tail
        let product = cl
            .development_factors
            .iter()
            .map(|d| d.volume_weighted)
            .fold(Decimal::ONE, |acc, f| acc * f);
        assert_eq!(cl.cumulative_factors[0], product);
    }

    // -----------------------------------------------------------------------
    // Test 3: Ultimates for each accident year
    // -----------------------------------------------------------------------
    #[test]
    fn test_cl_ultimates_5x5() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        assert_eq!(cl.ultimates.len(), 5);

        // 2018 (fully developed at col 4, CDF = tail = 1.0 since no tail)
        // Ultimate = 1760 * 1.0 = 1760
        assert_eq!(cl.ultimates[0].paid_to_date, dec!(1760));
        assert_eq!(
            cl.ultimates[0].ultimate,
            dec!(1760) * cl.cumulative_factors[4]
        );

        // 2022 (only col 0, so ultimate = 1400 * CDF[0])
        assert_eq!(cl.ultimates[4].paid_to_date, dec!(1400));
        let expected_ult_2022 = dec!(1400) * cl.cumulative_factors[0];
        assert_eq!(cl.ultimates[4].ultimate, expected_ult_2022);
    }

    // -----------------------------------------------------------------------
    // Test 4: IBNR = ultimate - paid
    // -----------------------------------------------------------------------
    #[test]
    fn test_cl_ibnr_equals_ultimate_minus_paid() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        for ult in &cl.ultimates {
            assert_eq!(
                ult.ibnr,
                ult.ultimate - ult.paid_to_date,
                "IBNR for {} should be ultimate ({}) - paid ({})",
                ult.accident_year,
                ult.ultimate,
                ult.paid_to_date
            );
        }
    }

    // -----------------------------------------------------------------------
    // Test 5: Most recent year has highest IBNR
    // -----------------------------------------------------------------------
    #[test]
    fn test_cl_most_recent_year_highest_ibnr() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        let max_ibnr = cl.ibnr_by_year.iter().max_by_key(|x| x.ibnr).unwrap();

        assert_eq!(
            max_ibnr.accident_year, 2022,
            "Most recent year (2022) should have the highest IBNR, but {} does",
            max_ibnr.accident_year
        );
    }

    // -----------------------------------------------------------------------
    // Test 6: Oldest year (fully developed) has 0 IBNR (no tail)
    // -----------------------------------------------------------------------
    #[test]
    fn test_cl_oldest_year_zero_ibnr() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        // With no tail factor, the oldest year (fully developed) has IBNR = 0
        assert_eq!(
            cl.ibnr_by_year[0].ibnr,
            Decimal::ZERO,
            "Oldest year (2018) should have 0 IBNR with no tail, got {}",
            cl.ibnr_by_year[0].ibnr
        );
    }

    // -----------------------------------------------------------------------
    // Test 7: BF requires premium and ELR
    // -----------------------------------------------------------------------
    #[test]
    fn test_bf_requires_premium() {
        let input = ReservingInput {
            line_of_business: "Test".to_string(),
            triangle: triangle_5x5(),
            method: ReservingMethod::BornhuetterFerguson,
            earned_premium: None,
            expected_loss_ratio: Some(dec!(0.65)),
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input);
        assert!(result.is_err());
        match result.unwrap_err() {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert_eq!(field, "earned_premium");
            }
            other => panic!("Expected InvalidInput for earned_premium, got: {other}"),
        }
    }

    // -----------------------------------------------------------------------
    // Test 8: BF ultimates blend a priori with development
    // -----------------------------------------------------------------------
    #[test]
    fn test_bf_ultimates_blend() {
        let input = standard_bf_input();
        let result = estimate_reserves(&input).unwrap();
        let bf = result.result.bornhuetter_ferguson.as_ref().unwrap();

        // BF ultimate should be between paid-to-date and some reasonable upper bound
        for ult in &bf.bf_ultimates {
            assert!(
                ult.ultimate >= ult.paid_to_date,
                "BF ultimate ({}) should be >= paid_to_date ({}) for year {}",
                ult.ultimate,
                ult.paid_to_date,
                ult.accident_year
            );
        }

        // BF IBNR for each year should be non-negative
        for ibnr in &bf.bf_ibnr_by_year {
            assert!(
                ibnr.ibnr >= Decimal::ZERO,
                "BF IBNR ({}) should be >= 0 for year {}",
                ibnr.ibnr,
                ibnr.accident_year
            );
        }
    }

    // -----------------------------------------------------------------------
    // Test 9: BF immature years closer to expected than CL
    // -----------------------------------------------------------------------
    #[test]
    fn test_bf_immature_closer_to_expected() {
        // Run both methods on the same triangle
        let mut input_both = standard_both_input();
        input_both.method = ReservingMethod::Both;
        let result = estimate_reserves(&input_both).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();
        let bf = result.result.bornhuetter_ferguson.as_ref().unwrap();

        // For the most recent year (2022, only 1 period of development =
        // very immature), BF ultimate should be closer to the expected
        // ultimate (premium * ELR) than CL ultimate.
        let expected_ult_2022 = dec!(3500) * dec!(0.65); // 2275
        let cl_ult_2022 = cl.ultimates[4].ultimate;
        let bf_ult_2022 = bf.bf_ultimates[4].ultimate;

        let cl_diff = (cl_ult_2022 - expected_ult_2022).abs();
        let bf_diff = (bf_ult_2022 - expected_ult_2022).abs();

        assert!(
            bf_diff <= cl_diff,
            "BF ultimate for immature year 2022 ({}) should be closer to expected ({}) \
             than CL ({}). BF diff={}, CL diff={}",
            bf_ult_2022,
            expected_ult_2022,
            cl_ult_2022,
            bf_diff,
            cl_diff
        );
    }

    // -----------------------------------------------------------------------
    // Test 10: BF mature years close to CL result
    // -----------------------------------------------------------------------
    #[test]
    fn test_bf_mature_years_close_to_cl() {
        let input = standard_both_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();
        let bf = result.result.bornhuetter_ferguson.as_ref().unwrap();

        // For the oldest year (2018, fully developed with no tail), both
        // methods should produce the same IBNR (0).
        let cl_ibnr_2018 = cl.ibnr_by_year[0].ibnr;
        let bf_ibnr_2018 = bf.bf_ibnr_by_year[0].ibnr;

        // Both should be zero or very close for a fully developed year
        assert!(
            cl_ibnr_2018.abs() < dec!(0.01),
            "CL IBNR for 2018 should be ~0, got {}",
            cl_ibnr_2018
        );
        assert!(
            bf_ibnr_2018.abs() < dec!(0.01),
            "BF IBNR for 2018 should be ~0, got {}",
            bf_ibnr_2018
        );
    }

    // -----------------------------------------------------------------------
    // Test 11: Both method selects BF for immature, CL for mature
    // -----------------------------------------------------------------------
    #[test]
    fn test_both_method_selection() {
        let input = standard_both_input();
        let result = estimate_reserves(&input).unwrap();
        let selected = &result.result.selected_reserves;

        // The most mature year (2018, fully developed) should use CL
        assert_eq!(
            selected[0].method, "ChainLadder",
            "Oldest year (2018) should use ChainLadder, got {}",
            selected[0].method
        );

        // The most recent year (2022, 1 period = very immature) should use BF
        assert_eq!(
            selected[4].method, "BornhuetterFerguson",
            "Most recent year (2022) should use BornhuetterFerguson, got {}",
            selected[4].method
        );
    }

    // -----------------------------------------------------------------------
    // Test 12: Tail factor increases ultimates
    // -----------------------------------------------------------------------
    #[test]
    fn test_tail_factor_increases_ultimates() {
        let mut input_no_tail = standard_cl_input();
        input_no_tail.tail_factor = None;
        let result_no_tail = estimate_reserves(&input_no_tail).unwrap();
        let cl_no_tail = result_no_tail.result.chain_ladder.as_ref().unwrap();

        let mut input_tail = standard_cl_input();
        input_tail.tail_factor = Some(dec!(1.05));
        let result_tail = estimate_reserves(&input_tail).unwrap();
        let cl_tail = result_tail.result.chain_ladder.as_ref().unwrap();

        // With a tail factor > 1, all ultimates should be higher
        for (no_tail, with_tail) in cl_no_tail.ultimates.iter().zip(cl_tail.ultimates.iter()) {
            assert!(
                with_tail.ultimate >= no_tail.ultimate,
                "Tail factor should increase ultimate for year {}: {} vs {}",
                no_tail.accident_year,
                with_tail.ultimate,
                no_tail.ultimate
            );
        }

        // Total IBNR should be higher with tail
        let ibnr_no_tail: Decimal = cl_no_tail.ibnr_by_year.iter().map(|x| x.ibnr).sum();
        let ibnr_tail: Decimal = cl_tail.ibnr_by_year.iter().map(|x| x.ibnr).sum();
        assert!(
            ibnr_tail > ibnr_no_tail,
            "Total IBNR with tail ({}) should exceed without tail ({})",
            ibnr_tail,
            ibnr_no_tail
        );
    }

    // -----------------------------------------------------------------------
    // Test 13: No tail factor (default 1.0)
    // -----------------------------------------------------------------------
    #[test]
    fn test_no_tail_factor_default() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        // The last cumulative factor should be exactly 1.0 with no tail
        assert_eq!(
            cl.cumulative_factors[4],
            Decimal::ONE,
            "CDF for last period should be 1.0 with no tail, got {}",
            cl.cumulative_factors[4]
        );
    }

    // -----------------------------------------------------------------------
    // Test 14: Present value discount reduces reserves
    // -----------------------------------------------------------------------
    #[test]
    fn test_present_value_discount_reduces_reserves() {
        let mut input = standard_cl_input();
        input.discount_rate = Some(dec!(0.05));
        let result = estimate_reserves(&input).unwrap();

        for r in &result.result.selected_reserves {
            if r.reserve > Decimal::ZERO {
                let pv = r.present_value_reserve.unwrap();
                assert!(
                    pv <= r.reserve,
                    "PV reserve ({}) should be <= nominal reserve ({}) for year {}",
                    pv,
                    r.reserve,
                    r.accident_year
                );
            }
        }

        // Total PV should be less than or equal to total nominal IBNR
        let total_nominal = result.result.summary.total_ibnr;
        let total_pv = result.result.summary.present_value_total.unwrap();
        assert!(
            total_pv <= total_nominal,
            "PV total ({}) should be <= nominal total ({})",
            total_pv,
            total_nominal
        );
    }

    // -----------------------------------------------------------------------
    // Test 15: Loss ratio calculation
    // -----------------------------------------------------------------------
    #[test]
    fn test_loss_ratio_calculation() {
        let mut input = standard_bf_input();
        // Use CL method but supply premium so loss ratio can be computed
        input.method = ReservingMethod::BornhuetterFerguson;
        let result = estimate_reserves(&input).unwrap();
        let summary = &result.result.summary;

        let lr = summary.overall_loss_ratio.unwrap();
        assert!(
            lr > Decimal::ZERO,
            "Loss ratio should be positive, got {}",
            lr
        );
        // Loss ratio = total_ultimate / total_premium
        let total_premium: Decimal = input.earned_premium.as_ref().unwrap().iter().sum();
        let expected_lr = summary.total_ultimate / total_premium;
        assert_eq!(lr, expected_lr);
    }

    // -----------------------------------------------------------------------
    // Test 16: Adequacy ratio
    // -----------------------------------------------------------------------
    #[test]
    fn test_adequacy_ratio() {
        let input = standard_bf_input();
        let result = estimate_reserves(&input).unwrap();
        let summary = &result.result.summary;

        let ar = summary.adequacy_ratio.unwrap();
        assert!(
            ar > Decimal::ZERO,
            "Adequacy ratio should be positive, got {}",
            ar
        );
        // Adequacy ratio = total_ultimate / total_expected
        let total_premium: Decimal = input.earned_premium.as_ref().unwrap().iter().sum();
        let total_expected = total_premium * input.expected_loss_ratio.unwrap();
        let expected_ar = summary.total_ultimate / total_expected;
        assert_eq!(ar, expected_ar);
    }

    // -----------------------------------------------------------------------
    // Test 17: 3x3 minimal triangle
    // -----------------------------------------------------------------------
    #[test]
    fn test_3x3_minimal_triangle() {
        let input = ReservingInput {
            line_of_business: "Property".to_string(),
            triangle: triangle_3x3(),
            method: ReservingMethod::ChainLadder,
            earned_premium: None,
            expected_loss_ratio: None,
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        assert_eq!(cl.development_factors.len(), 2);
        assert_eq!(cl.ultimates.len(), 3);

        // Oldest year should have 0 IBNR (no tail)
        assert_eq!(cl.ibnr_by_year[0].ibnr, Decimal::ZERO);

        // Most recent year should have positive IBNR
        assert!(
            cl.ibnr_by_year[2].ibnr > Decimal::ZERO,
            "Most recent year should have positive IBNR"
        );
    }

    // -----------------------------------------------------------------------
    // Test 18: 4x4 triangle
    // -----------------------------------------------------------------------
    #[test]
    fn test_4x4_triangle() {
        let input = ReservingInput {
            line_of_business: "Marine".to_string(),
            triangle: triangle_4x4(),
            method: ReservingMethod::ChainLadder,
            earned_premium: None,
            expected_loss_ratio: None,
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        assert_eq!(cl.development_factors.len(), 3);
        assert_eq!(cl.ultimates.len(), 4);
        assert_eq!(cl.completed_triangle.len(), 4);

        // All rows of the completed triangle should be fully filled
        for row in &cl.completed_triangle {
            assert_eq!(row.len(), 4);
            for val in row {
                assert!(
                    *val > Decimal::ZERO,
                    "Completed triangle cell should be positive"
                );
            }
        }
    }

    // -----------------------------------------------------------------------
    // Test 19: Triangle with missing values validation
    // -----------------------------------------------------------------------
    #[test]
    fn test_triangle_all_none_row_error() {
        let bad_tri = ClaimsTriangle {
            accident_years: vec![2020, 2021],
            development_periods: vec![1, 2],
            values: vec![
                vec![Some(dec!(100)), Some(dec!(150))],
                vec![None, None], // all None — invalid
            ],
        };
        let input = ReservingInput {
            line_of_business: "Test".to_string(),
            triangle: bad_tri,
            method: ReservingMethod::ChainLadder,
            earned_premium: None,
            expected_loss_ratio: None,
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input);
        assert!(result.is_err());
        match result.unwrap_err() {
            CorpFinanceError::InvalidInput { field, reason } => {
                assert_eq!(field, "triangle.values");
                assert!(
                    reason.contains("no observed values"),
                    "Error should mention missing values: {}",
                    reason
                );
            }
            other => panic!("Expected InvalidInput, got: {other}"),
        }
    }

    // -----------------------------------------------------------------------
    // Test 20: Edge case — single accident year
    // -----------------------------------------------------------------------
    #[test]
    fn test_single_accident_year() {
        let tri = ClaimsTriangle {
            accident_years: vec![2022],
            development_periods: vec![1, 2, 3],
            values: vec![vec![Some(dec!(1000)), Some(dec!(1500)), Some(dec!(1700))]],
        };
        let input = ReservingInput {
            line_of_business: "Test".to_string(),
            triangle: tri,
            method: ReservingMethod::ChainLadder,
            earned_premium: None,
            expected_loss_ratio: None,
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();

        // Single fully-developed year: IBNR = 0 (no tail)
        assert_eq!(cl.ultimates.len(), 1);
        assert_eq!(cl.ultimates[0].ibnr, Decimal::ZERO);
    }

    // -----------------------------------------------------------------------
    // Test 21: Mismatched premium count error
    // -----------------------------------------------------------------------
    #[test]
    fn test_mismatched_premium_count_error() {
        let input = ReservingInput {
            line_of_business: "Test".to_string(),
            triangle: triangle_5x5(),
            method: ReservingMethod::BornhuetterFerguson,
            earned_premium: Some(vec![dec!(1000), dec!(1100), dec!(1200)]), // 3 instead of 5
            expected_loss_ratio: Some(dec!(0.65)),
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input);
        assert!(result.is_err());
        match result.unwrap_err() {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert_eq!(field, "earned_premium");
            }
            other => panic!("Expected InvalidInput for earned_premium, got: {other}"),
        }
    }

    // -----------------------------------------------------------------------
    // Test 22: Summary totals
    // -----------------------------------------------------------------------
    #[test]
    fn test_summary_totals() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        let cl = result.result.chain_ladder.as_ref().unwrap();
        let summary = &result.result.summary;

        // total_ibnr should equal sum of individual IBNRs
        let sum_ibnr: Decimal = cl.ibnr_by_year.iter().map(|x| x.ibnr).sum();
        assert_eq!(
            summary.total_ibnr, sum_ibnr,
            "Summary total_ibnr ({}) should match sum of year IBNRs ({})",
            summary.total_ibnr, sum_ibnr
        );

        // total_paid should equal sum of latest diagonal
        let sum_paid = dec!(1760) + dec!(1920) + dec!(2040) + dec!(1950) + dec!(1400);
        assert_eq!(
            summary.total_paid, sum_paid,
            "Summary total_paid ({}) should match sum of diagonal ({})",
            summary.total_paid, sum_paid
        );

        // total_ultimate = total_paid + total_ibnr
        assert_eq!(
            summary.total_ultimate,
            summary.total_paid + summary.total_ibnr,
            "total_ultimate ({}) should equal total_paid + total_ibnr ({})",
            summary.total_ultimate,
            summary.total_paid + summary.total_ibnr
        );
    }

    // -----------------------------------------------------------------------
    // Test 23: BF requires ELR
    // -----------------------------------------------------------------------
    #[test]
    fn test_bf_requires_elr() {
        let input = ReservingInput {
            line_of_business: "Test".to_string(),
            triangle: triangle_5x5(),
            method: ReservingMethod::BornhuetterFerguson,
            earned_premium: Some(vec![
                dec!(2700),
                dec!(2900),
                dec!(3100),
                dec!(3300),
                dec!(3500),
            ]),
            expected_loss_ratio: None, // missing
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input);
        assert!(result.is_err());
        match result.unwrap_err() {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert_eq!(field, "expected_loss_ratio");
            }
            other => panic!("Expected InvalidInput for expected_loss_ratio, got: {other}"),
        }
    }

    // -----------------------------------------------------------------------
    // Test 24: Metadata is populated
    // -----------------------------------------------------------------------
    #[test]
    fn test_metadata_populated() {
        let input = standard_cl_input();
        let result = estimate_reserves(&input).unwrap();
        assert!(!result.methodology.is_empty());
        assert!(result.methodology.contains("Chain-Ladder"));
        assert_eq!(result.metadata.precision, "rust_decimal_128bit");
    }

    // -----------------------------------------------------------------------
    // Test 25: Row/column dimension mismatch error
    // -----------------------------------------------------------------------
    #[test]
    fn test_row_column_mismatch_error() {
        let bad_tri = ClaimsTriangle {
            accident_years: vec![2020, 2021],
            development_periods: vec![1, 2, 3],
            values: vec![
                vec![Some(dec!(100)), Some(dec!(150)), Some(dec!(170))],
                vec![Some(dec!(110)), Some(dec!(165))], // 2 cols instead of 3
            ],
        };
        let input = ReservingInput {
            line_of_business: "Test".to_string(),
            triangle: bad_tri,
            method: ReservingMethod::ChainLadder,
            earned_premium: None,
            expected_loss_ratio: None,
            tail_factor: None,
            discount_rate: None,
        };
        let result = estimate_reserves(&input);
        assert!(result.is_err());
    }
}