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
use crate::evaluator::*;
use conv::ConvUtil;
#[derive(Clone, Debug)]
pub struct BeyondNStd<T> {
nstd: T,
name: String,
description: String,
}
impl<T> BeyondNStd<T>
where
T: Float,
{
pub fn new(nstd: T) -> Self {
assert!(nstd > T::zero(), "nstd should be positive");
Self {
nstd,
name: format!("beyond_{:.0}_std", nstd),
description: format!(
"fraction of observations which magnitudes are beyond {:.3e} standard deviations \
from the mean magnitude",
nstd
),
}
}
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[inline]
pub fn default_nstd() -> T {
T::one()
}
}
lazy_info!(
BEYOND_N_STD_INFO,
size: 1,
min_ts_length: 2,
t_required: false,
m_required: true,
w_required: false,
sorting_required: false,
);
impl<T> Default for BeyondNStd<T>
where
T: Float,
{
fn default() -> Self {
Self::new(Self::default_nstd())
}
}
impl<T> FeatureEvaluator<T> for BeyondNStd<T>
where
T: Float,
{
fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
self.check_ts_length(ts)?;
let m_mean = ts.m.get_mean();
let threshold = ts.m.get_std() * self.nstd;
Ok(vec![
ts.m.sample
.iter()
.filter(|&&y| T::abs(y - m_mean) > threshold)
.count()
.value_as::<T>()
.unwrap()
/ ts.lenf(),
])
}
fn get_info(&self) -> &EvaluatorInfo {
&BEYOND_N_STD_INFO
}
fn get_names(&self) -> Vec<&str> {
vec![self.name.as_str()]
}
fn get_descriptions(&self) -> Vec<&str> {
vec![self.description.as_str()]
}
}
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]
mod tests {
use super::*;
use crate::tests::*;
eval_info_test!(beyond_n_std_info, BeyondNStd::default());
feature_test!(
beyond_n_std,
[
Box::new(BeyondNStd::default()),
Box::new(BeyondNStd::new(1.0)),
Box::new(BeyondNStd::new(2.0)),
],
[0.2, 0.2, 0.0],
[1.0_f32, 2.0, 3.0, 4.0, 100.0],
);
}