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
use special::Gamma;
pub fn x2_test(f_obs: &[u32], ps: &[f64]) -> (f64, f64) {
let k = f_obs.len();
let nf = f64::from(f_obs.iter().sum::<u32>());
let x2 = nf
* f_obs.iter().zip(ps.iter()).fold(0.0, |acc, (&o, &p)| {
acc + (f64::from(o) / nf - p).powi(2) / p
});
let df = (k - 1) as f64;
let p = 1.0 - (x2 / 2.0).inc_gamma(df / 2.0);
(x2, p)
}
#[cfg(test)]
mod tests {
use super::*;
const TOL: f64 = 1E-12;
#[test]
fn gof() {
let f_obs: Vec<u32> = vec![28, 31, 40, 35];
let ps: Vec<f64> = vec![0.25; 4];
let (x2, p) = x2_test(&f_obs, &ps);
assert::close(x2, 2.417_910_447_761_194, TOL);
assert::close(p, 0.490_309_306_965_388_3, TOL);
}
}