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
//! Shared mathematical utilities.
use rust_decimal::Decimal;
/// Square root approximation using Newton-Raphson.
pub(crate) fn decimal_sqrt(x: Decimal) -> Decimal {
if x <= Decimal::ZERO {
return Decimal::ZERO;
}
let mut guess = x / Decimal::from(2);
let tolerance = Decimal::new(1, 10);
for _ in 0..50 {
let next = (guess + x / guess) / Decimal::from(2);
if (next - guess).abs() < tolerance {
return next;
}
guess = next;
}
guess
}
/// Calculate mean of values.
pub(crate) fn mean(values: &[Decimal]) -> Decimal {
if values.is_empty() {
return Decimal::ZERO;
}
let sum: Decimal = values.iter().sum();
sum / Decimal::from(values.len() as u64)
}
/// Calculate sample standard deviation.
pub(crate) fn std_deviation(values: &[Decimal]) -> Decimal {
if values.len() < 2 {
return Decimal::ZERO;
}
let m = mean(values);
let variance: Decimal = values.iter().map(|&x| (x - m) * (x - m)).sum();
let variance = variance / Decimal::from((values.len() - 1) as u64);
decimal_sqrt(variance)
}
#[cfg(test)]
#[path = "math_tests.rs"]
mod tests;