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
use crate::error::StatsError;
use crate::function::gamma;
use std::f64;
use std::sync::{Once, ONCE_INIT};
use crate::Result;
pub const MAX_ARG: u64 = 170;
pub fn factorial(x: u64) -> f64 {
if x > MAX_ARG {
f64::INFINITY
} else {
get_fcache()[x as usize]
}
}
pub fn ln_factorial(x: u64) -> f64 {
if x <= 1 {
0.0
} else if x > MAX_ARG {
gamma::ln_gamma(x as f64 + 1.0)
} else {
get_fcache()[x as usize].ln()
}
}
pub fn binomial(n: u64, k: u64) -> f64 {
if k > n {
0.0
} else {
(0.5 + (ln_factorial(n) - ln_factorial(k) - ln_factorial(n - k)).exp()).floor()
}
}
pub fn ln_binomial(n: u64, k: u64) -> f64 {
if k > n {
f64::NEG_INFINITY
} else {
ln_factorial(n) - ln_factorial(k) - ln_factorial(n - k)
}
}
pub fn multinomial(n: u64, ni: &[u64]) -> f64 {
checked_multinomial(n, ni).unwrap()
}
pub fn checked_multinomial(n: u64, ni: &[u64]) -> Result<f64> {
let (sum, ret) = ni.iter().fold((0, ln_factorial(n)), |acc, &x| {
(acc.0 + x, acc.1 - ln_factorial(x))
});
if sum != n {
Err(StatsError::ContainerExpectedSumVar("ni", "n"))
} else {
Ok((0.5 + ret.exp()).floor())
}
}
const CACHE_SIZE: usize = 171;
static mut FCACHE: &'static mut [f64; CACHE_SIZE] = &mut [1.0; CACHE_SIZE];
static START: Once = ONCE_INIT;
fn get_fcache() -> &'static [f64; CACHE_SIZE] {
unsafe {
START.call_once(|| {
(1..CACHE_SIZE).fold(FCACHE[0], |acc, i| {
let fac = acc * i as f64;
FCACHE[i] = fac;
fac
});
});
FCACHE
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg(test)]
mod test {
use std::{f64, u64};
#[test]
fn test_factorial_and_ln_factorial() {
let mut factorial = 1.0;
for i in 1..171 {
factorial *= i as f64;
assert_eq!(super::factorial(i), factorial);
assert_eq!(super::ln_factorial(i), factorial.ln());
}
}
#[test]
fn test_factorial_overflow() {
assert_eq!(super::factorial(172), f64::INFINITY);
assert_eq!(super::factorial(u64::MAX), f64::INFINITY);
}
#[test]
fn test_ln_factorial_does_not_overflow() {
assert_eq!(super::ln_factorial(1 << 10), 6078.2118847500501140);
assert_almost_eq!(super::ln_factorial(1 << 12), 29978.648060844048236, 1e-11);
assert_eq!(super::ln_factorial(1 << 15), 307933.81973375485425);
assert_eq!(super::ln_factorial(1 << 17), 1413421.9939462073242);
}
#[test]
fn test_binomial() {
assert_eq!(super::binomial(1, 1), 1.0);
assert_eq!(super::binomial(5, 2), 10.0);
assert_eq!(super::binomial(7, 3), 35.0);
assert_eq!(super::binomial(1, 0), 1.0);
assert_eq!(super::binomial(0, 1), 0.0);
assert_eq!(super::binomial(5, 7), 0.0);
}
#[test]
fn test_ln_binomial() {
assert_eq!(super::ln_binomial(1, 1), 1f64.ln());
assert_almost_eq!(super::ln_binomial(5, 2), 10f64.ln(), 1e-14);
assert_almost_eq!(super::ln_binomial(7, 3), 35f64.ln(), 1e-14);
assert_eq!(super::ln_binomial(1, 0), 1f64.ln());
assert_eq!(super::ln_binomial(0, 1), 0f64.ln());
assert_eq!(super::ln_binomial(5, 7), 0f64.ln());
}
#[test]
fn test_multinomial() {
assert_eq!(1.0, super::multinomial(1, &[1, 0]));
assert_eq!(10.0, super::multinomial(5, &[3, 2]));
assert_eq!(10.0, super::multinomial(5, &[2, 3]));
assert_eq!(35.0, super::multinomial(7, &[3, 4]));
}
#[test]
#[should_panic]
fn test_multinomial_bad_ni() {
super::multinomial(1, &[1, 1]);
}
#[test]
fn test_checked_multinomial_bad_ni() {
assert!(super::checked_multinomial(1, &[1, 1]).is_err());
}
}