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
//! Logistic regression scorecard analytics.
//!
//! Covers:
//! 1. **Weight of Evidence (WoE)** -- ln(good_rate / bad_rate) per bin.
//! 2. **Information Value (IV)** -- predictive power of each variable.
//! 3. **Scorecard Points** -- transform WoE into additive score.
//! 4. **Gini Coefficient** -- 2*AUC - 1 from sorted predicted/actual pairs.
//! 5. **KS Statistic** -- max |CDF_good - CDF_bad| across thresholds.
//!
//! All arithmetic uses `rust_decimal::Decimal`. No `f64`.

use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::CorpFinanceResult;

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

/// Natural logarithm via Taylor series around 1. ln(x) for x > 0.
/// Uses the identity: ln(x) = 2 * sum_{k=0..N} (1/(2k+1)) * ((x-1)/(x+1))^(2k+1)
fn decimal_ln(x: Decimal) -> Decimal {
    if x <= Decimal::ZERO {
        return Decimal::ZERO;
    }
    // Reduce: factor out powers of e ~ 2.718281828
    // Use ln(x) = ln(x/2^n) + n*ln(2) to bring x into [0.5, 2.0]
    let ln2 = dec!(0.6931471805599453);
    let mut val = x;
    let mut adjust = Decimal::ZERO;
    while val > dec!(2.0) {
        val /= dec!(2);
        adjust += ln2;
    }
    while val < dec!(0.5) {
        val *= dec!(2);
        adjust -= ln2;
    }
    // Taylor series: ln(val) = 2 * sum_{k=0..40} (1/(2k+1)) * ((val-1)/(val+1))^(2k+1)
    let z = (val - Decimal::ONE) / (val + Decimal::ONE);
    let z2 = z * z;
    let mut term = z;
    let mut sum = z;
    for k in 1u32..40 {
        term *= z2;
        let denom = Decimal::from(2 * k + 1);
        sum += term / denom;
    }
    dec!(2) * sum + adjust
}

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

/// A single bin in a WoE analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WoeBin {
    /// Lower boundary of the bin (inclusive).
    pub lower: Decimal,
    /// Upper boundary of the bin (exclusive, except last bin).
    pub upper: Decimal,
    /// Number of "good" (non-default) observations in this bin.
    pub good_count: u64,
    /// Number of "bad" (default) observations in this bin.
    pub bad_count: u64,
}

/// Input for scorecard calculation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScorecardInput {
    /// WoE bins for the variable being scored.
    pub bins: Vec<WoeBin>,
    /// Target base score (e.g. 600).
    pub target_score: Decimal,
    /// Target odds (good:bad ratio at the target score, e.g. 50 means 50:1).
    pub target_odds: Decimal,
    /// Points to double the odds (PDO), e.g. 20.
    pub pdo: Decimal,
    /// Predicted-vs-actual pairs for Gini and KS (predicted probability, actual 0/1).
    pub predictions: Vec<PredictionPair>,
}

/// A single predicted/actual pair for discrimination metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionPair {
    /// Predicted probability of default.
    pub predicted: Decimal,
    /// Actual outcome: 0 = good, 1 = bad.
    pub actual: u8,
}

/// Per-bin WoE and IV detail.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BinResult {
    /// Lower boundary.
    pub lower: Decimal,
    /// Upper boundary.
    pub upper: Decimal,
    /// Weight of Evidence for this bin.
    pub woe: Decimal,
    /// Information Value contribution for this bin.
    pub iv: Decimal,
    /// Scorecard points for this bin.
    pub points: Decimal,
}

/// IV strength classification.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IvStrength {
    Useless,
    Weak,
    Medium,
    Strong,
    Suspicious,
}

impl std::fmt::Display for IvStrength {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IvStrength::Useless => write!(f, "Useless"),
            IvStrength::Weak => write!(f, "Weak"),
            IvStrength::Medium => write!(f, "Medium"),
            IvStrength::Strong => write!(f, "Strong"),
            IvStrength::Suspicious => write!(f, "Suspicious"),
        }
    }
}

/// Output of the scorecard calculation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScorecardOutput {
    /// Per-bin results with WoE, IV, and scorecard points.
    pub bin_results: Vec<BinResult>,
    /// Total Information Value across all bins.
    pub total_iv: Decimal,
    /// IV strength classification.
    pub iv_strength: String,
    /// Gini coefficient (2*AUC - 1).
    pub gini: Decimal,
    /// KS statistic: max |CDF_good - CDF_bad|.
    pub ks_statistic: Decimal,
    /// Factor used in scorecard point calculation.
    pub factor: Decimal,
    /// Offset used in scorecard point calculation.
    pub offset: Decimal,
}

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

/// Calculate scorecard analytics: WoE, IV, scorecard points, Gini, KS.
pub fn calculate_scorecard(input: &ScorecardInput) -> CorpFinanceResult<ScorecardOutput> {
    validate_scorecard_input(input)?;

    let ln2 = dec!(0.6931471805599453);

    // Factor and offset for scorecard points
    let factor = input.pdo / ln2;
    let offset = input.target_score - factor * decimal_ln(input.target_odds);
    let n_bins = Decimal::from(input.bins.len() as u64);

    // Total goods and bads
    let total_good: u64 = input.bins.iter().map(|b| b.good_count).sum();
    let total_bad: u64 = input.bins.iter().map(|b| b.bad_count).sum();
    let total_good_d = Decimal::from(total_good);
    let total_bad_d = Decimal::from(total_bad);

    let mut bin_results = Vec::with_capacity(input.bins.len());
    let mut total_iv = Decimal::ZERO;

    for bin in &input.bins {
        let good_rate = if total_good == 0 {
            Decimal::ZERO
        } else {
            Decimal::from(bin.good_count) / total_good_d
        };
        let bad_rate = if total_bad == 0 {
            Decimal::ZERO
        } else {
            Decimal::from(bin.bad_count) / total_bad_d
        };

        // Avoid division by zero / ln(0) by flooring rates at a small epsilon
        let eps = dec!(0.0001);
        let safe_good = if good_rate < eps { eps } else { good_rate };
        let safe_bad = if bad_rate < eps { eps } else { bad_rate };

        // WoE = ln(good_rate / bad_rate)
        let woe = decimal_ln(safe_good / safe_bad);

        // IV_i = (good_rate - bad_rate) * WoE
        let iv_i = (safe_good - safe_bad) * woe;

        // Scorecard points: -(WoE * factor + offset / n)
        let points = -(woe * factor + offset / n_bins);

        total_iv += iv_i;

        bin_results.push(BinResult {
            lower: bin.lower,
            upper: bin.upper,
            woe,
            iv: iv_i,
            points,
        });
    }

    // IV strength classification
    let iv_strength = classify_iv(total_iv);

    // Gini and KS from predictions
    let (gini, ks_statistic) = if input.predictions.is_empty() {
        (Decimal::ZERO, Decimal::ZERO)
    } else {
        let gini_val = calculate_gini(&input.predictions)?;
        let ks_val = calculate_ks(&input.predictions)?;
        (gini_val, ks_val)
    };

    Ok(ScorecardOutput {
        bin_results,
        total_iv,
        iv_strength: iv_strength.to_string(),
        gini,
        ks_statistic,
        factor,
        offset,
    })
}

// ---------------------------------------------------------------------------
// IV classification
// ---------------------------------------------------------------------------

fn classify_iv(iv: Decimal) -> IvStrength {
    if iv < dec!(0.02) {
        IvStrength::Useless
    } else if iv < dec!(0.1) {
        IvStrength::Weak
    } else if iv < dec!(0.3) {
        IvStrength::Medium
    } else if iv < dec!(0.5) {
        IvStrength::Strong
    } else {
        IvStrength::Suspicious
    }
}

// ---------------------------------------------------------------------------
// Gini = 2*AUC - 1
// ---------------------------------------------------------------------------

fn calculate_gini(predictions: &[PredictionPair]) -> CorpFinanceResult<Decimal> {
    // Sort by predicted descending
    let mut sorted: Vec<(Decimal, u8)> = predictions
        .iter()
        .map(|p| (p.predicted, p.actual))
        .collect();
    sorted.sort_by(|a, b| b.0.cmp(&a.0));

    let total_bad: u64 = sorted.iter().filter(|(_, a)| *a == 1).count() as u64;
    let total_good: u64 = sorted.iter().filter(|(_, a)| *a == 0).count() as u64;

    if total_bad == 0 || total_good == 0 {
        return Ok(Decimal::ZERO);
    }

    let total_bad_d = Decimal::from(total_bad);
    let total_good_d = Decimal::from(total_good);

    // AUC via trapezoidal: walk through sorted predictions
    let mut auc = Decimal::ZERO;
    let mut tp = Decimal::ZERO;
    let mut fp = Decimal::ZERO;
    let mut prev_tp = Decimal::ZERO;
    let mut prev_fp = Decimal::ZERO;

    let mut i = 0usize;
    while i < sorted.len() {
        let current_score = sorted[i].0;
        // Process all tied scores
        while i < sorted.len() && sorted[i].0 == current_score {
            if sorted[i].1 == 1 {
                tp += Decimal::ONE;
            } else {
                fp += Decimal::ONE;
            }
            i += 1;
        }
        // Trapezoidal rule
        let tpr = tp / total_bad_d;
        let fpr = fp / total_good_d;
        let prev_tpr = prev_tp / total_bad_d;
        let prev_fpr = prev_fp / total_good_d;
        auc += (fpr - prev_fpr) * (tpr + prev_tpr) / dec!(2);
        prev_tp = tp;
        prev_fp = fp;
    }

    let gini = dec!(2) * auc - Decimal::ONE;
    Ok(gini)
}

// ---------------------------------------------------------------------------
// KS statistic = max |CDF_good - CDF_bad|
// ---------------------------------------------------------------------------

fn calculate_ks(predictions: &[PredictionPair]) -> CorpFinanceResult<Decimal> {
    let mut sorted: Vec<(Decimal, u8)> = predictions
        .iter()
        .map(|p| (p.predicted, p.actual))
        .collect();
    sorted.sort_by(|a, b| a.0.cmp(&b.0));

    let total_bad: u64 = sorted.iter().filter(|(_, a)| *a == 1).count() as u64;
    let total_good: u64 = sorted.iter().filter(|(_, a)| *a == 0).count() as u64;

    if total_bad == 0 || total_good == 0 {
        return Ok(Decimal::ZERO);
    }

    let total_bad_d = Decimal::from(total_bad);
    let total_good_d = Decimal::from(total_good);

    let mut cum_good = Decimal::ZERO;
    let mut cum_bad = Decimal::ZERO;
    let mut max_ks = Decimal::ZERO;

    for (_, actual) in &sorted {
        if *actual == 0 {
            cum_good += Decimal::ONE;
        } else {
            cum_bad += Decimal::ONE;
        }
        let cdf_good = cum_good / total_good_d;
        let cdf_bad = cum_bad / total_bad_d;
        let diff = (cdf_good - cdf_bad).abs();
        if diff > max_ks {
            max_ks = diff;
        }
    }

    Ok(max_ks)
}

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

fn validate_scorecard_input(input: &ScorecardInput) -> CorpFinanceResult<()> {
    if input.bins.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one bin is required.".into(),
        ));
    }
    if input.pdo <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "pdo".into(),
            reason: "Points to double odds must be positive.".into(),
        });
    }
    if input.target_odds <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "target_odds".into(),
            reason: "Target odds must be positive.".into(),
        });
    }
    // Validate bin counts
    let total_good: u64 = input.bins.iter().map(|b| b.good_count).sum();
    let total_bad: u64 = input.bins.iter().map(|b| b.bad_count).sum();
    if total_good == 0 && total_bad == 0 {
        return Err(CorpFinanceError::InsufficientData(
            "Bins must contain at least one observation.".into(),
        ));
    }
    // Validate predictions
    for p in &input.predictions {
        if p.actual > 1 {
            return Err(CorpFinanceError::InvalidInput {
                field: "actual".into(),
                reason: "Actual must be 0 or 1.".into(),
            });
        }
        if p.predicted < Decimal::ZERO || p.predicted > Decimal::ONE {
            return Err(CorpFinanceError::InvalidInput {
                field: "predicted".into(),
                reason: "Predicted probability must be in [0, 1].".into(),
            });
        }
    }
    Ok(())
}

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

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

    fn approx_eq(a: Decimal, b: Decimal, eps: Decimal) -> bool {
        (a - b).abs() < eps
    }

    fn sample_bins() -> Vec<WoeBin> {
        vec![
            WoeBin {
                lower: dec!(0),
                upper: dec!(30),
                good_count: 400,
                bad_count: 100,
            },
            WoeBin {
                lower: dec!(30),
                upper: dec!(60),
                good_count: 300,
                bad_count: 200,
            },
            WoeBin {
                lower: dec!(60),
                upper: dec!(100),
                good_count: 200,
                bad_count: 300,
            },
        ]
    }

    fn sample_predictions() -> Vec<PredictionPair> {
        vec![
            PredictionPair {
                predicted: dec!(0.1),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.2),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.3),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.4),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.5),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.6),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.7),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.8),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.9),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.05),
                actual: 0,
            },
        ]
    }

    fn base_input() -> ScorecardInput {
        ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: sample_predictions(),
        }
    }

    #[test]
    fn test_woe_positive_for_good_dominant_bin() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        // First bin: 400 good, 100 bad => good_rate > bad_rate => WoE > 0
        assert!(out.bin_results[0].woe > Decimal::ZERO);
    }

    #[test]
    fn test_woe_negative_for_bad_dominant_bin() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        // Third bin: 200 good, 300 bad => good_rate < bad_rate => WoE < 0
        assert!(out.bin_results[2].woe < Decimal::ZERO);
    }

    #[test]
    fn test_iv_per_bin_non_negative() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        // IV_i = (good_rate - bad_rate) * WoE. Sign of diff and WoE align => non-negative
        for br in &out.bin_results {
            assert!(
                br.iv >= Decimal::ZERO,
                "IV {} should be non-negative",
                br.iv
            );
        }
    }

    #[test]
    fn test_total_iv_is_sum_of_bin_iv() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        let sum: Decimal = out.bin_results.iter().map(|b| b.iv).sum();
        assert!(approx_eq(out.total_iv, sum, dec!(0.0001)));
    }

    #[test]
    fn test_iv_strength_classification_useless() {
        assert_eq!(classify_iv(dec!(0.01)), IvStrength::Useless);
    }

    #[test]
    fn test_iv_strength_classification_weak() {
        assert_eq!(classify_iv(dec!(0.05)), IvStrength::Weak);
    }

    #[test]
    fn test_iv_strength_classification_medium() {
        assert_eq!(classify_iv(dec!(0.15)), IvStrength::Medium);
    }

    #[test]
    fn test_iv_strength_classification_strong() {
        assert_eq!(classify_iv(dec!(0.35)), IvStrength::Strong);
    }

    #[test]
    fn test_iv_strength_classification_suspicious() {
        assert_eq!(classify_iv(dec!(0.55)), IvStrength::Suspicious);
    }

    #[test]
    fn test_factor_calculation() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        let ln2 = dec!(0.6931471805599453);
        let expected_factor = dec!(20) / ln2;
        assert!(approx_eq(out.factor, expected_factor, dec!(0.001)));
    }

    #[test]
    fn test_gini_between_minus_one_and_one() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        assert!(out.gini >= dec!(-1) && out.gini <= Decimal::ONE);
    }

    #[test]
    fn test_gini_perfect_discrimination() {
        // All bads have higher predicted than all goods
        let preds = vec![
            PredictionPair {
                predicted: dec!(0.1),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.2),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.3),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.8),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.9),
                actual: 1,
            },
        ];
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: preds,
        };
        let out = calculate_scorecard(&input).unwrap();
        assert!(
            out.gini > dec!(0.9),
            "Perfect discrimination should give Gini near 1, got {}",
            out.gini
        );
    }

    #[test]
    fn test_ks_between_zero_and_one() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        assert!(out.ks_statistic >= Decimal::ZERO && out.ks_statistic <= Decimal::ONE);
    }

    #[test]
    fn test_ks_perfect_separation() {
        let preds = vec![
            PredictionPair {
                predicted: dec!(0.1),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.2),
                actual: 0,
            },
            PredictionPair {
                predicted: dec!(0.8),
                actual: 1,
            },
            PredictionPair {
                predicted: dec!(0.9),
                actual: 1,
            },
        ];
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: preds,
        };
        let out = calculate_scorecard(&input).unwrap();
        assert_eq!(out.ks_statistic, Decimal::ONE);
    }

    #[test]
    fn test_scorecard_points_computed_for_each_bin() {
        let input = base_input();
        let out = calculate_scorecard(&input).unwrap();
        assert_eq!(out.bin_results.len(), 3);
    }

    #[test]
    fn test_reject_empty_bins() {
        let input = ScorecardInput {
            bins: vec![],
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: vec![],
        };
        assert!(calculate_scorecard(&input).is_err());
    }

    #[test]
    fn test_reject_zero_pdo() {
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: Decimal::ZERO,
            predictions: vec![],
        };
        assert!(calculate_scorecard(&input).is_err());
    }

    #[test]
    fn test_reject_zero_target_odds() {
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: Decimal::ZERO,
            pdo: dec!(20),
            predictions: vec![],
        };
        assert!(calculate_scorecard(&input).is_err());
    }

    #[test]
    fn test_reject_invalid_actual() {
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: vec![PredictionPair {
                predicted: dec!(0.5),
                actual: 2,
            }],
        };
        assert!(calculate_scorecard(&input).is_err());
    }

    #[test]
    fn test_reject_prediction_out_of_range() {
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: vec![PredictionPair {
                predicted: dec!(1.5),
                actual: 0,
            }],
        };
        assert!(calculate_scorecard(&input).is_err());
    }

    #[test]
    fn test_empty_predictions_give_zero_gini_ks() {
        let input = ScorecardInput {
            bins: sample_bins(),
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: vec![],
        };
        let out = calculate_scorecard(&input).unwrap();
        assert_eq!(out.gini, Decimal::ZERO);
        assert_eq!(out.ks_statistic, Decimal::ZERO);
    }

    #[test]
    fn test_decimal_ln_of_one() {
        let result = decimal_ln(Decimal::ONE);
        assert!(approx_eq(result, Decimal::ZERO, dec!(0.0001)));
    }

    #[test]
    fn test_decimal_ln_of_e() {
        let e_approx = dec!(2.718281828);
        let result = decimal_ln(e_approx);
        assert!(approx_eq(result, Decimal::ONE, dec!(0.001)));
    }

    #[test]
    fn test_woe_equal_bins_near_zero() {
        let bins = vec![
            WoeBin {
                lower: dec!(0),
                upper: dec!(50),
                good_count: 100,
                bad_count: 100,
            },
            WoeBin {
                lower: dec!(50),
                upper: dec!(100),
                good_count: 100,
                bad_count: 100,
            },
        ];
        let input = ScorecardInput {
            bins,
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: vec![],
        };
        let out = calculate_scorecard(&input).unwrap();
        for br in &out.bin_results {
            assert!(
                approx_eq(br.woe, Decimal::ZERO, dec!(0.01)),
                "Equal bins WoE should be ~0"
            );
        }
    }

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

    #[test]
    fn test_all_good_no_bad_handles_gracefully() {
        let bins = vec![WoeBin {
            lower: dec!(0),
            upper: dec!(100),
            good_count: 500,
            bad_count: 0,
        }];
        let input = ScorecardInput {
            bins,
            target_score: dec!(600),
            target_odds: dec!(50),
            pdo: dec!(20),
            predictions: vec![],
        };
        let out = calculate_scorecard(&input).unwrap();
        assert_eq!(out.bin_results.len(), 1);
    }
}