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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::evaluator::*;
use conv::ConvUtil;
macro_const! {
const DOC: &str = r#"
Fraction of observations beyond $n\,\sigma\_m$ from the mean magnitude $\langle m \rangle$
$$
\mathrm{beyond}~n\,\sigma\_m \equiv \frac{\sum\_i I\_{|m - \langle m \rangle| > n\,\sigma\_m}(m_i)}{N},
$$
where $I$ is the [indicator function](https://en.wikipedia.org/wiki/Indicator_function),
$N$ is the number of observations,
$\langle m \rangle$ is the mean magnitude
and $\sigma_m = \sqrt{\sum_i (m_i - \langle m \rangle)^2 / (N-1)}$ is the magnitude standard deviation.
- Depends on: **magnitude**
- Minimum number of observations: **2**
- Number of features: **1**
D’Isanto et al. 2016 [DOI:10.1093/mnras/stw157](https://doi.org/10.1093/mnras/stw157)
"#;
}
#[doc = DOC!()]
#[cfg_attr(test, derive(PartialEq))]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(
from = "BeyondNStdParameters<T>",
into = "BeyondNStdParameters<T>",
bound = "T: Float"
)]
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()
}
}
impl<T> BeyondNStd<T> {
pub fn doc() -> &'static str {
DOC
}
}
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;
let count_beyond = ts.m.sample.fold(0, |count, &m| {
let beyond = T::abs(m - m_mean) > threshold;
count + (beyond as u32)
});
Ok(vec![count_beyond.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()]
}
}
#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename = "BeyondNStd")]
struct BeyondNStdParameters<T> {
nstd: T,
}
impl<T> From<BeyondNStd<T>> for BeyondNStdParameters<T> {
fn from(f: BeyondNStd<T>) -> Self {
Self { nstd: f.nstd }
}
}
impl<T> From<BeyondNStdParameters<T>> for BeyondNStd<T>
where
T: Float,
{
fn from(p: BeyondNStdParameters<T>) -> Self {
Self::new(p.nstd)
}
}
impl<T> JsonSchema for BeyondNStd<T>
where
T: Float,
{
json_schema!(BeyondNStdParameters<T>, false);
}
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]
mod tests {
use super::*;
use crate::tests::*;
use serde_test::{assert_tokens, Token};
check_feature!(BeyondNStd<f64>);
feature_test!(
beyond_n_std,
[
BeyondNStd::default(),
BeyondNStd::new(1.0),
BeyondNStd::new(2.0),
],
[0.2, 0.2, 0.0],
[1.0_f32, 2.0, 3.0, 4.0, 100.0],
);
#[test]
fn serialization() {
const NSTD: f64 = 2.34;
let beyond_n_std = BeyondNStd::new(NSTD);
assert_tokens(
&beyond_n_std,
&[
Token::Struct {
len: 1,
name: "BeyondNStd",
},
Token::String("nstd"),
Token::F64(NSTD),
Token::StructEnd,
],
)
}
}