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
use crate::evaluator::*;
use crate::lnerfc::ln_erfc;
use conv::ConvUtil;
#[derive(Clone, Default, Debug)]
pub struct AndersonDarlingNormal {}
impl AndersonDarlingNormal {
pub fn new() -> Self {
Self {}
}
}
lazy_info!(
ANDERSON_DARLING_NORMAL_INFO,
size: 1,
min_ts_length: 4,
t_required: false,
m_required: true,
w_required: false,
sorting_required: false,
);
impl<T> FeatureEvaluator<T> for AndersonDarlingNormal
where
T: Float,
{
fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
let size = self.check_ts_length(ts)?;
let m_std = get_nonzero_m_std(ts)?;
let m_mean = ts.m.get_mean();
let sum: f64 =
ts.m.get_sorted()
.iter()
.enumerate()
.map(|(i, &m)| {
let x = ((m - m_mean) / m_std).value_as::<f64>().unwrap()
* std::f64::consts::FRAC_1_SQRT_2;
((2 * i + 1) as f64) * ln_erfc(-x) + ((2 * (size - i) - 1) as f64) * ln_erfc(x)
})
.sum();
let n = ts.lenf();
Ok(vec![
(T::one() + T::four() / n - (T::five() / n).powi(2))
* (n * (T::two() * T::LN_2() - T::one()) - sum.approx_as::<T>().unwrap() / n),
])
}
fn get_info(&self) -> &EvaluatorInfo {
&ANDERSON_DARLING_NORMAL_INFO
}
fn get_names(&self) -> Vec<&str> {
vec!["anderson_darling_normal"]
}
}
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]
mod tests {
use super::*;
use crate::tests::*;
eval_info_test!(
anderson_darling_normal_info,
AndersonDarlingNormal::default()
);
feature_test!(
anderson_darling_normal,
[Box::new(AndersonDarlingNormal::new())],
[1.1354353876265415],
{
let mut m = linspace(0.0, 1.0, 101);
let mut rng = StdRng::seed_from_u64(0);
m.shuffle(&mut rng);
m
},
);
}