numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Advanced Financial Functions
//!
//! This module provides additional financial functions for comprehensive
//! financial analysis including:
//! - Payment breakdown (PPMT, IPMT)
//! - Cumulative payments (CUMIPMT, CUMPRINC)
//! - Rate conversions (EFFECT, NOMINAL)
//! - Depreciation methods (SLN, SYD, DB, DDB)
//!
//! Note: MIRR (Modified Internal Rate of Return) is available in internal_rate.rs

use crate::error::{NumRs2Error, Result};
use num_traits::Float;
use std::fmt::Debug;

// =============================================================================
// PAYMENT BREAKDOWN
// =============================================================================

/// Calculate the interest portion of a payment
///
/// Returns the interest payment for a given period for an investment
/// based on periodic, constant payments and a constant interest rate.
///
/// # Arguments
///
/// * `rate` - Interest rate per period
/// * `per` - Period for which to find the interest (1-based)
/// * `nper` - Total number of payment periods
/// * `pv` - Present value (loan amount)
/// * `fv` - Future value (default 0)
/// * `when` - When payments are due: 0 = end of period (default), 1 = beginning
///
/// # Returns
///
/// * `Result<T>` - Interest portion of the payment
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // $2,500 loan at 8.5% for 4 years
/// let interest = ipmt(0.085/12.0, 1, 48, 2500.0, 0.0, 0).expect("ipmt calculation failed");
/// // First month interest is approximately -17.71
/// ```
pub fn ipmt<T>(rate: T, per: usize, nper: usize, pv: T, fv: T, when: u8) -> Result<T>
where
    T: Float + Debug + Clone,
{
    if per < 1 || per > nper {
        return Err(NumRs2Error::ValueError(format!(
            "Period {} is out of range [1, {}]",
            per, nper
        )));
    }

    let nper_t = T::from(nper).expect("Failed to convert nper to type T");
    let when_t = T::from(when).expect("Failed to convert when to type T");

    // Calculate payment amount
    let pmt = calculate_pmt(rate, nper_t, pv, fv, when_t)?;

    // Calculate balance at beginning of period
    let per_t = T::from(per).expect("Failed to convert per to type T");
    let per_minus_1 = T::from(per - 1).expect("Failed to convert per-1 to type T");

    let balance = if rate.is_zero() {
        pv + pmt * per_minus_1
    } else {
        let factor = (T::one() + rate).powf(per_minus_1);
        pv * factor + pmt * (factor - T::one()) / rate * (T::one() + rate * when_t)
    };

    // Interest for this period (negative for outflow convention)
    let interest = if when == 1 && per == 1 {
        T::zero() // First payment at beginning has no interest yet
    } else {
        -balance * rate // Negate to match payment sign convention
    };

    Ok(interest)
}

/// Calculate the principal portion of a payment
///
/// Returns the principal payment for a given period for an investment
/// based on periodic, constant payments and a constant interest rate.
///
/// # Arguments
///
/// * `rate` - Interest rate per period
/// * `per` - Period for which to find the principal (1-based)
/// * `nper` - Total number of payment periods
/// * `pv` - Present value (loan amount)
/// * `fv` - Future value (default 0)
/// * `when` - When payments are due: 0 = end of period (default), 1 = beginning
///
/// # Returns
///
/// * `Result<T>` - Principal portion of the payment
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // $2,500 loan at 8.5% for 4 years
/// let principal = ppmt(0.085/12.0, 1, 48, 2500.0, 0.0, 0).expect("ppmt calculation failed");
/// // First month principal is approximately -43.00
/// ```
pub fn ppmt<T>(rate: T, per: usize, nper: usize, pv: T, fv: T, when: u8) -> Result<T>
where
    T: Float + Debug + Clone,
{
    let nper_t = T::from(nper).expect("Failed to convert nper to type T");
    let when_t = T::from(when).expect("Failed to convert when to type T");

    let pmt = calculate_pmt(rate, nper_t, pv, fv, when_t)?;
    let interest = ipmt(rate, per, nper, pv, fv, when)?;

    Ok(pmt - interest)
}

/// Calculate cumulative interest paid between two periods
///
/// # Arguments
///
/// * `rate` - Interest rate per period
/// * `nper` - Total number of payment periods
/// * `pv` - Present value (loan amount)
/// * `start_period` - Starting period (1-based, inclusive)
/// * `end_period` - Ending period (1-based, inclusive)
/// * `when` - When payments are due: 0 = end of period (default), 1 = beginning
///
/// # Returns
///
/// * `Result<T>` - Cumulative interest paid
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Total interest paid in first year of $125,000 mortgage at 9% for 30 years
/// let cum_interest = cumipmt(0.09/12.0, 360, 125000.0, 1, 12, 0).expect("cumipmt calculation failed");
/// // First year interest is approximately -11135.23
/// ```
pub fn cumipmt<T>(
    rate: T,
    nper: usize,
    pv: T,
    start_period: usize,
    end_period: usize,
    when: u8,
) -> Result<T>
where
    T: Float + Debug + Clone,
{
    if start_period < 1 || start_period > end_period || end_period > nper {
        return Err(NumRs2Error::ValueError(format!(
            "Invalid period range [{}, {}] for nper={}",
            start_period, end_period, nper
        )));
    }

    let mut total_interest = T::zero();
    for per in start_period..=end_period {
        total_interest = total_interest + ipmt(rate, per, nper, pv, T::zero(), when)?;
    }

    Ok(total_interest)
}

/// Calculate cumulative principal paid between two periods
///
/// # Arguments
///
/// * `rate` - Interest rate per period
/// * `nper` - Total number of payment periods
/// * `pv` - Present value (loan amount)
/// * `start_period` - Starting period (1-based, inclusive)
/// * `end_period` - Ending period (1-based, inclusive)
/// * `when` - When payments are due: 0 = end of period (default), 1 = beginning
///
/// # Returns
///
/// * `Result<T>` - Cumulative principal paid
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Total principal paid in first year of $125,000 mortgage at 9% for 30 years
/// let cum_principal = cumprinc(0.09/12.0, 360, 125000.0, 1, 12, 0).expect("cumprinc calculation failed");
/// // First year principal is approximately -927.43
/// ```
pub fn cumprinc<T>(
    rate: T,
    nper: usize,
    pv: T,
    start_period: usize,
    end_period: usize,
    when: u8,
) -> Result<T>
where
    T: Float + Debug + Clone,
{
    if start_period < 1 || start_period > end_period || end_period > nper {
        return Err(NumRs2Error::ValueError(format!(
            "Invalid period range [{}, {}] for nper={}",
            start_period, end_period, nper
        )));
    }

    let mut total_principal = T::zero();
    for per in start_period..=end_period {
        total_principal = total_principal + ppmt(rate, per, nper, pv, T::zero(), when)?;
    }

    Ok(total_principal)
}

// Helper function to calculate payment
fn calculate_pmt<T>(rate: T, nper: T, pv: T, fv: T, when: T) -> Result<T>
where
    T: Float + Debug,
{
    if rate.is_zero() {
        Ok(-(pv + fv) / nper)
    } else {
        let factor = (T::one() + rate).powf(nper);
        let denominator = (factor - T::one()) / rate * (T::one() + rate * when);
        Ok(-(pv * factor + fv) / denominator)
    }
}

// =============================================================================
// RATE CONVERSIONS
// =============================================================================

/// Convert nominal annual interest rate to effective annual rate
///
/// The effective rate takes into account the effect of compounding.
///
/// # Arguments
///
/// * `nominal_rate` - Nominal annual interest rate
/// * `nper` - Number of compounding periods per year
///
/// # Returns
///
/// * `Result<T>` - Effective annual interest rate
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Convert 10% nominal rate compounded monthly to effective rate
/// let effective = effect(0.10, 12).expect("effect calculation failed");
/// // Effective rate is approximately 0.1047 or 10.47%
/// ```
pub fn effect<T>(nominal_rate: T, nper: usize) -> Result<T>
where
    T: Float + Debug,
{
    if nper == 0 {
        return Err(NumRs2Error::ValueError(
            "Number of compounding periods must be positive".to_string(),
        ));
    }

    let nper_t = T::from(nper).expect("Failed to convert nper to type T");
    let periodic_rate = nominal_rate / nper_t;

    Ok((T::one() + periodic_rate).powf(nper_t) - T::one())
}

/// Convert effective annual interest rate to nominal annual rate
///
/// # Arguments
///
/// * `effective_rate` - Effective annual interest rate
/// * `nper` - Number of compounding periods per year
///
/// # Returns
///
/// * `Result<T>` - Nominal annual interest rate
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Convert 10.47% effective rate to nominal rate compounded monthly
/// let nominal = nominal(0.1047, 12).expect("nominal calculation failed");
/// // Nominal rate is approximately 0.10 or 10%
/// ```
pub fn nominal<T>(effective_rate: T, nper: usize) -> Result<T>
where
    T: Float + Debug,
{
    if nper == 0 {
        return Err(NumRs2Error::ValueError(
            "Number of compounding periods must be positive".to_string(),
        ));
    }

    let nper_t = T::from(nper).expect("Failed to convert nper to type T");

    // (1 + effective_rate)^(1/nper) - 1 = periodic_rate
    // nominal_rate = periodic_rate * nper
    let periodic_rate = (T::one() + effective_rate).powf(T::one() / nper_t) - T::one();

    Ok(periodic_rate * nper_t)
}

// =============================================================================
// DEPRECIATION METHODS
// =============================================================================

/// Straight-Line Depreciation
///
/// Returns the depreciation of an asset for one period using the
/// straight-line method.
///
/// # Arguments
///
/// * `cost` - Initial cost of the asset
/// * `salvage` - Value at the end of the depreciation (salvage value)
/// * `life` - Number of periods over which the asset is depreciated
///
/// # Returns
///
/// * `Result<T>` - Depreciation for one period
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Asset costing $30,000 with $7,500 salvage over 10 years
/// let depreciation = sln(30000.0, 7500.0, 10.0).expect("sln calculation failed");
/// // Annual depreciation is 2,250
/// ```
pub fn sln<T>(cost: T, salvage: T, life: T) -> Result<T>
where
    T: Float + Debug,
{
    if life <= T::zero() {
        return Err(NumRs2Error::ValueError("Life must be positive".to_string()));
    }

    Ok((cost - salvage) / life)
}

/// Sum-of-Years-Digits Depreciation
///
/// Returns the depreciation of an asset for a specified period using the
/// sum-of-years-digits method.
///
/// # Arguments
///
/// * `cost` - Initial cost of the asset
/// * `salvage` - Value at the end of the depreciation (salvage value)
/// * `life` - Number of periods over which the asset is depreciated
/// * `per` - Period for which to calculate depreciation (1-based)
///
/// # Returns
///
/// * `Result<T>` - Depreciation for the specified period
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Asset costing $30,000 with $7,500 salvage over 10 years, year 1
/// let depreciation = syd(30000.0, 7500.0, 10, 1).expect("syd calculation failed");
/// // First year depreciation is approximately 4,090.91
/// ```
pub fn syd<T>(cost: T, salvage: T, life: usize, per: usize) -> Result<T>
where
    T: Float + Debug,
{
    if life == 0 {
        return Err(NumRs2Error::ValueError("Life must be positive".to_string()));
    }

    if per < 1 || per > life {
        return Err(NumRs2Error::ValueError(format!(
            "Period {} is out of range [1, {}]",
            per, life
        )));
    }

    let life_t = T::from(life).expect("Failed to convert life to type T");
    let per_t = T::from(per).expect("Failed to convert per to type T");

    // Sum of years digits = n(n+1)/2
    let sum_of_years =
        life_t * (life_t + T::one()) / T::from(2.0).expect("Failed to convert 2.0 to type T");

    // Remaining life at start of period
    let remaining = life_t - per_t + T::one();

    Ok((cost - salvage) * remaining / sum_of_years)
}

/// Declining Balance Depreciation
///
/// Returns the depreciation of an asset for a specified period using the
/// fixed-declining balance method.
///
/// # Arguments
///
/// * `cost` - Initial cost of the asset
/// * `salvage` - Value at the end of the depreciation (salvage value)
/// * `life` - Number of periods over which the asset is depreciated
/// * `period` - Period for which to calculate depreciation (1-based)
/// * `month` - Number of months in the first year (default 12)
///
/// # Returns
///
/// * `Result<T>` - Depreciation for the specified period
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Asset costing $1,000,000 with $100,000 salvage over 6 years, year 1
/// let depreciation = db(1000000.0, 100000.0, 6, 1, 7).expect("db calculation failed");
/// // First year depreciation with 7 months is approximately 186,083.33
/// ```
pub fn db<T>(cost: T, salvage: T, life: usize, period: usize, month: usize) -> Result<T>
where
    T: Float + Debug,
{
    if life == 0 {
        return Err(NumRs2Error::ValueError("Life must be positive".to_string()));
    }

    if period < 1 || period > life + 1 {
        return Err(NumRs2Error::ValueError(format!(
            "Period {} is out of range [1, {}]",
            period,
            life + 1
        )));
    }

    let life_t = T::from(life).expect("Failed to convert life to type T");
    let month_t = T::from(month).expect("Failed to convert month to type T");

    // Calculate depreciation rate
    // rate = 1 - (salvage/cost)^(1/life), rounded to 3 decimal places
    let rate = T::one() - (salvage / cost).powf(T::one() / life_t);
    let thousand = T::from(1000.0).expect("Failed to convert 1000.0 to type T");
    let rate = (rate * thousand).round() / thousand;

    let mut book_value = cost;
    let twelve = T::from(12.0).expect("Failed to convert 12.0 to type T");

    // Calculate depreciation for each period up to the target
    for p in 1..=period {
        let depreciation = if p == 1 {
            // First year: prorated by months
            book_value * rate * month_t / twelve
        } else if p == life + 1 {
            // Last period: remaining book value minus salvage
            let remaining = book_value - salvage;
            if remaining > T::zero() {
                remaining
            } else {
                T::zero()
            }
        } else {
            // Normal year
            book_value * rate
        };

        if p == period {
            return Ok(depreciation);
        }

        book_value = book_value - depreciation;
    }

    Ok(T::zero())
}

/// Double Declining Balance Depreciation
///
/// Returns the depreciation of an asset for a specified period using the
/// double-declining balance method or other specified factor.
///
/// # Arguments
///
/// * `cost` - Initial cost of the asset
/// * `salvage` - Value at the end of the depreciation (salvage value)
/// * `life` - Number of periods over which the asset is depreciated
/// * `period` - Period for which to calculate depreciation (1-based)
/// * `factor` - Rate at which the balance declines (default 2 for double declining)
///
/// # Returns
///
/// * `Result<T>` - Depreciation for the specified period
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// // Asset costing $2,400 with $300 salvage over 10 years, year 1
/// let depreciation = ddb(2400.0, 300.0, 10, 1, 2.0).expect("ddb calculation failed");
/// // First year DDB depreciation is 480
/// ```
pub fn ddb<T>(cost: T, salvage: T, life: usize, period: usize, factor: T) -> Result<T>
where
    T: Float + Debug,
{
    if life == 0 {
        return Err(NumRs2Error::ValueError("Life must be positive".to_string()));
    }

    if period < 1 || period > life {
        return Err(NumRs2Error::ValueError(format!(
            "Period {} is out of range [1, {}]",
            period, life
        )));
    }

    let life_t = T::from(life).expect("Failed to convert life to type T");

    // Depreciation rate = factor / life
    let rate = factor / life_t;

    let mut book_value = cost;
    let mut depreciation = T::zero();

    for p in 1..=period {
        // Calculate depreciation for this period
        let max_depreciation = book_value * rate;

        // Cannot depreciate below salvage value
        let allowed = book_value - salvage;
        depreciation = if allowed > T::zero() {
            max_depreciation.min(allowed)
        } else {
            T::zero()
        };

        if p == period {
            return Ok(depreciation);
        }

        book_value = book_value - depreciation;
    }

    Ok(depreciation)
}

/// Amortization schedule
///
/// Generate a full amortization schedule for a loan.
///
/// # Arguments
///
/// * `principal` - Loan principal amount
/// * `rate` - Interest rate per period
/// * `nper` - Total number of payment periods
///
/// # Returns
///
/// * `Result<AmortizationSchedule>` - Schedule with period, payment, principal, interest, balance
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// let schedule = amortization_schedule(10000.0, 0.05/12.0, 12).expect("amortization_schedule calculation failed");
/// // Returns full 12-month schedule
/// ```
pub fn amortization_schedule<T>(
    principal: T,
    rate: T,
    nper: usize,
) -> Result<AmortizationSchedule<T>>
where
    T: Float + Debug + Clone,
{
    if nper == 0 {
        return Err(NumRs2Error::ValueError(
            "Number of periods must be positive".to_string(),
        ));
    }

    let nper_t = T::from(nper).expect("Failed to convert nper to type T");
    let pmt = calculate_pmt(rate, nper_t, principal, T::zero(), T::zero())?;

    let mut periods = Vec::with_capacity(nper);
    let mut payments = Vec::with_capacity(nper);
    let mut principals = Vec::with_capacity(nper);
    let mut interests = Vec::with_capacity(nper);
    let mut balances = Vec::with_capacity(nper);

    let mut balance = principal;

    for per in 1..=nper {
        let interest = balance * rate;
        let principal_payment = -pmt - interest;
        balance = balance - principal_payment;

        // Ensure balance doesn't go negative due to rounding
        if balance < T::zero() {
            balance = T::zero();
        }

        periods.push(per);
        payments.push(pmt);
        principals.push(-principal_payment);
        interests.push(-interest);
        balances.push(balance);
    }

    Ok(AmortizationSchedule {
        periods,
        payments,
        principals,
        interests,
        balances,
    })
}

/// Amortization schedule result
#[derive(Debug, Clone)]
pub struct AmortizationSchedule<T> {
    /// Period numbers (1-based)
    pub periods: Vec<usize>,
    /// Payment amounts (negative = outflow)
    pub payments: Vec<T>,
    /// Principal portions of payments
    pub principals: Vec<T>,
    /// Interest portions of payments
    pub interests: Vec<T>,
    /// Remaining balances after each payment
    pub balances: Vec<T>,
}

impl<T: Float + Clone> AmortizationSchedule<T> {
    /// Get total interest paid
    pub fn total_interest(&self) -> T {
        self.interests.iter().fold(T::zero(), |acc, &x| acc + x)
    }

    /// Get total payments made
    pub fn total_payments(&self) -> T {
        self.payments.iter().fold(T::zero(), |acc, &x| acc + x)
    }
}

// =============================================================================
// TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    #[test]
    fn test_ipmt() {
        // $2,500 loan at 8.5% annual for 4 years (monthly payments)
        let rate = 0.085 / 12.0;
        let nper = 48;
        let pv = 2500.0;

        let interest = ipmt(rate, 1, nper, pv, 0.0, 0).expect("ipmt calculation should succeed");
        // First month interest should be about -17.71
        assert_relative_eq!(interest, -17.708333, epsilon = 0.01);
    }

    #[test]
    fn test_ppmt() {
        let rate = 0.085 / 12.0;
        let nper = 48;
        let pv = 2500.0;

        let principal = ppmt(rate, 1, nper, pv, 0.0, 0).expect("ppmt calculation should succeed");
        // First month principal portion
        assert!(principal < 0.0); // Should be negative (outflow)
    }

    #[test]
    fn test_payment_breakdown() {
        // Verify IPMT + PPMT = PMT for any period
        let rate = 0.08 / 12.0;
        let nper = 60;
        let pv = 20000.0;

        let pmt =
            calculate_pmt(rate, nper as f64, pv, 0.0, 0.0).expect("pmt calculation should succeed");

        for per in 1..=nper {
            let interest =
                ipmt(rate, per, nper, pv, 0.0, 0).expect("ipmt calculation should succeed");
            let principal =
                ppmt(rate, per, nper, pv, 0.0, 0).expect("ppmt calculation should succeed");
            assert_relative_eq!(interest + principal, pmt, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_cumipmt() {
        // First year interest on a mortgage
        let rate = 0.09 / 12.0;
        let nper = 360;
        let pv = 125000.0;

        let cum_interest =
            cumipmt(rate, nper, pv, 1, 12, 0).expect("cumipmt calculation should succeed");
        // Verify it's negative and in reasonable range
        assert!(cum_interest < 0.0, "Cumulative interest should be negative");
        assert!(
            cum_interest > -12000.0 && cum_interest < -10000.0,
            "First year interest should be between -12000 and -10000, got {}",
            cum_interest
        );
    }

    #[test]
    fn test_cumprinc() {
        let rate = 0.09 / 12.0;
        let nper = 360;
        let pv = 125000.0;

        let cum_principal =
            cumprinc(rate, nper, pv, 1, 12, 0).expect("cumprinc calculation should succeed");
        // Verify it's negative (principal is being paid down)
        assert!(
            cum_principal < 0.0,
            "Cumulative principal should be negative"
        );

        // Verify relationship: cumipmt + cumprinc ≈ total payments for first year
        let cum_interest =
            cumipmt(rate, nper, pv, 1, 12, 0).expect("cumipmt calculation should succeed");
        let pmt =
            calculate_pmt(rate, nper as f64, pv, 0.0, 0.0).expect("pmt calculation should succeed");
        let total_payments = pmt * 12.0;

        assert_relative_eq!(
            cum_interest + cum_principal,
            total_payments,
            epsilon = 1e-10
        );
    }

    #[test]
    fn test_effect() {
        // 10% nominal compounded monthly
        let eff = effect(0.10, 12).expect("effect calculation should succeed");
        assert_relative_eq!(eff, 0.10471307, epsilon = 1e-6);
    }

    #[test]
    fn test_nominal() {
        // Convert back from effective
        let nom = nominal(0.10471307, 12).expect("nominal calculation should succeed");
        assert_relative_eq!(nom, 0.10, epsilon = 1e-6);
    }

    #[test]
    fn test_sln() {
        let dep = sln(30000.0, 7500.0, 10.0).expect("sln calculation should succeed");
        assert_relative_eq!(dep, 2250.0, epsilon = 0.01);
    }

    #[test]
    fn test_syd() {
        // First year SYD depreciation
        let dep = syd(30000.0, 7500.0, 10, 1).expect("syd calculation should succeed");
        assert_relative_eq!(dep, 4090.909, epsilon = 0.01);

        // Sum of all years should equal depreciable amount
        let mut total = 0.0;
        for per in 1..=10 {
            total += syd(30000.0, 7500.0, 10, per).expect("syd calculation should succeed");
        }
        assert_relative_eq!(total, 22500.0, epsilon = 0.01);
    }

    #[test]
    fn test_ddb() {
        // Double declining balance
        let dep = ddb(2400.0, 300.0, 10, 1, 2.0).expect("ddb calculation should succeed");
        assert_relative_eq!(dep, 480.0, epsilon = 0.01);

        // Year 2
        let dep2 = ddb(2400.0, 300.0, 10, 2, 2.0).expect("ddb calculation should succeed");
        assert_relative_eq!(dep2, 384.0, epsilon = 0.01);
    }

    #[test]
    fn test_amortization_schedule() {
        let schedule = amortization_schedule(10000.0, 0.05 / 12.0, 12)
            .expect("amortization_schedule calculation should succeed");

        // Should have 12 periods
        assert_eq!(schedule.periods.len(), 12);

        // Final balance should be close to zero
        assert!(
            schedule
                .balances
                .last()
                .expect("balances should not be empty")
                .abs()
                < 0.01
        );

        // Total principal paid should equal original loan
        let total_principal: f64 = schedule.principals.iter().sum();
        assert_relative_eq!(total_principal, -10000.0, epsilon = 0.01);
    }
}