finance-solution 0.4.1

Finance math: TVM, cashflow, amortization, equity path metrics, technical analysis (SMA/EMA/WMA/HMA/MACD/BB/Keltner/Donchian/Stoch/VWAP/RVOL/RSI/ATR/LinReg), and options (BSM, Black76, GK, CRR American) with Result-only APIs, solutions, tables, and incremental state.
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
//! Approximate years to double (Rule of 72 / 69 / 70) and exact doubling time.
//!
//! Prefer [`doubling_solution`] for formulas, comparison tables, and multi-rate scenarios.
//! Prefer [`doubling_time`] when you only need the exact discrete result.
//!
//! # Error handling (v0.1+)
//!
//! All public entry points return [`FinanceResult`]. Zero or negative rates and non-finite
//! inputs yield [`FinanceError`] — they do not panic.
use crate::util::error::{require_finite, FinanceError, FinanceResult};
use crate::{columns_with_strings, print_table_locale_opt};

// ---------------------------------------------------------------------------
// Scalars
// ---------------------------------------------------------------------------

/// Approximate years to double using the Rule of 72: `72 / (100 * rate)`.
///
/// `rate` is a decimal rate (e.g. `0.08` for 8%).
///
/// # Errors
/// [`FinanceError::ZeroValue`] if `rate == 0`, [`FinanceError::InvalidRate`] if `rate < 0`,
/// or [`FinanceError::NonFinite`] if non-finite.
///
/// # Examples
/// ```
/// use finance_solution::{rule_of_72, FinanceError};
///
/// assert_eq!((rule_of_72(0.08).unwrap() * 100.0).round() / 100.0, 9.0); // 72/8
///
/// match rule_of_72(0.0) {
///     Err(FinanceError::ZeroValue { field }) => assert_eq!(field, "rate"),
///     other => panic!("expected ZeroValue, got {other:?}"),
/// }
/// ```
pub fn rule_of_72(rate: f64) -> FinanceResult<f64> {
    require_positive_rate(rate)?;
    Ok(72.0 / (rate * 100.0))
}

/// Approximate years to double using the Rule of 70: `70 / (100 * rate)`.
///
/// # Errors
/// Same domain as [`rule_of_72`].
///
/// # Examples
/// ```
/// use finance_solution::{rule_of_70, FinanceError};
///
/// assert!((rule_of_70(0.08).unwrap() - 8.75).abs() < 1e-12); // 70/8
/// assert!(matches!(rule_of_70(0.0), Err(FinanceError::ZeroValue { .. })));
/// ```
pub fn rule_of_70(rate: f64) -> FinanceResult<f64> {
    require_positive_rate(rate)?;
    Ok(70.0 / (rate * 100.0))
}

/// Approximate years to double using the Rule of 69: `69 / (100 * rate)`.
///
/// Often closer to continuous compounding than the Rule of 72.
///
/// # Errors
/// Same domain as [`rule_of_72`].
///
/// # Examples
/// ```
/// use finance_solution::{rule_of_69, FinanceError};
///
/// assert!((rule_of_69(0.08).unwrap() - 8.625).abs() < 1e-12); // 69/8
/// match rule_of_69(-0.01) {
///     Err(FinanceError::InvalidRate { rate }) => assert!(rate < 0.0),
///     other => panic!("expected InvalidRate, got {other:?}"),
/// }
/// ```
pub fn rule_of_69(rate: f64) -> FinanceResult<f64> {
    require_positive_rate(rate)?;
    Ok(69.0 / (rate * 100.0))
}

/// Exact periods to double under discrete compounding: `ln(2) / ln(1 + rate)`.
///
/// # Errors
/// [`FinanceError::InvalidRate`] if `rate <= 0`, or non-finite / unsolvable rates.
///
/// # Examples
/// ```
/// use finance_solution::{doubling_time, rule_of_72, FinanceError};
///
/// let exact = doubling_time(0.08).unwrap();
/// let approx = rule_of_72(0.08).unwrap();
/// assert!((exact - approx).abs() < 0.5);
/// assert!((exact - 9.0065).abs() < 1e-3);
///
/// match doubling_time(-0.05) {
///     Err(FinanceError::InvalidRate { rate }) => assert!(rate < 0.0),
///     other => panic!("expected InvalidRate, got {other:?}"),
/// }
/// ```
pub fn doubling_time(rate: f64) -> FinanceResult<f64> {
    require_finite("rate", rate)?;
    if rate <= 0.0 {
        return Err(FinanceError::InvalidRate { rate });
    }
    let denom = (1.0 + rate).ln();
    if denom == 0.0 || !denom.is_finite() {
        return Err(FinanceError::Unsolvable {
            message: "cannot compute doubling time for this rate",
        });
    }
    Ok(2.0_f64.ln() / denom)
}

/// Exact time to double under continuous compounding: `ln(2) / rate`.
///
/// Slightly shorter than discrete [`doubling_time`] for the same nominal rate.
///
/// # Errors
/// Same domain as [`rule_of_72`] (strictly positive finite rate).
///
/// # Examples
/// ```
/// use finance_solution::{doubling_time, doubling_time_continuous, FinanceError};
///
/// let cont = doubling_time_continuous(0.08).unwrap();
/// let disc = doubling_time(0.08).unwrap();
/// assert!(cont < disc);
/// assert!(matches!(
///     doubling_time_continuous(0.0),
///     Err(FinanceError::ZeroValue { .. })
/// ));
/// ```
pub fn doubling_time_continuous(rate: f64) -> FinanceResult<f64> {
    require_positive_rate(rate)?;
    Ok(2.0_f64.ln() / rate)
}

// ---------------------------------------------------------------------------
// Solution
// ---------------------------------------------------------------------------

/// Comparison of doubling-time methods at a single rate.
///
/// Create with [`doubling_solution`].
///
/// # Examples
/// ```
/// use finance_solution::*;
///
/// let s = doubling_solution(0.08).unwrap();
/// assert_rounded_2!(s.rule_of_72(), 9.0);
/// assert_rounded_4!(s.exact(), 9.0065);
/// // Table of method vs years (and error vs exact):
/// s.print_table();
/// ```
///
/// Sample `print_table()` output at 8%:
///
/// ```text
/// method               years      error_vs_exact
/// ---------------  ---------  ----------------
/// rule_of_72          9.0000           -0.0065
/// rule_of_70          8.7500           -0.2565
/// rule_of_69          8.6250           -0.3815
/// exact_discrete      9.0065            0.0000
/// exact_continuous    8.6643           -0.3421
/// ```
#[derive(Clone, Debug)]
pub struct DoublingSolution {
    rate: f64,
    rule_of_72: f64,
    rule_of_70: f64,
    rule_of_69: f64,
    exact: f64,
    exact_continuous: f64,
    formula: String,
    symbolic_formula: String,
}

impl DoublingSolution {
    pub fn rate(&self) -> f64 {
        self.rate
    }
    pub fn rule_of_72(&self) -> f64 {
        self.rule_of_72
    }
    pub fn rule_of_70(&self) -> f64 {
        self.rule_of_70
    }
    pub fn rule_of_69(&self) -> f64 {
        self.rule_of_69
    }
    /// Exact discrete compounding: `ln(2) / ln(1+r)`.
    pub fn exact(&self) -> f64 {
        self.exact
    }
    /// Exact continuous compounding: `ln(2) / r`.
    pub fn exact_continuous(&self) -> f64 {
        self.exact_continuous
    }
    pub fn formula(&self) -> &str {
        &self.formula
    }
    pub fn symbolic_formula(&self) -> &str {
        &self.symbolic_formula
    }

    /// Absolute error of Rule of 72 vs exact discrete doubling time.
    pub fn error_rule_of_72(&self) -> f64 {
        self.rule_of_72 - self.exact
    }

    /// Print method comparison at this rate.
    pub fn print_table(&self) {
        self.print_table_locale_opt(None, None);
    }

    pub fn print_table_locale(&self, locale: &num_format::Locale, precision: usize) {
        self.print_table_locale_opt(Some(locale), Some(precision));
    }

    fn print_table_locale_opt(
        &self,
        locale: Option<&num_format::Locale>,
        precision: Option<usize>,
    ) {
        let columns = columns_with_strings(&[
            ("method", "s", true),
            ("years", "f", true),
            ("error_vs_exact", "f", true),
        ]);
        let rows = [
            ("rule_of_72", self.rule_of_72),
            ("rule_of_70", self.rule_of_70),
            ("rule_of_69", self.rule_of_69),
            ("exact_discrete", self.exact),
            ("exact_continuous", self.exact_continuous),
        ];
        let data = rows
            .iter()
            .map(|(name, years)| {
                vec![
                    name.to_string(),
                    years.to_string(),
                    (years - self.exact).to_string(),
                ]
            })
            .collect();
        print_table_locale_opt(&columns, data, locale, precision);
    }
}

/// Build a [`DoublingSolution`] comparing Rule of 72/70/69 with exact discrete and continuous times.
///
/// # Errors
/// Propagates validation failures from the underlying doubling formulas.
///
/// # Examples
/// ```
/// use finance_solution::{doubling_solution, FinanceError};
///
/// let s = doubling_solution(0.10).unwrap();
/// // Symmetry: $1 grown at r for exact periods ≈ $2
/// let grown = (1.0_f64 + s.rate()).powf(s.exact());
/// assert!((grown - 2.0).abs() < 1e-9);
///
/// match doubling_solution(-0.05) {
///     Err(FinanceError::InvalidRate { .. }) => {}
///     other => panic!("expected InvalidRate, got {other:?}"),
/// }
/// ```
pub fn doubling_solution(rate: f64) -> FinanceResult<DoublingSolution> {
    let r72 = rule_of_72(rate)?;
    let r70 = rule_of_70(rate)?;
    let r69 = rule_of_69(rate)?;
    let exact = doubling_time(rate)?;
    let exact_c = doubling_time_continuous(rate)?;
    let formula = format!(
        "exact {:.4} = ln(2) / ln(1 + {:.6}); rule_72 {:.4} = 72 / ({:.4})",
        exact,
        rate,
        r72,
        rate * 100.0
    );
    let symbolic =
        "exact = ln(2)/ln(1+r); rule_72 = 72/(100*r); rule_70 = 70/(100*r); rule_69 = 69/(100*r); continuous = ln(2)/r";
    Ok(DoublingSolution {
        rate,
        rule_of_72: r72,
        rule_of_70: r70,
        rule_of_69: r69,
        exact,
        exact_continuous: exact_c,
        formula,
        symbolic_formula: symbolic.to_string(),
    })
}

// ---------------------------------------------------------------------------
// Multi-rate scenario table
// ---------------------------------------------------------------------------

/// One row of a multi-rate doubling comparison.
#[derive(Clone, Debug)]
pub struct DoublingRateRow {
    rate: f64,
    rule_of_72: f64,
    rule_of_70: f64,
    rule_of_69: f64,
    exact: f64,
    exact_continuous: f64,
}

impl DoublingRateRow {
    pub fn rate(&self) -> f64 {
        self.rate
    }
    pub fn rule_of_72(&self) -> f64 {
        self.rule_of_72
    }
    pub fn rule_of_70(&self) -> f64 {
        self.rule_of_70
    }
    pub fn rule_of_69(&self) -> f64 {
        self.rule_of_69
    }
    pub fn exact(&self) -> f64 {
        self.exact
    }
    pub fn exact_continuous(&self) -> f64 {
        self.exact_continuous
    }
    pub fn error_rule_of_72(&self) -> f64 {
        self.rule_of_72 - self.exact
    }
}

/// Table of doubling estimates across many rates (teaching: when is Rule of 72 “good enough?”).
#[derive(Clone, Debug)]
pub struct DoublingRateSeries(Vec<DoublingRateRow>);

impl DoublingRateSeries {
    pub fn rows(&self) -> &[DoublingRateRow] {
        &self.0
    }

    pub fn print_table(&self) {
        self.print_table_locale_opt(None, None);
    }

    pub fn print_table_locale(&self, locale: &num_format::Locale, precision: usize) {
        self.print_table_locale_opt(Some(locale), Some(precision));
    }

    fn print_table_locale_opt(
        &self,
        locale: Option<&num_format::Locale>,
        precision: Option<usize>,
    ) {
        let columns = columns_with_strings(&[
            ("rate", "r", true),
            ("rule_72", "f", true),
            ("rule_70", "f", true),
            ("rule_69", "f", true),
            ("exact", "f", true),
            ("continuous", "f", true),
            ("err_72", "f", true),
        ]);
        let data = self
            .0
            .iter()
            .map(|row| {
                vec![
                    row.rate.to_string(),
                    row.rule_of_72.to_string(),
                    row.rule_of_70.to_string(),
                    row.rule_of_69.to_string(),
                    row.exact.to_string(),
                    row.exact_continuous.to_string(),
                    row.error_rule_of_72().to_string(),
                ]
            })
            .collect();
        print_table_locale_opt(&columns, data, locale, precision);
    }
}

/// Compare doubling rules across a list of rates (teaching: when is Rule of 72 “good enough?”).
///
/// # Examples
/// ```
/// use finance_solution::{doubling_compare_rates, FinanceError};
///
/// let table = doubling_compare_rates(&[0.05, 0.08]).unwrap();
/// assert_eq!(table.rows().len(), 2);
///
/// match doubling_compare_rates(&[]) {
///     Err(FinanceError::Unsolvable { message }) => assert!(message.contains("one rate")),
///     other => panic!("expected Unsolvable, got {other:?}"),
/// }
/// match doubling_compare_rates(&[0.05, -0.01]) {
///     Err(FinanceError::InvalidRate { .. }) => {}
///     other => panic!("expected InvalidRate, got {other:?}"),
/// }
/// ```
pub fn doubling_compare_rates(rates: &[f64]) -> FinanceResult<DoublingRateSeries> {
    if rates.is_empty() {
        return Err(FinanceError::Unsolvable {
            message: "doubling_compare_rates requires at least one rate",
        });
    }
    let mut rows = Vec::with_capacity(rates.len());
    for &rate in rates {
        let s = doubling_solution(rate)?;
        rows.push(DoublingRateRow {
            rate: s.rate,
            rule_of_72: s.rule_of_72,
            rule_of_70: s.rule_of_70,
            rule_of_69: s.rule_of_69,
            exact: s.exact,
            exact_continuous: s.exact_continuous,
        });
    }
    Ok(DoublingRateSeries(rows))
}

fn require_positive_rate(rate: f64) -> FinanceResult<()> {
    require_finite("rate", rate)?;
    if rate == 0.0 {
        return Err(FinanceError::ZeroValue { field: "rate" });
    }
    if rate < 0.0 {
        return Err(FinanceError::InvalidRate { rate });
    }
    Ok(())
}

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

    #[test]
    fn test_rule_of_72_eight_percent() {
        assert_rounded_2!(rule_of_72(0.08).unwrap(), 9.0);
        assert_rounded_2!(rule_of_70(0.08).unwrap(), 8.75);
        assert_rounded_2!(rule_of_69(0.08).unwrap(), 8.625);
    }

    #[test]
    fn test_doubling_time_positive() {
        let t = doubling_time(0.10).unwrap();
        assert!(t > 7.0 && t < 8.0);
        assert!(doubling_time_continuous(0.10).unwrap() < t);
    }

    #[test]
    fn test_rule_rejects_zero_and_negative() {
        assert!(matches!(
            rule_of_72(0.0),
            Err(FinanceError::ZeroValue { .. })
        ));
        assert!(matches!(
            doubling_time(-0.05),
            Err(FinanceError::InvalidRate { .. })
        ));
        assert!(doubling_compare_rates(&[]).is_err());
    }

    #[test]
    fn test_solution_and_symmetry() {
        let s = doubling_solution(0.08).unwrap();
        assert_rounded_2!(s.rule_of_72(), 9.0);
        assert_rounded_4!(s.exact(), 9.0065);
        let grown = (1.0 + s.rate()).powf(s.exact());
        assert!((grown - 2.0).abs() < 1e-9);
        assert!(!s.formula().is_empty());
    }

    #[test]
    fn test_compare_rates() {
        let t = doubling_compare_rates(&[0.01, 0.08, 0.12]).unwrap();
        assert_eq!(t.rows().len(), 3);
        // At low rates rule_72 overstates years vs exact.
        assert!(t.rows()[0].error_rule_of_72() > 0.0);
    }
}